From 73265c8965e4381400f8c3ddeda8b1de6341a4c1 Mon Sep 17 00:00:00 2001 From: Methapon2001 <61303214+Methapon2001@users.noreply.github.com> Date: Tue, 11 Jun 2024 14:00:45 +0700 Subject: [PATCH] refactor: update isolation level --- src/controllers/product/group-controller.ts | 43 +++++++++++---------- src/controllers/product/type-controller.ts | 43 +++++++++++---------- 2 files changed, 46 insertions(+), 40 deletions(-) diff --git a/src/controllers/product/group-controller.ts b/src/controllers/product/group-controller.ts index dd4873f..fb6ace0 100644 --- a/src/controllers/product/group-controller.ts +++ b/src/controllers/product/group-controller.ts @@ -63,27 +63,30 @@ export class ProductGroup extends Controller { @Post() async createProductGroup(@Request() req: RequestWithUser, @Body() body: ProductGroupCreate) { - const record = await prisma.$transaction(async (tx) => { - const last = await tx.runningNo.upsert({ - where: { - key: `PRODGRP`, - }, - create: { - key: `PRODGRP`, - value: 1, - }, - update: { value: { increment: 1 } }, - }); + const record = await prisma.$transaction( + async (tx) => { + const last = await tx.runningNo.upsert({ + where: { + key: `PRODGRP`, + }, + create: { + key: `PRODGRP`, + value: 1, + }, + update: { value: { increment: 1 } }, + }); - return await tx.productGroup.create({ - data: { - ...body, - code: `G${last.value.toString().padStart(2, "0")}`, - createdBy: req.user.name, - updateBy: req.user.name, - }, - }); - }); + return await tx.productGroup.create({ + data: { + ...body, + code: `G${last.value.toString().padStart(2, "0")}`, + createdBy: req.user.name, + updateBy: req.user.name, + }, + }); + }, + { isolationLevel: Prisma.TransactionIsolationLevel.Serializable }, + ); this.setStatus(HttpStatus.CREATED); diff --git a/src/controllers/product/type-controller.ts b/src/controllers/product/type-controller.ts index d27d834..c450cd5 100644 --- a/src/controllers/product/type-controller.ts +++ b/src/controllers/product/type-controller.ts @@ -77,27 +77,30 @@ export class ProductType extends Controller { ); } - const record = await prisma.$transaction(async (tx) => { - const last = await tx.runningNo.upsert({ - where: { - key: `PRODTYP_T${productGroup.code}`, - }, - create: { - key: `PRODTYP_T${productGroup.code}`, - value: 1, - }, - update: { value: { increment: 1 } }, - }); + const record = await prisma.$transaction( + async (tx) => { + const last = await tx.runningNo.upsert({ + where: { + key: `PRODTYP_T${productGroup.code}`, + }, + create: { + key: `PRODTYP_T${productGroup.code}`, + value: 1, + }, + update: { value: { increment: 1 } }, + }); - return await tx.productType.create({ - data: { - ...body, - code: `T${productGroup.code}${last.value.toString().padStart(2, "0")}`, - createdBy: req.user.name, - updateBy: req.user.name, - }, - }); - }); + return await tx.productType.create({ + data: { + ...body, + code: `T${productGroup.code}${last.value.toString().padStart(2, "0")}`, + createdBy: req.user.name, + updateBy: req.user.name, + }, + }); + }, + { isolationLevel: Prisma.TransactionIsolationLevel.Serializable }, + ); this.setStatus(HttpStatus.CREATED);