From 9c56d4ad91e9be95c69a2584e29011620e5eff16 Mon Sep 17 00:00:00 2001 From: Methapon2001 <61303214+Methapon2001@users.noreply.github.com> Date: Wed, 19 Jun 2024 17:06:52 +0700 Subject: [PATCH] feat: update add status --- src/controllers/product/group-controller.ts | 20 +++++++++++++++++--- src/controllers/product/type-controller.ts | 17 +++++++++++++---- 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/src/controllers/product/group-controller.ts b/src/controllers/product/group-controller.ts index 9f7735d..30e742e 100644 --- a/src/controllers/product/group-controller.ts +++ b/src/controllers/product/group-controller.ts @@ -23,12 +23,14 @@ type ProductGroupCreate = { name: string; detail: string; remark: string; + status?: Status; }; type ProductGroupUpdate = { name?: string; detail?: string; remark?: string; + status?: "ACTIVE" | "INACTIVE"; }; @Route("api/v1/product-group") @@ -66,7 +68,11 @@ export class ProductGroup extends Controller { }); if (!record) - throw new HttpError(HttpStatus.NOT_FOUND, "Product group cannot be found.", "productGroupNotFound"); + throw new HttpError( + HttpStatus.NOT_FOUND, + "Product group cannot be found.", + "productGroupNotFound", + ); return record; } @@ -110,7 +116,11 @@ export class ProductGroup extends Controller { @Path() groupId: string, ) { if (!(await prisma.productGroup.findUnique({ where: { id: groupId } }))) { - throw new HttpError(HttpStatus.NOT_FOUND, "Product group cannot be found.", "productGroupNotFound"); + throw new HttpError( + HttpStatus.NOT_FOUND, + "Product group cannot be found.", + "productGroupNotFound", + ); } const record = await prisma.productGroup.update({ @@ -126,7 +136,11 @@ export class ProductGroup extends Controller { const record = await prisma.productGroup.findFirst({ where: { id: groupId } }); if (!record) { - throw new HttpError(HttpStatus.NOT_FOUND, "Product group cannot be found.", "productGroupNotFound"); + throw new HttpError( + HttpStatus.NOT_FOUND, + "Product group cannot be found.", + "productGroupNotFound", + ); } if (record.status !== Status.CREATED) { diff --git a/src/controllers/product/type-controller.ts b/src/controllers/product/type-controller.ts index 24d5d4f..24eec0a 100644 --- a/src/controllers/product/type-controller.ts +++ b/src/controllers/product/type-controller.ts @@ -24,6 +24,7 @@ type ProductTypeCreate = { name: string; detail: string; remark: string; + status?: Status; }; type ProductTypeUpdate = { @@ -31,6 +32,7 @@ type ProductTypeUpdate = { name?: string; detail?: string; remark?: string; + status?: "ACTIVE" | "INACTIVE"; }; @Route("api/v1/product-type") @@ -139,10 +141,10 @@ export class ProductType extends Controller { @Body() body: ProductTypeUpdate, @Path() typeId: string, ) { - if ( - body.productGroupId && - !(await prisma.productGroup.findFirst({ where: { id: body.productGroupId } })) - ) { + const productGroup = await prisma.productGroup.findFirst({ + where: { id: body.productGroupId }, + }); + if (body.productGroupId && !productGroup) { throw new HttpError( HttpStatus.BAD_REQUEST, "Product group cannot be found.", @@ -163,6 +165,13 @@ export class ProductType extends Controller { where: { id: typeId }, }); + if (productGroup?.status === "CREATED") { + await prisma.productGroup.update({ + where: { id: body.productGroupId }, + data: { status: Status.ACTIVE }, + }); + } + return record; }