refactor: remove unused

This commit is contained in:
Methapon Metanipat 2024-10-22 10:56:46 +07:00
parent ab71a7ee20
commit 21e5467f55

View file

@ -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;