feat: update work endpoints
This commit is contained in:
parent
39206a6d76
commit
51fd6b0589
4 changed files with 76 additions and 72 deletions
|
|
@ -33,20 +33,28 @@ type ServiceCreate = {
|
|||
attributes?: {
|
||||
[key: string]: any;
|
||||
};
|
||||
workId: string[];
|
||||
work?: {
|
||||
name: string;
|
||||
productId: string[];
|
||||
attributes?: { [key: string]: any };
|
||||
}[];
|
||||
};
|
||||
|
||||
type ServiceUpdate = {
|
||||
name: string;
|
||||
detail: string;
|
||||
workId: string[];
|
||||
attributes?: {
|
||||
[key: string]: any;
|
||||
};
|
||||
work?: {
|
||||
name: string;
|
||||
productId: string[];
|
||||
attributes?: { [key: string]: any };
|
||||
}[];
|
||||
};
|
||||
|
||||
function imageLocation(id: string) {
|
||||
return `service/${id}/product-image`;
|
||||
return `service/${id}/service-image`;
|
||||
}
|
||||
|
||||
@Route("api/v1/service")
|
||||
|
|
@ -71,9 +79,7 @@ export class ServiceController extends Controller {
|
|||
const [result, total] = await prisma.$transaction([
|
||||
prisma.service.findMany({
|
||||
include: {
|
||||
workOnService: {
|
||||
include: { work: true },
|
||||
},
|
||||
work: true,
|
||||
},
|
||||
orderBy: { createdAt: "asc" },
|
||||
where,
|
||||
|
|
@ -104,18 +110,12 @@ export class ServiceController extends Controller {
|
|||
async getServiceById(@Path() serviceId: string) {
|
||||
const record = await prisma.service.findFirst({
|
||||
include: {
|
||||
workOnService: {
|
||||
work: {
|
||||
orderBy: { order: "asc" },
|
||||
include: {
|
||||
work: {
|
||||
include: {
|
||||
productOnWork: {
|
||||
include: {
|
||||
product: true,
|
||||
},
|
||||
orderBy: { order: "asc" },
|
||||
},
|
||||
},
|
||||
productOnWork: {
|
||||
include: { product: true },
|
||||
orderBy: { order: "asc" },
|
||||
},
|
||||
},
|
||||
},
|
||||
|
|
@ -138,9 +138,7 @@ export class ServiceController extends Controller {
|
|||
@Query() pageSize: number = 30,
|
||||
) {
|
||||
const where = {
|
||||
serviceOnWork: {
|
||||
some: { serviceId },
|
||||
},
|
||||
serviceId,
|
||||
} satisfies Prisma.WorkWhereInput;
|
||||
|
||||
const [result, total] = await prisma.$transaction([
|
||||
|
|
@ -150,6 +148,7 @@ export class ServiceController extends Controller {
|
|||
include: {
|
||||
product: true,
|
||||
},
|
||||
orderBy: { order: "asc" },
|
||||
},
|
||||
},
|
||||
where,
|
||||
|
|
@ -163,19 +162,7 @@ export class ServiceController extends Controller {
|
|||
|
||||
@Post()
|
||||
async createService(@Request() req: RequestWithUser, @Body() body: ServiceCreate) {
|
||||
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.",
|
||||
"someProductBadReq",
|
||||
);
|
||||
}
|
||||
const { work, ...payload } = body;
|
||||
|
||||
const record = await prisma.$transaction(
|
||||
async (tx) => {
|
||||
|
|
@ -192,18 +179,13 @@ export class ServiceController extends Controller {
|
|||
|
||||
return tx.service.create({
|
||||
include: {
|
||||
workOnService: {
|
||||
orderBy: { order: "asc" },
|
||||
work: {
|
||||
include: {
|
||||
work: {
|
||||
productOnWork: {
|
||||
include: {
|
||||
productOnWork: {
|
||||
include: {
|
||||
product: true,
|
||||
},
|
||||
orderBy: { order: "asc" },
|
||||
},
|
||||
product: true,
|
||||
},
|
||||
orderBy: { order: "asc" },
|
||||
},
|
||||
},
|
||||
},
|
||||
|
|
@ -211,12 +193,13 @@ export class ServiceController extends Controller {
|
|||
data: {
|
||||
...payload,
|
||||
code: `${body.code.toLocaleUpperCase()}${last.value.toString().padStart(3, "0")}`,
|
||||
workOnService: {
|
||||
work: {
|
||||
createMany: {
|
||||
data: workId.map((v, i) => ({
|
||||
order: i + 1,
|
||||
workId: v,
|
||||
})),
|
||||
data:
|
||||
work?.map((v, i) => ({
|
||||
...v,
|
||||
order: i + 1,
|
||||
})) || [],
|
||||
},
|
||||
},
|
||||
createdBy: req.user.name,
|
||||
|
|
@ -253,8 +236,23 @@ export class ServiceController extends Controller {
|
|||
throw new HttpError(HttpStatus.NOT_FOUND, "Service cannot be found.", "serviceNotFound");
|
||||
}
|
||||
|
||||
const { work, ...payload } = body;
|
||||
|
||||
const record = await prisma.service.update({
|
||||
data: { ...body, updateBy: req.user.name },
|
||||
data: {
|
||||
...payload,
|
||||
work: {
|
||||
deleteMany: {},
|
||||
createMany: {
|
||||
data:
|
||||
work?.map((v, i) => ({
|
||||
order: i + 1,
|
||||
...v,
|
||||
})) || [],
|
||||
},
|
||||
},
|
||||
updateBy: req.user.name,
|
||||
},
|
||||
where: { id: serviceId },
|
||||
});
|
||||
return Object.assign(record, {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue