feat: detect same username / email
This commit is contained in:
parent
4e5825c236
commit
3bb766cfb2
1 changed files with 44 additions and 24 deletions
|
|
@ -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)),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue