refactor: remove unused
This commit is contained in:
parent
ab71a7ee20
commit
21e5467f55
1 changed files with 10 additions and 146 deletions
|
|
@ -16,14 +16,12 @@ import { Prisma, Status } from "@prisma/client";
|
||||||
|
|
||||||
import prisma from "../db";
|
import prisma from "../db";
|
||||||
import { RequestWithUser } from "../interfaces/user";
|
import { RequestWithUser } from "../interfaces/user";
|
||||||
import HttpError from "../interfaces/http-error";
|
|
||||||
import HttpStatus from "../interfaces/http-status";
|
import HttpStatus from "../interfaces/http-status";
|
||||||
import { isUsedError, notFoundError } from "../utils/error";
|
import { isUsedError, notFoundError } from "../utils/error";
|
||||||
|
|
||||||
type WorkCreate = {
|
type WorkCreate = {
|
||||||
order: number;
|
order: number;
|
||||||
name: string;
|
name: string;
|
||||||
productId: string[];
|
|
||||||
attributes?: {
|
attributes?: {
|
||||||
[key: string]: any;
|
[key: string]: any;
|
||||||
};
|
};
|
||||||
|
|
@ -32,7 +30,6 @@ type WorkCreate = {
|
||||||
type WorkUpdate = {
|
type WorkUpdate = {
|
||||||
order?: number;
|
order?: number;
|
||||||
name?: string;
|
name?: string;
|
||||||
productId?: string[];
|
|
||||||
attributes?: {
|
attributes?: {
|
||||||
[key: string]: any;
|
[key: string]: any;
|
||||||
};
|
};
|
||||||
|
|
@ -57,12 +54,8 @@ export class WorkController extends Controller {
|
||||||
prisma.work.findMany({
|
prisma.work.findMany({
|
||||||
include: {
|
include: {
|
||||||
productOnWork: {
|
productOnWork: {
|
||||||
include: {
|
include: { product: true },
|
||||||
product: true,
|
orderBy: { order: "asc" },
|
||||||
},
|
|
||||||
orderBy: {
|
|
||||||
order: "asc",
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
createdBy: true,
|
createdBy: true,
|
||||||
updatedBy: true,
|
updatedBy: true,
|
||||||
|
|
@ -97,14 +90,7 @@ export class WorkController extends Controller {
|
||||||
@Query() pageSize: number = 30,
|
@Query() pageSize: number = 30,
|
||||||
) {
|
) {
|
||||||
const where = {
|
const where = {
|
||||||
AND: [
|
AND: [{ workProduct: { some: { workId } } }, { OR: [{ name: { contains: query } }] }],
|
||||||
{
|
|
||||||
workProduct: { some: { workId } },
|
|
||||||
},
|
|
||||||
{
|
|
||||||
OR: [{ name: { contains: query } }],
|
|
||||||
},
|
|
||||||
],
|
|
||||||
} satisfies Prisma.ProductWhereInput;
|
} satisfies Prisma.ProductWhereInput;
|
||||||
|
|
||||||
const [result, total] = await prisma.$transaction([
|
const [result, total] = await prisma.$transaction([
|
||||||
|
|
@ -127,88 +113,27 @@ export class WorkController extends Controller {
|
||||||
|
|
||||||
@Post()
|
@Post()
|
||||||
async createWork(@Request() req: RequestWithUser, @Body() body: WorkCreate) {
|
async createWork(@Request() req: RequestWithUser, @Body() body: WorkCreate) {
|
||||||
const { productId, ...payload } = body;
|
|
||||||
|
|
||||||
const exist = await prisma.work.findFirst({
|
const exist = await prisma.work.findFirst({
|
||||||
include: {
|
|
||||||
productOnWork: {
|
|
||||||
include: {
|
|
||||||
product: true,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
createdBy: true,
|
|
||||||
updatedBy: true,
|
|
||||||
},
|
|
||||||
where: {
|
where: {
|
||||||
productOnWork: {
|
name: body.name,
|
||||||
every: {
|
productOnWork: { none: {} },
|
||||||
productId: { in: productId },
|
|
||||||
},
|
|
||||||
},
|
|
||||||
NOT: {
|
|
||||||
OR: [
|
|
||||||
{
|
|
||||||
productOnWork: {
|
|
||||||
some: {
|
|
||||||
productId: { notIn: productId },
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
productOnWork: {
|
|
||||||
none: {},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
],
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
if (exist) return exist;
|
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({
|
const record = await prisma.work.create({
|
||||||
include: {
|
include: {
|
||||||
productOnWork: {
|
|
||||||
include: {
|
|
||||||
product: true,
|
|
||||||
},
|
|
||||||
orderBy: {
|
|
||||||
order: "asc",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
createdBy: true,
|
createdBy: true,
|
||||||
updatedBy: true,
|
updatedBy: true,
|
||||||
},
|
},
|
||||||
data: {
|
data: {
|
||||||
...payload,
|
...body,
|
||||||
productOnWork: {
|
|
||||||
createMany: {
|
|
||||||
data: productId.map((v, i) => ({
|
|
||||||
order: i + 1,
|
|
||||||
productId: v,
|
|
||||||
createdByUserId: req.user.sub,
|
|
||||||
updatedByUserId: req.user.sub,
|
|
||||||
})),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
createdByUserId: req.user.sub,
|
createdByUserId: req.user.sub,
|
||||||
updatedByUserId: 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);
|
this.setStatus(HttpStatus.CREATED);
|
||||||
|
|
||||||
return record;
|
return record;
|
||||||
|
|
@ -222,41 +147,13 @@ export class WorkController extends Controller {
|
||||||
) {
|
) {
|
||||||
const work = await prisma.work.findUnique({ where: { id: workId } });
|
const work = await prisma.work.findUnique({ where: { id: workId } });
|
||||||
|
|
||||||
const { productId, ...payload } = body;
|
|
||||||
|
|
||||||
if (!work) throw notFoundError("Work");
|
if (!work) throw notFoundError("Work");
|
||||||
|
|
||||||
const exist = await prisma.work.findFirst({
|
const exist = await prisma.work.findFirst({
|
||||||
include: {
|
|
||||||
productOnWork: {
|
|
||||||
include: {
|
|
||||||
product: true,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
where: {
|
where: {
|
||||||
productOnWork: {
|
id: { not: workId },
|
||||||
every: {
|
name: body.name,
|
||||||
productId: { in: productId },
|
productOnWork: { none: {} },
|
||||||
},
|
|
||||||
},
|
|
||||||
NOT: {
|
|
||||||
OR: [
|
|
||||||
{ id: workId },
|
|
||||||
{
|
|
||||||
productOnWork: {
|
|
||||||
some: {
|
|
||||||
productId: { notIn: productId },
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
productOnWork: {
|
|
||||||
none: {},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
],
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -264,44 +161,11 @@ export class WorkController extends Controller {
|
||||||
|
|
||||||
const record = await prisma.work.update({
|
const record = await prisma.work.update({
|
||||||
include: {
|
include: {
|
||||||
productOnWork: {
|
|
||||||
include: {
|
|
||||||
product: true,
|
|
||||||
},
|
|
||||||
orderBy: {
|
|
||||||
order: "asc",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
createdBy: true,
|
createdBy: true,
|
||||||
updatedBy: true,
|
updatedBy: true,
|
||||||
},
|
},
|
||||||
where: { id: workId },
|
where: { id: workId },
|
||||||
data: {
|
data: { ...body, updatedByUserId: req.user.sub },
|
||||||
...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,
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
|
|
||||||
return record;
|
return record;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue