jws-backend/src/controllers/branch/contact-controller.ts

152 lines
3.8 KiB
TypeScript
Raw Normal View History

2024-04-02 13:54:49 +07:00
import {
Body,
Controller,
Delete,
Get,
2024-04-03 10:54:46 +07:00
Put,
2024-04-02 13:54:49 +07:00
Path,
Post,
Query,
Request,
Route,
Security,
Tags,
} from "tsoa";
import prisma from "../../db";
import minio from "../../services/minio";
import HttpError from "../../interfaces/http-error";
import HttpStatus from "../../interfaces/http-status";
import { RequestWithUser } from "../../interfaces/user";
if (!process.env.MINIO_BUCKET) {
throw Error("Require MinIO bucket.");
}
const MINIO_BUCKET = process.env.MINIO_BUCKET;
2024-04-02 14:08:07 +07:00
type BranchContactCreate = {
lineId: string;
telephoneNo: string;
};
type BranchContactUpdate = {
lineId?: string;
telephoneNo?: string;
};
function imageLocation(id: string) {
return `branch/contact-${id}`;
}
2024-04-02 13:54:49 +07:00
@Route("api/branch/{branchId}/contact")
@Tags("Branch Contact")
@Security("keycloak")
export class BranchContactController extends Controller {
2024-04-02 15:25:28 +07:00
@Get()
async getBranchContact(
@Path() branchId: string,
@Query() page: number = 1,
@Query() pageSize: number = 30,
) {
const [result, total] = await prisma.$transaction([
prisma.branchContact.findMany({
where: { branchId },
take: pageSize,
skip: (page - 1) * pageSize,
}),
prisma.branchContact.count({ where: { branchId } }),
]);
return { result, page, pageSize, total };
}
@Get("{contactId}")
async getBranchContactById(@Path() branchId: string, @Path() contactId: string) {
const record = await prisma.branchContact.findFirst({ where: { id: contactId, branchId } });
2024-04-02 15:32:17 +07:00
if (!record) {
throw new HttpError(
HttpStatus.NOT_FOUND,
"Branch contact cannot be found.",
"data_not_found",
);
}
2024-04-02 15:25:28 +07:00
return Object.assign(record, {
qrCodeImageUrl: await minio.presignedGetObject(MINIO_BUCKET, imageLocation(record.id)),
});
}
2024-04-02 13:55:54 +07:00
@Post()
async createBranchContact(
@Request() req: RequestWithUser,
@Path() branchId: string,
@Body() body: BranchContactCreate,
) {
if (!(await prisma.branch.findFirst({ where: { id: branchId } }))) {
2024-04-02 15:32:17 +07:00
throw new HttpError(
HttpStatus.BAD_REQUEST,
"Branch not found.",
"missing_or_invalid_parameter",
);
2024-04-02 13:55:54 +07:00
}
const record = await prisma.branchContact.create({
include: { branch: true },
data: { ...body, branchId, createdBy: req.user.name, updateBy: req.user.name },
});
this.setStatus(HttpStatus.CREATED);
2024-04-02 13:55:54 +07:00
return Object.assign(record, {
qrCodeImageUrl: await minio.presignedGetObject(
MINIO_BUCKET,
imageLocation(record.id),
2024-04-02 13:55:54 +07:00
12 * 60 * 60,
),
qrCodeImageUploadUrl: await minio.presignedPutObject(
MINIO_BUCKET,
imageLocation(record.id),
2024-04-02 13:55:54 +07:00
12 * 60 * 60,
),
});
}
2024-04-02 13:56:40 +07:00
2024-04-03 10:54:46 +07:00
@Put("{contactId}")
2024-04-02 15:21:19 +07:00
async editBranchContact(
@Request() req: RequestWithUser,
@Body() body: BranchContactUpdate,
@Path() branchId: string,
@Path() contactId: string,
) {
const record = await prisma.branchContact.update({
include: { branch: true },
data: { ...body, updateBy: req.user.name },
where: { id: contactId, branchId },
});
if (!record) {
2024-04-02 15:32:17 +07:00
throw new HttpError(HttpStatus.NOT_FOUND, "Branch cannot be found.", "data_not_found");
2024-04-02 15:21:19 +07:00
}
return Object.assign(record, {
qrCodeImageUrl: await minio.presignedGetObject(
MINIO_BUCKET,
imageLocation(record.id),
12 * 60 * 60,
),
qrCodeImageUploadUrl: await minio.presignedPutObject(
MINIO_BUCKET,
imageLocation(record.id),
12 * 60 * 60,
),
});
}
2024-04-02 13:56:40 +07:00
@Delete("{contactId}")
async deleteBranchContact(@Path() branchId: string, @Path() contactId: string) {
const result = await prisma.branchContact.deleteMany({ where: { id: contactId, branchId } });
2024-04-02 15:32:17 +07:00
if (result.count <= 0) {
throw new HttpError(HttpStatus.NOT_FOUND, "Branch cannot be found.", "data_not_found");
}
2024-04-02 13:56:40 +07:00
}
2024-04-02 13:54:49 +07:00
}