Back to app-status-reports

SalesCandy Git archive

app-status-reports

Turns Presence events into daily salesperson availability timelines for Manager Portal.

README

This repository generates App Status Reports by observing Presence events. It handles the report generation and serving process for salespersons.

The service uses Presence data to create reports. Inputs are Presence events, and the output is the generated App Status Report.

Design study

Single responsibility

Give report compaction its own function

The report handler assembles the results, then calls simplifyData on each record. That helper merges neighbouring intervals with the same status, so the rules for compacting a report can change in one place.

Code from app/getAppStatusReport.js
function simplifyData(rec) {    let arr = rec.data.split('|')    let res = [];    let prev = null;    for (let i = 0; i < arr.length; i++){        let pair = arr[i].split(':');        if (prev == null) {            prev = pair;            continue;        }        if (parseInt(pair[0]) > 0) {            if (prev[1]===pair[1]) {                prev[0] = parseInt(prev[0])+ parseInt(pair[0]);                continue;            } else {                res.push(prev.join(':'));                prev = pair;            }        }    }    res.push(prev.join(':'));    rec.data = res.join('|');    return rec;}

View the introducing commit 506d4506c741

Reconstructed issues

Summaries inferred from the archive. Original issue records are unavailable here.

Report boundaries use user timezone

Previously, reports might incorrectly use the tenant's timezone for defining report boundaries. This change ensures the report generation correctly uses the requesting user's timezone, improving accuracy for individual users.

570157282daf

Git diff
Git diff excerpt from app/lib/candyBaseDb.js
      const dt = args.date ? moment(args.date) : moment();-    const tz = tenantData.timezone || 'UTC';+    const tz = perms.userTZ || 'UTC';     const date = dt.clone().tz(tz);     const local_date = date.format("YYYY-MM-DD");     const local_prevDate = dt.clone().tz(tz)         .subtract(1, 'days')         .format("YYYY-MM-DD");      const users = await getUsersByProjects(perms, args);      await replicaDb.end();

Fix validation error with lodash.isEmpty()

The system encountered a validation error when calling lodash.isEmpty() on a number type. This update corrects the logic to properly handle numeric inputs, preventing report generation failures.

20c11dce0464

Git diff
Git diff excerpt from app/updater.js
 }  function valid(message) {     return (         _.isEmpty(message)-        || _.isEmpty(message.tenantId)-        || _.isEmpty(message.userId)-        || _.isEmpty(message.timestamp)-        || _.isEmpty(message.status)+        || _.isEmpty(String(message.tenantId))+        || _.isEmpty(String(message.userId))+        || _.isEmpty(String(message.timestamp))+        || _.isEmpty(String(message.status))     ) === false; }

Use Secrets Manager for secrets

Secrets handling was updated to utilize the Secrets Manager service. This improves the security posture by centralizing and managing sensitive credentials used by the reporting service.

2b5047bfbcf3

