From 3a5339774549220cee8da0b6cb0e051fe4a00ef6 Mon Sep 17 00:00:00 2001 From: Methapon2001 <61303214+Methapon2001@users.noreply.github.com> Date: Tue, 2 Apr 2024 09:28:36 +0700 Subject: [PATCH 1/7] feat: add branch controller --- src/controllers/branch/branch-controller.ts | 25 +++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 src/controllers/branch/branch-controller.ts diff --git a/src/controllers/branch/branch-controller.ts b/src/controllers/branch/branch-controller.ts new file mode 100644 index 0000000..51d03f3 --- /dev/null +++ b/src/controllers/branch/branch-controller.ts @@ -0,0 +1,25 @@ +import { Prisma } from "@prisma/client"; +import { + Body, + Controller, + Delete, + Get, + Patch, + Path, + Post, + Query, + Request, + Route, + Security, + Tags, +} from "tsoa"; + +import prisma from "../../db"; +import HttpError from "../../interfaces/http-error"; +import HttpStatus from "../../interfaces/http-status"; +import { RequestWithUser } from "../../interfaces/user"; +@Route("api/branch") +@Tags("Branch") +@Security("keycloak") +export class BranchController extends Controller { +} From 59a79a3374405580aa491899c9cc7782bc4af8a9 Mon Sep 17 00:00:00 2001 From: Methapon2001 <61303214+Methapon2001@users.noreply.github.com> Date: Tue, 2 Apr 2024 09:29:20 +0700 Subject: [PATCH 2/7] feat: get branch endpoint --- src/controllers/branch/branch-controller.ts | 43 +++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/src/controllers/branch/branch-controller.ts b/src/controllers/branch/branch-controller.ts index 51d03f3..7f8c448 100644 --- a/src/controllers/branch/branch-controller.ts +++ b/src/controllers/branch/branch-controller.ts @@ -22,4 +22,47 @@ import { RequestWithUser } from "../../interfaces/user"; @Tags("Branch") @Security("keycloak") export class BranchController extends Controller { + @Get() + async getBranch( + @Query() zipCode?: string, + @Query() query: string = "", + @Query() page: number = 1, + @Query() pageSize: number = 30, + ) { + const where = { + OR: [ + { nameEN: { contains: query }, zipCode }, + { nameTH: { contains: query }, zipCode }, + { email: { contains: query }, zipCode }, + ], + } satisfies Prisma.BranchWhereInput; + + const [result, total] = await prisma.$transaction([ + prisma.branch.findMany({ + include: { + province: true, + district: true, + subDistrict: true, + }, + where, + take: pageSize, + skip: (page - 1) * pageSize, + }), + prisma.branch.count({ where }), + ]); + + return { result, page, pageSize, total }; + } + + @Get("{branchId}") + async getBranchById(@Path() branchId: string) { + return await prisma.branch.findFirst({ + include: { + province: true, + district: true, + subDistrict: true, + }, + where: { id: branchId }, + }); + } } From 9938dba0e1d7587bd8a8d7285f3fa7142571cb61 Mon Sep 17 00:00:00 2001 From: Methapon2001 <61303214+Methapon2001@users.noreply.github.com> Date: Tue, 2 Apr 2024 10:45:37 +0700 Subject: [PATCH 3/7] feat: delete branch endpoint --- src/controllers/branch/branch-controller.ts | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/controllers/branch/branch-controller.ts b/src/controllers/branch/branch-controller.ts index 7f8c448..99e78b2 100644 --- a/src/controllers/branch/branch-controller.ts +++ b/src/controllers/branch/branch-controller.ts @@ -65,4 +65,19 @@ export class BranchController extends Controller { where: { id: branchId }, }); } + + @Delete("{branchId}") + async deleteBranch(@Path() branchId: string) { + const record = await prisma.branch.findFirst({ where: { id: branchId } }); + + if (!record) { + throw new HttpError(HttpStatus.NOT_FOUND, "Branch cannot be found."); + } + + if (record.status === Status.USED) { + throw new HttpError(HttpStatus.FORBIDDEN, "Branch is in used."); + } + + return await prisma.branch.delete({ where: { id: branchId } }); + } } From 95463dfe7c6844d43b6a55b98d200fc60b708b6e Mon Sep 17 00:00:00 2001 From: Methapon2001 <61303214+Methapon2001@users.noreply.github.com> Date: Tue, 2 Apr 2024 10:47:36 +0700 Subject: [PATCH 4/7] feat: add type for create and update endpoint --- src/controllers/branch/branch-controller.ts | 41 ++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/src/controllers/branch/branch-controller.ts b/src/controllers/branch/branch-controller.ts index 99e78b2..ef032bd 100644 --- a/src/controllers/branch/branch-controller.ts +++ b/src/controllers/branch/branch-controller.ts @@ -1,4 +1,4 @@ -import { Prisma } from "@prisma/client"; +import { Prisma, Status } from "@prisma/client"; import { Body, Controller, @@ -18,6 +18,45 @@ import prisma from "../../db"; import HttpError from "../../interfaces/http-error"; import HttpStatus from "../../interfaces/http-status"; import { RequestWithUser } from "../../interfaces/user"; + +type BranchCreate = { + code: string; + taxNo: string; + nameEN: string; + nameTH: string; + addressEN: string; + addressTH: string; + zipCode: string; + email: string; + telephoneNo: string; + longitude: string; + latitude: string; + + subDistrictId?: string | null; + districtId?: string | null; + provinceId?: string | null; + headOfficeId?: string | null; +}; + +type BranchUpdate = { + code?: string; + taxNo?: string; + nameEN?: string; + nameTH?: string; + addressEN?: string; + addressTH?: string; + zipCode?: string; + email?: string; + telephoneNo?: string; + longitude?: string; + latitude?: string; + + subDistrictId?: string | null; + districtId?: string | null; + provinceId?: string | null; + headOfficeId?: string | null; +}; + @Route("api/branch") @Tags("Branch") @Security("keycloak") From a39b9de4232d6098183383dcefad7aaaeb9c8f3a Mon Sep 17 00:00:00 2001 From: Methapon2001 <61303214+Methapon2001@users.noreply.github.com> Date: Tue, 2 Apr 2024 10:48:35 +0700 Subject: [PATCH 5/7] feat: update database field --- prisma/schema.prisma | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/prisma/schema.prisma b/prisma/schema.prisma index abd8bb9..dcc50ef 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -65,6 +65,11 @@ model SubDistrict { employee Employee[] } +enum Status { + CREATED + USED +} + model Branch { id String @id @default(uuid()) code String @@ -96,7 +101,7 @@ model Branch { headOffice Branch? @relation(name: "HeadOfficeRelation", fields: [headOfficeId], references: [id]) headOfficeId String? - status String? + status Status @default(CREATED) createdBy String? createdAt DateTime @default(now()) @@ -109,10 +114,9 @@ model Branch { } model BranchContact { - id String @id @default(uuid()) - telephoneNo String - lineId String - qrCodeImageUrl String? + id String @id @default(uuid()) + telephoneNo String + lineId String branch Branch @relation(fields: [branchId], references: [id], onDelete: Cascade) branchId String @@ -141,6 +145,8 @@ model BranchUser { model User { id String @id @default(uuid()) + keycloakId String + code String firstNameTH String firstNameEN String @@ -169,8 +175,6 @@ model User { startDate DateTime retireDate DateTime - profileImageUrl String? - userType String userRole String @@ -185,7 +189,7 @@ model User { trainingPlace String - status String? + status Status @default(CREATED) createdBy String? createdAt DateTime @default(now()) @@ -368,11 +372,10 @@ model EmployeeOtherInfo { model Service { id String @id @default(uuid()) - code String - name String - detail String - imageUrl String - status String? + code String + name String + detail String + status String? createdBy String? createdAt DateTime @default(now()) From c841fa84edb2e4d611dc350eb0900adc9bb55746 Mon Sep 17 00:00:00 2001 From: Methapon2001 <61303214+Methapon2001@users.noreply.github.com> Date: Tue, 2 Apr 2024 11:19:12 +0700 Subject: [PATCH 6/7] feat: create branch endpoint --- src/controllers/branch/branch-controller.ts | 35 +++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/controllers/branch/branch-controller.ts b/src/controllers/branch/branch-controller.ts index ef032bd..6257321 100644 --- a/src/controllers/branch/branch-controller.ts +++ b/src/controllers/branch/branch-controller.ts @@ -105,6 +105,41 @@ export class BranchController extends Controller { }); } + @Post() + async createBranch(@Request() req: RequestWithUser, @Body() body: BranchCreate) { + if (body.provinceId || body.districtId || body.subDistrictId || body.headOfficeId) { + const [province, district, subDistrict, 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.branch.findFirst({ where: { id: body.headOfficeId || undefined } }), + ]); + if (body.provinceId && !province) + throw new HttpError(HttpStatus.BAD_REQUEST, "Province cannot be found."); + if (body.districtId && !district) + throw new HttpError(HttpStatus.BAD_REQUEST, "District cannot be found."); + if (body.subDistrictId && !subDistrict) + throw new HttpError(HttpStatus.BAD_REQUEST, "Sub-district cannot be found."); + if (body.headOfficeId && !branch) + throw new HttpError(HttpStatus.BAD_REQUEST, "Head branch cannot be found."); + } + + const { provinceId, districtId, subDistrictId, headOfficeId, ...rest } = body; + + return await prisma.branch.create({ + data: { + ...rest, + isHeadOffice: headOfficeId === null, + province: { connect: provinceId ? { id: provinceId } : undefined }, + district: { connect: districtId ? { id: districtId } : undefined }, + subDistrict: { connect: subDistrictId ? { id: subDistrictId } : undefined }, + headOffice: { connect: headOfficeId ? { id: headOfficeId } : undefined }, + createdBy: req.user.name, + updateBy: req.user.name, + }, + }); + } + @Delete("{branchId}") async deleteBranch(@Path() branchId: string) { const record = await prisma.branch.findFirst({ where: { id: branchId } }); From 72f9f5fe84ed95901df838afe6ae179fdbdedd97 Mon Sep 17 00:00:00 2001 From: Methapon2001 <61303214+Methapon2001@users.noreply.github.com> Date: Tue, 2 Apr 2024 11:52:55 +0700 Subject: [PATCH 7/7] feat: edit branch endpoint --- src/controllers/branch/branch-controller.ts | 56 +++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/src/controllers/branch/branch-controller.ts b/src/controllers/branch/branch-controller.ts index 6257321..197ec21 100644 --- a/src/controllers/branch/branch-controller.ts +++ b/src/controllers/branch/branch-controller.ts @@ -140,6 +140,62 @@ export class BranchController extends Controller { }); } + @Patch("{branchId}") + async editBranch( + @Request() req: RequestWithUser, + @Body() body: BranchUpdate, + @Path() branchId: string, + ) { + if (body.subDistrictId || body.districtId || body.provinceId || body.headOfficeId) { + const [province, district, subDistrict, 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.branch.findFirst({ where: { id: body.headOfficeId || undefined } }), + ]); + if (body.provinceId && !province) + throw new HttpError(HttpStatus.BAD_REQUEST, "Province cannot be found."); + if (body.districtId && !district) + throw new HttpError(HttpStatus.BAD_REQUEST, "District cannot be found."); + if (body.subDistrictId && !subDistrict) + throw new HttpError(HttpStatus.BAD_REQUEST, "Sub-district cannot be found."); + if (body.headOfficeId && !branch) + throw new HttpError(HttpStatus.BAD_REQUEST, "Head branch cannot be found."); + } + + const { provinceId, districtId, subDistrictId, headOfficeId, ...rest } = body; + + const record = await prisma.branch.update({ + include: { province: true, district: true, subDistrict: true }, + data: { + ...rest, + isHeadOffice: headOfficeId === null, + 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, + }, + headOffice: { + connect: headOfficeId ? { id: headOfficeId } : undefined, + disconnect: headOfficeId === null || undefined, + }, + updateBy: req.user.name, + }, + where: { id: branchId }, + }); + if (!record) { + throw new HttpError(HttpStatus.NOT_FOUND, "Branch cannot be found."); + } + return record; + } + @Delete("{branchId}") async deleteBranch(@Path() branchId: string) { const record = await prisma.branch.findFirst({ where: { id: branchId } });