Skip to content

Commit 1ee0497

Browse files
committed
modified the Post model function to move the date handling logic to utils file
1 parent f9fa025 commit 1ee0497

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

models/progresses.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const { Conflict } = require("http-errors");
22
const fireStore = require("../utils/firestore");
33
const progressesCollection = fireStore.collection("progresses");
4-
const { MILLISECONDS_IN_DAY, RESPONSE_MESSAGES } = require("../constants/progresses");
4+
const { RESPONSE_MESSAGES } = require("../constants/progresses");
55
const {
66
buildQueryToFetchDocs,
77
getProgressDocs,
@@ -10,6 +10,7 @@ const {
1010
assertUserOrTaskExists,
1111
buildQueryForPostingProgress,
1212
assertTaskExists,
13+
getProgressDateTimestamp,
1314
} = require("../utils/progresses");
1415
const { PROGRESS_ALREADY_CREATED } = RESPONSE_MESSAGES;
1516

@@ -21,13 +22,8 @@ const { PROGRESS_ALREADY_CREATED } = RESPONSE_MESSAGES;
2122
**/
2223
const createProgressDocument = async (progressData) => {
2324
const { type, taskId } = progressData;
24-
// Currently, we are primarily catering to Indian users for our apps, which is why we have implemented support for the IST (Indian Standard Time) timezone for progress updates.
2525
const createdAtTimestamp = new Date().getTime();
26-
const currentHourIST = new Date().getUTCHours() + 5.5; // IST offset is UTC+5:30
27-
const isBefore6amIST = currentHourIST < 6;
28-
const progressDateTimestamp = isBefore6amIST
29-
? new Date().setUTCHours(0, 0, 0, 0) - MILLISECONDS_IN_DAY
30-
: new Date().setUTCHours(0, 0, 0, 0);
26+
const progressDateTimestamp = getProgressDateTimestamp();
3127
if (taskId) {
3228
await assertTaskExists(taskId);
3329
}
@@ -36,7 +32,6 @@ const createProgressDocument = async (progressData) => {
3632
if (!existingDocumentSnapshot.empty) {
3733
throw new Conflict(`${type.charAt(0).toUpperCase() + type.slice(1)} ${PROGRESS_ALREADY_CREATED}`);
3834
}
39-
4035
const progressDocumentData = { ...progressData, createdAt: createdAtTimestamp, date: progressDateTimestamp };
4136
const { id } = await progressesCollection.add(progressDocumentData);
4237
return { id, ...progressDocumentData };

utils/progresses.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,22 @@ const { fetchUser } = require("../models/users");
44
const fireStore = require("../utils/firestore");
55
const {
66
RESPONSE_MESSAGES: { PROGRESS_DOCUMENT_NOT_FOUND },
7+
MILLISECONDS_IN_DAY,
78
} = require("../constants/progresses");
89
const progressesCollection = fireStore.collection("progresses");
910

11+
/**
12+
* Returns the progress date timestamp
13+
*
14+
* @returns {Date} A date object representing the current time in IST Timezone
15+
*/
16+
const getProgressDateTimestamp = () => {
17+
// Currently, we are primarily catering to Indian users for our apps, which is why we have implemented support for the IST (Indian Standard Time) timezone for progress updates.
18+
const currentHourIST = new Date().getUTCHours() + 5.5; // IST offset is UTC+5:30;
19+
const isBefore6amIST = currentHourIST < 6;
20+
return isBefore6amIST ? new Date().setUTCHours(0, 0, 0, 0) - MILLISECONDS_IN_DAY : new Date().setUTCHours(0, 0, 0, 0);
21+
};
22+
1023
/**
1124
* Builds a Firestore query for posting progress documents based on the given parameters.
1225
*
@@ -166,6 +179,7 @@ const getProgressRecords = async (query, queryParams) => {
166179
};
167180

168181
module.exports = {
182+
getProgressDateTimestamp,
169183
buildQueryForPostingProgress,
170184
assertUserExists,
171185
assertTaskExists,

0 commit comments

Comments
 (0)