Selected commits

  1. Update dependency aws-sdk to v2.969.0

    Renovate Bot ·

    ea41cd9bad157cb4064f013f9c1eb58ccf576b50

  2. Reduce logs

    Reza Rosli ·

    b75c61a9bf83b8e382d092099a48f07f934f4af1

    Code excerpt · 9 lines
    Git diff excerpt from app/updater.js
          const messages = event.Records.map(rec => {             const body = JSON.parse(rec.body);-            const msg = JSON.parse(body.Message);-            return msg;+            return JSON.parse(body.Message);         }     ); 
  3. Reduce logs

    Reza Rosli ·

    b4d7ca7ea9c69cbf3c401d0e7b00334fcb41b2ed

  4. Pin dependencies

    Renovate Bot ·

    5d7489c2747bdad1ada74504aadc62554af2f40a

  5. Update dependency serverless-pseudo-parameters to v2.6.0

    Renovate Bot ·

    46591fa5898ba347612aac2b7cd1a32032b2f31f

  6. Update dependency dotenv to v8.6.0

    Renovate Bot ·

    e3c70d70a951a39781eaa5787cc96508ace537fa

  7. Update dependency dotenv to v10

    Renovate Bot ·

    6825c652302660f8dfe08a0db7958313f1766a8b

  8. Use SSL option from secret

    Reza Rosli ·

    e181dd790f8067a5e1127dbaebc638332236b993

  9. Add renovate.json

    Renovate Bot ·

    355a135e4cac026f7c14562073465c3ca823ea16

  10. Fix validation error when using lodash.isEmpty() on a number -- it doesn't work with numbers!

    Reza Rosli ·

    20c11dce04641cc17f6a1a70ec84b3464069014d

    Code excerpt · 15 lines
    Git diff excerpt from app/updater.js
     }  function valid(message) {     return (         _.isEmpty(message)-        || _.isEmpty(message.tenantId)-        || _.isEmpty(message.userId)-        || _.isEmpty(message.timestamp)-        || _.isEmpty(message.status)+        || _.isEmpty(String(message.tenantId))+        || _.isEmpty(String(message.userId))+        || _.isEmpty(String(message.timestamp))+        || _.isEmpty(String(message.status))     ) === false; }
  11. Get the reports based on the user's timezone, not tenant timezone

    Reza Rosli ·

    570157282daf7883ba561c56a8e4fd0332466f5e

    Code excerpt · 13 lines
    Git diff excerpt from app/lib/candyBaseDb.js
          const dt = args.date ? moment(args.date) : moment();-    const tz = tenantData.timezone || 'UTC';+    const tz = perms.userTZ || 'UTC';     const date = dt.clone().tz(tz);     const local_date = date.format("YYYY-MM-DD");     const local_prevDate = dt.clone().tz(tz)         .subtract(1, 'days')         .format("YYYY-MM-DD");      const users = await getUsersByProjects(perms, args);      await replicaDb.end();
  12. Remove unnecessary permission

    Reza Rosli ·

    7a4edf4c384a1e009f23af19987147e7e196fca4

  13. Use Secrets Manager for secrets

    Reza Rosli ·

    2b5047bfbcf3d1fb1c43d26b75dab6c8712cc034

  14. Update packages and prune permissions

    Reza Rosli ·

    f97f73fff831c29515a10f73f7106ada621ef604

  15. Performance tweaks

    Reza Rosli ·

    c28e3d33179983546759ed71bc98b420c0475804

    Code excerpt · 16 lines
    Git diff excerpt from app/lib/candyBaseDb.js
             values: [tenantId]     }).catch(err => {         throw new Error(err.message);     }); +    await replicaDb.end();+     let result = null;      if (data.length > 0) {          const routingHours = {};         data.forEach(i => {             routingHours[lib.getWeekdayNumber(i.weekDay)] = {                 weekDayNum: lib.getWeekdayNumber(i.weekDay),                 weekDay: i.weekDay,
  16. Release/v3.1.5 - Housekeeping

    Reza Rosli ·

    10909423c80e36b7adf5ebfb81e88f0b7e990d5a

    Code excerpt · 16 lines
    Git diff excerpt from app/lib/candyBaseDb.js
             values: [tenantId]     }).catch(err => {         throw new Error(err.message);     }); +    await replicaDb.end();+     let result = null;      if (data.length > 0) {          const routingHours = {};         data.forEach(i => {             routingHours[lib.getWeekdayNumber(i.weekDay)] = {                 weekDayNum: lib.getWeekdayNumber(i.weekDay),                 weekDay: i.weekDay,
  17. Major refactoring

    Reza Rosli ·

    78686670d0a3df3605511ef92beb802dd8e0dc84

    Code excerpt · 27 lines
    Git diff excerpt from app/getAppStatusReport.js
    +        let pair = arr[i].split(':');++        if (prev == null) {+            prev = pair;+            continue;+        }++        if (parseInt(pair[0]) > 0) {+            if (prev[1]===pair[1]) {+                prev[0] = parseInt(prev[0]) + parseInt(pair[0]);+                continue;+            } else {+                res.push(prev.join(':'));+                prev = pair;+            }+        }     }+    res.push(prev.join(':'));+    rec.data = res.join('|');+    return rec;+} -    return records;-}; -function getRecord(tenantId, date)-{
  18. Return existing data updated to the minute, and refactor

    Reza Rosli ·

    ea7631975aec8658fd3a29fd612bc2a7f3fbcd00

  19. Remove status with 0 duration and join same status into contiguous blocks

    Reza Rosli ·

    506d4506c7417cba16a8355343fd4293781c5f84

  20. fix overwriting issue

    Shi Yek Lim ·

    14fb701fe9597aba081f5c40410bc92e0f9da77d

  21. fix 0 min status

    Shi Yek Lim ·

    fd1cbbf1e727823f90076dc1ea0c848ff841643c

  22. remake scheduler

    Shi Yek Lim ·

    5a853f62f50ed14249471e964fa8358d04be3ca9

  23. base for scheduler

    Shi Yek Lim ·

    c921475269b56cd62ce61b16ae6d15a8ee921d49

    Code excerpt · 24 lines
    Git diff excerpt from app/generator.js
    +        userIds.forEach(function(element) {+            data.push({+                PutRequest: {+                    Item: {+                        userId: element,+                        local_date: start,+                        modified: end+                    }+                }+            });+        });++        var params = {+            RequestItems: {+                [process.env.dynamodbTable]: data+            }+        };+        +        ddb.batchWrite(params, function(err, data) {+            if (err) console.log(err);+            else console.log(data);+        });+    }+}
  24. generator base

    Shi Yek Lim ·

    5e2601f12526d1e815d7a3764a6caa9dd3139694

    Code excerpt · 24 lines
    Git diff excerpt from app/generator.js
    - */-function createRecords(userIds)-{-    if (userIds !== undefined && userIds.length > 0) {-        var data = [];--        userIds.forEach(function(element) {-            data.push({-                PutRequest: {-                    Item: {-                        userId: element,-                        local_date: start,-                        modified: end-                    }-                }-            });-        }); -        var params = {-            RequestItems: {-                [process.env.dynamodbTable]: data-            }-        };-        
  25. base

    Shi Yek Lim ·

    f59216910455515eb0a8295e0035963fd8e6105d

    Code excerpt · 5 lines
    Git diff excerpt from app/generator.js
    +    });++    console.log(JSON.stringify(event));+    callback(null,true);+};
  26. Initial commit

    Reza Rosli ·

    156da169f3d44e65fe3243b53c4828407ab4c79d