diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 573b283..2e17439 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -260,13 +260,6 @@ model BranchContact { branch Branch @relation(fields: [branchId], references: [id], onDelete: Cascade) branchId String - - createdAt DateTime @default(now()) - createdBy User? @relation(name: "BranchContactCreatedByUser", fields: [createdByUserId], references: [id], onDelete: SetNull) - createdByUserId String? - updatedAt DateTime @updatedAt - updatedBy User? @relation(name: "BranchContactUpdatedByUser", fields: [updatedByUserId], references: [id], onDelete: SetNull) - updatedByUserId String? } model BranchUser { @@ -367,8 +360,6 @@ model User { userUpdated User[] @relation("UserUpdatedByUser") branchCreated Branch[] @relation("BranchCreatedByUser") branchUpdated Branch[] @relation("BranchUpdatedByUser") - branchContactCreated BranchContact[] @relation("BranchContactCreatedByUser") - branchContactUpdated BranchContact[] @relation("BranchContactUpdatedByUser") branchUserCreated BranchUser[] @relation("BranchUserCreatedByUser") branchUserUpdated BranchUser[] @relation("BranchUserUpdatedByUser") customerCreated Customer[] @relation("CustomerCreatedByUser") diff --git a/src/controllers/branch-contact-controller.ts b/src/controllers/branch-contact-controller.ts deleted file mode 100644 index b2cab4e..0000000 --- a/src/controllers/branch-contact-controller.ts +++ /dev/null @@ -1,134 +0,0 @@ -import { - Body, - Controller, - Delete, - Get, - Put, - Path, - Post, - Query, - Request, - Route, - Security, - Tags, -} from "tsoa"; - -import prisma from "../db"; -import HttpError from "../interfaces/http-error"; -import HttpStatus from "../interfaces/http-status"; -import { RequestWithUser } from "../interfaces/user"; - -type BranchContactCreate = { - telephoneNo: string; -}; - -type BranchContactUpdate = { - telephoneNo?: string; -}; - -@Route("api/v1/branch/{branchId}/contact") -@Tags("Branch Contact") -@Security("keycloak") -export class BranchContactController extends Controller { - @Get() - async getBranchContact( - @Path() branchId: string, - @Query() page: number = 1, - @Query() pageSize: number = 30, - ) { - const [result, total] = await prisma.$transaction([ - prisma.branchContact.findMany({ - include: { createdBy: true, updatedBy: true }, - orderBy: { createdAt: "asc" }, - 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 } }); - - if (!record) { - throw new HttpError( - HttpStatus.NOT_FOUND, - "Branch contact cannot be found.", - "branchContactNotFound", - ); - } - - return record; - } - - @Post() - async createBranchContact( - @Request() req: RequestWithUser, - @Path() branchId: string, - @Body() body: BranchContactCreate, - ) { - if (!(await prisma.branch.findFirst({ where: { id: branchId } }))) { - throw new HttpError(HttpStatus.BAD_REQUEST, "Branch cannot be found.", "branchBadReq"); - } - const record = await prisma.branchContact.create({ - include: { - createdBy: true, - updatedBy: true, - }, - data: { ...body, branchId, createdByUserId: req.user.sub, updatedByUserId: req.user.sub }, - }); - - this.setStatus(HttpStatus.CREATED); - - return record; - } - - @Put("{contactId}") - 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.", - "branchContactNotFound", - ); - } - - const record = await prisma.branchContact.update({ - include: { createdBy: true, updatedBy: true }, - data: { ...body, updatedByUserId: req.user.sub }, - where: { id: contactId, branchId }, - }); - - return record; - } - - @Delete("{contactId}") - async deleteBranchContact(@Path() branchId: string, @Path() contactId: string) { - const result = await prisma.branchContact.deleteMany({ where: { id: contactId, branchId } }); - if (result.count <= 0) { - throw new HttpError( - HttpStatus.NOT_FOUND, - "Branch contact cannot be found.", - "branchContactNotFound", - ); - } - } -}