Skip to content

Commit c55fea9

Browse files
committed
Merge branch 'develop' of https://github.com/ravikumar1002/website-backend into feat/github-created-at
2 parents 2e651f2 + 2721818 commit c55fea9

File tree

3 files changed

+45
-5
lines changed

3 files changed

+45
-5
lines changed

controllers/userStatus.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,10 @@ const updateUserStatus = async (req, res) => {
135135
*/
136136
const updateAllUserStatus = async (req, res) => {
137137
try {
138-
await userStatusModel.updateAllUserStatus();
138+
const data = await userStatusModel.updateAllUserStatus();
139139
return res.status(200).json({
140140
message: "All User Status updated successfully.",
141+
data,
141142
});
142143
} catch (err) {
143144
logger.error(`Error while updating the User Data: ${err}`);

models/userStatus.js

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,16 @@ const updateUserStatus = async (userId, newStatusData) => {
157157
*/
158158

159159
const updateAllUserStatus = async () => {
160+
const summary = {
161+
usersCount: 0,
162+
oooUsersAltered: 0,
163+
oooUsersUnaltered: 0,
164+
nonOooUsersAltered: 0,
165+
nonOooUsersUnaltered: 0,
166+
};
160167
try {
161168
const userStatusDocs = await userStatusModel.where("futureStatus.state", "in", ["ACTIVE", "IDLE", "OOO"]).get();
169+
summary.usersCount = userStatusDocs._size;
162170
const batch = firestore.batch();
163171
const today = new Date().getTime();
164172
userStatusDocs.forEach(async (document) => {
@@ -170,15 +178,23 @@ const updateAllUserStatus = async () => {
170178
const { state: futureState } = futureStatus;
171179
if (futureState === "ACTIVE" || futureState === "IDLE") {
172180
if (today >= futureStatus.from) {
181+
// OOO period is over and we need to update their current status
173182
newStatusData.currentStatus = { ...futureStatus, until: "", updatedAt: today };
174-
newStatusData.futureStatus = {};
183+
delete newStatusData.futureStatus;
175184
toUpdate = !toUpdate;
185+
summary.oooUsersAltered++;
186+
} else {
187+
summary.oooUsersUnaltered++;
176188
}
177189
} else {
190+
// futureState is OOO
178191
if (today > futureStatus.until) {
179-
newStatusData.futureStatus = {};
192+
// the OOO period is over
193+
delete newStatusData.futureStatus;
180194
toUpdate = !toUpdate;
195+
summary.nonOooUsersAltered++;
181196
} else if (today <= doc.futureStatus.until && today >= doc.futureStatus.from) {
197+
// the current date i.e today lies in between the from and until so we need to swap the status
182198
let newCurrentStatus = {};
183199
let newFutureStatus = {};
184200
newCurrentStatus = { ...futureStatus, updatedAt: today };
@@ -188,6 +204,9 @@ const updateAllUserStatus = async () => {
188204
newStatusData.currentStatus = newCurrentStatus;
189205
newStatusData.futureStatus = newFutureStatus;
190206
toUpdate = !toUpdate;
207+
summary.nonOooUsersAltered++;
208+
} else {
209+
summary.nonOooUsersUnaltered++;
191210
}
192211
}
193212
if (toUpdate) {
@@ -200,13 +219,12 @@ const updateAllUserStatus = async () => {
200219
);
201220
}
202221
await batch.commit();
203-
return { status: 204, message: "User Status updated Successfully." };
222+
return summary;
204223
} catch (error) {
205224
logger.error(`error in updating User Status Documents ${error}`);
206225
return { status: 500, message: "User Status couldn't be updated Successfully." };
207226
}
208227
};
209-
210228
/**
211229
* Updates the user status based on a new task assignment.
212230
* @param {string} userId - The ID of the user.

test/integration/userStatus.test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,13 @@ describe("UserStatus", function () {
175175
.send();
176176
expect(response3).to.have.status(200);
177177
expect(response3.body.message).to.equal("All User Status updated successfully.");
178+
expect(response3.body.data).to.deep.equal({
179+
usersCount: 1,
180+
oooUsersAltered: 0,
181+
oooUsersUnaltered: 0,
182+
nonOooUsersAltered: 1,
183+
nonOooUsersUnaltered: 0,
184+
});
178185

179186
// Checking the current status
180187
const response4 = await chai.request(app).get(`/users/status/self`).set("Cookie", `${cookieName}=${testUserJwt}`);
@@ -195,6 +202,13 @@ describe("UserStatus", function () {
195202
.send();
196203
expect(response5).to.have.status(200);
197204
expect(response5.body.message).to.equal("All User Status updated successfully.");
205+
expect(response5.body.data).to.deep.equal({
206+
usersCount: 1,
207+
oooUsersAltered: 1,
208+
oooUsersUnaltered: 0,
209+
nonOooUsersAltered: 0,
210+
nonOooUsersUnaltered: 0,
211+
});
198212

199213
const response6 = await chai.request(app).get(`/users/status/self`).set("Cookie", `${cookieName}=${testUserJwt}`);
200214
expect(response6).to.have.status(200);
@@ -236,6 +250,13 @@ describe("UserStatus", function () {
236250
.send();
237251
expect(response3).to.have.status(200);
238252
expect(response3.body.message).to.equal("All User Status updated successfully.");
253+
expect(response3.body.data).to.deep.equal({
254+
usersCount: 1,
255+
oooUsersAltered: 0,
256+
oooUsersUnaltered: 0,
257+
nonOooUsersAltered: 1,
258+
nonOooUsersUnaltered: 0,
259+
});
239260

240261
// Checking the current status
241262
const response4 = await chai.request(app).get(`/users/status/self`).set("Cookie", `${cookieName}=${testUserJwt}`);

0 commit comments

Comments
 (0)