2024-07-01 13:24:02 +07:00
|
|
|
import { Branch, Prisma, Status, User } from "@prisma/client";
|
2024-04-03 15:34:36 +07:00
|
|
|
import {
|
|
|
|
|
Body,
|
|
|
|
|
Controller,
|
|
|
|
|
Delete,
|
|
|
|
|
Get,
|
|
|
|
|
Path,
|
|
|
|
|
Post,
|
|
|
|
|
Query,
|
|
|
|
|
Request,
|
|
|
|
|
Route,
|
|
|
|
|
Security,
|
|
|
|
|
Tags,
|
|
|
|
|
} from "tsoa";
|
|
|
|
|
|
2024-04-05 11:30:18 +07:00
|
|
|
import prisma from "../db";
|
|
|
|
|
import HttpError from "../interfaces/http-error";
|
|
|
|
|
import HttpStatus from "../interfaces/http-status";
|
|
|
|
|
import { RequestWithUser } from "../interfaces/user";
|
2024-04-03 15:34:36 +07:00
|
|
|
|
|
|
|
|
type BranchUserBody = { user: string[] };
|
|
|
|
|
|
2024-04-23 11:19:16 +07:00
|
|
|
async function userBranchCodeGen(branch: Branch, user: User[]) {
|
|
|
|
|
await prisma.$transaction(
|
|
|
|
|
async (tx) => {
|
|
|
|
|
for (const usr of user) {
|
|
|
|
|
if (usr.code !== null) continue;
|
|
|
|
|
|
|
|
|
|
const typ = usr.userType;
|
|
|
|
|
|
2024-08-09 09:51:36 +07:00
|
|
|
const mapTypeNo = {
|
|
|
|
|
USER: 1,
|
|
|
|
|
MESSENGER: 2,
|
|
|
|
|
DELEGATE: 3,
|
|
|
|
|
AGENCY: 4,
|
|
|
|
|
}[typ];
|
|
|
|
|
|
2024-04-23 11:19:16 +07:00
|
|
|
const last = await tx.runningNo.upsert({
|
|
|
|
|
where: {
|
2024-08-09 09:51:36 +07:00
|
|
|
key: `BR_USR_${branch.code}_${mapTypeNo}`,
|
2024-04-23 11:19:16 +07:00
|
|
|
},
|
|
|
|
|
create: {
|
2024-08-09 09:51:36 +07:00
|
|
|
key: `BR_USR_${branch.code}_${mapTypeNo}`,
|
2024-04-23 11:19:16 +07:00
|
|
|
value: 1,
|
|
|
|
|
},
|
|
|
|
|
update: { value: { increment: 1 } },
|
|
|
|
|
});
|
|
|
|
|
|
2024-07-03 09:10:12 +07:00
|
|
|
await tx.user.update({
|
2024-04-23 11:19:16 +07:00
|
|
|
where: { id: usr.id },
|
|
|
|
|
data: {
|
2024-08-14 20:16:40 +07:00
|
|
|
code: mapTypeNo + `${last.value}`.padStart(6, "0"),
|
2024-04-23 11:19:16 +07:00
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
{ isolationLevel: Prisma.TransactionIsolationLevel.Serializable },
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-14 13:45:41 +07:00
|
|
|
@Route("api/v1/branch/{branchId}/admin")
|
|
|
|
|
@Tags("Branch User")
|
|
|
|
|
export class BranchAdminUserController extends Controller {
|
|
|
|
|
@Get()
|
|
|
|
|
@Security("keycloak")
|
|
|
|
|
async getBranchAdmin(@Path() branchId: string) {
|
|
|
|
|
return await prisma.user.findFirst({
|
|
|
|
|
where: {
|
|
|
|
|
branch: {
|
|
|
|
|
some: { branchId },
|
|
|
|
|
},
|
|
|
|
|
userRole: "branch_admin",
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-06 09:42:02 +07:00
|
|
|
@Route("api/v1/branch/{branchId}/user")
|
2024-04-03 15:34:36 +07:00
|
|
|
@Tags("Branch User")
|
|
|
|
|
export class BranchUserController extends Controller {
|
|
|
|
|
@Get()
|
2024-07-03 11:32:32 +07:00
|
|
|
@Security("keycloak")
|
2024-04-03 15:34:36 +07:00
|
|
|
async getBranchUser(
|
|
|
|
|
@Path() branchId: string,
|
|
|
|
|
@Query() zipCode?: string,
|
|
|
|
|
@Query() query: string = "",
|
|
|
|
|
@Query() page: number = 1,
|
|
|
|
|
@Query() pageSize: number = 30,
|
|
|
|
|
) {
|
|
|
|
|
const where = {
|
|
|
|
|
OR: [
|
2024-04-04 13:42:46 +07:00
|
|
|
{ user: { firstName: { contains: query }, zipCode }, branchId },
|
2024-04-03 15:34:36 +07:00
|
|
|
{ user: { firstNameEN: { contains: query }, zipCode }, branchId },
|
2024-04-04 13:42:46 +07:00
|
|
|
{ user: { lastName: { contains: query }, zipCode }, branchId },
|
2024-04-03 15:34:36 +07:00
|
|
|
{ user: { lastNameEN: { contains: query }, zipCode }, branchId },
|
|
|
|
|
{ user: { email: { contains: query }, zipCode }, branchId },
|
|
|
|
|
{ user: { telephoneNo: { contains: query }, zipCode }, branchId },
|
|
|
|
|
],
|
|
|
|
|
} satisfies Prisma.BranchUserWhereInput;
|
|
|
|
|
|
|
|
|
|
const [result, total] = await prisma.$transaction([
|
|
|
|
|
prisma.branchUser.findMany({
|
2024-04-09 08:51:22 +07:00
|
|
|
orderBy: { user: { createdAt: "asc" } },
|
2024-04-03 15:34:36 +07:00
|
|
|
include: {
|
|
|
|
|
user: {
|
|
|
|
|
include: {
|
|
|
|
|
province: true,
|
|
|
|
|
district: true,
|
|
|
|
|
subDistrict: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
where,
|
|
|
|
|
take: pageSize,
|
|
|
|
|
skip: (page - 1) * pageSize,
|
|
|
|
|
}),
|
|
|
|
|
prisma.branchUser.count({ where }),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
return { result: result.map((v) => v.user), page, pageSize, total };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Post()
|
2024-07-03 11:32:32 +07:00
|
|
|
@Security("keycloak", ["system", "head_of_admin", "admin", "branch_admin", "branch_manager"])
|
2024-04-03 15:34:36 +07:00
|
|
|
async createBranchUser(
|
|
|
|
|
@Request() req: RequestWithUser,
|
|
|
|
|
@Path() branchId: string,
|
|
|
|
|
@Body() body: BranchUserBody,
|
|
|
|
|
) {
|
2024-04-09 13:05:49 +07:00
|
|
|
const [branch, user] = await prisma.$transaction([
|
|
|
|
|
prisma.branch.findUnique({
|
2024-07-03 11:32:32 +07:00
|
|
|
include: {
|
|
|
|
|
user: {
|
|
|
|
|
where: { userId: req.user.sub },
|
|
|
|
|
},
|
|
|
|
|
},
|
2024-04-09 13:05:49 +07:00
|
|
|
where: { id: branchId },
|
|
|
|
|
}),
|
|
|
|
|
prisma.user.findMany({
|
|
|
|
|
include: { branch: true },
|
|
|
|
|
where: { id: { in: body.user } },
|
|
|
|
|
}),
|
|
|
|
|
]);
|
|
|
|
|
|
2024-07-03 11:32:32 +07:00
|
|
|
if (
|
|
|
|
|
!["system", "head_of_admin", "admin"].some((v) => req.user.roles?.includes(v)) &&
|
|
|
|
|
branch?.createdByUserId !== req.user.sub &&
|
|
|
|
|
!branch?.user.find((v) => v.userId === req.user.sub)
|
|
|
|
|
) {
|
|
|
|
|
throw new HttpError(
|
|
|
|
|
HttpStatus.FORBIDDEN,
|
|
|
|
|
"You do not have permission to perform this action.",
|
|
|
|
|
"noPermission",
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-09 13:05:49 +07:00
|
|
|
if (!branch) {
|
2024-07-01 13:24:02 +07:00
|
|
|
throw new HttpError(HttpStatus.BAD_REQUEST, "Branch cannot be found.", "branchBadReq");
|
2024-04-09 13:05:49 +07:00
|
|
|
}
|
2024-04-03 15:34:36 +07:00
|
|
|
|
|
|
|
|
if (user.length !== body.user.length) {
|
|
|
|
|
throw new HttpError(
|
|
|
|
|
HttpStatus.BAD_REQUEST,
|
|
|
|
|
"One or more user cannot be found.",
|
2024-06-14 05:44:03 +00:00
|
|
|
"oneOrMoreUserBadReq",
|
2024-04-03 15:34:36 +07:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-09 13:05:49 +07:00
|
|
|
await prisma.$transaction([
|
|
|
|
|
prisma.user.updateMany({
|
|
|
|
|
where: { id: { in: body.user }, status: Status.CREATED },
|
|
|
|
|
data: { status: Status.ACTIVE },
|
|
|
|
|
}),
|
|
|
|
|
prisma.branchUser.createMany({
|
|
|
|
|
data: user
|
|
|
|
|
.filter((a) => !a.branch.some((b) => b.branchId === branchId))
|
|
|
|
|
.map((v) => ({
|
|
|
|
|
branchId,
|
|
|
|
|
userId: v.id,
|
2024-07-01 13:24:02 +07:00
|
|
|
createdByUserId: req.user.sub,
|
|
|
|
|
updatedByUserId: req.user.sub,
|
2024-04-09 13:05:49 +07:00
|
|
|
})),
|
|
|
|
|
}),
|
|
|
|
|
]);
|
2024-04-03 15:34:36 +07:00
|
|
|
|
2024-04-23 11:19:16 +07:00
|
|
|
await userBranchCodeGen(branch, user);
|
2024-04-03 15:34:36 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Delete()
|
|
|
|
|
async deleteBranchUser(@Path() branchId: string, @Body() body: BranchUserBody) {
|
|
|
|
|
await prisma.$transaction(
|
|
|
|
|
body.user.map((v) => prisma.branchUser.deleteMany({ where: { branchId, userId: v } })),
|
|
|
|
|
);
|
2024-04-18 18:00:19 +07:00
|
|
|
await prisma.user.updateMany({
|
|
|
|
|
where: {
|
|
|
|
|
branch: { none: {} },
|
|
|
|
|
},
|
|
|
|
|
data: { code: null },
|
|
|
|
|
});
|
2024-04-03 15:34:36 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Delete("{userId}")
|
|
|
|
|
async deleteBranchUserById(@Path() branchId: string, @Path() userId: string) {
|
|
|
|
|
await prisma.branchUser.deleteMany({
|
|
|
|
|
where: { branchId, userId },
|
|
|
|
|
});
|
2024-04-18 18:00:19 +07:00
|
|
|
await prisma.user.updateMany({
|
|
|
|
|
where: {
|
|
|
|
|
id: userId,
|
|
|
|
|
branch: { none: {} },
|
|
|
|
|
},
|
|
|
|
|
data: { code: null },
|
|
|
|
|
});
|
2024-04-03 15:34:36 +07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type UserBranchBody = { branch: string[] };
|
|
|
|
|
|
2024-06-06 09:42:02 +07:00
|
|
|
@Route("api/v1/user/{userId}/branch")
|
2024-04-05 11:25:30 +07:00
|
|
|
@Tags("Branch User")
|
2024-04-03 15:34:36 +07:00
|
|
|
@Security("keycloak")
|
|
|
|
|
export class UserBranchController extends Controller {
|
|
|
|
|
@Get()
|
|
|
|
|
async getUserBranch(
|
|
|
|
|
@Path() userId: string,
|
|
|
|
|
@Query() zipCode?: string,
|
|
|
|
|
@Query() query: string = "",
|
|
|
|
|
@Query() page: number = 1,
|
|
|
|
|
@Query() pageSize: number = 30,
|
|
|
|
|
) {
|
|
|
|
|
const where = {
|
|
|
|
|
OR: [
|
2024-04-04 13:42:46 +07:00
|
|
|
{ branch: { name: { contains: query }, zipCode }, userId },
|
2024-04-03 15:34:36 +07:00
|
|
|
{ branch: { nameEN: { contains: query }, zipCode }, userId },
|
|
|
|
|
],
|
|
|
|
|
} satisfies Prisma.BranchUserWhereInput;
|
|
|
|
|
|
|
|
|
|
const [result, total] = await prisma.$transaction([
|
|
|
|
|
prisma.branchUser.findMany({
|
2024-04-09 08:51:22 +07:00
|
|
|
orderBy: { branch: { createdAt: "asc" } },
|
2024-04-03 15:34:36 +07:00
|
|
|
include: {
|
|
|
|
|
branch: {
|
|
|
|
|
include: {
|
|
|
|
|
province: true,
|
|
|
|
|
district: true,
|
|
|
|
|
subDistrict: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
where,
|
|
|
|
|
take: pageSize,
|
|
|
|
|
skip: (page - 1) * pageSize,
|
|
|
|
|
}),
|
|
|
|
|
prisma.branchUser.count({ where }),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
return { result: result.map((v) => v.branch), page, pageSize, total };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Post()
|
|
|
|
|
async createUserBranch(
|
|
|
|
|
@Request() req: RequestWithUser,
|
|
|
|
|
@Path() userId: string,
|
|
|
|
|
@Body() body: UserBranchBody,
|
|
|
|
|
) {
|
|
|
|
|
const branch = await prisma.branch.findMany({
|
|
|
|
|
include: { user: true },
|
|
|
|
|
where: { id: { in: body.branch } },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (branch.length !== body.branch.length) {
|
|
|
|
|
throw new HttpError(
|
|
|
|
|
HttpStatus.BAD_REQUEST,
|
|
|
|
|
"One or more branch cannot be found.",
|
2024-06-14 05:44:03 +00:00
|
|
|
"oneOrMoreBranchBadReq",
|
2024-04-03 15:34:36 +07:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await prisma.branch.updateMany({
|
2024-04-05 10:38:29 +07:00
|
|
|
where: { id: { in: body.branch }, status: Status.CREATED },
|
|
|
|
|
data: { status: Status.ACTIVE },
|
2024-04-03 15:34:36 +07:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await prisma.branchUser.createMany({
|
|
|
|
|
data: branch
|
|
|
|
|
.filter((a) => !a.user.some((b) => b.userId === userId))
|
|
|
|
|
.map((v) => ({
|
|
|
|
|
branchId: v.id,
|
|
|
|
|
userId,
|
2024-07-01 13:24:02 +07:00
|
|
|
createdByUserId: req.user.sub,
|
|
|
|
|
updatedByUserId: req.user.sub,
|
2024-04-03 15:34:36 +07:00
|
|
|
})),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
this.setStatus(HttpStatus.CREATED);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Delete()
|
2024-04-04 15:50:30 +07:00
|
|
|
async deleteUserBranch(@Path() userId: string, @Body() body: UserBranchBody) {
|
2024-04-03 15:34:36 +07:00
|
|
|
await prisma.$transaction(
|
2024-04-04 15:50:30 +07:00
|
|
|
body.branch.map((v) => prisma.branchUser.deleteMany({ where: { userId, branchId: v } })),
|
2024-04-03 15:34:36 +07:00
|
|
|
);
|
2024-04-18 18:00:19 +07:00
|
|
|
await prisma.user.updateMany({
|
|
|
|
|
where: {
|
|
|
|
|
branch: { none: {} },
|
|
|
|
|
},
|
|
|
|
|
data: { code: null },
|
|
|
|
|
});
|
2024-04-03 15:34:36 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Delete("{branchId}")
|
|
|
|
|
async deleteUserBranchById(@Path() branchId: string, @Path() userId: string) {
|
|
|
|
|
await prisma.branchUser.deleteMany({
|
|
|
|
|
where: { branchId, userId },
|
|
|
|
|
});
|
2024-04-18 18:00:19 +07:00
|
|
|
await prisma.user.updateMany({
|
|
|
|
|
where: {
|
|
|
|
|
id: userId,
|
|
|
|
|
branch: { none: {} },
|
|
|
|
|
},
|
|
|
|
|
data: { code: null },
|
|
|
|
|
});
|
2024-04-03 15:34:36 +07:00
|
|
|
}
|
|
|
|
|
}
|