feat: service and product with work relation
This commit is contained in:
parent
31ed48d190
commit
ee3ae91957
2 changed files with 259 additions and 30 deletions
|
|
@ -27,19 +27,20 @@ if (!process.env.MINIO_BUCKET) {
|
|||
const MINIO_BUCKET = process.env.MINIO_BUCKET;
|
||||
|
||||
type ServiceCreate = {
|
||||
code: string;
|
||||
code: "MOU" | "mou";
|
||||
name: string;
|
||||
detail: string;
|
||||
workId: string[];
|
||||
};
|
||||
|
||||
type ServiceUpdate = {
|
||||
code: string;
|
||||
name: string;
|
||||
detail: string;
|
||||
workId: string[];
|
||||
};
|
||||
|
||||
function imageLocation(id: string) {
|
||||
return `service/img-${id}`;
|
||||
return `service/${id}/product-image`;
|
||||
}
|
||||
|
||||
@Route("api/v1/service")
|
||||
|
|
@ -63,6 +64,13 @@ export class ServiceController extends Controller {
|
|||
|
||||
const [result, total] = await prisma.$transaction([
|
||||
prisma.service.findMany({
|
||||
include: {
|
||||
workOnService: {
|
||||
include: {
|
||||
service: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
orderBy: { createdAt: "asc" },
|
||||
where,
|
||||
take: pageSize,
|
||||
|
|
@ -87,6 +95,23 @@ export class ServiceController extends Controller {
|
|||
@Get("{serviceId}")
|
||||
async getServiceById(@Path() serviceId: string) {
|
||||
const record = await prisma.service.findFirst({
|
||||
include: {
|
||||
workOnService: {
|
||||
orderBy: { order: "asc" },
|
||||
include: {
|
||||
work: {
|
||||
include: {
|
||||
productOnWork: {
|
||||
include: {
|
||||
product: true,
|
||||
},
|
||||
orderBy: { order: "asc" },
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
where: { id: serviceId },
|
||||
});
|
||||
|
||||
|
|
@ -94,7 +119,7 @@ export class ServiceController extends Controller {
|
|||
throw new HttpError(HttpStatus.NOT_FOUND, "Service cannot be found.", "data_not_found");
|
||||
|
||||
return Object.assign(record, {
|
||||
imageUrl: await minio.presignedGetObject(MINIO_BUCKET, `service/img-${record.id}`, 60 * 60),
|
||||
imageUrl: await minio.presignedGetObject(MINIO_BUCKET, imageLocation(record.id), 60 * 60),
|
||||
});
|
||||
}
|
||||
|
||||
|
|
@ -104,27 +129,96 @@ export class ServiceController extends Controller {
|
|||
@Query() page: number = 1,
|
||||
@Query() pageSize: number = 30,
|
||||
) {
|
||||
const where = {
|
||||
serviceOnWork: {
|
||||
some: { serviceId },
|
||||
},
|
||||
} satisfies Prisma.WorkWhereInput;
|
||||
|
||||
const [result, total] = await prisma.$transaction([
|
||||
prisma.work.findMany({
|
||||
where: { serviceId },
|
||||
include: {
|
||||
productOnWork: {
|
||||
include: {
|
||||
product: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
where,
|
||||
take: pageSize,
|
||||
skip: (page - 1) * pageSize,
|
||||
}),
|
||||
prisma.work.count({ where: { serviceId } }),
|
||||
prisma.work.count({ where }),
|
||||
]);
|
||||
return { result, page, pageSize, total };
|
||||
}
|
||||
|
||||
@Post()
|
||||
async createService(@Request() req: RequestWithUser, @Body() body: ServiceCreate) {
|
||||
const record = await prisma.service.create({
|
||||
data: {
|
||||
...body,
|
||||
createdBy: req.user.name,
|
||||
updateBy: req.user.name,
|
||||
},
|
||||
const { workId, ...payload } = body;
|
||||
|
||||
const workList = await prisma.work.findMany({
|
||||
where: { id: { in: workId } },
|
||||
});
|
||||
|
||||
if (workList.length !== workList.length) {
|
||||
throw new HttpError(
|
||||
HttpStatus.BAD_REQUEST,
|
||||
"Some product not found.",
|
||||
"missing_or_invalid_parameter",
|
||||
);
|
||||
}
|
||||
|
||||
const record = await prisma.$transaction(
|
||||
async (tx) => {
|
||||
const last = await tx.runningNo.upsert({
|
||||
where: {
|
||||
key: `SERVICE_${body.code.toLocaleUpperCase()}`,
|
||||
},
|
||||
create: {
|
||||
key: `SERVICE_${body.code.toLocaleUpperCase()}`,
|
||||
value: 1,
|
||||
},
|
||||
update: { value: { increment: 1 } },
|
||||
});
|
||||
|
||||
return tx.service.create({
|
||||
include: {
|
||||
workOnService: {
|
||||
orderBy: { order: "asc" },
|
||||
include: {
|
||||
work: {
|
||||
include: {
|
||||
productOnWork: {
|
||||
include: {
|
||||
product: true,
|
||||
},
|
||||
orderBy: { order: "asc" },
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
data: {
|
||||
...payload,
|
||||
code: `${body.code.toLocaleUpperCase()}${last.value.toString().padStart(3, "0")}`,
|
||||
workOnService: {
|
||||
createMany: {
|
||||
data: workId.map((v, i) => ({
|
||||
order: i + 1,
|
||||
workId: v,
|
||||
})),
|
||||
},
|
||||
},
|
||||
createdBy: req.user.name,
|
||||
updateBy: req.user.name,
|
||||
},
|
||||
});
|
||||
},
|
||||
{ isolationLevel: Prisma.TransactionIsolationLevel.Serializable },
|
||||
);
|
||||
|
||||
this.setStatus(HttpStatus.CREATED);
|
||||
|
||||
return Object.assign(record, {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue