feat: relation and auto gen code

This commit is contained in:
Methapon2001 2024-06-11 13:01:20 +07:00
parent af4489dccc
commit f7c916b84a
4 changed files with 91 additions and 23 deletions

View file

@ -20,14 +20,12 @@ import HttpError from "../../interfaces/http-error";
import HttpStatus from "../../interfaces/http-status";
type ProductGroupCreate = {
code: string;
name: string;
detail: string;
remark: string;
};
type ProductGroupUpdate = {
code?: string;
name?: string;
detail?: string;
remark?: string;
@ -65,12 +63,26 @@ export class ProductGroup extends Controller {
@Post()
async createProductGroup(@Request() req: RequestWithUser, @Body() body: ProductGroupCreate) {
const record = await prisma.productGroup.create({
data: {
...body,
createdBy: req.user.name,
updateBy: req.user.name,
},
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,
},
});
});
this.setStatus(HttpStatus.CREATED);

View file

@ -20,14 +20,14 @@ import HttpError from "../../interfaces/http-error";
import HttpStatus from "../../interfaces/http-status";
type ProductTypeCreate = {
code: string;
productGroupId: string;
name: string;
detail: string;
remark: string;
};
type ProductTypeUpdate = {
code?: string;
productGroupId?: string;
name?: string;
detail?: string;
remark?: string;
@ -65,12 +65,38 @@ export class ProductType extends Controller {
@Post()
async createProductType(@Request() req: RequestWithUser, @Body() body: ProductTypeCreate) {
const record = await prisma.productType.create({
data: {
...body,
createdBy: req.user.name,
updateBy: req.user.name,
},
const productGroup = await prisma.productGroup.findFirst({
where: { id: body.productGroupId },
});
if (!productGroup) {
throw new HttpError(
HttpStatus.BAD_REQUEST,
"Product group associated cannot be found.",
"missing_or_invalid_parameter",
);
}
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.productGroup.create({
data: {
...body,
code: `T${productGroup.code}${last.value.toString().padStart(2, "0")}`,
createdBy: req.user.name,
updateBy: req.user.name,
},
});
});
this.setStatus(HttpStatus.CREATED);