Skip to content

Commit

Permalink
Whoops duplicated routes were causing an error
Browse files Browse the repository at this point in the history
  • Loading branch information
hadiahmed098 committed Jul 22, 2023
1 parent f302052 commit 72d6d0c
Showing 1 changed file with 0 additions and 86 deletions.
86 changes: 0 additions & 86 deletions api/src/routes/infra.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,90 +270,4 @@ router.post("/fiscal", async(req, res, next) => {
}
});

router.get("/fiscal", async(req, res, next) => {
try {
// first make sure user is actually a treasurer
const [results] = await Models.account.getUserTreasurer(req.context.request_user_id);
if (results.validuser === 0) {
res.status(200).send({});
return next();
}

const [results_1] = await Models.infra.getAllFiscalYears();
res.status(200).send(results_1);
return next();
} catch (err) {
logger.error(err.stack);
res.status(500).send("Internal Server Error");
return next();
}
});

router.post("/fiscal", async(req, res, next) => {
if (req.body.fiscal_year === undefined || req.body.fiscal_year === "") {
res.status(400).send("Must add fiscal year");
return next();
}

// This will technically error out in the year 9999
// But if we make it that far we may have bigger problems
// Otherwise time to enjoy ~~Y2K~~ ~~Y2K38~~ Y9K999
if (req.body.fiscal_year.length != 9) {
res.status(400).send("Fiscal Year must be the form 'XXXX-YYYY'");
return next();
}

if (req.body.fiscal_year[4] != "-") {
res.status(400).send("Fiscal Year must be the form 'XXXX-YYYY'");
return next();
}

const parts = req.body.fiscal_year.split("-");

const first_half = parseInt(parts[0], 10);
const second_half = parseInt(parts[1], 10);

if (isNaN(first_half) || isNaN(second_half)) {
res.status(400).send("Fiscal Year must consist of two numbers");
return next();
}

if (second_half !== (first_half + 1)) {
res.status(400).send("Second number must immediately follow the first number");
return next();
}

const end_of_current_fy = parseInt(current_fiscal_year_string.split("-")[1], 10);

if (first_half < end_of_current_fy) {
res.status(400).send("Fiscal year must start after the current fiscal year");
return next();
}

try {
// first make sure user is actually a treasurer
const [results] = await Models.account.getUserTreasurer(req.context.request_user_id);
if (results.validuser === 0) {
res.status(201).send("Created new fiscal year");
return next();
}

await Models.infra.createNewFiscalYear(req.body.fiscal_year);

const load_status = await fiscal_loader.update();
if (load_status) {
logger.info("Fiscal Year updated");
} else {
logger.error("Fiscal Year update failed");
}

res.status(201).send("Created new fiscal year");
return next();
} catch (err) {
logger.error(err.stack);
res.status(500).send("Internal Server Error");
return next();
}
});

export default router;

0 comments on commit 72d6d0c

Please sign in to comment.