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

195 lines
4.8 KiB
TypeScript
Raw Normal View History

2024-06-12 14:16:06 +07:00
import {
Body,
Controller,
Delete,
Get,
Put,
Path,
Post,
Request,
Route,
Security,
Tags,
2024-06-13 15:47:11 +07:00
Query,
2024-06-12 14:16:06 +07:00
} from "tsoa";
import { Prisma, Status } from "@prisma/client";
import prisma from "../../db";
import minio, { presignedGetObjectIfExist } from "../../services/minio";
import { RequestWithUser } from "../../interfaces/user";
import HttpError from "../../interfaces/http-error";
import HttpStatus from "../../interfaces/http-status";
if (!process.env.MINIO_BUCKET) {
throw Error("Require MinIO bucket.");
}
const MINIO_BUCKET = process.env.MINIO_BUCKET;
type ProductCreate = {
code: "AC" | "DO" | "ac" | "do";
name: string;
detail: string;
process: string;
price: number;
agentPrice: number;
serviceCharge: number;
};
type ProductUpdate = {
name: string;
detail: string;
process?: string;
price: number;
agentPrice: number;
serviceCharge: number;
};
function imageLocation(id: string) {
return `product/${id}/image`;
}
@Route("api/v1/product")
@Tags("Product")
@Security("keycloak")
export class ProductController extends Controller {
@Get()
async getProduct(
2024-06-13 15:47:11 +07:00
@Query() query: string = "",
@Query() page: number = 1,
@Query() pageSize: number = 30,
2024-06-12 14:16:06 +07:00
) {
const where = {
OR: [{ name: { contains: query } }, { detail: { contains: query } }],
} satisfies Prisma.ProductWhereInput;
const [result, total] = await prisma.$transaction([
prisma.product.findMany({
orderBy: { createdAt: "asc" },
where,
take: pageSize,
skip: (page - 1) * pageSize,
}),
prisma.product.count({ where }),
]);
return {
result: await Promise.all(
result.map(async (v) => ({
...v,
imageUrl: await presignedGetObjectIfExist(
MINIO_BUCKET,
imageLocation(v.id),
12 * 60 * 60,
),
})),
),
page,
pageSize,
total,
};
}
@Get("{productId}")
async getProductById(@Path() productId: string) {
const record = await prisma.product.findFirst({
where: { id: productId },
});
if (!record) {
throw new HttpError(HttpStatus.NOT_FOUND, "Product cannot be found.", "data_not_found");
}
return Object.assign(record, {
imageUrl: await presignedGetObjectIfExist(MINIO_BUCKET, imageLocation(record.id), 60 * 60),
});
}
@Post()
async createProduct(@Request() req: RequestWithUser, @Body() body: ProductCreate) {
const record = await prisma.$transaction(
async (tx) => {
const last = await tx.runningNo.upsert({
where: {
key: `PRODUCT_${body.code.toLocaleUpperCase()}`,
},
create: {
key: `PRODUCT_${body.code.toLocaleUpperCase()}`,
value: 1,
},
update: { value: { increment: 1 } },
});
return await prisma.product.create({
data: {
...body,
code: `${body.code.toLocaleUpperCase()}${last.value.toString().padStart(3, "0")}`,
createdBy: req.user.name,
updateBy: req.user.name,
},
});
},
{
isolationLevel: Prisma.TransactionIsolationLevel.Serializable,
},
);
this.setStatus(HttpStatus.CREATED);
return Object.assign(record, {
imageUrl: await presignedGetObjectIfExist(
MINIO_BUCKET,
imageLocation(record.id),
12 * 60 * 60,
),
imageUploadUrl: await minio.presignedPutObject(
MINIO_BUCKET,
imageLocation(record.id),
12 * 60 * 60,
),
});
}
@Put("{productId}")
async editProduct(
@Request() req: RequestWithUser,
@Body() body: ProductUpdate,
@Path() productId: string,
) {
if (!(await prisma.product.findUnique({ where: { id: productId } }))) {
throw new HttpError(HttpStatus.NOT_FOUND, "Product cannot be found.", "data_not_found");
}
const record = await prisma.product.update({
data: { ...body, updateBy: req.user.name },
where: { id: productId },
});
return Object.assign(record, {
profileImageUrl: await presignedGetObjectIfExist(
MINIO_BUCKET,
imageLocation(record.id),
12 * 60 * 60,
),
profileImageUploadUrl: await minio.presignedPutObject(
MINIO_BUCKET,
imageLocation(record.id),
12 * 60 * 60,
),
});
}
@Delete("{productId}")
async deleteProduct(@Path() productId: string) {
const record = await prisma.product.findFirst({ where: { id: productId } });
if (!record) {
throw new HttpError(HttpStatus.NOT_FOUND, "Product cannot be found.", "data_not_found");
}
if (record.status !== Status.CREATED) {
throw new HttpError(HttpStatus.FORBIDDEN, "Product is in used.", "data_in_used");
}
return await prisma.product.delete({ where: { id: productId } });
}
}