diff --git a/src/controllers/00-doc-template-controller.ts b/src/controllers/00-doc-template-controller.ts index 7f05110..bda7956 100644 --- a/src/controllers/00-doc-template-controller.ts +++ b/src/controllers/00-doc-template-controller.ts @@ -36,6 +36,7 @@ const quotationData = (id: string) => customerBranch: { include: { customer: true, + businessType: true, province: true, district: true, subDistrict: true, @@ -367,6 +368,9 @@ function gender(text: string, lang: "th" | "en" = "en") { } } +/** + * @deprecated + */ function businessType(text: string, lang: "th" | "en" = "en") { switch (lang) { case "th": diff --git a/src/controllers/03-customer-branch-controller.ts b/src/controllers/03-customer-branch-controller.ts index 7f13048..aada50c 100644 --- a/src/controllers/03-customer-branch-controller.ts +++ b/src/controllers/03-customer-branch-controller.ts @@ -87,7 +87,6 @@ export type CustomerBranchCreate = { authorizedCapital?: string; authorizedName?: string; authorizedNameEN?: string; - customerName?: string; telephoneNo: string; @@ -111,7 +110,7 @@ export type CustomerBranchCreate = { contactName: string; agentUserId?: string; - businessType: string; + businessTypeId?: string; jobPosition: string; jobDescription: string; payDate: string; @@ -145,7 +144,6 @@ export type CustomerBranchUpdate = { authorizedCapital?: string; authorizedName?: string; authorizedNameEN?: string; - customerName?: string; telephoneNo: string; @@ -169,7 +167,7 @@ export type CustomerBranchUpdate = { contactName?: string; agentUserId?: string; - businessType?: string; + businessTypeId?: string; jobPosition?: string; jobDescription?: string; payDate?: string; @@ -204,7 +202,6 @@ export class CustomerBranchController extends Controller { ) { const where = { OR: queryOrNot(query, [ - { customerName: { contains: query, mode: "insensitive" } }, { registerName: { contains: query, mode: "insensitive" } }, { registerNameEN: { contains: query, mode: "insensitive" } }, { email: { contains: query, mode: "insensitive" } }, @@ -249,6 +246,7 @@ export class CustomerBranchController extends Controller { createdBy: true, updatedBy: true, _count: true, + businessType: true, }, where, take: pageSize, @@ -271,6 +269,7 @@ export class CustomerBranchController extends Controller { subDistrict: true, createdBy: true, updatedBy: true, + businessType: true, }, where: { id: branchId }, }); @@ -381,7 +380,15 @@ export class CustomerBranchController extends Controller { (v) => (v.headOffice || v).code, ); - const { provinceId, districtId, subDistrictId, customerId, agentUserId, ...rest } = body; + const { + provinceId, + districtId, + subDistrictId, + customerId, + agentUserId, + businessTypeId, + ...rest + } = body; const record = await prisma.$transaction( async (tx) => { @@ -424,6 +431,7 @@ export class CustomerBranchController extends Controller { subDistrict: true, createdBy: true, updatedBy: true, + businessType: true, }, data: { ...rest, @@ -435,6 +443,7 @@ export class CustomerBranchController extends Controller { province: connectOrNot(provinceId), district: connectOrNot(districtId), subDistrict: connectOrNot(subDistrictId), + businessType: connectOrNot(businessTypeId), createdBy: { connect: { id: req.user.sub } }, updatedBy: { connect: { id: req.user.sub } }, }, @@ -465,6 +474,7 @@ export class CustomerBranchController extends Controller { }, }, }, + businessType: true, }, }); @@ -509,7 +519,15 @@ export class CustomerBranchController extends Controller { await permissionCheck(req.user, customer.registeredBranch); } - const { provinceId, districtId, subDistrictId, customerId, agentUserId, ...rest } = body; + const { + provinceId, + districtId, + subDistrictId, + customerId, + agentUserId, + businessTypeId, + ...rest + } = body; return await prisma.customerBranch.update({ where: { id: branchId }, @@ -519,6 +537,7 @@ export class CustomerBranchController extends Controller { subDistrict: true, createdBy: true, updatedBy: true, + businessType: true, }, data: { ...rest, @@ -528,6 +547,7 @@ export class CustomerBranchController extends Controller { province: connectOrDisconnect(provinceId), district: connectOrDisconnect(districtId), subDistrict: connectOrDisconnect(subDistrictId), + businessType: connectOrNot(businessTypeId), updatedBy: { connect: { id: req.user.sub } }, }, }); @@ -546,6 +566,7 @@ export class CustomerBranchController extends Controller { }, }, }, + businessType: true, }, }); @@ -598,6 +619,7 @@ export class CustomerBranchFileController extends Controller { }, }, }, + businessType: true, }, }); if (!data) throw notFoundError("Customer Branch"); diff --git a/src/controllers/03-customer-controller.ts b/src/controllers/03-customer-controller.ts index 082b067..0ff2b19 100644 --- a/src/controllers/03-customer-controller.ts +++ b/src/controllers/03-customer-controller.ts @@ -85,7 +85,6 @@ export type CustomerCreate = { authorizedCapital?: string; authorizedName?: string; authorizedNameEN?: string; - customerName?: string; telephoneNo: string; @@ -109,7 +108,7 @@ export type CustomerCreate = { contactName: string; agentUserId?: string; - businessType: string; + businessTypeId?: string | null; jobPosition: string; jobDescription: string; payDate: string; @@ -174,7 +173,6 @@ export class CustomerController extends Controller { const where = { OR: queryOrNot(query, [ { branch: { some: { namePrefix: { contains: query, mode: "insensitive" } } } }, - { branch: { some: { customerName: { contains: query, mode: "insensitive" } } } }, { branch: { some: { registerName: { contains: query, mode: "insensitive" } } } }, { branch: { some: { registerNameEN: { contains: query, mode: "insensitive" } } } }, { branch: { some: { firstName: { contains: query, mode: "insensitive" } } } }, @@ -203,6 +201,7 @@ export class CustomerController extends Controller { branch: includeBranch ? { include: { + businessType: true, province: true, district: true, subDistrict: true, @@ -220,6 +219,7 @@ export class CustomerController extends Controller { }, createdBy: true, updatedBy: true, + // businessType:true }, orderBy: [{ statusOrder: "asc" }, { createdAt: "asc" }], where, diff --git a/src/controllers/03-employee-visa-controller.ts b/src/controllers/03-employee-visa-controller.ts index eafb22a..7c51bf2 100644 --- a/src/controllers/03-employee-visa-controller.ts +++ b/src/controllers/03-employee-visa-controller.ts @@ -44,6 +44,7 @@ type EmployeeVisaPayload = { issuePlace: string; issueDate: Date; expireDate: Date; + reportDate?: Date | null; mrz?: string | null; remark?: string | null; diff --git a/src/controllers/04-invoice-controller.ts b/src/controllers/04-invoice-controller.ts index 5902521..cff9d20 100644 --- a/src/controllers/04-invoice-controller.ts +++ b/src/controllers/04-invoice-controller.ts @@ -117,7 +117,6 @@ export class InvoiceController extends Controller { customerBranch: { OR: [ { code: { contains: query, mode: "insensitive" } }, - { customerName: { contains: query, mode: "insensitive" } }, { registerName: { contains: query, mode: "insensitive" } }, { registerNameEN: { contains: query, mode: "insensitive" } }, { firstName: { contains: query, mode: "insensitive" } }, diff --git a/src/controllers/05-quotation-controller.ts b/src/controllers/05-quotation-controller.ts index 5a375e5..04353bb 100644 --- a/src/controllers/05-quotation-controller.ts +++ b/src/controllers/05-quotation-controller.ts @@ -225,7 +225,6 @@ export class QuotationController extends Controller { customerBranch: { OR: [ { code: { contains: query, mode: "insensitive" } }, - { customerName: { contains: query, mode: "insensitive" } }, { firstName: { contains: query, mode: "insensitive" } }, { firstNameEN: { contains: query, mode: "insensitive" } }, { lastName: { contains: query, mode: "insensitive" } }, diff --git a/src/controllers/06-request-list-controller.ts b/src/controllers/06-request-list-controller.ts index a20020f..37fd8e2 100644 --- a/src/controllers/06-request-list-controller.ts +++ b/src/controllers/06-request-list-controller.ts @@ -95,7 +95,6 @@ export class RequestDataController extends Controller { customerBranch: { OR: [ { code: { contains: query, mode: "insensitive" } }, - { customerName: { contains: query, mode: "insensitive" } }, { registerName: { contains: query, mode: "insensitive" } }, { registerNameEN: { contains: query, mode: "insensitive" } }, { firstName: { contains: query, mode: "insensitive" } }, diff --git a/src/controllers/07-task-controller.ts b/src/controllers/07-task-controller.ts index bac70dd..6707781 100644 --- a/src/controllers/07-task-controller.ts +++ b/src/controllers/07-task-controller.ts @@ -70,8 +70,9 @@ const permissionCheckCompany = createPermCheck((_) => true); @Tags("Task Order") export class TaskController extends Controller { @Get("stats") - async getTaskOrderStats() { + async getTaskOrderStats(@Request() req: RequestWithUser) { const task = await prisma.taskOrder.groupBy({ + where: { registeredBranch: { OR: permissionCondCompany(req.user) } }, by: ["taskOrderStatus"], _count: true, }); diff --git a/src/controllers/08-credit-note-controller.ts b/src/controllers/08-credit-note-controller.ts index f1bef3c..fd7a2a3 100644 --- a/src/controllers/08-credit-note-controller.ts +++ b/src/controllers/08-credit-note-controller.ts @@ -163,7 +163,6 @@ export class CreditNoteController extends Controller { customerBranch: { OR: [ { code: { contains: query, mode: "insensitive" } }, - { customerName: { contains: query, mode: "insensitive" } }, { firstName: { contains: query, mode: "insensitive" } }, { firstNameEN: { contains: query, mode: "insensitive" } }, { lastName: { contains: query, mode: "insensitive" } }, diff --git a/src/controllers/09-debit-note-controller.ts b/src/controllers/09-debit-note-controller.ts index badb52e..5fcdee9 100644 --- a/src/controllers/09-debit-note-controller.ts +++ b/src/controllers/09-debit-note-controller.ts @@ -211,7 +211,6 @@ export class DebitNoteController extends Controller { customerBranch: { OR: [ { code: { contains: query, mode: "insensitive" } }, - { customerName: { contains: query, mode: "insensitive" } }, { firstName: { contains: query, mode: "insensitive" } }, { firstNameEN: { contains: query, mode: "insensitive" } }, { lastName: { contains: query, mode: "insensitive" } }, diff --git a/src/controllers/09-line-controller.ts b/src/controllers/09-line-controller.ts index 91c06bb..8e708be 100644 --- a/src/controllers/09-line-controller.ts +++ b/src/controllers/09-line-controller.ts @@ -189,7 +189,6 @@ export class LineController extends Controller { customerBranch: { OR: [ { code: { contains: query, mode: "insensitive" } }, - { customerName: { contains: query, mode: "insensitive" } }, { registerName: { contains: query, mode: "insensitive" } }, { registerNameEN: { contains: query, mode: "insensitive" } }, { firstName: { contains: query, mode: "insensitive" } }, @@ -624,7 +623,7 @@ export class LineController extends Controller { customerBranch: { OR: [ { code: { contains: query, mode: "insensitive" } }, - { customerName: { contains: query, mode: "insensitive" } }, + { registerName: { contains: query, mode: "insensitive" } }, { firstName: { contains: query, mode: "insensitive" } }, { firstNameEN: { contains: query, mode: "insensitive" } }, { lastName: { contains: query, mode: "insensitive" } }, diff --git a/src/controllers/09-web-hook-controller.ts b/src/controllers/09-web-hook-controller.ts index 2914e20..028bf75 100644 --- a/src/controllers/09-web-hook-controller.ts +++ b/src/controllers/09-web-hook-controller.ts @@ -90,7 +90,7 @@ export class WebHookController extends Controller { firstNameEN: true, lastName: true, lastNameEN: true, - customerName: true, + registerName: true, customer: { select: { customerType: true, @@ -133,13 +133,13 @@ export class WebHookController extends Controller { let textData = ""; if (dataEmployee.length > 0) { - const customerName = - dataEmployee[0]?.employee?.customerBranch?.customerName ?? "ไม่ระบุ"; + const registerName = + dataEmployee[0]?.employee?.customerBranch?.registerName ?? "ไม่ระบุ"; const telephoneNo = dataEmployee[0]?.employee?.customerBranch?.customer.registeredBranch.telephoneNo ?? "ไม่ระบุ"; - const textEmployer = `เรียน คุณ${customerName}`; + const textEmployer = `เรียน คุณ${registerName}`; const textAlert = "ขอแจ้งให้ทราบว่าหนังสือเดินทางของลูกจ้าง"; const textAlert2 = "และจำเป็นต้องดำเนินการต่ออายุในเร็ว ๆ นี้"; const textExpDate = diff --git a/src/controllers/10-business-type-controller.ts b/src/controllers/10-business-type-controller.ts new file mode 100644 index 0000000..6d7373c --- /dev/null +++ b/src/controllers/10-business-type-controller.ts @@ -0,0 +1,113 @@ +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 { Prisma } from "@prisma/client"; +import { queryOrNot } from "../utils/relation"; +import { notFoundError } from "../utils/error"; + +type BusinessTypePayload = { + name: string; + nameEN: string; +}; + +@Route("api/v1/business-type") +@Tags("Business Type") +export class businessTypeController extends Controller { + @Get() + @Security("keycloak") + async getList( + @Request() req: RequestWithUser, + @Query() query: string = "", + @Query() page: number = 1, + @Query() pageSize: number = 30, + ) { + const where = { + OR: queryOrNot(query, [ + { name: { contains: query, mode: "insensitive" } }, + { nameEN: { contains: query, mode: "insensitive" } }, + ]), + } satisfies Prisma.BusinessTypeWhereInput; + + const [result, total] = await prisma.$transaction([ + prisma.businessType.findMany({ + where, + take: pageSize, + skip: (page - 1) * pageSize, + }), + prisma.businessType.count({ where }), + ]); + + return { result, page, pageSize, total }; + } + + @Post() + @Security("keycloak") + async createBusinessType(@Request() req: RequestWithUser, @Body() body: BusinessTypePayload) { + return await prisma.businessType.create({ + data: { + ...body, + createdByUserId: req.user.sub, + updatedByUserId: req.user.sub, + }, + }); + } + + @Get(":businessTypeId") + @Security("keycloak") + async getBusinessTypeById(@Path() businessTypeId: string) { + return await prisma.businessType.findUnique({ + where: { id: businessTypeId }, + }); + } + + @Put(":businessTypeId") + @Security("keycloak") + async updateBusinessType( + @Request() req: RequestWithUser, + @Path() businessTypeId: string, + @Body() body: BusinessTypePayload, + ) { + return await prisma.$transaction(async (tx) => { + const record = await tx.businessType.findUnique({ + where: { id: businessTypeId }, + }); + + if (!record) throw notFoundError("BusinessType"); + return await tx.businessType.update({ + where: { id: businessTypeId }, + data: { + ...body, + updatedByUserId: req.user.sub, + }, + }); + }); + } + + @Delete(":businessTypeId") + @Security("keycloak") + async deleteBusinessType(@Path() businessTypeId: string) { + return await prisma.$transaction(async (tx) => { + const record = await tx.businessType.findUnique({ + where: { id: businessTypeId }, + }); + + if (!record) throw notFoundError("BusinessType"); + return await tx.businessType.delete({ + where: { id: businessTypeId }, + }); + }); + } +}