2024-04-09 13:58:04 +07:00
|
|
|
import { CustomerType, Prisma, Status } from "@prisma/client";
|
2024-04-05 10:57:43 +07:00
|
|
|
import {
|
|
|
|
|
Body,
|
|
|
|
|
Controller,
|
|
|
|
|
Delete,
|
|
|
|
|
Get,
|
|
|
|
|
Path,
|
|
|
|
|
Post,
|
|
|
|
|
Put,
|
|
|
|
|
Query,
|
|
|
|
|
Request,
|
|
|
|
|
Route,
|
|
|
|
|
Security,
|
|
|
|
|
Tags,
|
|
|
|
|
} from "tsoa";
|
2024-04-05 10:53:52 +07:00
|
|
|
import { RequestWithUser } from "../interfaces/user";
|
|
|
|
|
import prisma from "../db";
|
|
|
|
|
import minio from "../services/minio";
|
|
|
|
|
import HttpStatus from "../interfaces/http-status";
|
|
|
|
|
import HttpError from "../interfaces/http-error";
|
|
|
|
|
|
|
|
|
|
if (!process.env.MINIO_BUCKET) {
|
|
|
|
|
throw Error("Require MinIO bucket.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const MINIO_BUCKET = process.env.MINIO_BUCKET;
|
|
|
|
|
|
|
|
|
|
export type CustomerCreate = {
|
|
|
|
|
status?: Status;
|
2024-04-09 13:58:04 +07:00
|
|
|
customerType: CustomerType;
|
2024-04-05 10:53:52 +07:00
|
|
|
customerName: string;
|
|
|
|
|
customerNameEN: string;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export type CustomerUpdate = {
|
|
|
|
|
status?: "ACTIVE" | "INACTIVE";
|
2024-04-09 15:43:21 +07:00
|
|
|
customerType?: CustomerType;
|
2024-04-05 10:53:52 +07:00
|
|
|
customerName?: string;
|
|
|
|
|
customerNameEN?: string;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function imageLocation(id: string) {
|
|
|
|
|
return `customer/img-${id}`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Route("api/customer")
|
|
|
|
|
@Tags("Customer")
|
2024-04-05 10:57:14 +07:00
|
|
|
@Security("keycloak")
|
2024-04-05 10:53:52 +07:00
|
|
|
export class CustomerController extends Controller {
|
2024-04-05 11:00:31 +07:00
|
|
|
@Get()
|
|
|
|
|
async list(
|
|
|
|
|
@Query() query: string = "",
|
|
|
|
|
@Query() page: number = 1,
|
|
|
|
|
@Query() pageSize: number = 30,
|
|
|
|
|
) {
|
|
|
|
|
const where = {
|
|
|
|
|
OR: [{ customerName: { contains: query } }, { customerNameEN: { contains: query } }],
|
|
|
|
|
} satisfies Prisma.CustomerWhereInput;
|
|
|
|
|
|
|
|
|
|
const [result, total] = await prisma.$transaction([
|
|
|
|
|
prisma.customer.findMany({
|
2024-04-09 08:51:22 +07:00
|
|
|
orderBy: { createdAt: "asc" },
|
2024-04-05 11:00:31 +07:00
|
|
|
where,
|
|
|
|
|
take: pageSize,
|
|
|
|
|
skip: (page - 1) * pageSize,
|
|
|
|
|
}),
|
|
|
|
|
prisma.customer.count({ where }),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
result: await Promise.all(
|
|
|
|
|
result.map(async (v) => ({
|
|
|
|
|
...v,
|
|
|
|
|
imageUrl: await minio.presignedGetObject(MINIO_BUCKET, imageLocation(v.id), 12 * 60 * 60),
|
|
|
|
|
})),
|
|
|
|
|
),
|
|
|
|
|
page,
|
|
|
|
|
pageSize,
|
|
|
|
|
total,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-05 11:00:44 +07:00
|
|
|
@Get("{customerId}")
|
|
|
|
|
async getById(@Path() customerId: string) {
|
|
|
|
|
const record = await prisma.customer.findFirst({ where: { id: customerId } });
|
|
|
|
|
if (!record)
|
|
|
|
|
throw new HttpError(HttpStatus.NOT_FOUND, "Customer cannot be found.", "data_not_found");
|
2024-04-05 11:06:01 +07:00
|
|
|
return Object.assign(record, {
|
|
|
|
|
imageUrl: await minio.presignedGetObject(
|
|
|
|
|
MINIO_BUCKET,
|
|
|
|
|
imageLocation(record.id),
|
|
|
|
|
12 * 60 * 60,
|
|
|
|
|
),
|
|
|
|
|
});
|
2024-04-05 11:00:44 +07:00
|
|
|
}
|
|
|
|
|
|
2024-04-05 10:53:52 +07:00
|
|
|
@Post()
|
|
|
|
|
async create(@Request() req: RequestWithUser, @Body() body: CustomerCreate) {
|
2024-04-10 12:23:59 +07:00
|
|
|
const last = await prisma.customer.findFirst({
|
|
|
|
|
orderBy: { createdAt: "desc" },
|
|
|
|
|
where: { customerType: body.customerType },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const code = `${body.customerType}${(+(last?.code.slice(-6) || 0) + 1).toString().padStart(6, "0")}`;
|
|
|
|
|
|
2024-04-05 10:53:52 +07:00
|
|
|
const record = await prisma.customer.create({
|
|
|
|
|
data: {
|
|
|
|
|
...body,
|
2024-04-10 12:23:59 +07:00
|
|
|
code,
|
2024-04-05 10:53:52 +07:00
|
|
|
createdBy: req.user.name,
|
|
|
|
|
updateBy: req.user.name,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
this.setStatus(HttpStatus.CREATED);
|
|
|
|
|
|
|
|
|
|
return Object.assign(record, {
|
|
|
|
|
imageUrl: await minio.presignedGetObject(
|
|
|
|
|
MINIO_BUCKET,
|
|
|
|
|
imageLocation(record.id),
|
|
|
|
|
12 * 60 * 60,
|
|
|
|
|
),
|
|
|
|
|
imageUploadUrl: await minio.presignedPutObject(
|
|
|
|
|
MINIO_BUCKET,
|
|
|
|
|
imageLocation(record.id),
|
|
|
|
|
12 * 60 * 60,
|
|
|
|
|
),
|
|
|
|
|
});
|
|
|
|
|
}
|
2024-04-05 11:02:23 +07:00
|
|
|
|
2024-04-05 11:18:01 +07:00
|
|
|
@Put("{customerId}")
|
|
|
|
|
async editById(
|
|
|
|
|
@Path() customerId: string,
|
|
|
|
|
@Request() req: RequestWithUser,
|
|
|
|
|
@Body() body: CustomerUpdate,
|
|
|
|
|
) {
|
2024-04-18 13:09:53 +07:00
|
|
|
if (!(await prisma.customer.findUnique({ where: { id: customerId } }))) {
|
|
|
|
|
throw new HttpError(HttpStatus.NOT_FOUND, "Customer cannot be found.", "data_not_found");
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-05 11:18:01 +07:00
|
|
|
const record = await prisma.customer.update({
|
|
|
|
|
where: { id: customerId },
|
|
|
|
|
data: {
|
|
|
|
|
...body,
|
|
|
|
|
createdBy: req.user.name,
|
|
|
|
|
updateBy: req.user.name,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return Object.assign(record, {
|
|
|
|
|
imageUrl: await minio.presignedGetObject(
|
|
|
|
|
MINIO_BUCKET,
|
|
|
|
|
imageLocation(record.id),
|
|
|
|
|
12 * 60 * 60,
|
|
|
|
|
),
|
|
|
|
|
imageUploadUrl: await minio.presignedPutObject(
|
|
|
|
|
MINIO_BUCKET,
|
|
|
|
|
imageLocation(record.id),
|
|
|
|
|
12 * 60 * 60,
|
|
|
|
|
),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-05 11:02:23 +07:00
|
|
|
@Delete("{customerId}")
|
|
|
|
|
async deleteById(@Path() customerId: string) {
|
|
|
|
|
const record = await prisma.customer.findFirst({ where: { id: customerId } });
|
|
|
|
|
|
|
|
|
|
if (!record) {
|
|
|
|
|
throw new HttpError(HttpStatus.NOT_FOUND, "Customer cannot be found.", "data_not_found");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (record.status !== Status.CREATED) {
|
|
|
|
|
throw new HttpError(HttpStatus.FORBIDDEN, "Customer is in used.", "data_in_used");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return await prisma.customer.delete({ where: { id: customerId } });
|
|
|
|
|
}
|
2024-04-05 10:53:52 +07:00
|
|
|
}
|