All files / src/middleware validateAccount.ts

85.71% Statements 12/14
72.72% Branches 8/11
100% Functions 2/2
85.71% Lines 12/14

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 3437x 1x   2x 4x       4x   4x       4x     4x 2x           2x           2x 2x    
import { Request, Response, NextFunction } from "express";
import guard from "@cooper/backend/src/middleware/guard";
 
export async function validateAccount(req: Request, res: Response, next: NextFunction) {
  const db = req.app.locals.database;
 
  // parseInt because we don't want to include "truthy" numbers
  // e.g. hello10 = 10 > this should be NaN
  const accountId = parseInt(req.params.accountId, 10);
 
  Iif (Number.isNaN(accountId)) {
    return res.status(401).json({ error: "Unauthorised" });
  }
 
  const account = db.budgeting.accounts.getAccount(accountId);
 
  // Transaction does not exist
  if (account == null) {
    return res.status(404).json({
      error: "Account does not exist",
    });
  }
 
  // Check that account being accessed belongs to the workspace
  Iif (account.workspace !== guard(res.workspace).workspaceId) {
    return res.status(401).json({
      error: "Unauthorised",
    });
  }
 
  res.account = account;
  return next();
}