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; price: number;
agentPrice: number; agentPrice: number;
serviceCharge: number; serviceCharge: number;
productTypeId: string;
remark?: string; remark?: string;
}; };
@ -45,6 +46,7 @@ type ProductUpdate = {
agentPrice?: number; agentPrice?: number;
serviceCharge?: number; serviceCharge?: number;
remark?: string; remark?: string;
productTypeId?: string;
}; };
function imageLocation(id: string) { function imageLocation(id: string) {
@ -58,6 +60,7 @@ export class ProductController extends Controller {
@Get() @Get()
async getProduct( async getProduct(
@Query() status?: Status, @Query() status?: Status,
@Query() productTypeId?: string,
@Query() query: string = "", @Query() query: string = "",
@Query() page: number = 1, @Query() page: number = 1,
@Query() pageSize: number = 30, @Query() pageSize: number = 30,
@ -72,8 +75,8 @@ export class ProductController extends Controller {
const where = { const where = {
OR: [ OR: [
{ name: { contains: query }, ...filterStatus(status) }, { name: { contains: query }, productTypeId, ...filterStatus(status) },
{ detail: { contains: query }, ...filterStatus(status) }, { detail: { contains: query }, productTypeId, ...filterStatus(status) },
], ],
} satisfies Prisma.ProductWhereInput; } 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() @Post()
async createProduct(@Request() req: RequestWithUser, @Body() body: ProductCreate) { 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( const record = await prisma.$transaction(
async (tx) => { async (tx) => {
const last = await tx.runningNo.upsert({ const last = await tx.runningNo.upsert({
@ -177,6 +202,19 @@ export class ProductController extends Controller {
data: { ...body, updateBy: req.user.name }, data: { ...body, updateBy: req.user.name },
where: { id: productId }, 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, { return Object.assign(record, {
profileImageUrl: await presignedGetObjectIfExist( profileImageUrl: await presignedGetObjectIfExist(
MINIO_BUCKET, MINIO_BUCKET,