feat: update work endpoints

This commit is contained in:
Methapon2001 2024-06-14 15:49:05 +07:00
parent 39206a6d76
commit 51fd6b0589
4 changed files with 76 additions and 72 deletions

View file

@ -0,0 +1,22 @@
/*
Warnings:
- You are about to drop the `WorkService` table. If the table is not empty, all the data it contains will be lost.
- Added the required column `order` to the `Work` table without a default value. This is not possible if the table is not empty.
*/
-- DropForeignKey
ALTER TABLE "WorkService" DROP CONSTRAINT "WorkService_serviceId_fkey";
-- DropForeignKey
ALTER TABLE "WorkService" DROP CONSTRAINT "WorkService_workId_fkey";
-- AlterTable
ALTER TABLE "Work" ADD COLUMN "order" INTEGER NOT NULL,
ADD COLUMN "serviceId" TEXT;
-- DropTable
DROP TABLE "WorkService";
-- AddForeignKey
ALTER TABLE "Work" ADD CONSTRAINT "Work_serviceId_fkey" FOREIGN KEY ("serviceId") REFERENCES "Service"("id") ON DELETE CASCADE ON UPDATE CASCADE;

View file

@ -543,45 +543,32 @@ model Service {
attributes Json?
status Status @default(CREATED)
work Work[]
createdBy String?
createdAt DateTime @default(now())
updateBy String?
updatedAt DateTime @updatedAt
workOnService WorkService[]
}
model Work {
id String @id @default(uuid())
order Int
name String
attributes Json?
status Status @default(CREATED)
service Service? @relation(fields: [serviceId], references: [id], onDelete: Cascade)
serviceId String?
createdBy String?
createdAt DateTime @default(now())
updateBy String?
updatedAt DateTime @updatedAt
productOnWork WorkProduct[]
serviceOnWork WorkService[]
}
model WorkService {
order Int
work Work @relation(fields: [workId], references: [id], onDelete: Cascade)
workId String
service Service @relation(fields: [serviceId], references: [id], onDelete: Cascade)
serviceId String
createdBy String?
createdAt DateTime @default(now())
updateBy String?
updatedAt DateTime @updatedAt
@@id([workId, serviceId])
}
model WorkProduct {

View file

@ -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, {

View file

@ -20,6 +20,7 @@ import HttpError from "../../interfaces/http-error";
import HttpStatus from "../../interfaces/http-status";
type WorkCreate = {
order: number;
name: string;
productId: string[];
attributes?: {
@ -28,6 +29,7 @@ type WorkCreate = {
};
type WorkUpdate = {
order?: number;
name?: string;
productId?: string[];
attributes?: {
@ -78,8 +80,7 @@ export class WorkController extends Controller {
where: { id: workId },
});
if (!record)
throw new HttpError(HttpStatus.NOT_FOUND, "Work cannot be found.", "workNotFound");
if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "Work cannot be found.", "workNotFound");
return record;
}
@ -160,11 +161,7 @@ export class WorkController extends Controller {
});
if (productList.length !== productId.length) {
throw new HttpError(
HttpStatus.BAD_REQUEST,
"Some product not found.",
"someProductBadReq",
);
throw new HttpError(HttpStatus.BAD_REQUEST, "Some product not found.", "someProductBadReq");
}
const record = await prisma.work.create({