|
| 1 | +const { Conflict, NotFound } = require("http-errors"); |
| 2 | +const { createProgressDocument, getProgressDocument, getRangeProgressData } = require("../models/progresses"); |
| 3 | +const { RESPONSE_MESSAGES } = require("../constants/progresses"); |
| 4 | +const { PROGRESS_DOCUMENT_RETRIEVAL_SUCCEEDED, PROGRESS_DOCUMENT_CREATED_SUCCEEDED } = RESPONSE_MESSAGES; |
| 5 | + |
| 6 | +/** |
| 7 | + * @typedef {Object} ProgressRequestBody |
| 8 | + * @property {string} type - The type of progress document. |
| 9 | + * @property {string} completed - The completed progress. |
| 10 | + * @property {string} planned - The planned progress. |
| 11 | + * @property {string} blockers - The blockers. |
| 12 | + * @property {string} [taskId] - The task ID (optional). |
| 13 | + */ |
| 14 | + |
| 15 | +/** |
| 16 | + * @typedef {Object} ProgressDocument |
| 17 | + * @property {string} type - The type of progress document. |
| 18 | + * @property {string} completed - The completed progress. |
| 19 | + * @property {string} planned - The planned progress. |
| 20 | + * @property {string} blockers - The blockers. |
| 21 | + * @property {string} userId - The User ID |
| 22 | + * @property {string} [taskId] - The task ID (optional). |
| 23 | + * @property {number} createdAt - The timestamp when the progress document was created. |
| 24 | + * @property {number} date - The timestamp for the day the progress document was created. |
| 25 | + */ |
| 26 | + |
| 27 | +/** |
| 28 | + * @typedef {Object} ProgressResponse |
| 29 | + * @property {ProgressDocument} data - The progress document data. |
| 30 | + * @property {string} message - The success message. |
| 31 | + */ |
| 32 | + |
| 33 | +/** |
| 34 | + * Creates a new progress document. |
| 35 | + * @param {Object} req - The HTTP request object. |
| 36 | + * @param {ProgressRequestBody} req.body - The progress document data. |
| 37 | + * @param {Object} res - The HTTP response object. |
| 38 | + * @returns {Promise<void>} A Promise that resolves when the response is sent. |
| 39 | + */ |
| 40 | + |
| 41 | +const createProgress = async (req, res) => { |
| 42 | + const { |
| 43 | + body: { type }, |
| 44 | + } = req; |
| 45 | + try { |
| 46 | + const data = await createProgressDocument({ ...req.body, userId: req.userData.id }); |
| 47 | + return res.status(201).json({ |
| 48 | + data, |
| 49 | + message: `${type.charAt(0).toUpperCase() + type.slice(1)} ${PROGRESS_DOCUMENT_CREATED_SUCCEEDED}`, |
| 50 | + }); |
| 51 | + } catch (error) { |
| 52 | + if (error instanceof Conflict) { |
| 53 | + return res.status(409).json({ |
| 54 | + message: error.message, |
| 55 | + }); |
| 56 | + } else if (error instanceof NotFound) { |
| 57 | + return res.status(404).json({ |
| 58 | + message: error.message, |
| 59 | + }); |
| 60 | + } |
| 61 | + return res.status(400).json({ |
| 62 | + message: error.message, |
| 63 | + }); |
| 64 | + } |
| 65 | +}; |
| 66 | + |
| 67 | +/** |
| 68 | + * @typedef {Object} ProgressQueryParams |
| 69 | + * @property {string} [type] - The type of progress document. |
| 70 | + * @property {string} [taskId] - The task ID (optional). |
| 71 | + * @property {string} [userId] - The user ID (optional). |
| 72 | + */ |
| 73 | + |
| 74 | +/** |
| 75 | + * @typedef {Object} ProgressDocument |
| 76 | + * @property {string} type - The type of progress document. |
| 77 | + * @property {string} completed - The completed progress. |
| 78 | + * @property {string} planned - The planned progress. |
| 79 | + * @property {string} blockers - The blockers. |
| 80 | + * @property {string} userId - The User ID |
| 81 | + * @property {string} [taskId] - The task ID (optional). |
| 82 | + * @property {number} createdAt - The timestamp when the progress document was created. |
| 83 | + * @property {number} date - The timestamp for the day the progress document was created. |
| 84 | + */ |
| 85 | + |
| 86 | +/** |
| 87 | + * @typedef {Object} GetProgressResponse |
| 88 | + * @property {string} message - The success message. |
| 89 | + * @property {number} count - The no of progress documents retrieved |
| 90 | + * @property {[ProgressDocument]} data - An array of progress documents |
| 91 | + */ |
| 92 | + |
| 93 | +/** |
| 94 | + * Retrieves the progress documents based on provided query parameters. |
| 95 | + * @param {Object} req - The HTTP request object. |
| 96 | + * @param {ProgressQueryParams} req.query - The query parameters |
| 97 | + * @param {Object} res - The HTTP response object. |
| 98 | + * @returns {Promise<void>} A Promise that resolves when the response is sent. |
| 99 | + */ |
| 100 | + |
| 101 | +const getProgress = async (req, res) => { |
| 102 | + try { |
| 103 | + const data = await getProgressDocument(req.query); |
| 104 | + return res.json({ |
| 105 | + message: PROGRESS_DOCUMENT_RETRIEVAL_SUCCEEDED, |
| 106 | + count: data.length, |
| 107 | + data, |
| 108 | + }); |
| 109 | + } catch (error) { |
| 110 | + if (error instanceof NotFound) { |
| 111 | + return res.status(404).json({ |
| 112 | + message: error.message, |
| 113 | + }); |
| 114 | + } |
| 115 | + return res.status(400).json({ |
| 116 | + message: error.message, |
| 117 | + }); |
| 118 | + } |
| 119 | +}; |
| 120 | + |
| 121 | +/** |
| 122 | + * @typedef {Object} ProgressQueryParams |
| 123 | + * @property {string} [taskId] - The task ID (optional). |
| 124 | + * @property {string} [userId] - The user ID (optional). |
| 125 | + * @property {string} startDate - The start date of the date range to retrieve progress records for. |
| 126 | + * @property {string} endDate - The end date of the date range to retrieve progress records for. |
| 127 | + */ |
| 128 | + |
| 129 | +/** |
| 130 | + * @typedef {Object} progressRecord |
| 131 | + * @property {boolean} date - the boolean indicating whether the progress was recorded or not for that date |
| 132 | +/** |
| 133 | +
|
| 134 | +/** |
| 135 | + * @typedef {Object} ProgressRangeData |
| 136 | + * @property {string} startDate - the start date for the progress records |
| 137 | + * @property {string} endDate - the end date for the progress records |
| 138 | + * @property {Object.<string, progressRecord>} progressRecords - An object where the keys are dates and the values are progress records. |
| 139 | +/** |
| 140 | +
|
| 141 | +/** |
| 142 | + * @typedef {Object} GetProgressRangeDataResponse |
| 143 | + * @property {string} message - The success message. |
| 144 | + * @property {ProgressRangeData} data - The progress range data. |
| 145 | + */ |
| 146 | + |
| 147 | +/** |
| 148 | + * Retrieves the progress documents based on provided query parameters. |
| 149 | + * @param {Object} req - The HTTP request object. |
| 150 | + * @param {ProgressQueryParams} req.query - The query parameters |
| 151 | + * @param {Object} res - The HTTP response object. |
| 152 | + * @returns {Promise<void>} A Promise that resolves when the response is sent. |
| 153 | + */ |
| 154 | + |
| 155 | +const getProgressRangeData = async (req, res) => { |
| 156 | + try { |
| 157 | + const data = await getRangeProgressData(req.query); |
| 158 | + return res.json({ |
| 159 | + message: PROGRESS_DOCUMENT_RETRIEVAL_SUCCEEDED, |
| 160 | + data, |
| 161 | + }); |
| 162 | + } catch (error) { |
| 163 | + if (error instanceof NotFound) { |
| 164 | + return res.status(404).json({ |
| 165 | + message: error.message, |
| 166 | + }); |
| 167 | + } |
| 168 | + return res.status(400).json({ |
| 169 | + message: error.message, |
| 170 | + }); |
| 171 | + } |
| 172 | +}; |
| 173 | + |
| 174 | +module.exports = { createProgress, getProgress, getProgressRangeData }; |
0 commit comments