101 lines
2.5 KiB
TypeScript
101 lines
2.5 KiB
TypeScript
import { Body, Controller, Get, Path, Put, Query, Request, Route, Security, Tags } from "tsoa";
|
|
import { RequestWithUser } from "../interfaces/user";
|
|
import prisma from "../db";
|
|
import { Prisma, RequestWorkStatus } from "@prisma/client";
|
|
import { createPermCondition } from "../services/permission";
|
|
|
|
// User in company can see.
|
|
const permissionCond = createPermCondition((_) => true);
|
|
|
|
@Route("api/v1/request-list")
|
|
@Tags("Request List")
|
|
export class RequestListController extends Controller {
|
|
@Get()
|
|
@Security("keycloak")
|
|
async getRequest(
|
|
@Request() req: RequestWithUser,
|
|
@Query() page: number = 1,
|
|
@Query() pageSize: number = 30,
|
|
) {
|
|
const where = {
|
|
request: {
|
|
quotation: {
|
|
customerBranch: {
|
|
customer: {
|
|
registeredBranch: { OR: permissionCond(req.user) },
|
|
},
|
|
},
|
|
},
|
|
},
|
|
} satisfies Prisma.RequestWorkWhereInput;
|
|
|
|
const [result, total] = await prisma.$transaction([
|
|
prisma.requestWork.findMany({
|
|
where,
|
|
include: {
|
|
request: {
|
|
include: {
|
|
quotation: true,
|
|
employee: true,
|
|
},
|
|
},
|
|
productService: {
|
|
include: {
|
|
service: true,
|
|
work: true,
|
|
product: true,
|
|
},
|
|
},
|
|
},
|
|
take: pageSize,
|
|
skip: (page - 1) * pageSize,
|
|
}),
|
|
prisma.requestWork.count({ where }),
|
|
]);
|
|
|
|
return { result, page, pageSize, total };
|
|
}
|
|
|
|
@Get("{requestId}")
|
|
async getRequestById(@Path() requestId: string) {
|
|
return await prisma.requestWork.findFirst({
|
|
include: {
|
|
request: {
|
|
include: {
|
|
quotation: true,
|
|
employee: true,
|
|
},
|
|
},
|
|
productService: {
|
|
include: {
|
|
service: true,
|
|
work: true,
|
|
product: true,
|
|
},
|
|
},
|
|
},
|
|
where: { id: requestId },
|
|
});
|
|
}
|
|
|
|
@Put("{requestId}")
|
|
async updateRequestById(
|
|
@Request() req: RequestWithUser,
|
|
@Path() requestId: string,
|
|
@Body() payload: { status: RequestWorkStatus; attributes: Record<string, any> },
|
|
) {
|
|
const record = await prisma.requestWork.update({
|
|
where: { id: requestId },
|
|
data: {
|
|
workStatus: payload.status,
|
|
request: {
|
|
update: { flow: payload.attributes },
|
|
},
|
|
},
|
|
});
|
|
|
|
return record;
|
|
}
|
|
}
|
|
|
|
export class RequestListAttachmentController extends Controller {}
|