parent
							
								
									c95e55f625
								
							
						
					
					
						commit
						1ab078fc54
					
				@ -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 } });
 | 
				
			||||
  }
 | 
				
			||||
}
 | 
				
			||||
@ -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");
 | 
				
			||||
  }
 | 
				
			||||
}
 | 
				
			||||
@ -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 {
 | 
				
			||||
}
 | 
				
			||||
					Loading…
					
					
				
		Reference in new issue