Skip to content

Diya 🔥 (BSemails): Fixed Blue Square assignment Email Hierarchy#2257

Merged
one-community merged 1 commit into
developmentfrom
Diya_Fix_BSEmailHierarchy
Jun 21, 2026
Merged

Diya 🔥 (BSemails): Fixed Blue Square assignment Email Hierarchy#2257
one-community merged 1 commit into
developmentfrom
Diya_Fix_BSEmailHierarchy

Conversation

@DiyaWadhwani

Copy link
Copy Markdown
Contributor

Description

Fixes the weekly auto-reply email hierarchy so that the correct contextual follow-up email is sent to users who receive a blue square on Sunday, and resolves a timezone mismatch that was silently preventing all auto-reply emails from firing.

Related PRs (if any):

None

Main changes explained:

helpers/userHelper.js

  • Fixed critical timezone/date mismatch in weeklyAutoReplyEmailFunction: assignBlueSquareForTimeNotMet runs at midnight LA time (Sunday 12 AM), but stores infringement dates as UTC YYYY-MM-DD strings. In summer (PDT, UTC-7), midnight LA = 7 AM UTC — still Sunday. However the previous code compared against moment().tz('America/Los_Angeles').format('YYYY-MM-DD') (today's date at 4 AM), which is correct, but the infringement date field was written using moment().utc().format('YYYY-MM-DD') at midnight UTC — which in some edge cases resolves to Saturday's date. Replaced todayDate with assignmentDate = moment().tz(COMPANY_TZ).startOf('week').format('YYYY-MM-DD'), which is always the canonical Sunday date regardless of when in Sunday the 4 AM job runs, and always matches what the assignment job wrote.

  • Fixed variable scope crash: Previous version had hasTodayBlueSquare computed before the for loop, referencing user which didn't exist yet — causing a ReferenceError that silently crashed the entire function before processing anyone. Moved it inside the loop.

  • Corrected priority hierarchy in resolveAutoReplyTemplate:

    • Added if (metHours) return null guard at the top to prevent double-emailing users already handled by completeHoursAndMissedSummary (priority 1)
    • Priority 3 (4th blue square) now correctly beats priorities 4–7, regardless of hours bucket — previously excluded near-miss and medium-miss users from the 4th BS email
    • Within priorities 4–7, <1MON_TWO_BLUESQUARE is now checked before <2MON_TWO_BLUESQUARE since numMonths < 1 is a strict subset of numMonths < 2
  • Added metHours && !hasSummary guard in weeklyAutoReplyEmailFunction to skip users already sent a priority-1 email by completeHoursAndMissedSummary, preventing double-emails. Fetch fields weeklySummaries, weeklySummaryOption, and weeklySummaryNotReq added to the DB query to support this check.

  • Replaced all hardcoded 'America/Los_Angeles' timezone strings throughout the file with the COMPANY_TZ constant imported from ../constants/company, ensuring timezone logic is managed from a single source of truth.

startup/userProfileJobs.js

  • No logic changes. Cron schedule is correct as-is:
    • allUserProfileJobs: 0 0 * * 0 — Sunday midnight, assigns blue squares
    • summaryNotSubmittedJobs: 0 4 * * 0 — Sunday 4 AM, sends auto-reply emails (runs after assignment)
    • dailyUserDeactivateJobs: 1 0 * * * — daily 12:01 AM, handles deactivation

How to test:

  1. Check out this branch and run npm install
  2. To test the email hierarchy manually, call weeklyAutoReplyEmailFunction with a targetUserId override:
   userhelper.weeklyAutoReplyEmailFunction({ targetUserId: '<your_test_user_id>', emailOverride: '<your_email>' })
  1. Verify the correct template fires for each scenario by checking the [autoReply] <email> → <templateKey> console log:

    Scenario Expected template
    Logged 25–64.9% of hours, 2+ months on team COMPLETED_HOURS_25%_64.9%
    Logged 65–84.9% of hours COMPLETED_HOURS_65%_84.9%
    Logged 85–99% of hours, fewer than 4 blue squares MISSED_HOURS_BY_<15%
    1st blue square, < 1 month on team <1MON_ONE_BLUESQUARE
    2nd blue square, < 1 month on team <1MON_TWO_BLUESQUARE
    2nd blue square, 1–2 months on team <2MON_TWO_BLUESQUARE
    3rd blue square, < 2 months on team <2MON_THREE_BLUESQUARE
    4th blue square, no time off 4TH_BLUE_SQUARE
    4th blue square, has time off SCHEDULED_TIME_OFF_AND_4TH_BLUE_SQUARE
    Met hours, no summary handled by completeHoursAndMissedSummary, no auto-reply email
  2. Confirm no user receives more than one auto-reply email in a single run.

  3. Confirm users who met their hours and missed their summary only receive the completeHoursAndMissedSummary email, not an additional auto-reply.

Note:

  • The root cause of the silent failure was the date mismatch between UTC infringement storage and LA-timezone date comparison. The fix (startOf('week') in LA time) is semantically more correct regardless — we're always looking for "this week's assignment run" blue square, not "today's" blue square.
  • COMPANY_TZ is confirmed to be 'America/Los_Angeles' in constants/company.js. All timezone references now use this constant.
  • The two old functions (inCompleteHoursEmailFunction, weeklyBlueSquareReminderFunction) remain commented out in userHelper.js for reference during the transition period and can be removed in a follow-up cleanup PR once weeklyAutoReplyEmailFunction is confirmed stable in production.

@one-community one-community changed the title Diya fix(BSemails): Fixed Blue Square assignment Email Hierarchy Diya 🔥 (BSemails): Fixed Blue Square assignment Email Hierarchy Jun 21, 2026
@one-community one-community merged commit 4aa9d4f into development Jun 21, 2026
2 checks passed
@sonarqubecloud

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants