diff --git a/prisma/migrations/20241119064105_refactor_status_relation/migration.sql b/prisma/migrations/20241119064105_refactor_status_relation/migration.sql new file mode 100644 index 0000000..1acb3db --- /dev/null +++ b/prisma/migrations/20241119064105_refactor_status_relation/migration.sql @@ -0,0 +1,20 @@ +/* + Warnings: + + - You are about to drop the column `workStatus` on the `RequestWork` table. All the data in the column will be lost. + +*/ +-- AlterTable +ALTER TABLE "RequestWork" DROP COLUMN "workStatus"; + +-- CreateTable +CREATE TABLE "RequestWorkStepStatus" ( + "step" INTEGER NOT NULL, + "workStatus" "RequestWorkStatus" NOT NULL DEFAULT 'Pending', + "requestWorkId" TEXT NOT NULL, + + CONSTRAINT "RequestWorkStepStatus_pkey" PRIMARY KEY ("step","requestWorkId") +); + +-- AddForeignKey +ALTER TABLE "RequestWorkStepStatus" ADD CONSTRAINT "RequestWorkStepStatus_requestWorkId_fkey" FOREIGN KEY ("requestWorkId") REFERENCES "RequestWork"("id") ON DELETE RESTRICT ON UPDATE CASCADE; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 7dec423..15b1502 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -1431,10 +1431,20 @@ model RequestWork { productService QuotationProductServiceList @relation(fields: [productServiceId], references: [id], onDelete: Cascade) productServiceId String - workStatus RequestWorkStatus @default(Pending) - processByUser User? @relation(fields: [processByUserId], references: [id]) processByUserId String? attributes Json? + + stepStatus RequestWorkStepStatus[] +} + +model RequestWorkStepStatus { + step Int + workStatus RequestWorkStatus @default(Pending) + + requestWork RequestWork @relation(fields: [requestWorkId], references: [id]) + requestWorkId String + + @@id([step, requestWorkId]) } diff --git a/src/controllers/05-quotation-controller.ts b/src/controllers/05-quotation-controller.ts index e46812f..0cb18c7 100644 --- a/src/controllers/05-quotation-controller.ts +++ b/src/controllers/05-quotation-controller.ts @@ -343,12 +343,7 @@ export class QuotationController extends Controller { tx.employee.findMany({ where: { id: { in: ids.employee } } }), tx.product.findMany({ where: { id: { in: ids.product } } }), ids.work.length ? tx.work.findMany({ where: { id: { in: ids.work } } }) : null, - ids.service.length - ? tx.service.findMany({ - include: { work: { include: { productOnWork: true } } }, - where: { id: { in: ids.service } }, - }) - : null, + ids.service.length ? tx.service.findMany({ where: { id: { in: ids.service } } }) : null, ]), ); @@ -430,13 +425,6 @@ export class QuotationController extends Controller { amount: v.amount, discount: v.discount || 0, installmentNo: v.installmentNo, - attributes: service - ?.find((s) => s.id === v.serviceId) - ?.work.find((w) => w.id === v.workId) - ?.productOnWork.find((p) => p.productId === v.productId)?.attributes as Record< - string, - unknown - >, vat, worker: { create: sortedEmployeeId @@ -675,7 +663,6 @@ export class QuotationController extends Controller { const vat = p.calcVat ? precisionRound((pricePerUnit * v.amount - (v.discount || 0)) * VAT_DEFAULT) : 0; - return { order: i + 1, productId: v.productId, @@ -686,13 +673,6 @@ export class QuotationController extends Controller { discount: v.discount || 0, installmentNo: v.installmentNo, vat, - attributes: service - ?.find((s) => s.id === v.serviceId) - ?.work.find((w) => w.id === v.workId) - ?.productOnWork.find((p) => p.productId === v.productId)?.attributes as Record< - string, - unknown - >, worker: { create: sortedEmployeeId .filter((_, i) => !v.workerIndex || i in v.workerIndex) diff --git a/src/controllers/06-request-list-controller.ts b/src/controllers/06-request-list-controller.ts index f59ba79..9931103 100644 --- a/src/controllers/06-request-list-controller.ts +++ b/src/controllers/06-request-list-controller.ts @@ -152,6 +152,7 @@ export class RequestListController extends Controller { employee: true, }, }, + stepStatus: true, productService: { include: { service: true, @@ -194,6 +195,7 @@ export class RequestListController extends Controller { employee: true, }, }, + stepStatus: true, productService: { include: { service: true, @@ -221,15 +223,15 @@ export class RequestListController extends Controller { } @Put("{requestWorkId}") + @Security("keycloak") async updateRequestWorkById( @Request() req: RequestWithUser, @Path() requestWorkId: string, - @Body() payload: { status: RequestWorkStatus; attributes: Record }, + @Body() payload: { attributes: Record }, ) { const record = await prisma.requestWork.update({ where: { id: requestWorkId }, data: { - workStatus: payload.status, request: { update: { flow: payload.attributes }, }, @@ -238,6 +240,32 @@ export class RequestListController extends Controller { return record; } + + @Put("{requestWorkId}/step-status/{step}") + @Security("keycloak") + async updateRequestWorkStepStatus( + @Path() requestWorkId: string, + @Path() step: number, + @Body() payload: { requestWorkStatus: RequestWorkStatus }, + ) { + const record = await prisma.requestWorkStepStatus.upsert({ + where: { + step_requestWorkId: { + step: step, + requestWorkId, + }, + }, + create: { + step: step, + requestWorkId, + workStatus: payload.requestWorkStatus, + }, + update: { + workStatus: payload.requestWorkStatus, + }, + }); + return record; + } } export class RequestListAttachmentController extends Controller {}