From 1ab078fc54ad8121eca313c0c44625e557d9ad36 Mon Sep 17 00:00:00 2001 From: Artem-Darius Atlas Date: Thu, 29 Jun 2023 16:11:33 +0300 Subject: [PATCH] feat: added user module for the auth service --- apps/auth/src/user/service/user.service.ts | 54 +++++++++++++++ apps/auth/src/user/user.controller.ts | 80 ++++++++++++++++++++++ apps/auth/src/user/user.module.ts | 11 +++ 3 files changed, 145 insertions(+) create mode 100644 apps/auth/src/user/service/user.service.ts create mode 100644 apps/auth/src/user/user.controller.ts create mode 100644 apps/auth/src/user/user.module.ts diff --git a/apps/auth/src/user/service/user.service.ts b/apps/auth/src/user/service/user.service.ts new file mode 100644 index 0000000..0c9008d --- /dev/null +++ b/apps/auth/src/user/service/user.service.ts @@ -0,0 +1,54 @@ +import { BadRequestException, Injectable } from "@nestjs/common"; +import { PrismaService } from "../../Prisma/service/prisma.service"; +import { DevicesDto } from "../dto/devices.dto"; +import { PrismaClientValidationError } from "@prisma/client/runtime"; + +@Injectable() +export class UserService { + constructor(private readonly prisma: PrismaService) { + } + + public async devices(params: DevicesDto): Promise<{ name?: string, fingerprint?: string, isBlocked?: boolean }[]> { + let select: { name?: boolean, fingerprint?: boolean, isBlocked?: boolean } | undefined = { + name: true, fingerprint: true, isBlocked: true + }; + if (params.select !== undefined && params.select !== null) { + select = {}; + if (params.select.name !== undefined && params.select.name !== null) select.name = params.select.name; + if (params.select.fingerprint !== undefined && params.select.fingerprint !== null) select.fingerprint = params.select.fingerprint; + if (params.select.isBlocked !== undefined && params.select.isBlocked !== null) select.isBlocked = params.select.isBlocked; + } + try { + const res: { name?: string, isBlocked?: boolean, fingerprint?: Buffer }[] = + await this.prisma.userDevice.findMany({ where: { userUuid: params.uuid }, select }); + return res.map(value => ({ ...value, fingerprint: value.fingerprint?.toString("hex") })); + } catch (e) { + if (!(e instanceof PrismaClientValidationError)) throw e; + throw new BadRequestException("The select statement needs at least one truthy value."); + } + } + + public async isExists(params: { uuid: string }) { + return (await this.prisma.user.findUnique({ where: params, select: null }) !== null); + } + + public register(params: { uuid: string }) { + return this.prisma.user.create({ data: params, select: null }); + } + + public async delete(params: { uuid: string }) { + await this.prisma.user.delete({ where: params, select: null }); + } + + public async block(params: { uuid: string }) { + await this.prisma.user.update({ where: params, data: { isBlocked: true }, select: null }); + } + + public async unblock(params: { uuid: string }) { + await this.prisma.user.update({ where: params, data: { isBlocked: false }, select: null }); + } + + public isBlocked(params: { uuid: string }) { + return this.prisma.user.findUnique({ where: params, select: { isBlocked: true } }); + } +} \ No newline at end of file diff --git a/apps/auth/src/user/user.controller.ts b/apps/auth/src/user/user.controller.ts new file mode 100644 index 0000000..da3ad07 --- /dev/null +++ b/apps/auth/src/user/user.controller.ts @@ -0,0 +1,80 @@ +import { Body, ConflictException, Controller, HttpCode, Post, ValidationPipe } from "@nestjs/common"; +import { UserService } from "./service/user.service"; +import { UuidDto } from "./dto/uuid.dto"; +import { PrismaClientKnownRequestError } from "@prisma/client/runtime"; +import { DevicesDto } from "./dto/devices.dto"; + +@Controller("user") +export class UserController { + constructor(private readonly user: UserService) { + } + + @Post("devices") + @HttpCode(200) + async Devices(@Body(new ValidationPipe()) params: DevicesDto) { + if (await this.user.isExists({ uuid: params.uuid })) return await this.user.devices(params); + else throw new ConflictException("user with this uuid does not exist"); + } + + @Post("is-exists") + @HttpCode(200) + async IsExists(@Body(new ValidationPipe()) params: UuidDto) { + return await this.user.isExists(params); + } + + @Post("register") + @HttpCode(200) + async Register(@Body(new ValidationPipe()) params: UuidDto) { + try { + await this.user.register(params); + } catch (e) { + if (!(e instanceof PrismaClientKnownRequestError)) throw e; + if (e.code !== "P2002") throw e; + throw new ConflictException("user with the same uuid already exists"); + } + } + + @Post("delete") + @HttpCode(200) + async Delete(@Body(new ValidationPipe()) params: UuidDto) { + try { + await this.user.delete(params); + } catch (e) { + if (!(e instanceof PrismaClientKnownRequestError)) throw e; + if (e.code !== "P2025") throw e; + throw new ConflictException("user with this uuid does not exist"); + } + } + + @Post("block") + @HttpCode(200) + async Block(@Body(new ValidationPipe()) params: UuidDto) { + try { + await this.user.block(params); + } catch (e) { + if (!(e instanceof PrismaClientKnownRequestError)) throw e; + if (e.code !== "P2025") throw e; + throw new ConflictException("user with this uuid does not exist"); + } + } + + @Post("unblock") + @HttpCode(200) + async Unblock(@Body(new ValidationPipe()) params: UuidDto) { + try { + await this.user.unblock(params); + } catch (e) { + if (!(e instanceof PrismaClientKnownRequestError)) throw e; + if (e.code !== "P2025") throw e; + throw new ConflictException("user with this uuid does not exist"); + } + } + + @Post("is-blocked") + @HttpCode(200) + async IsBlocked(@Body(new ValidationPipe()) params: UuidDto) { + const isBlocked = await this.user.isBlocked(params); + if (isBlocked !== null) return isBlocked.isBlocked; + throw new ConflictException("user with this uuid does not exist"); + } +} diff --git a/apps/auth/src/user/user.module.ts b/apps/auth/src/user/user.module.ts new file mode 100644 index 0000000..f246f20 --- /dev/null +++ b/apps/auth/src/user/user.module.ts @@ -0,0 +1,11 @@ +import { Module } from "@nestjs/common"; +import { UserController } from "./user.controller"; +import { UserService } from "./service/user.service"; + +@Module({ + providers: [UserService], + controllers: [UserController], + exports: [UserService] +}) +export class UserModule { +} \ No newline at end of file