generated from Real-Dev-Squad/website-template
-
Notifications
You must be signed in to change notification settings - Fork 264
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added controllere for TCR (#2055)
- Loading branch information
1 parent
2da9bff
commit a33e9ea
Showing
4 changed files
with
255 additions
and
1 deletion.
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
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,174 @@ | ||
import { REQUEST_STATE, TASK_REQUEST_MESSAGES } from "../constants/requests"; | ||
import { TASK_REQUEST_TYPE } from "../constants/taskRequests"; | ||
import { addLog } from "../models/logs"; | ||
import { createRequest, getRequestByKeyValues } from "../models/requests"; | ||
import { fetchTask } from "../models/tasks"; | ||
import { fetchUser } from "../models/users"; | ||
import { fetchIssuesById } from "../services/githubService"; | ||
import { CustomResponse } from "../typeDefinitions/global"; | ||
import { userData } from "../types/global"; | ||
import { TaskRequestRequest } from "../types/taskRequests"; | ||
|
||
export const createTaskRequestController = async (req: TaskRequestRequest, res: CustomResponse) => { | ||
const taskRequestData = req.body; | ||
const requestedBy = req?.userData?.id; | ||
|
||
if (!requestedBy) { | ||
return res.boom.unauthorized(); | ||
} | ||
|
||
if (req.userData.id !== taskRequestData.userId && !req.userData.roles?.super_user) { | ||
return res.boom.forbidden(TASK_REQUEST_MESSAGES.NOT_AUTHORIZED_TO_CREATE_REQUEST); | ||
} | ||
|
||
const userPromise: any = await fetchUser({ userId: taskRequestData.userId }); | ||
const userData: userData = userPromise.user; | ||
if (!userData.id || !userData.username) { | ||
return res.boom.notFound(TASK_REQUEST_MESSAGES.USER_NOT_FOUND); | ||
} | ||
try { | ||
switch (taskRequestData.requestType) { | ||
case TASK_REQUEST_TYPE.ASSIGNMENT: { | ||
if (!req.userData.roles?.super_user) { | ||
return res.boom.unauthorized(TASK_REQUEST_MESSAGES.NOT_AUTHORIZED_TO_CREATE_REQUEST); | ||
} | ||
const { taskData } = await fetchTask(taskRequestData.taskId); | ||
if (!taskData) { | ||
return res.boom.badRequest(TASK_REQUEST_MESSAGES.TASK_NOT_EXIST); | ||
} | ||
taskRequestData.taskTitle = taskData?.title; | ||
break; | ||
} | ||
case TASK_REQUEST_TYPE.CREATION: { | ||
let issueData: any; | ||
try { | ||
const url = new URL(taskRequestData.externalIssueUrl); | ||
const issueUrlPaths = url.pathname.split("/"); | ||
const repositoryName = issueUrlPaths[3]; | ||
const issueNumber = issueUrlPaths[5]; | ||
issueData = await fetchIssuesById(repositoryName, issueNumber); | ||
} catch (error) { | ||
return res.boom.badRequest(TASK_REQUEST_MESSAGES.INVALID_EXTERNAL_ISSUE_URL); | ||
} | ||
if (!issueData) { | ||
return res.boom.badRequest(TASK_REQUEST_MESSAGES.ISSUE_NOT_EXIST); | ||
} | ||
taskRequestData.taskTitle = issueData?.title; | ||
break; | ||
} | ||
} | ||
const existingRequest = await getRequestByKeyValues({ | ||
externalIssueUrl: taskRequestData.externalIssueUrl, | ||
requestType: taskRequestData.requestType, | ||
}); | ||
|
||
if ( | ||
existingRequest && | ||
existingRequest.state === REQUEST_STATE.PENDING && | ||
existingRequest.requestors.includes(requestedBy) | ||
) { | ||
return res.boom.badRequest(TASK_REQUEST_MESSAGES.TASK_REQUEST_EXISTS); | ||
} else if ( | ||
existingRequest && | ||
existingRequest.state === REQUEST_STATE.PENDING && | ||
!existingRequest.requestors.includes(requestedBy) | ||
) { | ||
existingRequest.requestors.push(requestedBy); | ||
existingRequest.users.push({ | ||
userId: userData.id, | ||
username: userData.username, | ||
proposedStartDate: taskRequestData.proposedStartDate, | ||
proposedDeadline: taskRequestData.proposedDeadline, | ||
description: taskRequestData.description, | ||
markdownEnabled: taskRequestData.markdownEnabled, | ||
firstName: userData.first_name, | ||
lastName: userData.last_name, | ||
state: REQUEST_STATE.PENDING, | ||
requestedAt: Date.now(), | ||
}); | ||
const updatedRequest = await createRequest(existingRequest); | ||
const taskRequestLog = { | ||
type: "taskRequests", | ||
meta: { | ||
taskRequestId: updatedRequest.id, | ||
action: "update", | ||
createdBy: req.userData.id, | ||
createdAt: Date.now(), | ||
lastModifiedBy: req.userData.id, | ||
lastModifiedAt: Date.now(), | ||
}, | ||
body: updatedRequest, | ||
}; | ||
await addLog(taskRequestLog.type, taskRequestLog.meta, taskRequestLog.body); | ||
const data = { | ||
message: TASK_REQUEST_MESSAGES.TASK_REQUEST_UPDATED_SUCCESS, | ||
data: { | ||
id: updatedRequest.id, | ||
...updatedRequest, | ||
}, | ||
}; | ||
return res.status(200).json(data); | ||
} | ||
|
||
taskRequestData.requestedBy = requestedBy; | ||
const createtaskRequestData = { | ||
externalIssueUrl: taskRequestData.externalIssueUrl, | ||
externalIssueHtmlUrl: taskRequestData.externalIssueHtmlUrl, | ||
requestType: taskRequestData.requestType, | ||
type: taskRequestData.type, | ||
state: REQUEST_STATE.PENDING, | ||
requestedBy: requestedBy, | ||
taskTitle: taskRequestData.taskTitle, | ||
users: [ | ||
{ | ||
userId: userData.id, | ||
username: userData.username, | ||
proposedStartDate: taskRequestData.proposedStartDate, | ||
proposedDeadline: taskRequestData.proposedDeadline, | ||
description: taskRequestData.description, | ||
markdownEnabled: taskRequestData.markdownEnabled, | ||
firstName: userData.first_name, | ||
lastName: userData.last_name, | ||
state: REQUEST_STATE.PENDING, | ||
requestedAt: Date.now(), | ||
}, | ||
], | ||
|
||
requestors: [requestedBy], | ||
}; | ||
const newTaskRequest = await createRequest(createtaskRequestData); | ||
|
||
if (newTaskRequest.isCreationRequestApproved) { | ||
return res.boom.badRequest(TASK_REQUEST_MESSAGES.TASK_EXISTS_FOR_GIVEN_ISSUE); | ||
} | ||
if (newTaskRequest.alreadyRequesting) { | ||
return res.boom.badRequest(TASK_REQUEST_MESSAGES.TASK_ALREADY_REQUESTED); | ||
} | ||
|
||
const taskRequestLog = { | ||
type: "taskRequests", | ||
meta: { | ||
taskRequestId: newTaskRequest.id, | ||
action: "create", | ||
createdBy: req.userData.id, | ||
createdAt: Date.now(), | ||
lastModifiedBy: req.userData.id, | ||
lastModifiedAt: Date.now(), | ||
}, | ||
body: newTaskRequest, | ||
}; | ||
await addLog(taskRequestLog.type, taskRequestLog.meta, taskRequestLog.body); | ||
|
||
const data = { | ||
message: TASK_REQUEST_MESSAGES.TASK_REQUEST_CREATED_SUCCESS, | ||
data: { | ||
id: newTaskRequest.id, | ||
...newTaskRequest, | ||
}, | ||
}; | ||
return res.status(201).json(data); | ||
} catch (err) { | ||
logger.error(`${TASK_REQUEST_MESSAGES.ERROR_CREATING_TASK_REQUEST} : ${err}`); | ||
return res.boom.serverUnavailable(TASK_REQUEST_MESSAGES.ERROR_CREATING_TASK_REQUEST); | ||
} | ||
}; |
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