feat: relation and auto gen code
This commit is contained in:
parent
af4489dccc
commit
f7c916b84a
4 changed files with 91 additions and 23 deletions
|
|
@ -0,0 +1,25 @@
|
||||||
|
/*
|
||||||
|
Warnings:
|
||||||
|
|
||||||
|
- You are about to drop the column `productGroupId` on the `Product` table. All the data in the column will be lost.
|
||||||
|
- Added the required column `productGroupId` to the `ProductType` table without a default value. This is not possible if the table is not empty.
|
||||||
|
- Added the required column `productId` to the `WorkProduct` table without a default value. This is not possible if the table is not empty.
|
||||||
|
|
||||||
|
*/
|
||||||
|
-- DropForeignKey
|
||||||
|
ALTER TABLE "Product" DROP CONSTRAINT "Product_productGroupId_fkey";
|
||||||
|
|
||||||
|
-- AlterTable
|
||||||
|
ALTER TABLE "Product" DROP COLUMN "productGroupId";
|
||||||
|
|
||||||
|
-- AlterTable
|
||||||
|
ALTER TABLE "ProductType" ADD COLUMN "productGroupId" TEXT NOT NULL;
|
||||||
|
|
||||||
|
-- AlterTable
|
||||||
|
ALTER TABLE "WorkProduct" ADD COLUMN "productId" TEXT NOT NULL;
|
||||||
|
|
||||||
|
-- AddForeignKey
|
||||||
|
ALTER TABLE "WorkProduct" ADD CONSTRAINT "WorkProduct_productId_fkey" FOREIGN KEY ("productId") REFERENCES "Product"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
||||||
|
|
||||||
|
-- AddForeignKey
|
||||||
|
ALTER TABLE "ProductType" ADD CONSTRAINT "ProductType_productGroupId_fkey" FOREIGN KEY ("productGroupId") REFERENCES "ProductGroup"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
||||||
|
|
@ -570,8 +570,10 @@ model Work {
|
||||||
model WorkProduct {
|
model WorkProduct {
|
||||||
id String @id @default(uuid())
|
id String @id @default(uuid())
|
||||||
|
|
||||||
work Work @relation(fields: [workId], references: [id], onDelete: Cascade)
|
work Work @relation(fields: [workId], references: [id], onDelete: Cascade)
|
||||||
workId String
|
workId String
|
||||||
|
product Product @relation(fields: [productId], references: [id], onDelete: Cascade)
|
||||||
|
productId String
|
||||||
|
|
||||||
createdBy String?
|
createdBy String?
|
||||||
createdAt DateTime @default(now())
|
createdAt DateTime @default(now())
|
||||||
|
|
@ -590,10 +592,11 @@ model ProductGroup {
|
||||||
status Status @default(CREATED)
|
status Status @default(CREATED)
|
||||||
|
|
||||||
createdBy String?
|
createdBy String?
|
||||||
createdAt DateTime @default(now())
|
createdAt DateTime @default(now())
|
||||||
updateBy String?
|
updateBy String?
|
||||||
updatedAt DateTime @updatedAt
|
updatedAt DateTime @updatedAt
|
||||||
Product Product[]
|
|
||||||
|
type ProductType[]
|
||||||
}
|
}
|
||||||
|
|
||||||
model ProductType {
|
model ProductType {
|
||||||
|
|
@ -611,6 +614,9 @@ model ProductType {
|
||||||
updateBy String?
|
updateBy String?
|
||||||
updatedAt DateTime @updatedAt
|
updatedAt DateTime @updatedAt
|
||||||
|
|
||||||
|
productGroup ProductGroup @relation(fields: [productGroupId], references: [id], onDelete: Cascade)
|
||||||
|
productGroupId String
|
||||||
|
|
||||||
product Product[]
|
product Product[]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -630,8 +636,7 @@ model Product {
|
||||||
productType ProductType? @relation(fields: [productTypeId], references: [id], onDelete: SetNull)
|
productType ProductType? @relation(fields: [productTypeId], references: [id], onDelete: SetNull)
|
||||||
productTypeId String?
|
productTypeId String?
|
||||||
|
|
||||||
productGroup ProductGroup? @relation(fields: [productGroupId], references: [id], onDelete: SetNull)
|
workProduct WorkProduct[]
|
||||||
productGroupId String?
|
|
||||||
|
|
||||||
createdBy String?
|
createdBy String?
|
||||||
createdAt DateTime @default(now())
|
createdAt DateTime @default(now())
|
||||||
|
|
|
||||||
|
|
@ -20,14 +20,12 @@ import HttpError from "../../interfaces/http-error";
|
||||||
import HttpStatus from "../../interfaces/http-status";
|
import HttpStatus from "../../interfaces/http-status";
|
||||||
|
|
||||||
type ProductGroupCreate = {
|
type ProductGroupCreate = {
|
||||||
code: string;
|
|
||||||
name: string;
|
name: string;
|
||||||
detail: string;
|
detail: string;
|
||||||
remark: string;
|
remark: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
type ProductGroupUpdate = {
|
type ProductGroupUpdate = {
|
||||||
code?: string;
|
|
||||||
name?: string;
|
name?: string;
|
||||||
detail?: string;
|
detail?: string;
|
||||||
remark?: string;
|
remark?: string;
|
||||||
|
|
@ -65,12 +63,26 @@ export class ProductGroup extends Controller {
|
||||||
|
|
||||||
@Post()
|
@Post()
|
||||||
async createProductGroup(@Request() req: RequestWithUser, @Body() body: ProductGroupCreate) {
|
async createProductGroup(@Request() req: RequestWithUser, @Body() body: ProductGroupCreate) {
|
||||||
const record = await prisma.productGroup.create({
|
const record = await prisma.$transaction(async (tx) => {
|
||||||
data: {
|
const last = await tx.runningNo.upsert({
|
||||||
...body,
|
where: {
|
||||||
createdBy: req.user.name,
|
key: `PRODGRP`,
|
||||||
updateBy: req.user.name,
|
},
|
||||||
},
|
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);
|
this.setStatus(HttpStatus.CREATED);
|
||||||
|
|
|
||||||
|
|
@ -20,14 +20,14 @@ import HttpError from "../../interfaces/http-error";
|
||||||
import HttpStatus from "../../interfaces/http-status";
|
import HttpStatus from "../../interfaces/http-status";
|
||||||
|
|
||||||
type ProductTypeCreate = {
|
type ProductTypeCreate = {
|
||||||
code: string;
|
productGroupId: string;
|
||||||
name: string;
|
name: string;
|
||||||
detail: string;
|
detail: string;
|
||||||
remark: string;
|
remark: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
type ProductTypeUpdate = {
|
type ProductTypeUpdate = {
|
||||||
code?: string;
|
productGroupId?: string;
|
||||||
name?: string;
|
name?: string;
|
||||||
detail?: string;
|
detail?: string;
|
||||||
remark?: string;
|
remark?: string;
|
||||||
|
|
@ -65,12 +65,38 @@ export class ProductType extends Controller {
|
||||||
|
|
||||||
@Post()
|
@Post()
|
||||||
async createProductType(@Request() req: RequestWithUser, @Body() body: ProductTypeCreate) {
|
async createProductType(@Request() req: RequestWithUser, @Body() body: ProductTypeCreate) {
|
||||||
const record = await prisma.productType.create({
|
const productGroup = await prisma.productGroup.findFirst({
|
||||||
data: {
|
where: { id: body.productGroupId },
|
||||||
...body,
|
});
|
||||||
createdBy: req.user.name,
|
|
||||||
updateBy: req.user.name,
|
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);
|
this.setStatus(HttpStatus.CREATED);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue