(req: TaskRequestRequest, res: CustomResponse)
| 10 | import { TaskRequestRequest } from "../types/taskRequests"; |
| 11 | |
| 12 | export const createTaskRequestController = async (req: TaskRequestRequest, res: CustomResponse) => { |
| 13 | const taskRequestData = req.body; |
| 14 | const requestedBy = req?.userData?.id; |
| 15 | |
| 16 | if (!requestedBy) { |
| 17 | return res.boom.unauthorized(); |
| 18 | } |
| 19 | |
| 20 | if (req.userData.id !== taskRequestData.userId && !req.userData.roles?.super_user) { |
| 21 | return res.boom.forbidden(TASK_REQUEST_MESSAGES.NOT_AUTHORIZED_TO_CREATE_REQUEST); |
| 22 | } |
| 23 | |
| 24 | const userPromise: any = await fetchUser({ userId: taskRequestData.userId }); |
| 25 | const userData: userData = userPromise.user; |
| 26 | if (!userData.id || !userData.username) { |
| 27 | return res.boom.notFound(TASK_REQUEST_MESSAGES.USER_NOT_FOUND); |
| 28 | } |
| 29 | try { |
| 30 | switch (taskRequestData.requestType) { |
| 31 | case TASK_REQUEST_TYPE.ASSIGNMENT: { |
| 32 | if (!req.userData.roles?.super_user) { |
| 33 | return res.boom.unauthorized(TASK_REQUEST_MESSAGES.NOT_AUTHORIZED_TO_CREATE_REQUEST); |
| 34 | } |
| 35 | const { taskData } = await fetchTask(taskRequestData.taskId); |
| 36 | if (!taskData) { |
| 37 | return res.boom.badRequest(TASK_REQUEST_MESSAGES.TASK_NOT_EXIST); |
| 38 | } |
| 39 | taskRequestData.taskTitle = taskData?.title; |
| 40 | break; |
| 41 | } |
| 42 | case TASK_REQUEST_TYPE.CREATION: { |
| 43 | let issueData: any; |
| 44 | try { |
| 45 | const url = new URL(taskRequestData.externalIssueUrl); |
| 46 | const issueUrlPaths = url.pathname.split("/"); |
| 47 | const repositoryName = issueUrlPaths[3]; |
| 48 | const issueNumber = issueUrlPaths[5]; |
| 49 | issueData = await fetchIssuesById(repositoryName, issueNumber); |
| 50 | } catch (error) { |
| 51 | return res.boom.badRequest(TASK_REQUEST_MESSAGES.INVALID_EXTERNAL_ISSUE_URL); |
| 52 | } |
| 53 | if (!issueData) { |
| 54 | return res.boom.badRequest(TASK_REQUEST_MESSAGES.ISSUE_NOT_EXIST); |
| 55 | } |
| 56 | taskRequestData.taskTitle = issueData?.title; |
| 57 | break; |
| 58 | } |
| 59 | } |
| 60 | const existingRequest = await getRequestByKeyValues({ |
| 61 | externalIssueUrl: taskRequestData.externalIssueUrl, |
| 62 | requestType: taskRequestData.requestType, |
| 63 | }); |
| 64 | |
| 65 | if ( |
| 66 | existingRequest && |
| 67 | existingRequest.state === REQUEST_STATE.PENDING && |
| 68 | existingRequest.requestors.includes(requestedBy) |
| 69 | ) { |
no test coverage detected