feat: add productType relation i/o

This commit is contained in:
Methapon2001 2024-06-17 16:52:06 +07:00
parent 09097df5f8
commit e0184631da

View file

@ -34,6 +34,7 @@ type ProductCreate = {
price: number;
agentPrice: number;
serviceCharge: number;
productTypeId: string;
remark?: string;
};
@ -45,6 +46,7 @@ type ProductUpdate = {
agentPrice?: number;
serviceCharge?: number;
remark?: string;
productTypeId?: string;
};
function imageLocation(id: string) {
@ -58,6 +60,7 @@ export class ProductController extends Controller {
@Get()
async getProduct(
@Query() status?: Status,
@Query() productTypeId?: string,
@Query() query: string = "",
@Query() page: number = 1,
@Query() pageSize: number = 30,
@ -72,8 +75,8 @@ export class ProductController extends Controller {
const where = {
OR: [
{ name: { contains: query }, ...filterStatus(status) },
{ detail: { contains: query }, ...filterStatus(status) },
{ name: { contains: query }, productTypeId, ...filterStatus(status) },
{ detail: { contains: query }, productTypeId, ...filterStatus(status) },
],
} satisfies Prisma.ProductWhereInput;
@ -119,8 +122,30 @@ export class ProductController extends Controller {
});
}
@Get("{productId}/image")
async getProductImageById(@Request() req: RequestWithUser, @Path() productId: string) {
const url = await presignedGetObjectIfExist(MINIO_BUCKET, imageLocation(productId), 60 * 60);
if (!url) {
throw new HttpError(HttpStatus.NOT_FOUND, "Image cannot be found", "imageNotFound");
}
return;
}
@Post()
async createProduct(@Request() req: RequestWithUser, @Body() body: ProductCreate) {
const productType = await prisma.productType.findFirst({
where: { id: body.productTypeId },
});
if (!productType) {
throw new HttpError(
HttpStatus.BAD_REQUEST,
"Product Type cannot be found.",
"relationProductTypeNotFound",
);
}
const record = await prisma.$transaction(
async (tx) => {
const last = await tx.runningNo.upsert({
@ -177,6 +202,19 @@ export class ProductController extends Controller {
data: { ...body, updateBy: req.user.name },
where: { id: productId },
});
const productType = await prisma.productType.findFirst({
where: { id: body.productTypeId },
});
if (!productType) {
throw new HttpError(
HttpStatus.BAD_REQUEST,
"Product Type cannot be found.",
"relationProductTypeNotFound",
);
}
return Object.assign(record, {
profileImageUrl: await presignedGetObjectIfExist(
MINIO_BUCKET,