|
| 1 | +const { Conflict, NotFound } = require("http-errors"); |
| 2 | +const { INTERNAL_SERVER_ERROR_MESSAGE } = require("../constants/progresses"); |
| 3 | +const { |
| 4 | + createTrackedProgressDocument, |
| 5 | + updateTrackedProgressDocument, |
| 6 | + getTrackedProgressDocuments, |
| 7 | +} = require("../models/monitor"); |
| 8 | +const { RESPONSE_MESSAGES } = require("../constants/monitor"); |
| 9 | +const { RESOURCE_CREATED_SUCCESSFULLY, RESOURCE_UPDATED_SUCCESSFULLY, RESOURCE_RETRIEVED_SUCCESSFULLY } = |
| 10 | + RESPONSE_MESSAGES; |
| 11 | +/** |
| 12 | + * @typedef {Object} TrackedProgressRequestBody |
| 13 | + * @property {string} type - The type of tracked progress ("user" or "task"). |
| 14 | + * @property {string} [userId] - The user ID (required if type is "user"). |
| 15 | + * @property {string} [taskId] - The task ID (required if type is "task"). |
| 16 | + * @property {boolean} monitored - Indicates if the progress is currently being tracked. |
| 17 | + * @property {number} [frequency=1] - The frequency of tracking.By default 1 if not specified |
| 18 | + */ |
| 19 | + |
| 20 | +/** |
| 21 | + * @typedef {Object} TrackedProgressResponseData |
| 22 | + * @property {string} id - The ID of the tracked progress document. |
| 23 | + * @property {string} type - The type of tracked progress ("user" or "task"). |
| 24 | + * @property {string} userId - The user ID. |
| 25 | + * @property {boolean} monitored - Indicates if the progress is currently being tracked. |
| 26 | + * @property {number} frequency - The frequency of tracking. |
| 27 | + * @property {string} createdAt - The timestamp when the document was created. |
| 28 | + * @property {string} updatedAt - The timestamp when the document was last updated. |
| 29 | + */ |
| 30 | + |
| 31 | +/** |
| 32 | + * @typedef {Object} TrackedProgressResponse |
| 33 | + * @property {TrackedProgressResponseData} data - The data of the tracked progress document. |
| 34 | + * @property {string} message - The success message. |
| 35 | + */ |
| 36 | + |
| 37 | +/** |
| 38 | + * Controller function for creating a tracked progress document. |
| 39 | + * |
| 40 | + * @param {Express.Request} req - The Express request object. |
| 41 | + * @param {TrackedProgressRequestBody} req.body - The Request body object. |
| 42 | + * @param {Express.Response} res - The Express response object. |
| 43 | + * @returns {Promise<void>} - A Promise that resolves when the response has been sent. |
| 44 | + */ |
| 45 | + |
| 46 | +const createTrackedProgressController = async (req, res) => { |
| 47 | + try { |
| 48 | + const data = await createTrackedProgressDocument({ ...req.body }); |
| 49 | + return res.status(201).json({ |
| 50 | + message: RESOURCE_CREATED_SUCCESSFULLY, |
| 51 | + data, |
| 52 | + }); |
| 53 | + } catch (error) { |
| 54 | + if (error instanceof Conflict) { |
| 55 | + return res.status(409).json({ |
| 56 | + message: error.message, |
| 57 | + }); |
| 58 | + } else if (error instanceof NotFound) { |
| 59 | + return res.status(404).json({ |
| 60 | + message: error.message, |
| 61 | + }); |
| 62 | + } |
| 63 | + return res.status(500).json({ |
| 64 | + message: INTERNAL_SERVER_ERROR_MESSAGE, |
| 65 | + }); |
| 66 | + } |
| 67 | +}; |
| 68 | + |
| 69 | +/** |
| 70 | + * @typedef {Object} UpdateTrackedProgressRequestParams |
| 71 | + * @property {string} type - The type of tracked progress ("user" or "task"). |
| 72 | + * @property {string} id - The ID of the tracked progress document. |
| 73 | + */ |
| 74 | + |
| 75 | +/** |
| 76 | + * @typedef {Object} UpdateTrackedProgressRequestBody |
| 77 | + * @property {number} frequency - The frequency of tracking. |
| 78 | + * @property {boolean} monitored - Indicates if the progress is currently being tracked. |
| 79 | + */ |
| 80 | + |
| 81 | +/** |
| 82 | + * @typedef {Object} UpdateTrackedProgressResponseData |
| 83 | + * @property {string} id - The ID of the tracked progress document. |
| 84 | + * @property {string} createdAt - The timestamp when the document was created. |
| 85 | + * @property {string} type - The type of tracked progress ("user" or "task"). |
| 86 | + * @property {string} userId - The user ID. |
| 87 | + * @property {number} frequency - The frequency of tracking. |
| 88 | + * @property {boolean} monitored - Indicates if the progress is currently being tracked. |
| 89 | + * @property {string} updatedAt - The timestamp when the document was last updated. |
| 90 | + */ |
| 91 | + |
| 92 | +/** |
| 93 | + * @typedef {Object} UpdateTrackedProgressResponse |
| 94 | + * @property {UpdateTrackedProgressResponseData} data - The data of the tracked progress document. |
| 95 | + * @property {string} message - The success message. |
| 96 | + */ |
| 97 | + |
| 98 | +/** |
| 99 | + * Controller function for updating a tracked progress document. |
| 100 | + * |
| 101 | + * @param {Express.Request} req - The Express request object. |
| 102 | + * @param {UpdateTrackedProgressRequestParams} req.params - The request path parameters. |
| 103 | + * @param {UpdateTrackedProgressRequestBody} req.body - The request body object. |
| 104 | + * @param {Express.Response} res - The Express response object. |
| 105 | + * @returns {Promise<void>} - A Promise that resolves when the response has been sent. |
| 106 | + */ |
| 107 | + |
| 108 | +const updateTrackedProgressController = async (req, res) => { |
| 109 | + try { |
| 110 | + const data = await updateTrackedProgressDocument({ ...req }); |
| 111 | + return res.status(200).json({ |
| 112 | + data, |
| 113 | + message: RESOURCE_UPDATED_SUCCESSFULLY, |
| 114 | + }); |
| 115 | + } catch (error) { |
| 116 | + if (error instanceof NotFound) { |
| 117 | + return res.status(404).json({ |
| 118 | + message: error.message, |
| 119 | + }); |
| 120 | + } |
| 121 | + return res.status(500).json({ |
| 122 | + message: INTERNAL_SERVER_ERROR_MESSAGE, |
| 123 | + }); |
| 124 | + } |
| 125 | +}; |
| 126 | + |
| 127 | +/** |
| 128 | + * @typedef {Object} GetTrackedProgressRequestParams |
| 129 | + * @property {string} [type] - The type of tracked progress ("user" or "task"). |
| 130 | + * @property {string} [monitored] - Indicates if the progress is currently being tracked. |
| 131 | + * @property {string} [userId] - The ID of the User who is currently being tracked. |
| 132 | + * @property {string} [taskId] - The ID of the task which is currently being tracked. |
| 133 | + */ |
| 134 | + |
| 135 | +/** |
| 136 | + * @typedef {Object} TrackedProgressData |
| 137 | + * @property {string} id - The ID of the tracked progress document. |
| 138 | + * @property {boolean} monitored - Indicates if the progress is currently being tracked. |
| 139 | + * @property {string} createdAt - The timestamp when the document was created. |
| 140 | + * @property {string} type - The type of tracked progress ("user" or "task"). |
| 141 | + * @property {string} [userId] - The user ID. |
| 142 | + * @property {string} [taskId] - The task ID. |
| 143 | + * @property {number} frequency - The frequency of tracking. |
| 144 | + * @property {string} updatedAt - The timestamp when the document was last updated. |
| 145 | + */ |
| 146 | + |
| 147 | +/** |
| 148 | + * @typedef {Object} GetTrackedProgressResponse |
| 149 | + * @property {string} message - The success message. |
| 150 | + * @property {TrackedProgressData | TrackedProgressData[]} data - The data of the tracked progress document(s). |
| 151 | + */ |
| 152 | + |
| 153 | +/** |
| 154 | + * Controller function for retrieving tracked progress documents. |
| 155 | + * |
| 156 | + * @param {Express.Request} req - The Express request object. |
| 157 | + * @param {Express.Response} res - The Express response object. |
| 158 | + * @returns {Promise<void>} A Promise that resolves when the response has been sent. |
| 159 | + */ |
| 160 | + |
| 161 | +const getTrackedProgressController = async (req, res) => { |
| 162 | + try { |
| 163 | + const data = await getTrackedProgressDocuments({ ...req.query }); |
| 164 | + return res.status(200).json({ |
| 165 | + message: RESOURCE_RETRIEVED_SUCCESSFULLY, |
| 166 | + data, |
| 167 | + }); |
| 168 | + } catch (error) { |
| 169 | + if (error instanceof NotFound) { |
| 170 | + const response = { |
| 171 | + message: error.message, |
| 172 | + }; |
| 173 | + if (req.query.type) { |
| 174 | + response.data = []; |
| 175 | + } |
| 176 | + return res.status(404).json(response); |
| 177 | + } |
| 178 | + return res.status(500).json({ |
| 179 | + message: INTERNAL_SERVER_ERROR_MESSAGE, |
| 180 | + }); |
| 181 | + } |
| 182 | +}; |
| 183 | + |
| 184 | +module.exports = { |
| 185 | + createTrackedProgressController, |
| 186 | + updateTrackedProgressController, |
| 187 | + getTrackedProgressController, |
| 188 | +}; |
0 commit comments