MCPcopy Create free account
hub / github.com/RealDevSquad/website-backend / updateCurrentStatusToState

Function updateCurrentStatusToState

utils/userStatus.js:253–300  ·  view source on GitHub ↗
(collection, latestStatusData, newState)

Source from the content-addressed store, hash-verified

251 * @throws {Error} If there is an error while updating the status
252 */
253const updateCurrentStatusToState = async (collection, latestStatusData, newState) => {
254 const {
255 id,
256 data: { currentStatus = {}, ...docData },
257 } = latestStatusData;
258 const previousState = currentStatus.state;
259 const previousUntil = currentStatus.until;
260 const currentTimeStamp = new Date().getTime();
261 const updatedStatusData = {
262 ...docData,
263 currentStatus: {
264 state: newState,
265 message: "",
266 from: currentTimeStamp,
267 until: "",
268 updatedAt: currentTimeStamp,
269 },
270 };
271 const lastOooUntilUpdate = resolveLastOooUntil({
272 previousState,
273 previousUntil,
274 nextState: newState,
275 fallbackTimestamp: currentTimeStamp,
276 });
277 if (lastOooUntilUpdate !== undefined) {
278 updatedStatusData.lastOooUntil = lastOooUntilUpdate;
279 }
280 if (newState === userState.IDLE && previousState === userState.ACTIVE) {
281 updatedStatusData.idleFrom = currentTimeStamp;
282 } else if (newState === userState.ACTIVE) {
283 updatedStatusData.idleFrom = null;
284 }
285 try {
286 await collection.doc(id).update(updatedStatusData);
287 } catch (err) {
288 logger.error(`error updating status for user id ${docData.userId} - ${err.message}`);
289 throw new Error(`error updating the current status.`);
290 }
291
292 return {
293 status: "success",
294 message: `The status has been updated to ${newState}`,
295 data: {
296 previousStatus: previousState,
297 currentStatus: newState,
298 },
299 };
300};
301
302/**
303 * Returns the Response for the Future Status Update to Active State

Calls 1

resolveLastOooUntilFunction · 0.85

Tested by

no test coverage detected