Skip to content

Commit

Permalink
created issue service
Browse files Browse the repository at this point in the history
  • Loading branch information
HeeManSu committed Jul 4, 2024
1 parent 91576e0 commit 727f5c2
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 7 deletions.
6 changes: 2 additions & 4 deletions prisma/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: '[email protected]',
organizationId: organization.organization_id,
Expand All @@ -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: '[email protected]',
organizationId: organization.organization_id,
Expand Down
15 changes: 12 additions & 3 deletions src/controllers/issuesController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});

Expand Down
38 changes: 38 additions & 0 deletions src/repositories/issueRepository.ts
Original file line number Diff line number Diff line change
@@ -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<any>) {
return prisma.$transaction(callback);
}
}


export default new IssueRepository();
54 changes: 54 additions & 0 deletions src/services/issueService.ts
Original file line number Diff line number Diff line change
@@ -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();

0 comments on commit 727f5c2

Please sign in to comment.