jws-backend/src/controllers/employee-controller.ts

655 lines
18 KiB
TypeScript
Raw Normal View History

2024-04-09 14:40:55 +07:00
import { Prisma, Status } from "@prisma/client";
import {
Body,
Controller,
Delete,
Get,
Path,
Post,
Put,
Query,
Request,
Route,
Security,
Tags,
} from "tsoa";
import { RequestWithUser } from "../interfaces/user";
import prisma from "../db";
import HttpStatus from "../interfaces/http-status";
import HttpError from "../interfaces/http-error";
2024-06-10 11:14:21 +07:00
import minio, { presignedGetObjectIfExist } from "../services/minio";
2024-04-09 14:40:55 +07:00
if (!process.env.MINIO_BUCKET) {
throw Error("Require MinIO bucket.");
}
const MINIO_BUCKET = process.env.MINIO_BUCKET;
function imageLocation(id: string) {
2024-06-11 11:27:00 +07:00
return `employee/${id}/profile-image`;
2024-04-09 14:40:55 +07:00
}
type EmployeeCreate = {
customerBranchId: string;
status?: Status;
nrcNo: string;
dateOfBirth: Date;
gender: string;
nationality: string;
firstName: string;
firstNameEN: string;
lastName: string;
lastNameEN: string;
addressEN: string;
address: string;
zipCode: string;
2024-06-10 16:50:17 +07:00
passportType: string;
passportNumber: string;
passportIssueDate: Date;
passportExpiryDate: Date;
passportIssuingCountry: string;
passportIssuingPlace: string;
previousPassportReference?: string;
2024-06-12 17:01:42 +07:00
visaType?: string | null;
visaNumber?: string | null;
visaIssueDate?: Date | null;
visaExpiryDate?: Date | null;
visaIssuingPlace?: string | null;
visaStayUntilDate?: Date | null;
tm6Number?: string | null;
entryDate?: Date | null;
workerStatus?: string | null;
2024-04-09 14:40:55 +07:00
subDistrictId?: string | null;
districtId?: string | null;
provinceId?: string | null;
employeeWork?: {
2024-06-11 11:27:00 +07:00
ownerName?: string | null;
positionName?: string | null;
jobType?: string | null;
workplace?: string | null;
workPermitNo?: string | null;
workPermitIssuDate?: Date | null;
workPermitExpireDate?: Date | null;
workEndDate?: Date | null;
remark?: string | null;
}[];
employeeCheckup?: {
2024-06-11 11:27:00 +07:00
checkupType?: string | null;
checkupResult?: string | null;
provinceId?: string | null;
2024-06-11 11:27:00 +07:00
hospitalName?: string | null;
remark?: string | null;
medicalBenefitScheme?: string | null;
insuranceCompany?: string | null;
coverageStartDate?: Date | null;
coverageExpireDate?: Date | null;
}[];
2024-06-11 11:27:00 +07:00
employeeOtherInfo?: {
citizenId?: string | null;
fatherFirstName?: string | null;
fatherLastName?: string | null;
2024-06-11 16:13:43 +07:00
fatherBirthPlace?: string | null;
2024-06-11 11:27:00 +07:00
motherFirstName?: string | null;
motherLastName?: string | null;
2024-06-11 16:13:43 +07:00
motherBirthPlace?: string | null;
2024-06-11 11:27:00 +07:00
fatherFirstNameEN?: string | null;
fatherLastNameEN?: string | null;
motherFirstNameEN?: string | null;
motherLastNameEN?: string | null;
};
2024-04-09 14:40:55 +07:00
};
type EmployeeUpdate = {
customerBranchId?: string;
status?: "ACTIVE" | "INACTIVE";
nrcNo?: string;
dateOfBirth?: Date;
gender?: string;
nationality?: string;
firstName?: string;
firstNameEN?: string;
lastName?: string;
lastNameEN?: string;
addressEN?: string;
address?: string;
zipCode?: string;
2024-06-10 16:50:17 +07:00
passportType?: string;
passportNumber?: string;
passportIssueDate?: Date;
passportExpiryDate?: Date;
passportIssuingCountry?: string;
passportIssuingPlace?: string;
previousPassportReference?: string;
2024-06-12 17:01:42 +07:00
visaType?: string | null;
visaNumber?: string | null;
visaIssueDate?: Date | null;
visaExpiryDate?: Date | null;
visaIssuingPlace?: string | null;
visaStayUntilDate?: Date | null;
tm6Number?: string | null;
entryDate?: Date | null;
workerStatus?: string | null;
2024-04-09 14:40:55 +07:00
subDistrictId?: string | null;
districtId?: string | null;
provinceId?: string | null;
employeeWork?: {
id?: string;
2024-06-11 11:27:00 +07:00
ownerName?: string | null;
positionName?: string | null;
jobType?: string | null;
workplace?: string | null;
workPermitNo?: string | null;
workPermitIssuDate?: Date | null;
workPermitExpireDate?: Date | null;
workEndDate?: Date | null;
remark?: string | null;
}[];
employeeCheckup?: {
id?: string;
2024-06-11 11:27:00 +07:00
checkupType?: string | null;
checkupResult?: string | null;
provinceId?: string | null;
2024-06-11 11:27:00 +07:00
hospitalName?: string | null;
remark?: string | null;
medicalBenefitScheme?: string | null;
insuranceCompany?: string | null;
coverageStartDate?: Date | null;
coverageExpireDate?: Date | null;
}[];
employeeOtherInfo: {
2024-06-11 11:27:00 +07:00
citizenId?: string | null;
fatherFirstName?: string | null;
fatherLastName?: string | null;
fatherBirthPlace?: string | null;
motherFirstName?: string | null;
motherLastName?: string | null;
motherBirthPlace?: string | null;
fatherFirstNameEN?: string | null;
fatherLastNameEN?: string | null;
motherFirstNameEN?: string | null;
motherLastNameEN?: string | null;
};
2024-04-09 14:40:55 +07:00
};
2024-06-06 09:42:02 +07:00
@Route("api/v1/employee")
2024-04-09 14:40:55 +07:00
@Tags("Employee")
@Security("keycloak")
export class EmployeeController extends Controller {
2024-06-11 09:48:11 +07:00
@Get("stats")
async getEmployeeStats(@Query() customerBranchId?: string) {
return await prisma.employee.count({
where: { customerBranchId },
});
}
2024-06-12 15:52:57 +07:00
@Get("stats/gender")
async getEmployeeStatsGender(@Query() customerBranchId?: string) {
return await prisma.employee
.groupBy({
_count: true,
by: ["gender"],
where: { customerBranchId },
})
.then((res) =>
res.reduce<Record<string, number>>((a, c) => {
a[c.gender] = c._count;
return a;
}, {}),
);
}
2024-04-09 14:40:55 +07:00
@Get()
async list(
@Query() zipCode?: string,
@Query() query: string = "",
@Query() page: number = 1,
@Query() pageSize: number = 30,
) {
const where = {
OR: [
{ firstName: { contains: query }, zipCode },
{ firstNameEN: { contains: query }, zipCode },
{ lastName: { contains: query }, zipCode },
{ lastNameEN: { contains: query }, zipCode },
],
} satisfies Prisma.EmployeeWhereInput;
const [result, total] = await prisma.$transaction([
prisma.employee.findMany({
orderBy: { createdAt: "asc" },
include: {
province: true,
district: true,
subDistrict: true,
},
where,
take: pageSize,
skip: (page - 1) * pageSize,
}),
prisma.employee.count({ where }),
]);
return {
result: await Promise.all(
result.map(async (v) => ({
...v,
2024-06-10 11:14:21 +07:00
profileImageUrl: await presignedGetObjectIfExist(
2024-04-09 14:40:55 +07:00
MINIO_BUCKET,
imageLocation(v.id),
12 * 60 * 60,
),
})),
),
page,
pageSize,
total,
};
}
@Get("{employeeId}")
async getById(@Path() employeeId: string) {
const record = await prisma.employee.findFirst({
include: {
province: true,
district: true,
subDistrict: true,
},
where: { id: employeeId },
});
if (!record) {
throw new HttpError(HttpStatus.NOT_FOUND, "Employee cannot be found.", "data_not_found");
}
return record;
}
@Post()
async create(@Request() req: RequestWithUser, @Body() body: EmployeeCreate) {
2024-06-06 13:47:13 +07:00
const [province, district, subDistrict, customerBranch] = 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.customerBranch.findFirst({
2024-06-13 16:45:08 +07:00
where: { id: body.customerBranchId },
include: { customer: true },
2024-06-06 13:47:13 +07:00
}),
]);
if (body.provinceId && !province)
throw new HttpError(
HttpStatus.BAD_REQUEST,
"Province cannot be found.",
"missing_or_invalid_parameter",
);
if (body.districtId && !district)
throw new HttpError(
HttpStatus.BAD_REQUEST,
"District cannot be found.",
"missing_or_invalid_parameter",
);
if (body.subDistrictId && !subDistrict)
throw new HttpError(
HttpStatus.BAD_REQUEST,
"Sub-district cannot be found.",
"missing_or_invalid_parameter",
);
if (!customerBranch)
throw new HttpError(
HttpStatus.BAD_REQUEST,
"Customer Branch cannot be found.",
"missing_or_invalid_parameter",
);
2024-04-09 14:40:55 +07:00
const {
provinceId,
districtId,
subDistrictId,
customerBranchId,
employeeWork,
employeeCheckup,
employeeOtherInfo,
...rest
} = body;
const listProvinceId = employeeCheckup?.reduce<string[]>((acc, cur) => {
if (cur.provinceId && !acc.includes(cur.provinceId)) return acc.concat(cur.provinceId);
2024-06-11 16:41:39 +07:00
if (!cur.provinceId) cur.provinceId = null;
return acc;
}, []);
if (listProvinceId) {
const [listProvince] = await prisma.$transaction([
prisma.province.findMany({ where: { id: { in: listProvinceId } } }),
]);
if (listProvince.length !== listProvinceId.length) {
throw new HttpError(
HttpStatus.BAD_REQUEST,
"Some province cannot be found.",
"missing_or_invalid_parameter",
);
}
}
2024-04-09 14:40:55 +07:00
2024-06-06 13:47:13 +07:00
const record = await prisma.$transaction(
async (tx) => {
const last = await tx.runningNo.upsert({
where: {
2024-06-11 14:44:55 +07:00
key: `EMPLOYEE_${customerBranch.customer.code}-${customerBranch.branchNo.toString().padStart(2, "0")}-${new Date().getFullYear().toString().slice(-2).padStart(2, "0")}`,
2024-06-06 13:47:13 +07:00
},
create: {
2024-06-11 14:44:55 +07:00
key: `EMPLOYEE_${customerBranch.customer.code}-${customerBranch.branchNo.toString().padStart(2, "0")}-${new Date().getFullYear().toString().slice(-2).padStart(2, "0")}`,
2024-06-06 13:47:13 +07:00
value: 1,
},
update: { value: { increment: 1 } },
});
return await prisma.employee.create({
include: {
province: true,
district: true,
subDistrict: true,
employeeOtherInfo: true,
employeeCheckup: {
include: {
province: true,
},
},
employeeWork: true,
2024-06-06 13:47:13 +07:00
},
data: {
...rest,
2024-06-11 14:44:55 +07:00
code: `${customerBranch.customer.code}-${customerBranch.branchNo.toString().padStart(2, "0")}-${new Date().getFullYear().toString().slice(-2).padStart(2, "0")}${last.value.toString().padStart(4, "0")}`,
employeeWork: {
createMany: {
data: employeeWork || [],
},
},
employeeCheckup: {
createMany: {
data: employeeCheckup || [],
},
},
employeeOtherInfo: {
create: employeeOtherInfo,
},
2024-06-06 13:47:13 +07:00
province: { connect: provinceId ? { id: provinceId } : undefined },
district: { connect: districtId ? { id: districtId } : undefined },
subDistrict: { connect: subDistrictId ? { id: subDistrictId } : undefined },
customerBranch: { connect: { id: customerBranchId } },
createdBy: req.user.name,
updateBy: req.user.name,
},
});
2024-04-09 14:40:55 +07:00
},
2024-06-06 13:47:13 +07:00
{ isolationLevel: Prisma.TransactionIsolationLevel.Serializable },
);
2024-04-09 14:40:55 +07:00
await prisma.customerBranch.updateMany({
where: { id: customerBranchId, status: Status.CREATED },
data: { status: Status.ACTIVE },
});
await prisma.customer.updateMany({
where: {
branch: {
some: { id: customerBranchId },
},
status: Status.CREATED,
},
data: { status: Status.ACTIVE },
});
2024-04-09 14:40:55 +07:00
this.setStatus(HttpStatus.CREATED);
return Object.assign(record, {
2024-06-10 11:14:21 +07:00
profileImageUrl: await presignedGetObjectIfExist(
2024-04-09 14:40:55 +07:00
MINIO_BUCKET,
imageLocation(record.id),
12 * 60 * 60,
),
profileImageUploadUrl: await minio.presignedPutObject(
MINIO_BUCKET,
imageLocation(record.id),
12 * 60 * 60,
),
});
}
@Put("{employeeId}")
async editById(
@Request() req: RequestWithUser,
@Body() body: EmployeeUpdate,
@Path() employeeId: string,
) {
2024-06-13 16:45:08 +07:00
const [province, district, subDistrict, customerBranch, employee] = 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.customerBranch.findFirst({
where: { id: body.customerBranchId || undefined },
include: { customer: true },
}),
prisma.employee.findFirst({ where: { id: employeeId } }),
]);
if (body.provinceId && !province)
throw new HttpError(
HttpStatus.BAD_REQUEST,
"Province cannot be found.",
"missing_or_invalid_parameter",
);
if (body.districtId && !district)
throw new HttpError(
HttpStatus.BAD_REQUEST,
"District cannot be found.",
"missing_or_invalid_parameter",
);
if (body.subDistrictId && !subDistrict)
throw new HttpError(
HttpStatus.BAD_REQUEST,
"Sub-district cannot be found.",
"missing_or_invalid_parameter",
);
if (body.customerBranchId && !customerBranch)
throw new HttpError(
HttpStatus.BAD_REQUEST,
"Customer cannot be found.",
"missing_or_invalid_parameter",
);
if (!employee) {
throw new HttpError(HttpStatus.NOT_FOUND, "Employee cannot be found.", "data_not_found");
2024-04-09 14:40:55 +07:00
}
const {
provinceId,
districtId,
subDistrictId,
customerBranchId,
employeeWork,
employeeCheckup,
employeeOtherInfo,
...rest
} = body;
const listProvinceId = employeeCheckup?.reduce<string[]>((acc, cur) => {
if (cur.provinceId && !acc.includes(cur.provinceId)) return acc.concat(cur.provinceId);
return acc;
}, []);
if (listProvinceId) {
const [listProvince] = await prisma.$transaction([
prisma.province.findMany({ where: { id: { in: listProvinceId } } }),
]);
if (listProvince.length !== listProvinceId.length) {
throw new HttpError(
HttpStatus.BAD_REQUEST,
"Some province cannot be found.",
"missing_or_invalid_parameter",
);
}
}
2024-04-09 14:40:55 +07:00
2024-06-13 16:45:08 +07:00
const record = await prisma.$transaction(async (tx) => {
let code: string | undefined;
2024-06-13 16:54:42 +07:00
if (customerBranch && customerBranch.id !== employee.customerBranchId) {
2024-06-13 16:45:08 +07:00
const last = await tx.runningNo.upsert({
where: {
key: `EMPLOYEE_${customerBranch.customer.code}-${customerBranch.branchNo.toString().padStart(2, "0")}-${new Date().getFullYear().toString().slice(-2).padStart(2, "0")}`,
},
2024-06-13 16:45:08 +07:00
create: {
key: `EMPLOYEE_${customerBranch.customer.code}-${customerBranch.branchNo.toString().padStart(2, "0")}-${new Date().getFullYear().toString().slice(-2).padStart(2, "0")}`,
value: 1,
},
update: { value: { increment: 1 } },
});
code = `${customerBranch.customer.code}-${customerBranch.branchNo.toString().padStart(2, "0")}-${new Date().getFullYear().toString().slice(-2).padStart(2, "0")}${last.value.toString().padStart(4, "0")}`;
}
return await prisma.employee.update({
where: { id: employeeId },
include: {
province: true,
district: true,
subDistrict: true,
employeeOtherInfo: true,
employeeCheckup: {
include: {
province: true,
},
},
employeeWork: true,
},
2024-06-13 16:45:08 +07:00
data: {
...rest,
code,
customerBranch: { connect: customerBranchId ? { id: customerBranchId } : undefined },
employeeWork: employeeWork
? {
deleteMany: {
id: {
notIn: employeeWork.map((v) => v.id).filter((v): v is string => !!v) || [],
},
},
2024-06-13 16:45:08 +07:00
upsert: employeeWork.map((v) => ({
where: { id: v.id || "" },
create: {
...v,
createdBy: req.user.name,
updateBy: req.user.name,
id: undefined,
},
update: {
...v,
updateBy: req.user.name,
},
})),
}
: undefined,
employeeCheckup: employeeCheckup
? {
deleteMany: {
id: {
notIn: employeeCheckup.map((v) => v.id).filter((v): v is string => !!v) || [],
},
},
2024-06-13 16:45:08 +07:00
upsert: employeeCheckup.map((v) => ({
where: { id: v.id || "" },
create: {
...v,
createdBy: req.user.name,
updateBy: req.user.name,
id: undefined,
},
update: {
...v,
updateBy: req.user.name,
},
})),
}
: undefined,
employeeOtherInfo: employeeOtherInfo
? {
deleteMany: {},
create: employeeOtherInfo,
}
: undefined,
province: {
connect: provinceId ? { id: provinceId } : undefined,
disconnect: provinceId === null || undefined,
},
district: {
connect: districtId ? { id: districtId } : undefined,
disconnect: districtId === null || undefined,
},
subDistrict: {
connect: subDistrictId ? { id: subDistrictId } : undefined,
disconnect: subDistrictId === null || undefined,
},
createdBy: req.user.name,
updateBy: req.user.name,
2024-04-09 14:40:55 +07:00
},
2024-06-13 16:45:08 +07:00
});
2024-04-09 14:40:55 +07:00
});
2024-06-13 11:05:39 +07:00
return Object.assign(record, {
profileImageUrl: await presignedGetObjectIfExist(
MINIO_BUCKET,
imageLocation(record.id),
12 * 60 * 60,
),
profileImageUploadUrl: await minio.presignedPutObject(
MINIO_BUCKET,
imageLocation(record.id),
12 * 60 * 60,
),
});
2024-04-09 14:40:55 +07:00
}
@Delete("{employeeId}")
async delete(@Path() employeeId: string) {
const record = await prisma.employee.findFirst({ where: { id: employeeId } });
if (!record) {
throw new HttpError(HttpStatus.NOT_FOUND, "Employee cannot be found.", "data_not_found");
}
if (record.status !== Status.CREATED) {
throw new HttpError(
HttpStatus.FORBIDDEN,
"Employee is in used.",
2024-04-09 14:40:55 +07:00
"missing_or_invalid_parameter",
);
}
return await prisma.employee.delete({ where: { id: employeeId } });
}
}