From 727f5c2e02073cf3540eeae1620a31e154534967 Mon Sep 17 00:00:00 2001 From: Himanshu Sharma Date: Thu, 4 Jul 2024 13:06:17 +0530 Subject: [PATCH] created issue service --- prisma/seed.ts | 6 ++-- src/controllers/issuesController.ts | 15 ++++++-- src/repositories/issueRepository.ts | 38 ++++++++++++++++++++ src/services/issueService.ts | 54 +++++++++++++++++++++++++++++ 4 files changed, 106 insertions(+), 7 deletions(-) create mode 100644 src/repositories/issueRepository.ts create mode 100644 src/services/issueService.ts diff --git a/prisma/seed.ts b/prisma/seed.ts index b335013..6c5d265 100644 --- a/prisma/seed.ts +++ b/prisma/seed.ts @@ -39,8 +39,7 @@ async function main() { const customer1 = await prisma.customer.create({ data: { - first_name: 'John', - last_name: 'Doe', + name: 'John Dow', phone: 11111, email: 'john.doe@example.com', organizationId: organization.organization_id, @@ -49,8 +48,7 @@ async function main() { const customer2 = await prisma.customer.create({ data: { - first_name: 'Jane', - last_name: 'Smith', + name: 'Jane Smith', phone: 22222, email: 'jane.smith@example.com', organizationId: organization.organization_id, diff --git a/src/controllers/issuesController.ts b/src/controllers/issuesController.ts index 7fd451c..e1c8042 100644 --- a/src/controllers/issuesController.ts +++ b/src/controllers/issuesController.ts @@ -2,15 +2,24 @@ import { Request, Response, NextFunction } from "express"; import { catchAsyncError } from "../middlewares/catchAsyncError"; import ErrorHandlerClass from "../utils/errorClass"; import prisma from "../config/primsa-client"; +import issueService from "../services/issueService"; export const createIssue = catchAsyncError(async (req: Request, res: Response, next: NextFunction) => { + + // const { title, description, state, priority, customerName, team_memberName } = req.body; + try { - const organization_id = await prisma.$queryRaw`SELECT organization_id FROM organizations` - console.log("organization_id: ", organization_id); + const newIssue = await issueService.createIssue(req.body); + res.status(201).json({ + success: true, + message: "New issue created", + issue: newIssue, + }); } catch (error) { - console.error(error) + // next(new ErrorHandlerClass("Unable to create issue", 400)); + console.error(error); } }); diff --git a/src/repositories/issueRepository.ts b/src/repositories/issueRepository.ts new file mode 100644 index 0000000..fd40466 --- /dev/null +++ b/src/repositories/issueRepository.ts @@ -0,0 +1,38 @@ +import prisma from "../config/primsa-client"; + + +class IssueRepository { + async findOrganization() { + return prisma.organization.findFirst(); + } + + async findCustomerByName(customerName: string) { + return prisma.customer.findFirst({ + where: { + OR: [ + { name: { contains: customerName, mode: 'insensitive' } } + ], + }, + }); + } + + async findTeamMemberByName(teamMemberName: string) { + return prisma.organization_People.findFirst({ + where: { + name: { contains: teamMemberName, mode: 'insensitive' } + }, + }); + } + + + async createIssue(data: any) { + return prisma.issue.create({ data }); + } + + async transaction(callback: (trx: any) => Promise) { + return prisma.$transaction(callback); + } +} + + +export default new IssueRepository(); \ No newline at end of file diff --git a/src/services/issueService.ts b/src/services/issueService.ts new file mode 100644 index 0000000..165021a --- /dev/null +++ b/src/services/issueService.ts @@ -0,0 +1,54 @@ +import issueRepository from "../repositories/issueRepository"; +import ErrorHandlerClass from "../utils/errorClass"; + + +class IssueService { + async createIssue(issueData: any) { + const { title, description, state, priority, customerName, teamMemberName } = issueData; + + const organization = await issueRepository.findOrganization(); + + + + if (!organization) { + throw new ErrorHandlerClass("Organization Not found", 404); + } + + const newIssue = await issueRepository.transaction(async (trx) => { + const customer = await trx.customer.findFirst({ + where: { + name: { contains: customerName as string, mode: 'insensitive' } + } + }); + + + if (!customer) { + throw new ErrorHandlerClass("Customer Not Found", 404) + } + + const teamMember = await trx.organization_People.findFirst({ + where: { name: { contains: teamMemberName as string, mode: 'insensitive' } } + }); + + if (!teamMember) { + throw new ErrorHandlerClass('Team member not found', 404) + } + + const issue = await trx.issue.create({ + data: { + title, + description, + priority, + state, + customerId: customer.customer_id, + teamMemberId: teamMember.team_member_id, + organizationId: organization.organization_id, + }, + }); + return issue; + }); + return newIssue; + } +} + +export default new IssueService(); \ No newline at end of file