feat: service and product with work relation
This commit is contained in:
parent
31ed48d190
commit
ee3ae91957
2 changed files with 259 additions and 30 deletions
|
|
@ -20,15 +20,13 @@ import HttpError from "../../interfaces/http-error";
|
|||
import HttpStatus from "../../interfaces/http-status";
|
||||
|
||||
type WorkCreate = {
|
||||
order: number;
|
||||
name: string;
|
||||
serviceId: string;
|
||||
productId: string[];
|
||||
};
|
||||
|
||||
type WorkUpdate = {
|
||||
order?: number;
|
||||
name?: string;
|
||||
serviceId?: string;
|
||||
productId?: string[];
|
||||
};
|
||||
|
||||
@Route("api/v1/work")
|
||||
|
|
@ -47,8 +45,17 @@ export class WorkController extends Controller {
|
|||
|
||||
const [result, total] = await prisma.$transaction([
|
||||
prisma.work.findMany({
|
||||
include: {
|
||||
productOnWork: {
|
||||
include: {
|
||||
product: true,
|
||||
},
|
||||
orderBy: {
|
||||
order: "asc",
|
||||
},
|
||||
},
|
||||
},
|
||||
orderBy: { createdAt: "asc" },
|
||||
include: { service: true },
|
||||
where,
|
||||
take: pageSize,
|
||||
skip: (page - 1) * pageSize,
|
||||
|
|
@ -62,7 +69,6 @@ export class WorkController extends Controller {
|
|||
@Get("{workId}")
|
||||
async getWorkById(@Path() workId: string) {
|
||||
const record = await prisma.work.findFirst({
|
||||
include: { service: true },
|
||||
where: { id: workId },
|
||||
});
|
||||
|
||||
|
|
@ -71,6 +77,7 @@ export class WorkController extends Controller {
|
|||
|
||||
return record;
|
||||
}
|
||||
|
||||
@Get("{workId}/product")
|
||||
async getProductOfWork(
|
||||
@Path() workId: string,
|
||||
|
|
@ -81,9 +88,7 @@ export class WorkController extends Controller {
|
|||
const where = {
|
||||
AND: [
|
||||
{
|
||||
workProduct: {
|
||||
some: { workId },
|
||||
},
|
||||
workProduct: { some: { workId } },
|
||||
},
|
||||
{
|
||||
OR: [{ name: { contains: query } }],
|
||||
|
|
@ -107,28 +112,85 @@ export class WorkController extends Controller {
|
|||
|
||||
@Post()
|
||||
async createWork(@Request() req: RequestWithUser, @Body() body: WorkCreate) {
|
||||
const service = await prisma.service.findFirst({
|
||||
where: { id: body.serviceId },
|
||||
const { productId, ...payload } = body;
|
||||
|
||||
const exist = await prisma.work.findFirst({
|
||||
include: {
|
||||
productOnWork: {
|
||||
include: {
|
||||
product: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
where: {
|
||||
productOnWork: {
|
||||
every: {
|
||||
productId: { in: productId },
|
||||
},
|
||||
},
|
||||
NOT: {
|
||||
OR: [
|
||||
{
|
||||
productOnWork: {
|
||||
some: {
|
||||
productId: { notIn: productId },
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
productOnWork: {
|
||||
none: {},
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (!service) {
|
||||
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,
|
||||
"Service not found.",
|
||||
"Some product not found.",
|
||||
"missing_or_invalid_parameter",
|
||||
);
|
||||
}
|
||||
|
||||
const record = await prisma.work.create({
|
||||
include: {
|
||||
productOnWork: {
|
||||
include: {
|
||||
product: true,
|
||||
},
|
||||
orderBy: {
|
||||
order: "asc",
|
||||
},
|
||||
},
|
||||
},
|
||||
data: {
|
||||
...body,
|
||||
...payload,
|
||||
productOnWork: {
|
||||
createMany: {
|
||||
data: productId.map((v, i) => ({
|
||||
order: i + 1,
|
||||
productId: v,
|
||||
createdBy: req.user.name,
|
||||
updateBy: req.user.name,
|
||||
})),
|
||||
},
|
||||
},
|
||||
createdBy: req.user.name,
|
||||
updateBy: req.user.name,
|
||||
},
|
||||
});
|
||||
|
||||
await prisma.service.updateMany({
|
||||
where: { id: service.id },
|
||||
await prisma.product.updateMany({
|
||||
where: { id: { in: body.productId }, status: Status.CREATED },
|
||||
data: { status: Status.ACTIVE },
|
||||
});
|
||||
|
||||
|
|
@ -138,18 +200,91 @@ export class WorkController extends Controller {
|
|||
}
|
||||
|
||||
@Put("{workId}")
|
||||
async editService(
|
||||
async editWork(
|
||||
@Request() req: RequestWithUser,
|
||||
@Body() body: WorkUpdate,
|
||||
@Path() workId: string,
|
||||
) {
|
||||
const { productId, ...payload } = body;
|
||||
|
||||
if (!(await prisma.work.findUnique({ where: { id: workId } }))) {
|
||||
throw new HttpError(HttpStatus.NOT_FOUND, "Work cannot be found.", "data_not_found");
|
||||
}
|
||||
|
||||
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: {},
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (exist) return exist;
|
||||
|
||||
const record = await prisma.work.update({
|
||||
data: { ...body, updateBy: req.user.name },
|
||||
include: {
|
||||
productOnWork: {
|
||||
include: {
|
||||
product: true,
|
||||
},
|
||||
orderBy: {
|
||||
order: "asc",
|
||||
},
|
||||
},
|
||||
},
|
||||
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,
|
||||
createdBy: req.user.name,
|
||||
updateBy: req.user.name,
|
||||
},
|
||||
})),
|
||||
}
|
||||
: undefined,
|
||||
updateBy: req.user.name,
|
||||
},
|
||||
});
|
||||
|
||||
return record;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue