From 21e5467f557b4353fcbe52cbc415c1f229a2b62e Mon Sep 17 00:00:00 2001 From: Methapon Metanipat Date: Tue, 22 Oct 2024 10:56:46 +0700 Subject: [PATCH] refactor: remove unused --- src/controllers/04-work-controller.ts | 156 ++------------------------ 1 file changed, 10 insertions(+), 146 deletions(-) diff --git a/src/controllers/04-work-controller.ts b/src/controllers/04-work-controller.ts index 773ee42..9b875b6 100644 --- a/src/controllers/04-work-controller.ts +++ b/src/controllers/04-work-controller.ts @@ -16,14 +16,12 @@ import { Prisma, Status } from "@prisma/client"; import prisma from "../db"; import { RequestWithUser } from "../interfaces/user"; -import HttpError from "../interfaces/http-error"; import HttpStatus from "../interfaces/http-status"; import { isUsedError, notFoundError } from "../utils/error"; type WorkCreate = { order: number; name: string; - productId: string[]; attributes?: { [key: string]: any; }; @@ -32,7 +30,6 @@ type WorkCreate = { type WorkUpdate = { order?: number; name?: string; - productId?: string[]; attributes?: { [key: string]: any; }; @@ -57,12 +54,8 @@ export class WorkController extends Controller { prisma.work.findMany({ include: { productOnWork: { - include: { - product: true, - }, - orderBy: { - order: "asc", - }, + include: { product: true }, + orderBy: { order: "asc" }, }, createdBy: true, updatedBy: true, @@ -97,14 +90,7 @@ export class WorkController extends Controller { @Query() pageSize: number = 30, ) { const where = { - AND: [ - { - workProduct: { some: { workId } }, - }, - { - OR: [{ name: { contains: query } }], - }, - ], + AND: [{ workProduct: { some: { workId } } }, { OR: [{ name: { contains: query } }] }], } satisfies Prisma.ProductWhereInput; const [result, total] = await prisma.$transaction([ @@ -127,88 +113,27 @@ export class WorkController extends Controller { @Post() async createWork(@Request() req: RequestWithUser, @Body() body: WorkCreate) { - const { productId, ...payload } = body; - const exist = await prisma.work.findFirst({ - include: { - productOnWork: { - include: { - product: true, - }, - }, - createdBy: true, - updatedBy: true, - }, where: { - productOnWork: { - every: { - productId: { in: productId }, - }, - }, - NOT: { - OR: [ - { - productOnWork: { - some: { - productId: { notIn: productId }, - }, - }, - }, - { - productOnWork: { - none: {}, - }, - }, - ], - }, + name: body.name, + productOnWork: { none: {} }, }, }); if (exist) return exist; - const productList = await prisma.product.findMany({ - where: { id: { in: productId } }, - }); - - if (productList.length !== productId.length) { - throw new HttpError(HttpStatus.BAD_REQUEST, "Some product not found.", "someProductBadReq"); - } - const record = await prisma.work.create({ include: { - productOnWork: { - include: { - product: true, - }, - orderBy: { - order: "asc", - }, - }, createdBy: true, updatedBy: true, }, data: { - ...payload, - productOnWork: { - createMany: { - data: productId.map((v, i) => ({ - order: i + 1, - productId: v, - createdByUserId: req.user.sub, - updatedByUserId: req.user.sub, - })), - }, - }, + ...body, createdByUserId: req.user.sub, updatedByUserId: req.user.sub, }, }); - await prisma.product.updateMany({ - where: { id: { in: body.productId }, status: Status.CREATED }, - data: { status: Status.ACTIVE }, - }); - this.setStatus(HttpStatus.CREATED); return record; @@ -222,41 +147,13 @@ export class WorkController extends Controller { ) { const work = await prisma.work.findUnique({ where: { id: workId } }); - const { productId, ...payload } = body; - if (!work) throw notFoundError("Work"); const exist = await prisma.work.findFirst({ - include: { - productOnWork: { - include: { - product: true, - }, - }, - }, where: { - productOnWork: { - every: { - productId: { in: productId }, - }, - }, - NOT: { - OR: [ - { id: workId }, - { - productOnWork: { - some: { - productId: { notIn: productId }, - }, - }, - }, - { - productOnWork: { - none: {}, - }, - }, - ], - }, + id: { not: workId }, + name: body.name, + productOnWork: { none: {} }, }, }); @@ -264,44 +161,11 @@ export class WorkController extends Controller { const record = await prisma.work.update({ include: { - productOnWork: { - include: { - product: true, - }, - orderBy: { - order: "asc", - }, - }, createdBy: true, updatedBy: true, }, where: { id: workId }, - data: { - ...payload, - productOnWork: productId - ? { - deleteMany: { - productId: { notIn: productId }, - }, - upsert: productId.map((v, i) => ({ - where: { - workId_productId: { - workId, - productId: v, - }, - }, - update: { order: i + 1 }, - create: { - order: i + 1, - productId: v, - createdByUserId: req.user.sub, - updatedByUserId: req.user.sub, - }, - })), - } - : undefined, - updatedByUserId: req.user.sub, - }, + data: { ...body, updatedByUserId: req.user.sub }, }); return record;