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

129 lines
3 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";
2024-04-05 11:30:18 +07:00
import prisma from "../db";
import HttpError from "../interfaces/http-error";
import HttpStatus from "../interfaces/http-status";
import { RequestWithUser } from "../interfaces/user";
2024-04-02 13:54:49 +07:00
2024-04-02 14:08:07 +07:00
type BranchContactCreate = {
telephoneNo: string;
};
type BranchContactUpdate = {
telephoneNo?: string;
};
2024-06-06 09:42:02 +07:00
@Route("api/v1/branch/{branchId}/contact")
2024-04-02 13:54:49 +07:00
@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({
2024-04-09 08:51:22 +07:00
orderBy: { createdAt: "asc" },
2024-04-02 15:25:28 +07:00
where: { branchId },
take: pageSize,
skip: (page - 1) * pageSize,
}),
prisma.branchContact.count({ where: { branchId } }),
]);
2024-04-03 12:49:42 +07:00
return {
2024-04-17 11:40:23 +07:00
result,
2024-04-03 12:49:42 +07:00
page,
pageSize,
total,
};
2024-04-02 15:25:28 +07:00
}
@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.",
2024-06-14 05:44:03 +00:00
"branchContactNotFound",
2024-04-02 15:32:17 +07:00
);
}
2024-04-02 15:25:28 +07:00
2024-04-17 11:40:23 +07:00
return record;
2024-04-02 15:25:28 +07:00
}
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-06-24 13:14:44 +07:00
throw new HttpError(HttpStatus.BAD_REQUEST, "Branch cannot be found.", "branchBadReq");
2024-04-02 13:55:54 +07:00
}
const record = await prisma.branchContact.create({
2024-07-01 13:24:02 +07:00
data: { ...body, branchId, createdByUserId: req.user.sub, updatedByUserId: req.user.sub },
2024-04-02 13:55:54 +07:00
});
this.setStatus(HttpStatus.CREATED);
2024-04-17 11:40:23 +07:00
return record;
2024-04-02 13:55:54 +07:00
}
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,
) {
if (
!(await prisma.branchContact.findUnique({
where: { id: contactId, branchId },
}))
) {
throw new HttpError(
HttpStatus.NOT_FOUND,
"Branch contact cannot be found.",
2024-06-14 05:44:03 +00:00
"branchContactNotFound",
);
}
2024-04-02 15:21:19 +07:00
const record = await prisma.branchContact.update({
2024-07-01 13:24:02 +07:00
data: { ...body, updatedByUserId: req.user.sub },
2024-04-02 15:21:19 +07:00
where: { id: contactId, branchId },
});
2024-04-17 11:40:23 +07:00
return record;
2024-04-02 15:21:19 +07:00
}
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 contact cannot be found.",
2024-06-14 05:44:03 +00:00
"branchContactNotFound",
);
2024-04-02 15:32:17 +07:00
}
2024-04-02 13:56:40 +07:00
}
2024-04-02 13:54:49 +07:00
}