Skip to content

Commit

Permalink
refactor qusrterly form generation files
Browse files Browse the repository at this point in the history
  • Loading branch information
karla-vm committed Jan 23, 2024
1 parent d07b678 commit 308c515
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 32 deletions.
30 changes: 30 additions & 0 deletions services/app-api/handlers/forms/post/generateQuarterForms.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,40 @@ export const main = handler(async (event, context) => {

// verify whether there is a user logged in
const currentUser = await getUserCredentialsFromJwt(event);
console.log("this is the current user", currentUser);
if (!currentUser) {
throw new Error("No authorized user.");
}

// TODO: parameters here
const result = await generateQuarterlyForms(event);
const { failureList, specifiedQuarter, specifiedYear } = result;

if (failureList.length > 0) {
return {
status: 500,
message: `Failed to write all entries to database.`,
};
}

return {
status: 200,
message: `Forms successfully created for Quarter ${specifiedQuarter} of ${specifiedYear}`,
};
});

/**
* Generates initial form data and statuses for all states given a year and quarter
* NOTE: This function is triggered on a schedule by Cron
*/
export const scheduledJob = handler(async (event, context) => {
// *** if this invocation is a pre-warm, do nothing and return
if (event.source === "serverless-plugin-warmup") {
console.log("Warmed up!");
return null;
}

// TODO: parameters here
const result = await generateQuarterlyForms(event);
const { failureList, specifiedQuarter, specifiedYear } = result;

Expand Down
30 changes: 0 additions & 30 deletions services/app-api/handlers/forms/post/generateQuarterFormsCron.js

This file was deleted.

9 changes: 9 additions & 0 deletions services/app-api/handlers/shared/quarterlyForms.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import dynamoDb from "../../libs/dynamodb-lib";
import {
findExistingStateForms,
getStatesList,
getFormDescriptions,
getQuestionsByYear,
fetchOrCreateQuestions,
getAnswersSet,
} from "../../../app-api/handlers/shared/sharedFunctions";

/**
* Generates initial form data and statuses for all states given a year and quarter
Expand Down Expand Up @@ -90,6 +98,7 @@ export async function generateQuarterlyForms(event) {
let specifiedQuarter;
let restoreMissingAnswers = false;

// TODO: pass the quarter, year, and restore missing answer as a parameter from the handler
// If a data object is sent use those values.
if (event.body && event.body !== "undefined") {
let data =
Expand Down
4 changes: 2 additions & 2 deletions services/app-api/serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -514,8 +514,8 @@ functions:
method: post
cors: true
authorizer: ${self:custom.authValue.${self:custom.stage}, ""}
generateQuarterFormsCron:
handler: handlers/forms/post/generateQuarterFormsCron.main
generateQuarterFormsScheduledJob:
handler: handlers/forms/post/generateQuarterForms.scheduledJob
role: LambdaApiRole
events:
- schedule:
Expand Down

0 comments on commit 308c515

Please sign in to comment.