2024-06-10 17:47:40 +07:00
|
|
|
import {
|
|
|
|
|
Body,
|
|
|
|
|
Controller,
|
|
|
|
|
Delete,
|
|
|
|
|
Get,
|
|
|
|
|
Put,
|
|
|
|
|
Path,
|
|
|
|
|
Post,
|
|
|
|
|
Query,
|
|
|
|
|
Request,
|
|
|
|
|
Route,
|
|
|
|
|
Security,
|
|
|
|
|
Tags,
|
|
|
|
|
} from "tsoa";
|
|
|
|
|
import { Prisma, Status } from "@prisma/client";
|
|
|
|
|
|
|
|
|
|
import prisma from "../../db";
|
2024-06-12 16:37:56 +07:00
|
|
|
import minio, { presignedGetObjectIfExist } from "../../services/minio";
|
2024-06-10 17:47:40 +07:00
|
|
|
import { RequestWithUser } from "../../interfaces/user";
|
|
|
|
|
import HttpError from "../../interfaces/http-error";
|
|
|
|
|
import HttpStatus from "../../interfaces/http-status";
|
|
|
|
|
|
|
|
|
|
if (!process.env.MINIO_BUCKET) {
|
|
|
|
|
throw Error("Require MinIO bucket.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const MINIO_BUCKET = process.env.MINIO_BUCKET;
|
2024-07-03 17:28:00 +07:00
|
|
|
const MANAGE_ROLES = [
|
|
|
|
|
"system",
|
|
|
|
|
"head_of_admin",
|
|
|
|
|
"admin",
|
|
|
|
|
"branch_admin",
|
|
|
|
|
"branch_manager",
|
|
|
|
|
"accountant",
|
|
|
|
|
"branch_accountant",
|
|
|
|
|
];
|
2024-06-10 17:47:40 +07:00
|
|
|
|
|
|
|
|
type ServiceCreate = {
|
2024-06-12 16:32:17 +07:00
|
|
|
code: "MOU" | "mou";
|
2024-06-10 17:47:40 +07:00
|
|
|
name: string;
|
|
|
|
|
detail: string;
|
2024-06-13 15:47:11 +07:00
|
|
|
attributes?: {
|
|
|
|
|
[key: string]: any;
|
|
|
|
|
};
|
2024-06-24 14:22:14 +07:00
|
|
|
status?: Status;
|
2024-06-14 15:49:05 +07:00
|
|
|
work?: {
|
|
|
|
|
name: string;
|
|
|
|
|
productId: string[];
|
|
|
|
|
attributes?: { [key: string]: any };
|
|
|
|
|
}[];
|
2024-07-03 11:33:12 +07:00
|
|
|
productTypeId: string;
|
2024-07-03 17:28:00 +07:00
|
|
|
registeredBranchId?: string;
|
2024-06-10 17:47:40 +07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
type ServiceUpdate = {
|
2024-06-24 14:17:17 +07:00
|
|
|
name?: string;
|
|
|
|
|
detail?: string;
|
2024-06-13 15:47:11 +07:00
|
|
|
attributes?: {
|
|
|
|
|
[key: string]: any;
|
|
|
|
|
};
|
2024-06-24 16:19:49 +07:00
|
|
|
status?: "ACTIVE" | "INACTIVE";
|
2024-06-14 15:49:05 +07:00
|
|
|
work?: {
|
|
|
|
|
name: string;
|
|
|
|
|
productId: string[];
|
|
|
|
|
attributes?: { [key: string]: any };
|
|
|
|
|
}[];
|
2024-07-03 11:33:12 +07:00
|
|
|
productTypeId?: string;
|
2024-07-03 17:28:00 +07:00
|
|
|
registeredBranchId?: string;
|
2024-06-10 17:47:40 +07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function imageLocation(id: string) {
|
2024-06-14 15:49:05 +07:00
|
|
|
return `service/${id}/service-image`;
|
2024-06-10 17:47:40 +07:00
|
|
|
}
|
|
|
|
|
|
2024-07-03 17:28:00 +07:00
|
|
|
function globalAllow(roles?: string[]) {
|
|
|
|
|
return ["system", "head_of_admin", "admin", "accountant"].some((v) => roles?.includes(v));
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-11 13:35:54 +07:00
|
|
|
@Route("api/v1/service")
|
2024-06-10 17:47:40 +07:00
|
|
|
@Tags("Service")
|
|
|
|
|
export class ServiceController extends Controller {
|
2024-06-11 09:31:10 +07:00
|
|
|
@Get("stats")
|
2024-06-21 14:39:00 +07:00
|
|
|
@Security("keycloak")
|
2024-06-11 09:27:37 +07:00
|
|
|
async getServiceStats() {
|
|
|
|
|
return await prisma.service.count();
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-10 17:47:40 +07:00
|
|
|
@Get()
|
2024-06-21 14:39:00 +07:00
|
|
|
@Security("keycloak")
|
2024-06-10 17:47:40 +07:00
|
|
|
async getService(
|
|
|
|
|
@Query() query: string = "",
|
|
|
|
|
@Query() page: number = 1,
|
|
|
|
|
@Query() pageSize: number = 30,
|
2024-06-25 14:09:54 +07:00
|
|
|
@Query() status?: Status,
|
2024-07-03 11:35:03 +07:00
|
|
|
@Query() productTypeId?: string,
|
2024-07-04 11:27:19 +07:00
|
|
|
@Query() registeredBranchId?: string,
|
2024-07-18 16:21:57 +07:00
|
|
|
@Query() fullDetail?: boolean,
|
2024-06-10 17:47:40 +07:00
|
|
|
) {
|
2024-06-25 14:09:54 +07:00
|
|
|
const filterStatus = (val?: Status) => {
|
|
|
|
|
if (!val) return {};
|
|
|
|
|
|
|
|
|
|
return val !== Status.CREATED && val !== Status.ACTIVE
|
|
|
|
|
? { status: val }
|
|
|
|
|
: { OR: [{ status: Status.CREATED }, { status: Status.ACTIVE }] };
|
|
|
|
|
};
|
|
|
|
|
|
2024-06-10 17:47:40 +07:00
|
|
|
const where = {
|
2024-06-25 14:09:54 +07:00
|
|
|
OR: [
|
2024-07-03 11:35:03 +07:00
|
|
|
{ name: { contains: query }, productTypeId, ...filterStatus(status) },
|
|
|
|
|
{ detail: { contains: query }, productTypeId, ...filterStatus(status) },
|
2024-06-25 14:09:54 +07:00
|
|
|
],
|
2024-07-04 11:27:19 +07:00
|
|
|
AND: registeredBranchId
|
|
|
|
|
? {
|
|
|
|
|
OR: [{ registeredBranchId: registeredBranchId }, { registeredBranchId: null }],
|
|
|
|
|
}
|
|
|
|
|
: undefined,
|
2024-06-10 17:47:40 +07:00
|
|
|
} satisfies Prisma.ServiceWhereInput;
|
|
|
|
|
|
|
|
|
|
const [result, total] = await prisma.$transaction([
|
|
|
|
|
prisma.service.findMany({
|
2024-06-12 16:32:17 +07:00
|
|
|
include: {
|
2024-07-18 16:21:57 +07:00
|
|
|
work: fullDetail
|
|
|
|
|
? {
|
|
|
|
|
orderBy: { order: "asc" },
|
|
|
|
|
include: {
|
|
|
|
|
productOnWork: {
|
|
|
|
|
include: { product: true },
|
|
|
|
|
orderBy: { order: "asc" },
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
: true,
|
2024-07-01 14:38:07 +07:00
|
|
|
createdBy: true,
|
|
|
|
|
updatedBy: true,
|
2024-06-12 16:32:17 +07:00
|
|
|
},
|
2024-06-24 13:20:59 +07:00
|
|
|
orderBy: [{ statusOrder: "asc" }, { createdAt: "asc" }],
|
2024-06-10 17:47:40 +07:00
|
|
|
where,
|
|
|
|
|
take: pageSize,
|
|
|
|
|
skip: (page - 1) * pageSize,
|
|
|
|
|
}),
|
|
|
|
|
prisma.service.count({ where }),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
result: await Promise.all(
|
|
|
|
|
result.map(async (v) => ({
|
|
|
|
|
...v,
|
2024-06-12 16:37:56 +07:00
|
|
|
imageUrl: await presignedGetObjectIfExist(
|
|
|
|
|
MINIO_BUCKET,
|
|
|
|
|
imageLocation(v.id),
|
|
|
|
|
12 * 60 * 60,
|
|
|
|
|
),
|
2024-06-10 17:47:40 +07:00
|
|
|
})),
|
|
|
|
|
),
|
|
|
|
|
page,
|
|
|
|
|
pageSize,
|
|
|
|
|
total,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Get("{serviceId}")
|
2024-06-21 14:39:00 +07:00
|
|
|
@Security("keycloak")
|
2024-06-10 17:47:40 +07:00
|
|
|
async getServiceById(@Path() serviceId: string) {
|
|
|
|
|
const record = await prisma.service.findFirst({
|
2024-06-12 16:32:17 +07:00
|
|
|
include: {
|
2024-06-14 15:49:05 +07:00
|
|
|
work: {
|
2024-06-12 16:32:17 +07:00
|
|
|
orderBy: { order: "asc" },
|
|
|
|
|
include: {
|
2024-06-14 15:49:05 +07:00
|
|
|
productOnWork: {
|
|
|
|
|
include: { product: true },
|
|
|
|
|
orderBy: { order: "asc" },
|
2024-06-12 16:32:17 +07:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
2024-07-01 14:38:07 +07:00
|
|
|
createdBy: true,
|
|
|
|
|
updatedBy: true,
|
2024-06-12 16:32:17 +07:00
|
|
|
},
|
2024-06-10 17:47:40 +07:00
|
|
|
where: { id: serviceId },
|
|
|
|
|
});
|
|
|
|
|
|
2024-06-14 17:57:58 +07:00
|
|
|
if (!record) {
|
2024-06-14 05:43:32 +00:00
|
|
|
throw new HttpError(HttpStatus.NOT_FOUND, "Service cannot be found.", "serviceNotFound");
|
2024-06-14 17:57:58 +07:00
|
|
|
}
|
2024-06-10 17:47:40 +07:00
|
|
|
|
|
|
|
|
return Object.assign(record, {
|
2024-06-12 16:37:56 +07:00
|
|
|
imageUrl: await presignedGetObjectIfExist(MINIO_BUCKET, imageLocation(record.id), 60 * 60),
|
2024-06-10 17:47:40 +07:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Get("{serviceId}/work")
|
2024-06-21 14:39:00 +07:00
|
|
|
@Security("keycloak")
|
2024-06-10 17:47:40 +07:00
|
|
|
async getWorkOfService(
|
|
|
|
|
@Path() serviceId: string,
|
|
|
|
|
@Query() page: number = 1,
|
|
|
|
|
@Query() pageSize: number = 30,
|
|
|
|
|
) {
|
2024-06-12 16:32:17 +07:00
|
|
|
const where = {
|
2024-06-14 15:49:05 +07:00
|
|
|
serviceId,
|
2024-06-12 16:32:17 +07:00
|
|
|
} satisfies Prisma.WorkWhereInput;
|
|
|
|
|
|
2024-06-10 17:47:40 +07:00
|
|
|
const [result, total] = await prisma.$transaction([
|
|
|
|
|
prisma.work.findMany({
|
2024-06-12 16:32:17 +07:00
|
|
|
include: {
|
|
|
|
|
productOnWork: {
|
|
|
|
|
include: {
|
|
|
|
|
product: true,
|
|
|
|
|
},
|
2024-06-14 15:49:05 +07:00
|
|
|
orderBy: { order: "asc" },
|
2024-06-12 16:32:17 +07:00
|
|
|
},
|
2024-07-01 14:38:07 +07:00
|
|
|
createdBy: true,
|
|
|
|
|
updatedBy: true,
|
2024-06-12 16:32:17 +07:00
|
|
|
},
|
|
|
|
|
where,
|
2024-06-10 17:47:40 +07:00
|
|
|
take: pageSize,
|
|
|
|
|
skip: (page - 1) * pageSize,
|
|
|
|
|
}),
|
2024-06-12 16:32:17 +07:00
|
|
|
prisma.work.count({ where }),
|
2024-06-10 17:47:40 +07:00
|
|
|
]);
|
|
|
|
|
return { result, page, pageSize, total };
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-21 14:39:00 +07:00
|
|
|
@Get("{serviceId}/image")
|
|
|
|
|
async getServiceImageById(@Request() req: RequestWithUser, @Path() serviceId: string) {
|
|
|
|
|
const url = await presignedGetObjectIfExist(MINIO_BUCKET, imageLocation(serviceId), 60 * 60);
|
|
|
|
|
|
|
|
|
|
if (!url) {
|
|
|
|
|
throw new HttpError(HttpStatus.NOT_FOUND, "Image cannot be found", "imageNotFound");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return req.res?.redirect(url);
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-10 17:47:40 +07:00
|
|
|
@Post()
|
2024-07-03 17:28:00 +07:00
|
|
|
@Security("keycloak", MANAGE_ROLES)
|
2024-06-10 17:47:40 +07:00
|
|
|
async createService(@Request() req: RequestWithUser, @Body() body: ServiceCreate) {
|
2024-07-03 11:33:12 +07:00
|
|
|
const { work, productTypeId, ...payload } = body;
|
|
|
|
|
|
2024-07-03 17:28:00 +07:00
|
|
|
const [productType, branch] = await prisma.$transaction([
|
|
|
|
|
prisma.productType.findFirst({
|
|
|
|
|
include: {
|
|
|
|
|
createdBy: true,
|
|
|
|
|
updatedBy: true,
|
|
|
|
|
},
|
|
|
|
|
where: { id: body.productTypeId },
|
|
|
|
|
}),
|
|
|
|
|
prisma.branch.findFirst({
|
2024-08-14 20:40:31 +07:00
|
|
|
include: { user: { where: { userId: req.user.sub } } },
|
2024-07-03 17:28:00 +07:00
|
|
|
where: { id: body.registeredBranchId },
|
|
|
|
|
}),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
if (!globalAllow(req.user.roles) && !branch?.user.find((v) => v.userId === req.user.sub)) {
|
|
|
|
|
throw new HttpError(
|
|
|
|
|
HttpStatus.FORBIDDEN,
|
|
|
|
|
"You do not have permission to perform this action.",
|
|
|
|
|
"noPermission",
|
|
|
|
|
);
|
|
|
|
|
}
|
2024-07-03 11:33:12 +07:00
|
|
|
|
|
|
|
|
if (!productType) {
|
|
|
|
|
throw new HttpError(
|
|
|
|
|
HttpStatus.BAD_REQUEST,
|
|
|
|
|
"Product Type cannot be found.",
|
|
|
|
|
"relationProductTypeNotFound",
|
|
|
|
|
);
|
|
|
|
|
}
|
2024-06-12 16:32:17 +07:00
|
|
|
|
2024-07-03 17:28:00 +07:00
|
|
|
if (body.registeredBranchId && !branch) {
|
|
|
|
|
throw new HttpError(
|
|
|
|
|
HttpStatus.BAD_REQUEST,
|
|
|
|
|
"Branch cannot be found.",
|
|
|
|
|
"relationBranchNotFound",
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-12 16:32:17 +07:00
|
|
|
const record = await prisma.$transaction(
|
|
|
|
|
async (tx) => {
|
|
|
|
|
const last = await tx.runningNo.upsert({
|
|
|
|
|
where: {
|
|
|
|
|
key: `SERVICE_${body.code.toLocaleUpperCase()}`,
|
|
|
|
|
},
|
|
|
|
|
create: {
|
|
|
|
|
key: `SERVICE_${body.code.toLocaleUpperCase()}`,
|
|
|
|
|
value: 1,
|
|
|
|
|
},
|
|
|
|
|
update: { value: { increment: 1 } },
|
|
|
|
|
});
|
|
|
|
|
|
2024-06-18 14:35:06 +07:00
|
|
|
const workList = await Promise.all(
|
|
|
|
|
(work || []).map(async (w, wIdx) =>
|
|
|
|
|
tx.work.create({
|
|
|
|
|
data: {
|
|
|
|
|
name: w.name,
|
|
|
|
|
order: wIdx + 1,
|
|
|
|
|
attributes: w.attributes,
|
|
|
|
|
productOnWork: {
|
|
|
|
|
createMany: {
|
|
|
|
|
data: w.productId.map((p, pIdx) => ({
|
|
|
|
|
productId: p,
|
|
|
|
|
order: pIdx + 1,
|
|
|
|
|
})),
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
|
2024-06-12 16:32:17 +07:00
|
|
|
return tx.service.create({
|
|
|
|
|
include: {
|
2024-06-14 15:49:05 +07:00
|
|
|
work: {
|
2024-06-12 16:32:17 +07:00
|
|
|
include: {
|
2024-06-14 15:49:05 +07:00
|
|
|
productOnWork: {
|
2024-06-12 16:32:17 +07:00
|
|
|
include: {
|
2024-06-14 15:49:05 +07:00
|
|
|
product: true,
|
2024-06-12 16:32:17 +07:00
|
|
|
},
|
2024-06-14 15:49:05 +07:00
|
|
|
orderBy: { order: "asc" },
|
2024-06-12 16:32:17 +07:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
2024-07-01 14:38:07 +07:00
|
|
|
createdBy: true,
|
|
|
|
|
updatedBy: true,
|
2024-06-12 16:32:17 +07:00
|
|
|
},
|
|
|
|
|
data: {
|
|
|
|
|
...payload,
|
2024-07-03 11:33:12 +07:00
|
|
|
productTypeId,
|
2024-06-24 14:23:10 +07:00
|
|
|
statusOrder: +(body.status === "INACTIVE"),
|
2024-06-12 16:32:17 +07:00
|
|
|
code: `${body.code.toLocaleUpperCase()}${last.value.toString().padStart(3, "0")}`,
|
2024-06-18 14:35:06 +07:00
|
|
|
work: { connect: workList.map((v) => ({ id: v.id })) },
|
2024-07-01 13:24:02 +07:00
|
|
|
createdByUserId: req.user.sub,
|
|
|
|
|
updatedByUserId: req.user.sub,
|
2024-06-12 16:32:17 +07:00
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
{ isolationLevel: Prisma.TransactionIsolationLevel.Serializable },
|
|
|
|
|
);
|
2024-06-10 17:47:40 +07:00
|
|
|
this.setStatus(HttpStatus.CREATED);
|
|
|
|
|
|
|
|
|
|
return Object.assign(record, {
|
2024-06-12 16:37:56 +07:00
|
|
|
imageUrl: await presignedGetObjectIfExist(
|
2024-06-10 17:47:40 +07:00
|
|
|
MINIO_BUCKET,
|
|
|
|
|
imageLocation(record.id),
|
|
|
|
|
12 * 60 * 60,
|
|
|
|
|
),
|
|
|
|
|
imageUploadUrl: await minio.presignedPutObject(
|
|
|
|
|
MINIO_BUCKET,
|
|
|
|
|
imageLocation(record.id),
|
|
|
|
|
12 * 60 * 60,
|
|
|
|
|
),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Put("{serviceId}")
|
2024-07-03 17:28:00 +07:00
|
|
|
@Security("keycloak", MANAGE_ROLES)
|
2024-06-10 17:47:40 +07:00
|
|
|
async editService(
|
|
|
|
|
@Request() req: RequestWithUser,
|
|
|
|
|
@Body() body: ServiceUpdate,
|
|
|
|
|
@Path() serviceId: string,
|
|
|
|
|
) {
|
|
|
|
|
if (!(await prisma.service.findUnique({ where: { id: serviceId } }))) {
|
2024-06-14 05:43:32 +00:00
|
|
|
throw new HttpError(HttpStatus.NOT_FOUND, "Service cannot be found.", "serviceNotFound");
|
2024-06-10 17:47:40 +07:00
|
|
|
}
|
2024-07-03 17:28:00 +07:00
|
|
|
const { work, productTypeId, ...payload } = body;
|
|
|
|
|
|
|
|
|
|
const [service, productType, branch] = await prisma.$transaction([
|
|
|
|
|
prisma.service.findUnique({
|
|
|
|
|
include: {
|
|
|
|
|
registeredBranch: {
|
|
|
|
|
where: {
|
|
|
|
|
user: { some: { userId: req.user.sub } },
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
where: { id: serviceId },
|
|
|
|
|
}),
|
|
|
|
|
prisma.productType.findFirst({
|
|
|
|
|
include: {
|
|
|
|
|
createdBy: true,
|
|
|
|
|
updatedBy: true,
|
|
|
|
|
},
|
|
|
|
|
where: { id: body.productTypeId },
|
|
|
|
|
}),
|
|
|
|
|
prisma.branch.findFirst({ where: { id: body.registeredBranchId } }),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
if (!service) {
|
|
|
|
|
throw new HttpError(HttpStatus.NOT_FOUND, "Service cannot be found.", "serviceNotFound");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!globalAllow(req.user.roles) && !service.registeredBranch) {
|
|
|
|
|
throw new HttpError(
|
|
|
|
|
HttpStatus.FORBIDDEN,
|
|
|
|
|
"You do not have permission to perform this action.",
|
|
|
|
|
"noPermission",
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!productType) {
|
|
|
|
|
throw new HttpError(
|
|
|
|
|
HttpStatus.BAD_REQUEST,
|
|
|
|
|
"Product Type cannot be found.",
|
|
|
|
|
"relationProductTypeNotFound",
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (body.registeredBranchId && !branch) {
|
|
|
|
|
throw new HttpError(
|
|
|
|
|
HttpStatus.BAD_REQUEST,
|
|
|
|
|
"Branch cannot be found.",
|
|
|
|
|
"relationBranchNotFound",
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-21 14:14:10 +07:00
|
|
|
const record = await prisma.$transaction(async (tx) => {
|
|
|
|
|
const workList = await Promise.all(
|
|
|
|
|
(work || []).map(async (w, wIdx) =>
|
|
|
|
|
tx.work.create({
|
|
|
|
|
data: {
|
|
|
|
|
name: w.name,
|
|
|
|
|
order: wIdx + 1,
|
|
|
|
|
attributes: w.attributes,
|
|
|
|
|
productOnWork: {
|
|
|
|
|
createMany: {
|
|
|
|
|
data: w.productId.map((p, pIdx) => ({
|
|
|
|
|
productId: p,
|
|
|
|
|
order: pIdx + 1,
|
|
|
|
|
})),
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}),
|
|
|
|
|
),
|
|
|
|
|
);
|
2024-06-21 14:23:03 +07:00
|
|
|
|
|
|
|
|
return await tx.service.update({
|
2024-07-01 14:38:07 +07:00
|
|
|
include: {
|
|
|
|
|
createdBy: true,
|
|
|
|
|
updatedBy: true,
|
|
|
|
|
},
|
2024-06-21 14:14:10 +07:00
|
|
|
data: {
|
|
|
|
|
...payload,
|
2024-06-24 14:23:10 +07:00
|
|
|
statusOrder: +(payload.status === "INACTIVE"),
|
2024-06-21 14:14:10 +07:00
|
|
|
work: {
|
|
|
|
|
deleteMany: {},
|
|
|
|
|
connect: workList.map((v) => ({ id: v.id })),
|
2024-06-14 15:49:05 +07:00
|
|
|
},
|
2024-07-01 13:24:02 +07:00
|
|
|
updatedByUserId: req.user.sub,
|
2024-06-14 15:49:05 +07:00
|
|
|
},
|
2024-06-21 14:14:10 +07:00
|
|
|
where: { id: serviceId },
|
|
|
|
|
});
|
2024-06-10 17:47:40 +07:00
|
|
|
});
|
2024-06-21 14:14:10 +07:00
|
|
|
|
2024-06-10 17:47:40 +07:00
|
|
|
return Object.assign(record, {
|
2024-06-12 16:37:56 +07:00
|
|
|
imageUrl: await presignedGetObjectIfExist(
|
2024-06-10 17:47:40 +07:00
|
|
|
MINIO_BUCKET,
|
|
|
|
|
imageLocation(record.id),
|
|
|
|
|
12 * 60 * 60,
|
|
|
|
|
),
|
2024-06-12 16:37:56 +07:00
|
|
|
imageUploadUrl: await minio.presignedPutObject(
|
2024-06-10 17:47:40 +07:00
|
|
|
MINIO_BUCKET,
|
|
|
|
|
imageLocation(record.id),
|
|
|
|
|
12 * 60 * 60,
|
|
|
|
|
),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Delete("{serviceId}")
|
2024-07-03 17:28:00 +07:00
|
|
|
@Security("keycloak", MANAGE_ROLES)
|
|
|
|
|
async deleteService(@Request() req: RequestWithUser, @Path() serviceId: string) {
|
|
|
|
|
const record = await prisma.service.findFirst({
|
|
|
|
|
include: {
|
|
|
|
|
registeredBranch: {
|
|
|
|
|
where: {
|
|
|
|
|
user: { some: { userId: req.user.sub } },
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
where: { id: serviceId },
|
|
|
|
|
});
|
2024-06-10 17:47:40 +07:00
|
|
|
|
|
|
|
|
if (!record) {
|
2024-06-14 05:43:32 +00:00
|
|
|
throw new HttpError(HttpStatus.NOT_FOUND, "Service cannot be found.", "serviceNotFound");
|
2024-06-10 17:47:40 +07:00
|
|
|
}
|
|
|
|
|
|
2024-07-03 17:28:00 +07:00
|
|
|
if (!globalAllow(req.user.roles) && !record.registeredBranch) {
|
|
|
|
|
throw new HttpError(
|
|
|
|
|
HttpStatus.FORBIDDEN,
|
|
|
|
|
"You do not have permission to perform this action.",
|
|
|
|
|
"noPermission",
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-10 17:47:40 +07:00
|
|
|
if (record.status !== Status.CREATED) {
|
2024-06-14 05:43:32 +00:00
|
|
|
throw new HttpError(HttpStatus.FORBIDDEN, "Service is in used.", "serviceInUsed");
|
2024-06-10 17:47:40 +07:00
|
|
|
}
|
|
|
|
|
|
2024-07-01 14:38:07 +07:00
|
|
|
return await prisma.service.delete({
|
|
|
|
|
include: {
|
|
|
|
|
createdBy: true,
|
|
|
|
|
updatedBy: true,
|
|
|
|
|
},
|
|
|
|
|
where: { id: serviceId },
|
|
|
|
|
});
|
2024-06-10 17:47:40 +07:00
|
|
|
}
|
|
|
|
|
}
|