Skip to content

Commit 029f594

Browse files
committed
created new route for sync
1 parent a4551b1 commit 029f594

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

controllers/userStatus.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,31 @@ const updateUserStatusController = async (req, res, next) => {
240240
}
241241
};
242242

243+
const syncUserStatus = async (req, res, next) => {
244+
try {
245+
await updateAllUserStatus(req, res, next);
246+
const usersData = await getTaskBasedUsersStatus(req, res, next);
247+
248+
if (!usersData?.data?.users || usersData.data.users.length === 0) {
249+
const errorMessage = "Error: Users data is not in the expected format or no users found";
250+
logger.error(errorMessage);
251+
return res.boom.badImplementation(errorMessage);
252+
}
253+
254+
const data = await userStatusModel.batchUpdateUsersStatus(usersData.data.users);
255+
256+
return res.json({
257+
message: "Users status updated successfully.",
258+
data,
259+
});
260+
} catch (error) {
261+
logger.error(error.message);
262+
return res.status(500).json({
263+
message: "The server has encountered an unexpected error. Please contact the administrator for more information.",
264+
});
265+
}
266+
};
267+
243268
module.exports = {
244269
deleteUserStatus,
245270
getUserStatus,
@@ -250,4 +275,5 @@ module.exports = {
250275
getUserStatusControllers,
251276
batchUpdateUsersStatus,
252277
updateUserStatusController,
278+
syncUserStatus,
253279
};

routes/userStatus.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const {
77
batchUpdateUsersStatus,
88
getUserStatusControllers,
99
updateUserStatusController,
10+
syncUserStatus,
1011
} = require("../controllers/userStatus");
1112
const router = express.Router();
1213
const authenticate = require("../middlewares/authenticate");
@@ -24,6 +25,8 @@ const { Services } = require("../constants/bot");
2425
router.get("/", validateGetQueryParams, getUserStatusControllers);
2526
router.get("/self", authenticate, getUserStatus);
2627
router.get("/:userId", getUserStatus);
28+
router.patch("/sync", authenticate, authorizeRoles([SUPERUSER]), syncUserStatus);
29+
2730
router.patch("/self", authenticate, validateUserStatus, updateUserStatusController);
2831
router.patch("/update", authorizeAndAuthenticate([ROLES.SUPERUSER], [Services.CRON_JOB_HANDLER]), updateAllUserStatus);
2932
router.patch(

test/integration/userStatus.test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,10 @@ describe("UserStatus", function () {
275275
});
276276
});
277277

278+
describe("PATCH /users/status/sync", function () {
279+
// TODO: Added test cases here
280+
});
281+
278282
describe("PATCH /users/status/:userid", function () {
279283
let testUserId;
280284
let testUserJwt;

0 commit comments

Comments
 (0)