From 659fb325b1300c1a982a811f275675c43cf286bb Mon Sep 17 00:00:00 2001 From: Methapon2001 <61303214+Methapon2001@users.noreply.github.com> Date: Wed, 8 Jan 2025 16:13:38 +0700 Subject: [PATCH] feat: add canceled work count to quotation --- src/controllers/05-quotation-controller.ts | 54 ++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/src/controllers/05-quotation-controller.ts b/src/controllers/05-quotation-controller.ts index b432b2c..4df4293 100644 --- a/src/controllers/05-quotation-controller.ts +++ b/src/controllers/05-quotation-controller.ts @@ -29,6 +29,7 @@ import { queryOrNot } from "../utils/relation"; import { deleteFile, fileLocation, getFile, getPresigned, listFile, setFile } from "../utils/minio"; import HttpError from "../interfaces/http-error"; import HttpStatus from "../interfaces/http-status"; +import { RequestWorkStatus } from "../generated/kysely/types"; type QuotationCreate = { registeredBranchId: string; @@ -191,6 +192,7 @@ export class QuotationController extends Controller { @Query() status?: QuotationStatus, @Query() urgentFirst?: boolean, @Query() includeRegisteredBranch?: boolean, + @Query() hasCancel?: boolean, @Query() code?: string, @Query() query = "", ) { @@ -215,6 +217,18 @@ export class QuotationController extends Controller { payCondition, registeredBranch: isSystem(req.user) ? undefined : { OR: permissionCond(req.user) }, quotationStatus: status, + requestData: hasCancel + ? { + some: { + requestWork: { + some: { + creditNoteId: null, + stepStatus: { some: { workStatus: RequestWorkStatus.Canceled } }, + }, + }, + }, + } + : undefined, } satisfies Prisma.QuotationWhereInput; const [result, total] = await prisma.$transaction([ @@ -248,6 +262,46 @@ export class QuotationController extends Controller { prisma.quotation.count({ where }), ]); + if (hasCancel) { + const canceled = await prisma.requestData.findMany({ + include: { + _count: { + select: { + requestWork: { + where: { + creditNoteId: null, + stepStatus: { some: { workStatus: RequestWorkStatus.Canceled } }, + }, + }, + }, + }, + }, + where: { + requestWork: { + some: { + creditNoteId: null, + stepStatus: { some: { workStatus: RequestWorkStatus.Canceled } }, + }, + }, + quotationId: { in: result.map((v) => v.id) }, + }, + }); + + return { + result: result.map((v) => { + const canceledCount = canceled + .filter((item) => item.quotationId === v.id) + .reduce((a, c) => a + c._count.requestWork, 0); + return Object.assign(v, { + _count: { ...v._count, canceledWork: canceledCount }, + }); + }), + page, + pageSize, + total, + }; + } + return { result: result, page, pageSize, total }; }