Skip to content

Commit 45e79de

Browse files
committed
initial
1 parent 2ac202e commit 45e79de

File tree

5 files changed

+174
-3
lines changed

5 files changed

+174
-3
lines changed

controllers/progresses.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ const getProgressRangeData = async (req, res) => {
217217

218218
const getProgressBydDateController = async (req, res) => {
219219
try {
220-
const data = await getProgressByDate(req.params);
220+
const data = await getProgressByDate(req.params, req.query);
221221
return res.json({
222222
message: PROGRESS_DOCUMENT_RETRIEVAL_SUCCEEDED,
223223
data,

middlewares/validators/progresses.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ const validateGetProgressRecordsQuery = async (req, res, next) => {
6363
taskId: joi.string().optional().allow("").messages({
6464
"string.base": "taskId must be a string",
6565
}),
66+
dev: joi.boolean().optional().messages({
67+
"boolean.base": "dev must be a boolean value (true or false).",
68+
}),
6669
orderBy: joi
6770
.string()
6871
.optional()
@@ -92,6 +95,7 @@ const validateGetRangeProgressRecordsParams = async (req, res, next) => {
9295
taskId: joi.string().optional(),
9396
startDate: joi.date().iso().required(),
9497
endDate: joi.date().iso().min(joi.ref("startDate")).required(),
98+
dev: joi.boolean().optional(),
9599
})
96100
.xor("userId", "taskId")
97101
.messages({
@@ -121,6 +125,7 @@ const validateGetDayProgressParams = async (req, res, next) => {
121125
}),
122126
typeId: joi.string().required(),
123127
date: joi.date().iso().required(),
128+
dev: joi.boolean().optional(),
124129
});
125130
try {
126131
await schema.validateAsync(req.params, { abortEarly: false });

models/progresses.js

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const {
1313
getProgressDateTimestamp,
1414
buildQueryToSearchProgressByDay,
1515
} = require("../utils/progresses");
16+
const { fetchUser } = require("./users");
1617
const { PROGRESS_ALREADY_CREATED, PROGRESS_DOCUMENT_NOT_FOUND } = PROGRESSES_RESPONSE_MESSAGES;
1718

1819
/**
@@ -47,9 +48,31 @@ const createProgressDocument = async (progressData) => {
4748
* @throws {Error} If the userId or taskId is invalid or does not exist.
4849
**/
4950
const getProgressDocument = async (queryParams) => {
51+
const { dev } = queryParams;
5052
await assertUserOrTaskExists(queryParams);
5153
const query = buildQueryToFetchDocs(queryParams);
5254
const progressDocs = await getProgressDocs(query);
55+
56+
if (dev) {
57+
try {
58+
const uniqueUserIds = [...new Set(progressDocs.map((doc) => doc.userId))];
59+
const usersData = await Promise.all(uniqueUserIds.map((userId) => fetchUser({ userId })));
60+
61+
const userLookupMap = usersData.reduce((lookup, { user }) => {
62+
if (user) lookup[user.id] = user;
63+
return lookup;
64+
}, {});
65+
66+
const progressDocsWithUserDetails = progressDocs.map((doc) => {
67+
const userDetails = userLookupMap[doc.userId] || null;
68+
return { ...doc, userData: userDetails };
69+
});
70+
71+
return progressDocsWithUserDetails;
72+
} catch (err) {
73+
return progressDocs.map((doc) => ({ ...doc, userData: null }));
74+
}
75+
}
5376
return progressDocs;
5477
};
5578

@@ -77,16 +100,23 @@ const getRangeProgressData = async (queryParams) => {
77100
* @returns {Promise<object>} A Promise that resolves with the progress records of the queried user or task.
78101
* @throws {Error} If the userId or taskId is invalid or does not exist.
79102
**/
80-
async function getProgressByDate(pathParams) {
103+
async function getProgressByDate(pathParams, queryParams) {
81104
const { type, typeId, date } = pathParams;
105+
const { dev } = queryParams;
82106
await assertUserOrTaskExists({ [TYPE_MAP[type]]: typeId });
83107
const query = buildQueryToSearchProgressByDay({ [TYPE_MAP[type]]: typeId, date });
84108
const result = await query.get();
85109
if (!result.size) {
86110
throw new NotFound(PROGRESS_DOCUMENT_NOT_FOUND);
87111
}
88112
const doc = result.docs[0];
89-
return { id: doc.id, ...doc.data() };
113+
const docData = doc.data();
114+
if (dev) {
115+
const { user: userData } = await fetchUser({ userId: docData.userId });
116+
return { id: doc.id, ...docData, userData };
117+
}
118+
119+
return { id: doc.id, ...docData };
90120
}
91121

92122
module.exports = { createProgressDocument, getProgressDocument, getRangeProgressData, getProgressByDate };

test/integration/progressesTasks.test.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,34 @@ describe("Test Progress Updates API for Tasks", function () {
222222
});
223223
});
224224

225+
it("Returns the progress array for the task with userData object when dev is true", function (done) {
226+
chai
227+
.request(app)
228+
.get(`/progresses?taskId=${taskId1}&dev=true`)
229+
.end((err, res) => {
230+
if (err) return done(err);
231+
expect(res).to.have.status(200);
232+
expect(res.body).to.have.keys(["message", "data", "count"]);
233+
expect(res.body.data).to.be.an("array");
234+
expect(res.body.message).to.be.equal("Progress document retrieved successfully.");
235+
res.body.data.forEach((progress) => {
236+
expect(progress).to.have.keys([
237+
"id",
238+
"taskId",
239+
"type",
240+
"completed",
241+
"planned",
242+
"blockers",
243+
"userData",
244+
"userId",
245+
"createdAt",
246+
"date",
247+
]);
248+
});
249+
return done();
250+
});
251+
});
252+
225253
it("Gives 400 status when anything other than -date or date is supplied", function (done) {
226254
chai
227255
.request(app)
@@ -311,6 +339,35 @@ describe("Test Progress Updates API for Tasks", function () {
311339
});
312340
});
313341

342+
it("Returns the progress array for all the tasks with userData object when dev is true", function (done) {
343+
chai
344+
.request(app)
345+
.get(`/progresses?type=task&dev=true`)
346+
.end((err, res) => {
347+
if (err) return done(err);
348+
expect(res).to.have.status(200);
349+
expect(res.body).to.have.keys(["message", "data", "count"]);
350+
expect(res.body.data).to.be.an("array");
351+
expect(res.body.message).to.be.equal("Progress document retrieved successfully.");
352+
expect(res.body.count).to.be.equal(4);
353+
res.body.data.forEach((progress) => {
354+
expect(progress).to.have.keys([
355+
"id",
356+
"taskId",
357+
"type",
358+
"completed",
359+
"planned",
360+
"blockers",
361+
"userData",
362+
"userId",
363+
"createdAt",
364+
"date",
365+
]);
366+
});
367+
return done();
368+
});
369+
});
370+
314371
it("Returns 400 for bad request", function (done) {
315372
chai
316373
.request(app)

test/integration/progressesUsers.test.js

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,60 @@ describe("Test Progress Updates API for Users", function () {
226226
});
227227
});
228228

229+
it("Returns the progress array for a specific user with userData object when dev is true", function (done) {
230+
chai
231+
.request(app)
232+
.get(`/progresses?userId=${userId1}&dev=true`)
233+
.end((err, res) => {
234+
if (err) return done(err);
235+
expect(res).to.have.status(200);
236+
expect(res.body).to.have.keys(["message", "data", "count"]);
237+
expect(res.body.data).to.be.an("array");
238+
expect(res.body.message).to.be.equal("Progress document retrieved successfully.");
239+
res.body.data.forEach((progress) => {
240+
expect(progress).to.have.keys([
241+
"id",
242+
"type",
243+
"completed",
244+
"planned",
245+
"blockers",
246+
"userId",
247+
"userData",
248+
"createdAt",
249+
"date",
250+
]);
251+
});
252+
return done();
253+
});
254+
});
255+
256+
it("Returns the progress array for all the user with userData object when dev is true", function (done) {
257+
chai
258+
.request(app)
259+
.get(`/progresses?type=user&dev=true`)
260+
.end((err, res) => {
261+
if (err) return done(err);
262+
expect(res).to.have.status(200);
263+
expect(res.body).to.have.keys(["message", "data", "count"]);
264+
expect(res.body.data).to.be.an("array");
265+
expect(res.body.message).to.be.equal("Progress document retrieved successfully.");
266+
res.body.data.forEach((progress) => {
267+
expect(progress).to.have.keys([
268+
"id",
269+
"type",
270+
"completed",
271+
"planned",
272+
"blockers",
273+
"userId",
274+
"userData",
275+
"createdAt",
276+
"date",
277+
]);
278+
});
279+
return done();
280+
});
281+
});
282+
229283
it("Returns 400 for bad request", function (done) {
230284
chai
231285
.request(app)
@@ -370,6 +424,31 @@ describe("Test Progress Updates API for Users", function () {
370424
});
371425
});
372426

427+
it("Returns the progress data for a specific user with userDat object when dev is true", function (done) {
428+
chai
429+
.request(app)
430+
.get(`/progresses/user/${userId}/date/2023-05-02?dev=true`)
431+
.end((err, res) => {
432+
if (err) return done(err);
433+
expect(res).to.have.status(200);
434+
expect(res.body).to.have.keys(["message", "data"]);
435+
expect(res.body.data).to.be.an("object");
436+
expect(res.body.message).to.be.equal("Progress document retrieved successfully.");
437+
expect(res.body.data).to.have.keys([
438+
"id",
439+
"type",
440+
"completed",
441+
"planned",
442+
"blockers",
443+
"userData",
444+
"userId",
445+
"createdAt",
446+
"date",
447+
]);
448+
return done();
449+
});
450+
});
451+
373452
it("Should return 404 No progress records found if the document doesn't exist", function (done) {
374453
chai
375454
.request(app)

0 commit comments

Comments
 (0)