|
| 1 | +import { Router } from 'express'; |
| 2 | +import RateLimit from 'express-rate-limit'; |
| 3 | +import getConfig from 'next/config'; |
| 4 | +import getAuthRole from '../../utils/getAuthRole'; |
| 5 | +import { performQuery } from './graphql'; |
| 6 | +import handleEmailNotification from './emails/handleEmailNotification'; |
| 7 | +import notifyCommunityReportDue from './emails/templates/notifyCommunityReportDue'; |
| 8 | +import validateKeycloakToken from './keycloakValidate'; |
| 9 | + |
| 10 | +const limiter = RateLimit({ |
| 11 | + windowMs: 1 * 60 * 1000, |
| 12 | + max: 30, |
| 13 | +}); |
| 14 | + |
| 15 | +const communityReportDueDate = Router(); |
| 16 | + |
| 17 | +function getNextQuarterStartDate(today: Date): Date { |
| 18 | + const currentYear = today.getFullYear(); |
| 19 | + // Define quarter start months: March, June, September, December |
| 20 | + const quarterStartMonths = [2, 5, 8, 11]; // 0-based: 2=March, 5=June, 8=September, 11=December |
| 21 | + |
| 22 | + const currentQuarterStartMonth = quarterStartMonths.find((month) => { |
| 23 | + const quarterStartDate = new Date(currentYear, month, 1, 0, 0, 0, 0); |
| 24 | + return quarterStartDate > today; |
| 25 | + }); |
| 26 | + |
| 27 | + if (currentQuarterStartMonth !== undefined) { |
| 28 | + return new Date(currentYear, currentQuarterStartMonth, 1, 0, 0, 0, 0); |
| 29 | + } |
| 30 | + // If no quarter start date is after today in the current year, return March 1 of next year |
| 31 | + return new Date(currentYear + 1, 2, 1, 0, 0, 0, 0); // 2=March |
| 32 | +} |
| 33 | + |
| 34 | +const processCommunityReportsDueDates = async (req, res) => { |
| 35 | + const runtimeConfig = getConfig()?.publicRuntimeConfig ?? {}; |
| 36 | + const isEnabledTimeMachine = runtimeConfig.ENABLE_MOCK_TIME; |
| 37 | + // GraphQL query to get all milestones with archivedAt: null |
| 38 | + const sowCommunityProgressQuery = ` |
| 39 | + query MilestoneDatesQuery { |
| 40 | + allApplicationSowData( |
| 41 | + orderBy: AMENDMENT_NUMBER_DESC |
| 42 | + filter: {archivedAt: {isNull: true}} |
| 43 | + ) { |
| 44 | + nodes { |
| 45 | + applicationId |
| 46 | + applicationByApplicationId { |
| 47 | + ccbcNumber |
| 48 | + organizationName |
| 49 | + projectName |
| 50 | + } |
| 51 | + jsonData |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + `; |
| 56 | + let result; |
| 57 | + const applicationRowIdsVisited = new Set(); |
| 58 | + |
| 59 | + try { |
| 60 | + result = await performQuery(sowCommunityProgressQuery, {}, req); |
| 61 | + } catch (error) { |
| 62 | + return res.status(500).json({ error: result.error }).end(); |
| 63 | + } |
| 64 | + let today = null; |
| 65 | + if (isEnabledTimeMachine) { |
| 66 | + const mockedDate = req.cookies['mocks.mocked_date']; |
| 67 | + today = mockedDate ? new Date(mockedDate) : new Date(); |
| 68 | + today.setUTCHours(0, 0, 0, 0); |
| 69 | + } else { |
| 70 | + today = new Date(); |
| 71 | + today.setUTCHours(0, 0, 0, 0); |
| 72 | + } |
| 73 | + const nextQuarterDate = getNextQuarterStartDate(today); |
| 74 | + |
| 75 | + // Function to check if a given due date string is within 30 to 31 days from today. |
| 76 | + const isWithin30To31Days = (dueDate: Date) => { |
| 77 | + const timeDiff = dueDate.getTime() - today.getTime(); |
| 78 | + const daysDiff = Math.round(timeDiff / (1000 * 3600 * 24)); |
| 79 | + return daysDiff === 30; |
| 80 | + }; |
| 81 | + |
| 82 | + // Traverse the result, if there is a milestone due date within 30 to 31 days from today, |
| 83 | + // add the application row ID, CCBC number, and whether it is a milestone 1 or 2 to a list. |
| 84 | + const communityReportData = result.data.allApplicationSowData.nodes.reduce( |
| 85 | + (acc, node) => { |
| 86 | + const { applicationId, applicationByApplicationId, jsonData } = node; |
| 87 | + if (applicationRowIdsVisited.has(applicationId)) { |
| 88 | + return acc; |
| 89 | + } |
| 90 | + const { ccbcNumber, organizationName, projectName } = |
| 91 | + applicationByApplicationId; |
| 92 | + |
| 93 | + const projectStartDateString = jsonData.projectStartDate; |
| 94 | + |
| 95 | + const projectStartDate = new Date(projectStartDateString); |
| 96 | + |
| 97 | + if (today.getTime() > projectStartDate.getTime()) { |
| 98 | + if (isWithin30To31Days(nextQuarterDate)) { |
| 99 | + const applicationRowId = applicationId; |
| 100 | + if (!applicationRowIdsVisited.has(applicationRowId)) { |
| 101 | + acc.push({ |
| 102 | + applicationRowId, |
| 103 | + ccbcNumber, |
| 104 | + organizationName, |
| 105 | + projectName, |
| 106 | + dueDate: nextQuarterDate.toLocaleDateString(), |
| 107 | + }); |
| 108 | + applicationRowIdsVisited.add(applicationRowId); |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + return acc; |
| 113 | + }, |
| 114 | + [] |
| 115 | + ); |
| 116 | + |
| 117 | + if (communityReportData.length > 0) { |
| 118 | + // Send an email to the analyst with the list of applications that have milestones due within 30 to 31 days. |
| 119 | + return handleEmailNotification( |
| 120 | + req, |
| 121 | + res, |
| 122 | + notifyCommunityReportDue, |
| 123 | + { communityReportData }, |
| 124 | + true |
| 125 | + ); |
| 126 | + } |
| 127 | + |
| 128 | + return res |
| 129 | + .status(200) |
| 130 | + .json({ message: 'No community progress reports due in 30 days' }) |
| 131 | + .end(); |
| 132 | +}; |
| 133 | + |
| 134 | +communityReportDueDate.get( |
| 135 | + '/api/analyst/community/upcoming', |
| 136 | + limiter, |
| 137 | + (req, res) => { |
| 138 | + const authRole = getAuthRole(req); |
| 139 | + const isRoleAuthorized = |
| 140 | + authRole?.pgRole === 'ccbc_admin' || authRole?.pgRole === 'super_admin'; |
| 141 | + |
| 142 | + if (!isRoleAuthorized) { |
| 143 | + return res.status(404).end(); |
| 144 | + } |
| 145 | + return processCommunityReportsDueDates(req, res); |
| 146 | + } |
| 147 | +); |
| 148 | + |
| 149 | +communityReportDueDate.get( |
| 150 | + '/api/analyst/cron-community', |
| 151 | + limiter, |
| 152 | + validateKeycloakToken, |
| 153 | + (req, res) => { |
| 154 | + req.claims.identity_provider = 'serviceaccount'; |
| 155 | + processCommunityReportsDueDates(req, res); |
| 156 | + } |
| 157 | +); |
| 158 | + |
| 159 | +export default communityReportDueDate; |
0 commit comments