Skip to content

Commit

Permalink
Generate monthly average for small group attendance report
Browse files Browse the repository at this point in the history
  • Loading branch information
kakoma committed Jul 17, 2023
1 parent 3fc2086 commit a751b65
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
17 changes: 8 additions & 9 deletions src/reports/reports.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
ApiResponse,
ReportSubmissionsApiResponse,
} from "./types/report-api.types";
import { getFormattedDateString } from "src/utils/stringHelpers";

@UseInterceptors(SentryInterceptor)
@UseGuards(JwtAuthGuard)
Expand Down Expand Up @@ -120,10 +121,8 @@ export class ReportsController {
currentSunday.setDate(startOfMonth.getDate() + (7 - startOfMonth.getDay()));

while (currentSunday <= endOfMonth) {
const sundayKey = currentSunday
.toISOString()
.slice(0, 10)
.replace(/-/g, "");
const sundayKey = getFormattedDateString(currentSunday);

const sundayLabel = currentSunday.toLocaleDateString("en-US", {
year: "numeric",
month: "short",
Expand All @@ -139,10 +138,10 @@ export class ReportsController {

// Process submissions and organize the data by smallGroupName and Sunday
submissions.data.forEach((submission: any) => {
const smallGroupName = submission.smallGroupName; // @TODO Change smallGroupName to smallGroupName
const smallGroupNumberOfMembers = submission.smallGroupNumberOfMembers; // @TODO change to smallGroupNumberOfMembers
const mcParticipantCount = parseInt(submission.smallGroupAttendanceCount); // @TODO change to smallGroupAttendanceCount. What differs from church to church is the labels
const submittedAt = submission.submittedAt.toISOString(); // @TODO Group by submittedAt or by the date of the meeting? I think the latter
const smallGroupName = submission.smallGroupName;
const smallGroupNumberOfMembers = submission.smallGroupNumberOfMembers;
const mcParticipantCount = parseInt(submission.smallGroupAttendanceCount);
const submittedAt = submission.date; // Group by the date of the meeting
const submittedDate = new Date(submittedAt);

// Find the next Sunday date after the submitted date
Expand All @@ -152,7 +151,7 @@ export class ReportsController {
);

// Generate the key in the format "YYYYMMDD" for the Sunday date
const sundayKey = nextSunday.toISOString().slice(0, 10).replace(/-/g, "");
const sundayKey = getFormattedDateString(nextSunday);

// Create the mcData object if it doesn't exist
if (!mcDataMap[smallGroupName]) {
Expand Down
12 changes: 12 additions & 0 deletions src/utils/stringHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,15 @@ export function generateRandomPassword(length) {

return password;
}

/**
* Generate the current date in the YYYYMMDD format
* @param currentDate Date
* @returns string Date in the YYYYMMDD format
*/
export function getFormattedDateString(currentDate: Date) {
const year = currentDate.getFullYear();
const month = String(currentDate.getMonth() + 1).padStart(2, "0");
const day = String(currentDate.getDate()).padStart(2, "0");
return `${year}${month}${day}`;
}

0 comments on commit a751b65

Please sign in to comment.