jws-backend/src/controllers/product/group-controller.ts

139 lines
3.5 KiB
TypeScript
Raw Normal View History

2024-06-06 16:50:44 +07:00
import {
Body,
Controller,
Delete,
Get,
Put,
Path,
Post,
Query,
Request,
Route,
Security,
Tags,
} from "tsoa";
import { Prisma, Status } from "@prisma/client";
import prisma from "../../db";
import { RequestWithUser } from "../../interfaces/user";
import HttpError from "../../interfaces/http-error";
import HttpStatus from "../../interfaces/http-status";
type ProductGroupCreate = {
name: string;
detail: string;
remark: string;
};
type ProductGroupUpdate = {
name?: string;
detail?: string;
remark?: string;
};
2024-06-11 13:35:54 +07:00
@Route("api/v1/product-group")
2024-06-06 16:50:44 +07:00
@Tags("Product Group")
@Security("keycloak")
export class ProductGroup extends Controller {
2024-06-11 09:31:10 +07:00
@Get("stats")
2024-06-11 09:27:37 +07:00
async getProductGroupStats() {
return await prisma.productGroup.count();
}
2024-06-06 16:50:44 +07:00
@Get()
async getProductGroup(@Query() query: string = "", @Query() status?: Status) {
const filterStatus = (val?: Status) => {
if (!val) return {};
return val !== Status.CREATED && val !== Status.ACTIVE
? { status: val }
: { OR: [{ status: Status.CREATED }, { status: Status.ACTIVE }] };
};
2024-06-06 16:50:44 +07:00
const where = {
OR: [
{ name: { contains: query }, ...filterStatus(status) },
{ detail: { contains: query }, ...filterStatus(status) },
],
2024-06-06 16:50:44 +07:00
} satisfies Prisma.ProductGroupWhereInput;
return prisma.productGroup.findMany({ orderBy: { createdAt: "asc" }, where });
2024-06-06 16:50:44 +07:00
}
@Get("{groupId}")
async getProductGroupById(@Path() groupId: string) {
const record = await prisma.productGroup.findFirst({
where: { id: groupId },
});
if (!record)
2024-06-14 04:40:10 +00:00
throw new HttpError(HttpStatus.NOT_FOUND, "Product group cannot be found.", "productGroupNotFound");
2024-06-06 16:50:44 +07:00
return record;
}
@Post()
async createProductGroup(@Request() req: RequestWithUser, @Body() body: ProductGroupCreate) {
2024-06-11 14:00:45 +07:00
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 } },
});
2024-06-11 13:01:20 +07:00
2024-06-11 14:00:45 +07:00
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 },
);
2024-06-06 16:50:44 +07:00
this.setStatus(HttpStatus.CREATED);
return record;
}
@Put("{groupId}")
async editProductGroup(
@Request() req: RequestWithUser,
@Body() body: ProductGroupUpdate,
@Path() groupId: string,
) {
2024-06-11 16:16:22 +07:00
if (!(await prisma.productGroup.findUnique({ where: { id: groupId } }))) {
2024-06-14 04:40:10 +00:00
throw new HttpError(HttpStatus.NOT_FOUND, "Product group cannot be found.", "productGroupNotFound");
2024-06-06 16:50:44 +07:00
}
2024-06-11 16:16:22 +07:00
const record = await prisma.productGroup.update({
2024-06-06 16:50:44 +07:00
data: { ...body, updateBy: req.user.name },
where: { id: groupId },
});
return record;
}
@Delete("{groupId}")
async deleteProductGroup(@Path() groupId: string) {
const record = await prisma.productGroup.findFirst({ where: { id: groupId } });
if (!record) {
2024-06-14 04:40:10 +00:00
throw new HttpError(HttpStatus.NOT_FOUND, "Product group cannot be found.", "productGroupNotFound");
2024-06-06 16:50:44 +07:00
}
if (record.status !== Status.CREATED) {
2024-06-14 04:40:10 +00:00
throw new HttpError(HttpStatus.FORBIDDEN, "Product group is in used.", "productGroupInUsed");
2024-06-06 16:50:44 +07:00
}
return await prisma.productGroup.delete({ where: { id: groupId } });
}
}