Revert "feat: detect same username / email"

This reverts commit 3bb766cfb2.
This commit is contained in:
Methapon Metanipat 2024-10-17 14:11:37 +07:00
parent 3bb766cfb2
commit 65f15da95a

View file

@ -332,7 +332,7 @@ export class UserController extends Controller {
where: { id: { in: Array.isArray(body.branchId) ? body.branchId : [body.branchId] } }, where: { id: { in: Array.isArray(body.branchId) ? body.branchId : [body.branchId] } },
}), }),
prisma.user.findFirst({ prisma.user.findFirst({
where: { OR: [{ username: body.username }, { email: body.email }] }, where: { username: body.username },
}), }),
]); ]);
if (body.provinceId && !province) throw relationError("Province"); if (body.provinceId && !province) throw relationError("Province");
@ -348,11 +348,8 @@ export class UserController extends Controller {
await Promise.all(branch.map((branch) => permissionCheck(req.user, branch))); await Promise.all(branch.map((branch) => permissionCheck(req.user, branch)));
if (user && user.username === body.username) { if (user) {
throw new HttpError(HttpStatus.BAD_REQUEST, "User exists.", "userExistsSameUserName"); throw new HttpError(HttpStatus.BAD_REQUEST, "User exists.", "userExists");
}
if (user && user.email === body.email) {
throw new HttpError(HttpStatus.BAD_REQUEST, "User exists.", "userExistsSameEmail");
} }
const setRoleIndex = MANAGE_ROLES.findIndex((v) => v === body.userRole); const setRoleIndex = MANAGE_ROLES.findIndex((v) => v === body.userRole);
@ -457,40 +454,31 @@ export class UserController extends Controller {
@Body() body: UserUpdate, @Body() body: UserUpdate,
@Path() userId: string, @Path() userId: string,
) { ) {
const [province, district, subDistrict, user, branch, conflictUser] = await prisma.$transaction( const [province, district, subDistrict, user, branch] = await prisma.$transaction([
[ prisma.province.findFirst({ where: { id: body.provinceId || undefined } }),
prisma.province.findFirst({ where: { id: body.provinceId || undefined } }), prisma.district.findFirst({ where: { id: body.districtId || undefined } }),
prisma.district.findFirst({ where: { id: body.districtId || undefined } }), prisma.subDistrict.findFirst({ where: { id: body.subDistrictId || undefined } }),
prisma.subDistrict.findFirst({ where: { id: body.subDistrictId || undefined } }), prisma.user.findFirst({
prisma.user.findFirst({ include: {
include: { branch: {
branch: { include: {
include: { branch: {
branch: { include: branchRelationPermInclude(req.user),
include: branchRelationPermInclude(req.user),
},
}, },
}, },
}, },
where: { id: userId }, },
}), where: { id: userId },
prisma.branch.findMany({ }),
include: branchRelationPermInclude(req.user), prisma.branch.findMany({
where: { include: branchRelationPermInclude(req.user),
id: { where: {
in: Array.isArray(body.branchId) id: {
? body.branchId in: Array.isArray(body.branchId) ? body.branchId : 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 (!user) throw notFoundError("User");
if (body.provinceId && !province) throw relationError("Province"); if (body.provinceId && !province) throw relationError("Province");
if (body.districtId && !district) throw relationError("District"); if (body.districtId && !district) throw relationError("District");
@ -502,14 +490,6 @@ export class UserController extends Controller {
"minimumBranchNotMet", "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([ await Promise.all([
...user.branch.map(async ({ branch }) => await permissionCheck(req.user, branch)), ...user.branch.map(async ({ branch }) => await permissionCheck(req.user, branch)),
...branch.map(async (branch) => await permissionCheck(req.user, branch)), ...branch.map(async (branch) => await permissionCheck(req.user, branch)),