feat: add canceled work count to quotation
This commit is contained in:
parent
f4ae8073aa
commit
659fb325b1
1 changed files with 54 additions and 0 deletions
|
|
@ -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 };
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue