Skip to content

Commit

Permalink
controllers updated for adding sprints while creating issue
Browse files Browse the repository at this point in the history
  • Loading branch information
HeeManSu committed Jul 8, 2024
1 parent 3670aa2 commit ca3299c
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 17 deletions.
8 changes: 6 additions & 2 deletions src/controllers/issuesController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ import { Request, Response, NextFunction } from "express";
import ErrorHandlerClass from "../utils/errorClass";
import issueService from "../services/issueService";
import issueRepository from "../repositories/issueRepository";
import { IssueBodyData } from "../interfaces/issueInterface";

export const createIssue = async (req: Request, res: Response, next: NextFunction): Promise<void> => {

try {
const newIssue = await issueService.createIssue(req.body);

const requestBody: IssueBodyData = req.body;
const newIssue = await issueService.createIssue(requestBody);

res.status(201).json({
success: true,
Expand Down Expand Up @@ -36,8 +39,9 @@ export const getAllIssues = async (req: Request, res: Response, next: NextFuncti
export const updateIssue = async (req: Request, res: Response, next: NextFunction): Promise<void> => {
try {
const { issue_id } = req.params;
const updateData: Partial<IssueBodyData> = req.body;

const updatedIssue = await issueService.updateIssue(Number(issue_id), req.body);
const updatedIssue = await issueService.updateIssue(Number(issue_id), updateData);

res.status(200).json({
success: true,
Expand Down
2 changes: 2 additions & 0 deletions src/interfaces/issueInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export interface IssueBodyData {
priority: 'high' | 'medium' | 'low';
customerName: string;
teamMemberName: string;
sprintName?: string;
}

export interface IssueCreateData {
Expand All @@ -15,4 +16,5 @@ export interface IssueCreateData {
customer: { connect: { customer_id: number } };
team_member: { connect: { team_member_id: number } };
organization: { connect: { organization_id: number } };
sprint?: { connect: { sprint_id: number } };
}
14 changes: 12 additions & 2 deletions src/repositories/issueRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,24 @@ class IssueRepository {
customer: true,
team_member: true,
organization: true,
sprint: true
},
});
}

async updateIssue(issue_id: number, data: Partial<IssueCreateData>) {
return prisma.issue.update({
async updateIssue(issue_id: number, data: Partial<IssueCreateData>, trx?: Prisma.TransactionClient) {

const client = trx || prisma;

return await client.issue.update({
where: { issue_id },
data,
include: {
customer: true,
team_member: true,
organization: true,
sprint: true
}
});
}

Expand Down
12 changes: 7 additions & 5 deletions src/repositories/sprintRepository.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { Organization, Prisma, Sprint } from "@prisma/client";
import prisma from "../config/primsa-client";



class SprintRepository {
async createSprint(data: Prisma.SprintCreateInput): Promise<Sprint> {
return await prisma.sprint.create({ data });
Expand All @@ -16,18 +14,22 @@ class SprintRepository {
return await prisma.sprint.findMany({
include: {
organization: true,
issue: true
},
});
}

async updateSprint(sprint_id: number, data: Partial<Sprint>): Promise<Sprint | null> {
return await prisma.sprint.update({
async updateSprint(sprint_id: number, data: Partial<Sprint>, trx?: Prisma.TransactionClient): Promise<Sprint | null> {
const client = trx || prisma;
return await client.sprint.update({
where: { sprint_id },
data,
include: {
issue: true,
},
});
}


async transaction(callback: (trx: Prisma.TransactionClient) => Promise<any>): Promise<any> {
return prisma.$transaction(callback);
}
Expand Down
52 changes: 48 additions & 4 deletions src/services/issueService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Issue, Prisma } from "@prisma/client";

class IssueService {
async createIssue(issueData: IssueBodyData): Promise<Issue> {
const { title, description, state, priority, customerName, teamMemberName } = issueData;
const { title, description, state, priority, customerName, teamMemberName, sprintName } = issueData;

const organization = await issueRepository.findOrganization();

Expand All @@ -32,6 +32,20 @@ class IssueService {
throw new ErrorHandlerClass('Team member not found', 404);
}

let sprint = null;
if (sprintName) {
sprint = await trx.sprint.findFirst({
where: {
name: {
contains: sprintName as string, mode: 'insensitive'
}
}
})
if (!sprint) {
throw new ErrorHandlerClass('Sprint Not found', 404)
}
}

const issue = await trx.issue.create({
data: {
title,
Expand All @@ -41,11 +55,13 @@ class IssueService {
customer: { connect: { customer_id: customer.customer_id } },
team_member: { connect: { team_member_id: teamMember.team_member_id } },
organization: { connect: { organization_id: organization.organization_id } },
...(sprint && { sprint: { connect: { sprint_id: sprint.sprint_id } } })
},
include: {
customer: true,
team_member: true,
organization: true
organization: true,
sprint: true
},
});
return issue;
Expand All @@ -63,8 +79,36 @@ class IssueService {
return await issueRepository.getAllIssues();
}

async updateIssue(issue_id: number, issueData: Partial<IssueBodyData>) {
const updateIssue = await issueRepository.updateIssue(issue_id, issueData);
async updateIssue(issue_id: number, issueData: Partial<IssueBodyData>): Promise<Issue> {

const { sprintName, ...rest } = issueData;

const updateIssue = await issueRepository.transaction(async (trx: Prisma.TransactionClient) => {
let sprint = null;
if (sprintName) {
sprint = await trx.sprint.findFirst({
where: {
name: {
contains: sprintName, mode: 'insensitive'
}
}
})
if (!sprint) {
throw new ErrorHandlerClass('Sprint not found', 404);
}
}

const issue = await issueRepository.updateIssue(issue_id, {
...rest,
...(sprint && { sprint: { connect: { sprint_id: sprint.sprint_id } } })
}, trx)

return issue;
})





if (!updateIssue) {
throw new ErrorHandlerClass("Issue Not Found", 404);
Expand Down
12 changes: 8 additions & 4 deletions src/services/sprintService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,15 @@ class SprintService {
}

async updateSprint(sprint_id: number, sprintData: Partial<SprintBodyInput>): Promise<Sprint> {
const updatedSprint = await sprintRepository.updateSprint(sprint_id, sprintData);
const updatedSprint = await sprintRepository.transaction(async (trx: Prisma.TransactionClient) => {
const sprint = await sprintRepository.updateSprint(sprint_id, sprintData, trx);

if (!updatedSprint) {
throw new ErrorHandlerClass("Sprint Not Found", 404);
}
if (!sprint) {
throw new ErrorHandlerClass("Sprint Not Found", 404);
}

return sprint;
});

return updatedSprint;
}
Expand Down

0 comments on commit ca3299c

Please sign in to comment.