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? attributes Json?
status Status @default(CREATED) status Status @default(CREATED)
work Work[]
createdBy String? createdBy String?
createdAt DateTime @default(now()) createdAt DateTime @default(now())
updateBy String? updateBy String?
updatedAt DateTime @updatedAt updatedAt DateTime @updatedAt
workOnService WorkService[]
} }
model Work { model Work {
id String @id @default(uuid()) id String @id @default(uuid())
order Int
name String name String
attributes Json? attributes Json?
status Status @default(CREATED) status Status @default(CREATED)
service Service? @relation(fields: [serviceId], references: [id], onDelete: Cascade)
serviceId String?
createdBy String? createdBy String?
createdAt DateTime @default(now()) createdAt DateTime @default(now())
updateBy String? updateBy String?
updatedAt DateTime @updatedAt updatedAt DateTime @updatedAt
productOnWork WorkProduct[] 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 { model WorkProduct {

View file

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

View file

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