From af4489dccc589867d883008afc99b7e7e7558eb0 Mon Sep 17 00:00:00 2001 From: Methapon2001 <61303214+Methapon2001@users.noreply.github.com> Date: Tue, 11 Jun 2024 11:27:00 +0700 Subject: [PATCH] feat: update field --- .../migration.sql | 31 +++++ .../20240611042536_add_field/migration.sql | 10 ++ prisma/schema.prisma | 53 ++++---- .../employee-checkup-controller.ts | 36 ++---- src/controllers/employee-controller.ts | 113 +++++++++--------- .../employee-other-info-controller.ts | 43 +++---- src/controllers/employee-work-controller.ts | 36 ++---- 7 files changed, 162 insertions(+), 160 deletions(-) create mode 100644 prisma/migrations/20240611040600_make_field_optional/migration.sql create mode 100644 prisma/migrations/20240611042536_add_field/migration.sql diff --git a/prisma/migrations/20240611040600_make_field_optional/migration.sql b/prisma/migrations/20240611040600_make_field_optional/migration.sql new file mode 100644 index 0000000..595a50d --- /dev/null +++ b/prisma/migrations/20240611040600_make_field_optional/migration.sql @@ -0,0 +1,31 @@ +-- AlterTable +ALTER TABLE "EmployeeCheckup" ALTER COLUMN "checkupResult" DROP NOT NULL, +ALTER COLUMN "checkupType" DROP NOT NULL, +ALTER COLUMN "hospitalName" DROP NOT NULL, +ALTER COLUMN "remark" DROP NOT NULL, +ALTER COLUMN "medicalBenefitScheme" DROP NOT NULL, +ALTER COLUMN "insuranceCompany" DROP NOT NULL, +ALTER COLUMN "coverageStartDate" DROP NOT NULL, +ALTER COLUMN "coverageExpireDate" DROP NOT NULL; + +-- AlterTable +ALTER TABLE "EmployeeOtherInfo" ALTER COLUMN "citizenId" DROP NOT NULL, +ALTER COLUMN "fatherFirstName" DROP NOT NULL, +ALTER COLUMN "fatherFirstNameEN" DROP NOT NULL, +ALTER COLUMN "fatherLastName" DROP NOT NULL, +ALTER COLUMN "fatherLastNameEN" DROP NOT NULL, +ALTER COLUMN "motherFirstName" DROP NOT NULL, +ALTER COLUMN "motherFirstNameEN" DROP NOT NULL, +ALTER COLUMN "motherLastName" DROP NOT NULL, +ALTER COLUMN "motherLastNameEN" DROP NOT NULL, +ALTER COLUMN "birthPlace" DROP NOT NULL; + +-- AlterTable +ALTER TABLE "EmployeeWork" ALTER COLUMN "ownerName" DROP NOT NULL, +ALTER COLUMN "positionName" DROP NOT NULL, +ALTER COLUMN "jobType" DROP NOT NULL, +ALTER COLUMN "workplace" DROP NOT NULL, +ALTER COLUMN "workPermitNo" DROP NOT NULL, +ALTER COLUMN "workPermitIssuDate" DROP NOT NULL, +ALTER COLUMN "workPermitExpireDate" DROP NOT NULL, +ALTER COLUMN "workEndDate" DROP NOT NULL; diff --git a/prisma/migrations/20240611042536_add_field/migration.sql b/prisma/migrations/20240611042536_add_field/migration.sql new file mode 100644 index 0000000..d98d0b1 --- /dev/null +++ b/prisma/migrations/20240611042536_add_field/migration.sql @@ -0,0 +1,10 @@ +/* + Warnings: + + - You are about to drop the column `birthPlace` on the `EmployeeOtherInfo` table. All the data in the column will be lost. + +*/ +-- AlterTable +ALTER TABLE "EmployeeOtherInfo" DROP COLUMN "birthPlace", +ADD COLUMN "fatherBirthPlace" TEXT, +ADD COLUMN "motherBirthPlace" TEXT; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index a93e1dc..f8bf42e 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -467,18 +467,18 @@ model EmployeeCheckup { employee Employee @relation(fields: [employeeId], references: [id], onDelete: Cascade) employeeId String - checkupResult String - checkupType String + checkupResult String? + checkupType String? province Province? @relation(fields: [provinceId], references: [id], onDelete: SetNull) provinceId String? - hospitalName String - remark String - medicalBenefitScheme String - insuranceCompany String - coverageStartDate DateTime - coverageExpireDate DateTime + hospitalName String? + remark String? + medicalBenefitScheme String? + insuranceCompany String? + coverageStartDate DateTime? + coverageExpireDate DateTime? createdBy String? createdAt DateTime @default(now()) @@ -492,14 +492,14 @@ model EmployeeWork { employee Employee @relation(fields: [employeeId], references: [id], onDelete: Cascade) employeeId String - ownerName String - positionName String - jobType String - workplace String - workPermitNo String - workPermitIssuDate DateTime - workPermitExpireDate DateTime - workEndDate DateTime + ownerName String? + positionName String? + jobType String? + workplace String? + workPermitNo String? + workPermitIssuDate DateTime? + workPermitExpireDate DateTime? + workEndDate DateTime? remark String? createdBy String? @@ -514,17 +514,18 @@ model EmployeeOtherInfo { employee Employee @relation(fields: [employeeId], references: [id], onDelete: Cascade) employeeId String - citizenId String - birthPlace String - fatherFirstName String - fatherLastName String - motherFirstName String - motherLastName String + citizenId String? + fatherBirthPlace String? + fatherFirstName String? + fatherLastName String? + motherBirthPlace String? + motherFirstName String? + motherLastName String? - fatherFirstNameEN String - fatherLastNameEN String - motherFirstNameEN String - motherLastNameEN String + fatherFirstNameEN String? + fatherLastNameEN String? + motherFirstNameEN String? + motherLastNameEN String? createdBy String? createdAt DateTime @default(now()) diff --git a/src/controllers/employee-checkup-controller.ts b/src/controllers/employee-checkup-controller.ts index eff4b5c..ede52f4 100644 --- a/src/controllers/employee-checkup-controller.ts +++ b/src/controllers/employee-checkup-controller.ts @@ -16,32 +16,18 @@ import prisma from "../db"; import HttpStatus from "../interfaces/http-status"; import HttpError from "../interfaces/http-error"; -type EmployeeCheckupCreate = { - checkupType: string; - checkupResult: string; +type EmployeeCheckupPayload = { + checkupType?: string | null; + checkupResult?: string | null; provinceId?: string | null; - hospitalName: string; - remark: string; - medicalBenefitScheme: string; - insuranceCompany: string; - coverageStartDate: Date; - coverageExpireDate: Date; -}; - -type EmployeeCheckupEdit = { - checkupType?: string; - checkupResult?: string; - - provinceId?: string | null; - - hospitalName?: string; - remark?: string; - medicalBenefitScheme?: string; - insuranceCompany?: string; - coverageStartDate?: Date; - coverageExpireDate?: Date; + hospitalName?: string | null; + remark?: string | null; + medicalBenefitScheme?: string | null; + insuranceCompany?: string | null; + coverageStartDate?: Date | null; + coverageExpireDate?: Date | null; }; @Route("api/v1/employee/{employeeId}/checkup") @@ -75,7 +61,7 @@ export class EmployeeCheckupController extends Controller { async create( @Request() req: RequestWithUser, @Path() employeeId: string, - @Body() body: EmployeeCheckupCreate, + @Body() body: EmployeeCheckupPayload, ) { if (body.provinceId || employeeId) { const [province, employee] = await prisma.$transaction([ @@ -119,7 +105,7 @@ export class EmployeeCheckupController extends Controller { @Request() req: RequestWithUser, @Path() employeeId: string, @Path() checkupId: string, - @Body() body: EmployeeCheckupEdit, + @Body() body: EmployeeCheckupPayload, ) { if (body.provinceId || employeeId) { const [province, employee] = await prisma.$transaction([ diff --git a/src/controllers/employee-controller.ts b/src/controllers/employee-controller.ts index 6f7f297..966f437 100644 --- a/src/controllers/employee-controller.ts +++ b/src/controllers/employee-controller.ts @@ -26,7 +26,7 @@ if (!process.env.MINIO_BUCKET) { const MINIO_BUCKET = process.env.MINIO_BUCKET; function imageLocation(id: string) { - return `employee/profile-img-${id}`; + return `employee/${id}/profile-image`; } type EmployeeCreate = { @@ -72,43 +72,43 @@ type EmployeeCreate = { provinceId?: string | null; employeeWork?: { - ownerName: string; - positionName: string; - jobType: string; - workplace: string; - workPermitNo: string; - workPermitIssuDate: Date; - workPermitExpireDate: Date; - workEndDate: Date; - remark?: string; + 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?: { - checkupType: string; - checkupResult: string; + checkupType?: string | null; + checkupResult?: string | null; provinceId?: string | null; - hospitalName: string; - remark: string; - medicalBenefitScheme: string; - insuranceCompany: string; - coverageStartDate: Date; - coverageExpireDate: Date; + hospitalName?: string | null; + remark?: string | null; + medicalBenefitScheme?: string | null; + insuranceCompany?: string | null; + coverageStartDate?: Date | null; + coverageExpireDate?: Date | null; }[]; - employeeOtherInfo: { - citizenId: string; - fatherFirstName: string; - fatherLastName: string; - motherFirstName: string; - motherLastName: string; + employeeOtherInfo?: { + citizenId?: string | null; + fatherFirstName?: string | null; + fatherLastName?: string | null; + motherFirstName?: string | null; + motherLastName?: string | null; - fatherFirstNameEN: string; - fatherLastNameEN: string; - motherFirstNameEN: string; - motherLastNameEN: string; - birthPlace: string; + fatherFirstNameEN?: string | null; + fatherLastNameEN?: string | null; + motherFirstNameEN?: string | null; + motherLastNameEN?: string | null; + birthPlace?: string | null; }; }; @@ -155,44 +155,45 @@ type EmployeeUpdate = { employeeWork?: { id?: string; - ownerName: string; - positionName: string; - jobType: string; - workplace: string; - workPermitNo: string; - workPermitIssuDate: Date; - workPermitExpireDate: Date; - workEndDate: Date; - remark?: string; + 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; - checkupType: string; - checkupResult: string; + checkupType?: string | null; + checkupResult?: string | null; provinceId?: string | null; - hospitalName: string; - remark: string; - medicalBenefitScheme: string; - insuranceCompany: string; - coverageStartDate: Date; - coverageExpireDate: Date; + hospitalName?: string | null; + remark?: string | null; + medicalBenefitScheme?: string | null; + insuranceCompany?: string | null; + coverageStartDate?: Date | null; + coverageExpireDate?: Date | null; }[]; employeeOtherInfo: { - citizenId: string; - fatherFirstName: string; - fatherLastName: string; - motherFirstName: string; - motherLastName: string; + citizenId?: string | null; + fatherFirstName?: string | null; + fatherLastName?: string | null; + fatherBirthPlace?: string | null; + motherFirstName?: string | null; + motherLastName?: string | null; + motherBirthPlace?: string | null; - fatherFirstNameEN: string; - fatherLastNameEN: string; - motherFirstNameEN: string; - motherLastNameEN: string; - birthPlace: string; + fatherFirstNameEN?: string | null; + fatherLastNameEN?: string | null; + motherFirstNameEN?: string | null; + motherLastNameEN?: string | null; }; }; diff --git a/src/controllers/employee-other-info-controller.ts b/src/controllers/employee-other-info-controller.ts index c866678..58e0ff5 100644 --- a/src/controllers/employee-other-info-controller.ts +++ b/src/controllers/employee-other-info-controller.ts @@ -1,4 +1,3 @@ -import { Prisma, Status } from "@prisma/client"; import { Body, Controller, @@ -7,7 +6,6 @@ import { Put, Path, Post, - Query, Request, Route, Security, @@ -19,32 +17,19 @@ import HttpError from "../interfaces/http-error"; import HttpStatus from "../interfaces/http-status"; import { RequestWithUser } from "../interfaces/user"; -type EmployeeOtherInfoCreate = { - citizenId: string; - fatherFirstName: string; - fatherLastName: string; - motherFirstName: string; - motherLastName: string; +type EmployeeOtherInfoPayload = { + citizenId?: string | null; + fatherFirstName?: string | null; + fatherLastName?: string | null; + fatherBirthPlace?: string | null; + motherFirstName?: string | null; + motherLastName?: string | null; + motherBirthPlace?: string | null; - fatherFirstNameEN: string; - fatherLastNameEN: string; - motherFirstNameEN: string; - motherLastNameEN: string; - birthPlace: string; -}; - -type EmployeeOtherInfoUpdate = { - citizenId: string; - fatherFirstName: string; - fatherLastName: string; - motherFirstName: string; - motherLastName: string; - - fatherFirstNameEN: string; - fatherLastNameEN: string; - motherFirstNameEN: string; - motherLastNameEN: string; - birthPlace: string; + fatherFirstNameEN?: string | null; + fatherLastNameEN?: string | null; + motherFirstNameEN?: string | null; + motherLastNameEN?: string | null; }; @Route("api/v1/employee/{employeeId}/other-info") @@ -74,7 +59,7 @@ export class EmployeeOtherInfo extends Controller { async create( @Request() req: RequestWithUser, @Path() employeeId: string, - @Body() body: EmployeeOtherInfoCreate, + @Body() body: EmployeeOtherInfoPayload, ) { if (!(await prisma.employee.findUnique({ where: { id: employeeId } }))) throw new HttpError( @@ -102,7 +87,7 @@ export class EmployeeOtherInfo extends Controller { @Request() req: RequestWithUser, @Path() employeeId: string, @Path() otherInfoId: string, - @Body() body: EmployeeOtherInfoUpdate, + @Body() body: EmployeeOtherInfoPayload, ) { if (!(await prisma.employeeOtherInfo.findUnique({ where: { id: otherInfoId, employeeId } }))) { throw new HttpError( diff --git a/src/controllers/employee-work-controller.ts b/src/controllers/employee-work-controller.ts index 0b48fbb..90f1c8b 100644 --- a/src/controllers/employee-work-controller.ts +++ b/src/controllers/employee-work-controller.ts @@ -16,28 +16,16 @@ import prisma from "../db"; import HttpStatus from "../interfaces/http-status"; import HttpError from "../interfaces/http-error"; -type EmployeeWorkCreate = { - ownerName: string; - positionName: string; - jobType: string; - workplace: string; - workPermitNo: string; - workPermitIssuDate: Date; - workPermitExpireDate: Date; - workEndDate: Date; - remark?: string; -}; - -type EmployeeWorkUpdate = { - ownerName?: string; - positionName?: string; - jobType?: string; - workplace?: string; - workPermitNo?: string; - workPermitIssuDate?: Date; - workPermitExpireDate?: Date; - workEndDate?: Date; - remark?: string; +type EmployeeWorkPayload = { + 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; }; @Route("api/v1/employee/{employeeId}/work") @@ -67,7 +55,7 @@ export class EmployeeWorkController extends Controller { async create( @Request() req: RequestWithUser, @Path() employeeId: string, - @Body() body: EmployeeWorkCreate, + @Body() body: EmployeeWorkPayload, ) { if (!(await prisma.employee.findUnique({ where: { id: employeeId } }))) throw new HttpError( @@ -95,7 +83,7 @@ export class EmployeeWorkController extends Controller { @Request() req: RequestWithUser, @Path() employeeId: string, @Path() workId: string, - @Body() body: EmployeeWorkUpdate, + @Body() body: EmployeeWorkPayload, ) { if (!(await prisma.employeeWork.findUnique({ where: { id: workId, employeeId } }))) { throw new HttpError(HttpStatus.NOT_FOUND, "Employee work cannot be found.", "data_not_found");