Skip to content

Commit

Permalink
Added Top-Level Try Catch
Browse files Browse the repository at this point in the history
  • Loading branch information
YashTotale committed Mar 10, 2021
1 parent c51d736 commit 37ff995
Showing 1 changed file with 80 additions and 76 deletions.
156 changes: 80 additions & 76 deletions src/script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ import Logger from "./Helpers/Logger";
import { FIELDS } from "./Utils/constants";

process.on("unhandledRejection", (e) => {
console.error(e);
Logger.error(e);
process.exit(1);
});

process.on("uncaughtException", (e) => {
console.error(e);
Logger.error(e);
process.exit(1);
});

Expand All @@ -41,85 +41,89 @@ const script = async () => {
const dryRun = args["dry-run"] || process.env.DRY_RUN !== "false";

const logger = new Logger(dryRun);
try {
const table = Airtable.base(process.env.AIRTABLE_BASE_ID ?? "");

const standardQuestions = await getStandardQuestions(table);

table("Projects")
.select()
.eachPage(
async (projects, nextPage) => {
for (const project of projects) {
const data = parseProject(project);
const { projectName } = data;

// Checks for empty rows
if (typeof projectName !== "string") continue;

// Log the project's name
Logger.line();
Logger.bold(projectName);

// Make sure the project has all required fields. If not, throw an error.
const checkedData = checkRequiredFields(data);

// Flatten fields that are supposed to be strings (but Airtable returns an array with 1 element)
const flattenedData = flattenFields(checkedData);
const { projectStatus, projectSuccessData } = flattenedData;

// If project is abandoned, skip
if (!(await checkProjectStatus(logger, projectStatus))) continue;

const successData = await getProjectSuccessData(
table,
projectSuccessData
);

// If project is not in use by nonprofit, skip
if (
!(await checkInUse(
successData,
project,
standardQuestions,
dryRun,
logger
))
)
continue;

const reminderNeeded = await checkReminderNeeded(
flattenedData,
logger
);

const table = Airtable.base(process.env.AIRTABLE_BASE_ID ?? "");

const standardQuestions = await getStandardQuestions(table);

table("Projects")
.select()
.eachPage(
async (projects, nextPage) => {
for (const project of projects) {
const data = parseProject(project);
const { projectName } = data;

// Checks for empty rows
if (typeof projectName !== "string") continue;

// Log the project's name
Logger.line();
Logger.bold(projectName);

// Make sure the project has all required fields. If not, throw an error.
const checkedData = checkRequiredFields(data);

// Flatten fields that are supposed to be strings (but Airtable returns an array with 1 element)
const flattenedData = flattenFields(checkedData);
const { projectStatus, projectSuccessData } = flattenedData;

// If project is abandoned, skip
if (!(await checkProjectStatus(logger, projectStatus))) continue;

const successData = await getProjectSuccessData(
table,
projectSuccessData
);
// If reminder not needed, skip
if (reminderNeeded === null) continue;

// If project is not in use by nonprofit, skip
if (
!(await checkInUse(
successData,
await createGoogleForm(
project,
standardQuestions,
flattenedData,
reminderNeeded,
dryRun,
logger
))
)
continue;

const reminderNeeded = await checkReminderNeeded(
flattenedData,
logger
);

// If reminder not needed, skip
if (reminderNeeded === null) continue;

await createGoogleForm(
project,
flattenedData,
reminderNeeded,
dryRun,
logger
);
await sendReminderEmail(
flattenedData,
reminderNeeded,
logger,
dryRun
);

!dryRun &&
(await project.updateFields({
[FIELDS.lastSent]: reminderNeeded,
}));
}

nextPage();
},
() => logger.finish()
);
);
await sendReminderEmail(
flattenedData,
reminderNeeded,
logger,
dryRun
);

!dryRun &&
(await project.updateFields({
[FIELDS.lastSent]: reminderNeeded,
}));
}

nextPage();
},
() => logger.finish()
);
} catch (e) {
logger.error(e.message);
process.exit(1);
}
};

script();

0 comments on commit 37ff995

Please sign in to comment.