-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
106 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, | ||
|
@@ -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, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |