2024-04-09 13:58:04 +07:00
|
|
|
import { CustomerType, Prisma, Status } from "@prisma/client";
|
2024-04-05 10:57:43 +07:00
|
|
|
import {
|
|
|
|
|
Body,
|
|
|
|
|
Controller,
|
|
|
|
|
Delete,
|
|
|
|
|
Get,
|
|
|
|
|
Path,
|
|
|
|
|
Post,
|
|
|
|
|
Put,
|
|
|
|
|
Query,
|
|
|
|
|
Request,
|
|
|
|
|
Route,
|
|
|
|
|
Security,
|
|
|
|
|
Tags,
|
|
|
|
|
} from "tsoa";
|
2024-04-05 10:53:52 +07:00
|
|
|
import { RequestWithUser } from "../interfaces/user";
|
|
|
|
|
import prisma from "../db";
|
2024-09-10 09:56:46 +07:00
|
|
|
import minio, { deleteFolder } from "../services/minio";
|
2024-04-05 10:53:52 +07:00
|
|
|
import HttpStatus from "../interfaces/http-status";
|
|
|
|
|
import HttpError from "../interfaces/http-error";
|
2024-09-05 09:01:34 +07:00
|
|
|
import { isSystem } from "../utils/keycloak";
|
2024-09-10 13:23:03 +07:00
|
|
|
import {
|
|
|
|
|
branchRelationPermInclude,
|
|
|
|
|
createPermCheck,
|
|
|
|
|
createPermCondition,
|
|
|
|
|
} from "../services/permission";
|
2024-09-09 14:51:17 +07:00
|
|
|
import { filterStatus } from "../services/prisma";
|
2024-09-10 09:56:46 +07:00
|
|
|
import { fileLocation, listFile } from "../utils/minio";
|
2024-04-05 10:53:52 +07:00
|
|
|
|
|
|
|
|
if (!process.env.MINIO_BUCKET) {
|
|
|
|
|
throw Error("Require MinIO bucket.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const MINIO_BUCKET = process.env.MINIO_BUCKET;
|
2024-09-06 11:58:28 +07:00
|
|
|
const MANAGE_ROLES = [
|
|
|
|
|
"system",
|
|
|
|
|
"head_of_admin",
|
|
|
|
|
"admin",
|
|
|
|
|
"head_of_account",
|
|
|
|
|
"account",
|
|
|
|
|
"head_of_sale",
|
2024-09-09 11:13:53 +07:00
|
|
|
"sale",
|
2024-09-06 11:58:28 +07:00
|
|
|
];
|
2024-09-04 15:21:11 +07:00
|
|
|
|
|
|
|
|
function globalAllow(user: RequestWithUser["user"]) {
|
2024-09-06 11:58:28 +07:00
|
|
|
const allowList = ["system", "head_of_admin", "admin", "head_of_account", "head_of_sale"];
|
2024-09-04 15:21:11 +07:00
|
|
|
return allowList.some((v) => user.roles?.includes(v));
|
|
|
|
|
}
|
2024-04-05 10:53:52 +07:00
|
|
|
|
2024-09-10 13:23:03 +07:00
|
|
|
const permissionCond = createPermCondition(globalAllow);
|
2024-09-09 14:40:18 +07:00
|
|
|
const permissionCheck = createPermCheck(globalAllow);
|
|
|
|
|
|
2024-04-05 10:53:52 +07:00
|
|
|
export type CustomerCreate = {
|
2024-07-03 14:36:11 +07:00
|
|
|
registeredBranchId?: string;
|
|
|
|
|
|
2024-04-05 10:53:52 +07:00
|
|
|
status?: Status;
|
2024-08-20 15:41:48 +07:00
|
|
|
|
2024-04-09 13:58:04 +07:00
|
|
|
customerType: CustomerType;
|
2024-08-20 15:41:48 +07:00
|
|
|
namePrefix: string;
|
|
|
|
|
firstName: string;
|
|
|
|
|
firstNameEN?: string;
|
|
|
|
|
lastName: string;
|
|
|
|
|
lastNameEN?: string;
|
|
|
|
|
gender: string;
|
|
|
|
|
birthDate: Date;
|
2024-04-05 10:53:52 +07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export type CustomerUpdate = {
|
2024-07-03 14:36:11 +07:00
|
|
|
registeredBranchId?: string;
|
|
|
|
|
|
2024-04-05 10:53:52 +07:00
|
|
|
status?: "ACTIVE" | "INACTIVE";
|
2024-08-20 15:41:48 +07:00
|
|
|
|
2024-08-27 13:28:51 +07:00
|
|
|
customerType?: CustomerType;
|
|
|
|
|
namePrefix?: string;
|
|
|
|
|
firstName?: string;
|
2024-08-20 15:41:48 +07:00
|
|
|
firstNameEN?: string;
|
2024-08-27 13:28:51 +07:00
|
|
|
lastName?: string;
|
2024-08-20 15:41:48 +07:00
|
|
|
lastNameEN?: string;
|
2024-08-27 13:28:51 +07:00
|
|
|
gender?: string;
|
|
|
|
|
birthDate?: Date;
|
2024-04-05 10:53:52 +07:00
|
|
|
};
|
|
|
|
|
|
2024-06-06 09:42:02 +07:00
|
|
|
@Route("api/v1/customer")
|
2024-04-05 10:53:52 +07:00
|
|
|
@Tags("Customer")
|
|
|
|
|
export class CustomerController extends Controller {
|
2024-06-07 09:09:49 +07:00
|
|
|
@Get("type-stats")
|
2024-07-02 17:29:51 +07:00
|
|
|
@Security("keycloak")
|
2024-09-09 09:10:41 +07:00
|
|
|
async stat(@Request() req: RequestWithUser) {
|
2024-06-07 09:09:49 +07:00
|
|
|
const list = await prisma.customer.groupBy({
|
|
|
|
|
by: "customerType",
|
|
|
|
|
_count: true,
|
2024-09-09 09:10:41 +07:00
|
|
|
where: {
|
2024-09-10 13:23:03 +07:00
|
|
|
registeredBranch: isSystem(req.user) ? undefined : { OR: permissionCond(req.user) },
|
2024-09-09 09:10:41 +07:00
|
|
|
},
|
2024-06-07 09:09:49 +07:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return list.reduce<Record<CustomerType, number>>(
|
|
|
|
|
(a, c) => {
|
|
|
|
|
a[c.customerType] = c._count;
|
|
|
|
|
return a;
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
CORP: 0,
|
|
|
|
|
PERS: 0,
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-05 11:00:31 +07:00
|
|
|
@Get()
|
2024-07-02 17:29:51 +07:00
|
|
|
@Security("keycloak")
|
2024-04-05 11:00:31 +07:00
|
|
|
async list(
|
2024-09-05 16:14:03 +07:00
|
|
|
@Request() req: RequestWithUser,
|
2024-06-10 14:11:45 +07:00
|
|
|
@Query() customerType?: CustomerType,
|
2024-04-05 11:00:31 +07:00
|
|
|
@Query() query: string = "",
|
2024-06-13 17:21:22 +07:00
|
|
|
@Query() status?: Status,
|
2024-04-05 11:00:31 +07:00
|
|
|
@Query() page: number = 1,
|
|
|
|
|
@Query() pageSize: number = 30,
|
2024-04-22 17:16:30 +07:00
|
|
|
@Query() includeBranch: boolean = false,
|
2024-04-05 11:00:31 +07:00
|
|
|
) {
|
|
|
|
|
const where = {
|
2024-06-10 14:11:45 +07:00
|
|
|
OR: [
|
2024-08-20 15:41:48 +07:00
|
|
|
{ namePrefix: { contains: query } },
|
|
|
|
|
{ firstName: { contains: query } },
|
|
|
|
|
{ firstNameEN: { contains: query } },
|
2024-06-10 14:11:45 +07:00
|
|
|
],
|
2024-09-05 16:14:03 +07:00
|
|
|
AND: {
|
|
|
|
|
customerType,
|
|
|
|
|
...filterStatus(status),
|
2024-09-10 13:23:03 +07:00
|
|
|
registeredBranch: isSystem(req.user) ? undefined : { OR: permissionCond(req.user) },
|
2024-09-05 16:14:03 +07:00
|
|
|
},
|
2024-04-05 11:00:31 +07:00
|
|
|
} satisfies Prisma.CustomerWhereInput;
|
|
|
|
|
|
|
|
|
|
const [result, total] = await prisma.$transaction([
|
|
|
|
|
prisma.customer.findMany({
|
2024-04-22 17:16:30 +07:00
|
|
|
include: {
|
2024-06-12 14:08:18 +07:00
|
|
|
_count: true,
|
2024-04-22 17:16:30 +07:00
|
|
|
branch: includeBranch
|
|
|
|
|
? {
|
|
|
|
|
include: {
|
|
|
|
|
province: true,
|
|
|
|
|
district: true,
|
|
|
|
|
subDistrict: true,
|
|
|
|
|
},
|
2024-08-20 15:41:48 +07:00
|
|
|
orderBy: { createdAt: "asc" },
|
2024-04-22 17:16:30 +07:00
|
|
|
}
|
|
|
|
|
: undefined,
|
2024-07-01 14:38:07 +07:00
|
|
|
createdBy: true,
|
|
|
|
|
updatedBy: true,
|
2024-04-22 17:16:30 +07:00
|
|
|
},
|
2024-06-24 13:20:59 +07:00
|
|
|
orderBy: [{ statusOrder: "asc" }, { createdAt: "asc" }],
|
2024-04-05 11:00:31 +07:00
|
|
|
where,
|
|
|
|
|
take: pageSize,
|
|
|
|
|
skip: (page - 1) * pageSize,
|
|
|
|
|
}),
|
|
|
|
|
prisma.customer.count({ where }),
|
|
|
|
|
]);
|
|
|
|
|
|
2024-08-20 15:41:48 +07:00
|
|
|
return { result, page, pageSize, total };
|
2024-04-05 11:00:31 +07:00
|
|
|
}
|
|
|
|
|
|
2024-04-05 11:00:44 +07:00
|
|
|
@Get("{customerId}")
|
2024-07-02 17:29:51 +07:00
|
|
|
@Security("keycloak")
|
2024-04-05 11:00:44 +07:00
|
|
|
async getById(@Path() customerId: string) {
|
2024-08-30 09:43:34 +07:00
|
|
|
const [record, countEmployee] = await prisma.$transaction([
|
|
|
|
|
prisma.customer.findFirst({
|
|
|
|
|
include: {
|
|
|
|
|
branch: {
|
|
|
|
|
include: {
|
|
|
|
|
province: true,
|
|
|
|
|
district: true,
|
|
|
|
|
subDistrict: true,
|
|
|
|
|
},
|
|
|
|
|
orderBy: { createdAt: "asc" },
|
2024-04-22 17:16:30 +07:00
|
|
|
},
|
2024-08-30 09:43:34 +07:00
|
|
|
createdBy: true,
|
|
|
|
|
updatedBy: true,
|
2024-04-22 17:16:30 +07:00
|
|
|
},
|
2024-08-30 09:43:34 +07:00
|
|
|
where: { id: customerId },
|
|
|
|
|
}),
|
|
|
|
|
prisma.employee.count({ where: { customerBranch: { customerId } } }),
|
|
|
|
|
]);
|
2024-08-20 15:41:48 +07:00
|
|
|
|
|
|
|
|
if (!record) {
|
2024-06-14 05:58:14 +00:00
|
|
|
throw new HttpError(HttpStatus.NOT_FOUND, "Customer cannot be found.", "customerNotFound");
|
2024-08-20 15:41:48 +07:00
|
|
|
}
|
2024-08-30 09:43:34 +07:00
|
|
|
return Object.assign(record, { _count: { employee: countEmployee } });
|
2024-04-05 11:00:44 +07:00
|
|
|
}
|
|
|
|
|
|
2024-04-05 10:53:52 +07:00
|
|
|
@Post()
|
2024-07-03 14:36:11 +07:00
|
|
|
@Security("keycloak", MANAGE_ROLES)
|
2024-04-05 10:53:52 +07:00
|
|
|
async create(@Request() req: RequestWithUser, @Body() body: CustomerCreate) {
|
2024-09-04 15:21:11 +07:00
|
|
|
// NOTE: handle empty string
|
|
|
|
|
if (!body.registeredBranchId) {
|
|
|
|
|
body.registeredBranchId = undefined;
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-20 15:41:48 +07:00
|
|
|
const [branch] = await prisma.$transaction([
|
2024-09-04 15:21:11 +07:00
|
|
|
prisma.branch.findFirst({
|
|
|
|
|
where: { id: body.registeredBranchId },
|
2024-09-09 14:40:18 +07:00
|
|
|
include: branchRelationPermInclude(req.user),
|
2024-09-04 15:21:11 +07:00
|
|
|
}),
|
2024-04-22 16:17:50 +07:00
|
|
|
]);
|
|
|
|
|
|
2024-07-23 11:23:12 +07:00
|
|
|
if (!!body.registeredBranchId && !branch) {
|
2024-07-03 14:36:11 +07:00
|
|
|
throw new HttpError(
|
|
|
|
|
HttpStatus.BAD_REQUEST,
|
|
|
|
|
"Branch cannot be found.",
|
|
|
|
|
"relationBranchNotFound",
|
|
|
|
|
);
|
|
|
|
|
}
|
2024-08-20 15:41:48 +07:00
|
|
|
|
2024-09-09 14:40:18 +07:00
|
|
|
if (body.registeredBranchId !== undefined && branch) {
|
|
|
|
|
await permissionCheck(req.user, branch);
|
2024-07-23 11:23:12 +07:00
|
|
|
}
|
2024-04-22 16:17:50 +07:00
|
|
|
|
2024-04-23 11:56:40 +07:00
|
|
|
const record = await prisma.$transaction(
|
|
|
|
|
async (tx) => {
|
2024-08-20 15:41:48 +07:00
|
|
|
await tx.branch.updateMany({
|
|
|
|
|
where: {
|
|
|
|
|
id: body.registeredBranchId,
|
|
|
|
|
status: "CREATED",
|
|
|
|
|
},
|
|
|
|
|
data: {
|
|
|
|
|
status: "INACTIVE",
|
|
|
|
|
statusOrder: 1,
|
|
|
|
|
},
|
2024-08-09 09:44:51 +07:00
|
|
|
});
|
2024-08-20 15:41:48 +07:00
|
|
|
return await tx.customer.create({
|
2024-04-22 16:17:50 +07:00
|
|
|
include: {
|
2024-04-23 11:56:40 +07:00
|
|
|
branch: {
|
|
|
|
|
include: {
|
|
|
|
|
province: true,
|
|
|
|
|
district: true,
|
|
|
|
|
subDistrict: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
2024-07-01 14:38:07 +07:00
|
|
|
createdBy: true,
|
|
|
|
|
updatedBy: true,
|
2024-04-22 16:17:50 +07:00
|
|
|
},
|
2024-04-23 11:56:40 +07:00
|
|
|
data: {
|
2024-08-20 15:41:48 +07:00
|
|
|
...body,
|
|
|
|
|
statusOrder: +(body.status === "INACTIVE"),
|
2024-07-01 13:24:02 +07:00
|
|
|
createdByUserId: req.user.sub,
|
|
|
|
|
updatedByUserId: req.user.sub,
|
2024-04-22 16:17:50 +07:00
|
|
|
},
|
2024-04-23 11:56:40 +07:00
|
|
|
});
|
2024-04-05 10:53:52 +07:00
|
|
|
},
|
2024-04-23 11:56:40 +07:00
|
|
|
{ isolationLevel: Prisma.TransactionIsolationLevel.Serializable },
|
|
|
|
|
);
|
2024-04-05 10:53:52 +07:00
|
|
|
|
|
|
|
|
this.setStatus(HttpStatus.CREATED);
|
|
|
|
|
|
2024-08-20 15:41:48 +07:00
|
|
|
return record;
|
2024-04-05 10:53:52 +07:00
|
|
|
}
|
2024-04-05 11:02:23 +07:00
|
|
|
|
2024-04-05 11:18:01 +07:00
|
|
|
@Put("{customerId}")
|
2024-07-03 14:36:11 +07:00
|
|
|
@Security("keycloak", MANAGE_ROLES)
|
2024-04-05 11:18:01 +07:00
|
|
|
async editById(
|
|
|
|
|
@Path() customerId: string,
|
|
|
|
|
@Request() req: RequestWithUser,
|
|
|
|
|
@Body() body: CustomerUpdate,
|
|
|
|
|
) {
|
2024-09-04 15:21:11 +07:00
|
|
|
if (body.registeredBranchId === "") {
|
|
|
|
|
body.registeredBranchId = undefined;
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-09 14:40:18 +07:00
|
|
|
const customer = await prisma.customer.findUnique({
|
|
|
|
|
where: { id: customerId },
|
|
|
|
|
include: {
|
|
|
|
|
registeredBranch: {
|
|
|
|
|
include: branchRelationPermInclude(req.user),
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
});
|
2024-06-11 15:11:08 +07:00
|
|
|
|
|
|
|
|
if (!customer) {
|
2024-06-14 05:58:14 +00:00
|
|
|
throw new HttpError(HttpStatus.NOT_FOUND, "Customer cannot be found.", "customerNotFound");
|
2024-04-18 13:09:53 +07:00
|
|
|
}
|
|
|
|
|
|
2024-08-20 15:41:48 +07:00
|
|
|
const [branch] = await prisma.$transaction([
|
2024-09-04 15:21:11 +07:00
|
|
|
prisma.branch.findFirst({
|
|
|
|
|
where: { id: body.registeredBranchId },
|
2024-09-09 14:40:18 +07:00
|
|
|
include: branchRelationPermInclude(req.user),
|
2024-09-04 15:21:11 +07:00
|
|
|
}),
|
2024-04-22 16:17:50 +07:00
|
|
|
]);
|
|
|
|
|
|
2024-08-20 15:41:48 +07:00
|
|
|
if (!!body.registeredBranchId && !branch) {
|
2024-06-07 14:34:34 +07:00
|
|
|
throw new HttpError(
|
|
|
|
|
HttpStatus.BAD_REQUEST,
|
2024-08-20 15:41:48 +07:00
|
|
|
"Branch cannot be found.",
|
|
|
|
|
"relationBranchNotFound",
|
2024-06-07 14:34:34 +07:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-10 09:53:05 +07:00
|
|
|
if (customer.registeredBranch) {
|
|
|
|
|
await permissionCheck(req.user, customer.registeredBranch);
|
|
|
|
|
}
|
2024-09-09 14:40:18 +07:00
|
|
|
|
|
|
|
|
if (body.registeredBranchId !== undefined && branch) {
|
|
|
|
|
await permissionCheck(req.user, branch);
|
2024-07-03 17:05:31 +07:00
|
|
|
}
|
|
|
|
|
|
2024-08-20 15:41:48 +07:00
|
|
|
const record = await prisma.$transaction(async (tx) => {
|
|
|
|
|
return await tx.customer.update({
|
2024-06-07 11:14:34 +07:00
|
|
|
include: {
|
|
|
|
|
branch: {
|
|
|
|
|
include: {
|
|
|
|
|
province: true,
|
|
|
|
|
district: true,
|
|
|
|
|
subDistrict: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
2024-07-01 14:38:07 +07:00
|
|
|
createdBy: true,
|
|
|
|
|
updatedBy: true,
|
2024-06-07 11:14:34 +07:00
|
|
|
},
|
|
|
|
|
where: { id: customerId },
|
|
|
|
|
data: {
|
2024-08-20 15:41:48 +07:00
|
|
|
...body,
|
|
|
|
|
statusOrder: +(body.status === "INACTIVE"),
|
2024-07-01 13:24:02 +07:00
|
|
|
updatedByUserId: req.user.sub,
|
2024-06-07 11:14:34 +07:00
|
|
|
},
|
|
|
|
|
});
|
2024-04-05 11:18:01 +07:00
|
|
|
});
|
2024-08-20 15:41:48 +07:00
|
|
|
|
|
|
|
|
return record;
|
2024-04-05 11:18:01 +07:00
|
|
|
}
|
|
|
|
|
|
2024-04-05 11:02:23 +07:00
|
|
|
@Delete("{customerId}")
|
2024-07-03 14:36:11 +07:00
|
|
|
@Security("keycloak", MANAGE_ROLES)
|
2024-09-04 15:21:11 +07:00
|
|
|
async deleteById(@Path() customerId: string, @Request() req: RequestWithUser) {
|
|
|
|
|
const record = await prisma.customer.findFirst({
|
|
|
|
|
where: { id: customerId },
|
|
|
|
|
include: {
|
|
|
|
|
registeredBranch: {
|
2024-09-09 14:40:18 +07:00
|
|
|
include: branchRelationPermInclude(req.user),
|
2024-09-04 15:21:11 +07:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
});
|
2024-04-05 11:02:23 +07:00
|
|
|
|
|
|
|
|
if (!record) {
|
2024-06-14 05:58:14 +00:00
|
|
|
throw new HttpError(HttpStatus.NOT_FOUND, "Customer cannot be found.", "customerNotFound");
|
2024-04-05 11:02:23 +07:00
|
|
|
}
|
|
|
|
|
|
2024-09-09 14:40:18 +07:00
|
|
|
await permissionCheck(req.user, record.registeredBranch);
|
2024-09-04 15:21:11 +07:00
|
|
|
|
2024-04-05 11:02:23 +07:00
|
|
|
if (record.status !== Status.CREATED) {
|
2024-06-14 05:58:14 +00:00
|
|
|
throw new HttpError(HttpStatus.FORBIDDEN, "Customer is in used.", "customerInUsed");
|
2024-04-05 11:02:23 +07:00
|
|
|
}
|
|
|
|
|
|
2024-08-20 15:41:48 +07:00
|
|
|
return await prisma.customer
|
|
|
|
|
.delete({ where: { id: customerId } })
|
|
|
|
|
.then(
|
|
|
|
|
async (data) => await deleteFolder(MINIO_BUCKET, `customer/${customerId}`).then(() => data),
|
|
|
|
|
);
|
2024-04-05 11:02:23 +07:00
|
|
|
}
|
2024-09-10 09:56:46 +07:00
|
|
|
}
|
2024-07-31 15:25:05 +07:00
|
|
|
|
2024-09-10 09:56:46 +07:00
|
|
|
@Route("api/v1/customer/{customerId}/image")
|
|
|
|
|
@Tags("Customer")
|
|
|
|
|
export class CustomerImageController extends Controller {
|
|
|
|
|
@Get()
|
|
|
|
|
@Security("keycloak")
|
|
|
|
|
async listImage(@Path() customerId: string) {
|
|
|
|
|
const customer = await prisma.customer.findUnique({
|
|
|
|
|
where: { id: customerId },
|
|
|
|
|
});
|
|
|
|
|
if (!customer) {
|
|
|
|
|
throw new HttpError(HttpStatus.NOT_FOUND, "Customer cannot be found", "customerNotFound");
|
2024-07-31 15:25:05 +07:00
|
|
|
}
|
2024-09-10 09:56:46 +07:00
|
|
|
return await listFile(fileLocation.customer.img(customerId));
|
|
|
|
|
}
|
2024-07-31 15:25:05 +07:00
|
|
|
|
2024-09-10 09:56:46 +07:00
|
|
|
@Get("{name}")
|
|
|
|
|
async getImage(
|
|
|
|
|
@Request() req: RequestWithUser,
|
|
|
|
|
@Path() customerId: string,
|
|
|
|
|
@Path() name: string,
|
|
|
|
|
) {
|
|
|
|
|
return req.res?.redirect(
|
|
|
|
|
await minio.presignedGetObject(
|
|
|
|
|
MINIO_BUCKET,
|
|
|
|
|
fileLocation.customer.img(customerId, name),
|
|
|
|
|
12 * 60 * 60,
|
|
|
|
|
),
|
|
|
|
|
);
|
2024-07-31 15:25:05 +07:00
|
|
|
}
|
|
|
|
|
|
2024-09-10 09:56:46 +07:00
|
|
|
@Put("{name}")
|
|
|
|
|
@Security("keycloak")
|
|
|
|
|
async putImage(
|
|
|
|
|
@Request() req: RequestWithUser,
|
|
|
|
|
@Path() customerId: string,
|
|
|
|
|
@Path() name: string,
|
|
|
|
|
) {
|
|
|
|
|
const customer = await prisma.customer.findUnique({
|
2024-09-04 15:21:11 +07:00
|
|
|
where: { id: customerId },
|
2024-09-09 14:40:18 +07:00
|
|
|
include: {
|
|
|
|
|
registeredBranch: {
|
|
|
|
|
include: branchRelationPermInclude(req.user),
|
|
|
|
|
},
|
|
|
|
|
},
|
2024-07-31 15:25:05 +07:00
|
|
|
});
|
2024-09-10 09:56:46 +07:00
|
|
|
if (!customer) {
|
|
|
|
|
throw new HttpError(HttpStatus.NOT_FOUND, "Image cannot be found", "imageNotFound");
|
2024-07-31 15:25:05 +07:00
|
|
|
}
|
2024-09-10 09:56:46 +07:00
|
|
|
console.log(customer.registeredBranch);
|
|
|
|
|
await permissionCheck(req.user, customer.registeredBranch);
|
2024-07-31 15:25:05 +07:00
|
|
|
return req.res?.redirect(
|
2024-09-10 09:56:46 +07:00
|
|
|
await minio.presignedPutObject(
|
|
|
|
|
MINIO_BUCKET,
|
|
|
|
|
fileLocation.customer.img(customerId, name),
|
|
|
|
|
12 * 60 * 60,
|
|
|
|
|
),
|
2024-07-31 15:25:05 +07:00
|
|
|
);
|
|
|
|
|
}
|
2024-09-10 09:56:46 +07:00
|
|
|
|
|
|
|
|
@Delete("{name}")
|
|
|
|
|
@Security("keycloak")
|
|
|
|
|
async deleteImage(
|
|
|
|
|
@Request() req: RequestWithUser,
|
|
|
|
|
@Path() customerId: string,
|
|
|
|
|
@Path() name: string,
|
|
|
|
|
) {
|
|
|
|
|
const customer = await prisma.customer.findUnique({
|
|
|
|
|
where: { id: customerId },
|
|
|
|
|
include: {
|
|
|
|
|
registeredBranch: {
|
|
|
|
|
include: branchRelationPermInclude(req.user),
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
if (!customer) {
|
|
|
|
|
throw new HttpError(HttpStatus.NOT_FOUND, "Image cannot be found", "imageNotFound");
|
|
|
|
|
}
|
|
|
|
|
await permissionCheck(req.user, customer.registeredBranch);
|
|
|
|
|
await minio.removeObject(MINIO_BUCKET, fileLocation.customer.img(customerId, name), {
|
|
|
|
|
forceDelete: true,
|
|
|
|
|
});
|
|
|
|
|
}
|
2024-04-05 10:53:52 +07:00
|
|
|
}
|