From 3bb766cfb2fcc341df0b5bf4bb544e7bfdcb02b8 Mon Sep 17 00:00:00 2001 From: Methapon Metanipat Date: Thu, 17 Oct 2024 14:04:47 +0700 Subject: [PATCH] feat: detect same username / email --- src/controllers/02-user-controller.ts | 68 +++++++++++++++++---------- 1 file changed, 44 insertions(+), 24 deletions(-) diff --git a/src/controllers/02-user-controller.ts b/src/controllers/02-user-controller.ts index 8cebb7f..ac0ae93 100644 --- a/src/controllers/02-user-controller.ts +++ b/src/controllers/02-user-controller.ts @@ -332,7 +332,7 @@ export class UserController extends Controller { where: { id: { in: Array.isArray(body.branchId) ? body.branchId : [body.branchId] } }, }), prisma.user.findFirst({ - where: { username: body.username }, + where: { OR: [{ username: body.username }, { email: body.email }] }, }), ]); if (body.provinceId && !province) throw relationError("Province"); @@ -348,8 +348,11 @@ export class UserController extends Controller { await Promise.all(branch.map((branch) => permissionCheck(req.user, branch))); - if (user) { - throw new HttpError(HttpStatus.BAD_REQUEST, "User exists.", "userExists"); + if (user && user.username === body.username) { + throw new HttpError(HttpStatus.BAD_REQUEST, "User exists.", "userExistsSameUserName"); + } + if (user && user.email === body.email) { + throw new HttpError(HttpStatus.BAD_REQUEST, "User exists.", "userExistsSameEmail"); } const setRoleIndex = MANAGE_ROLES.findIndex((v) => v === body.userRole); @@ -454,31 +457,40 @@ export class UserController extends Controller { @Body() body: UserUpdate, @Path() userId: string, ) { - const [province, district, subDistrict, user, branch] = await prisma.$transaction([ - prisma.province.findFirst({ where: { id: body.provinceId || undefined } }), - prisma.district.findFirst({ where: { id: body.districtId || undefined } }), - prisma.subDistrict.findFirst({ where: { id: body.subDistrictId || undefined } }), - prisma.user.findFirst({ - include: { - branch: { - include: { - branch: { - include: branchRelationPermInclude(req.user), + const [province, district, subDistrict, user, branch, conflictUser] = await prisma.$transaction( + [ + prisma.province.findFirst({ where: { id: body.provinceId || undefined } }), + prisma.district.findFirst({ where: { id: body.districtId || undefined } }), + prisma.subDistrict.findFirst({ where: { id: body.subDistrictId || undefined } }), + prisma.user.findFirst({ + include: { + branch: { + include: { + branch: { + include: branchRelationPermInclude(req.user), + }, }, }, }, - }, - where: { id: userId }, - }), - prisma.branch.findMany({ - include: branchRelationPermInclude(req.user), - where: { - id: { - in: Array.isArray(body.branchId) ? body.branchId : body.branchId ? [body.branchId] : [], + where: { id: userId }, + }), + prisma.branch.findMany({ + include: branchRelationPermInclude(req.user), + where: { + id: { + in: Array.isArray(body.branchId) + ? body.branchId + : body.branchId + ? [body.branchId] + : [], + }, }, - }, - }), - ]); + }), + prisma.user.findFirst({ + where: { OR: [{ username: body.username }, { email: body.email }], NOT: { id: userId } }, + }), + ], + ); if (!user) throw notFoundError("User"); if (body.provinceId && !province) throw relationError("Province"); if (body.districtId && !district) throw relationError("District"); @@ -490,6 +502,14 @@ export class UserController extends Controller { "minimumBranchNotMet", ); } + + if (conflictUser && conflictUser.username === body.username) { + throw new HttpError(HttpStatus.BAD_REQUEST, "User exists.", "userExistsSameUserName"); + } + if (conflictUser && conflictUser.email === body.email) { + throw new HttpError(HttpStatus.BAD_REQUEST, "User exists.", "userExistsSameEmail"); + } + await Promise.all([ ...user.branch.map(async ({ branch }) => await permissionCheck(req.user, branch)), ...branch.map(async (branch) => await permissionCheck(req.user, branch)),