All files / src/routes/protected/budgeting accounts.ts

94.73% Statements 36/38
70% Branches 7/10
100% Functions 5/5
94.73% Lines 36/38

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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 10390x   1x 1x 1x 1x       1x 4x 4x   4x   4x             1x             1x 16x 16x 16x   16x   16x   16x       16x               1x             1x 1x   1x 1x 1x   1x   1x       1x             1x             1x 1x   1x   1x   1x             1x     1x  
import { AppRouteImplementation } from "@ts-rest/express";
import { contract } from "@cooper/ts-rest/src/contract";
import { authenticate } from "@cooper/backend/src/middleware/authenticate";
import { validateWorkspace } from "@cooper/backend/src/middleware/validateWorkspace";
import { validateAccount } from "@cooper/backend/src/middleware/validateAccount";
import guard from "@cooper/backend/src/middleware/guard";
 
const getAccountsHandler: AppRouteImplementation<
  typeof contract.protected.budgeting.workspaces.byId.accounts.getAccounts
> = async function ({ req, res }) {
  const db = req.app.locals.database;
  const workspaceId = guard(res.workspace).workspaceId;
 
  const accounts = db.budgeting.accounts.getWorkspaceAccounts(workspaceId);
 
  return {
    status: 200,
    body: {
      accounts,
    },
  };
};
export const getAccounts = {
  middleware: [authenticate, validateWorkspace],
  handler: getAccountsHandler,
};
 
const newAccountHandler: AppRouteImplementation<
  typeof contract.protected.budgeting.workspaces.byId.accounts.newAccount
> = async function ({ req, res, body }) {
  const db = req.app.locals.database;
  const createdBy = guard(res.session).username;
  const workspaceId = guard(res.workspace).workspaceId;
 
  const { description, name, bank } = body;
 
  const newAccount = db.budgeting.accounts.createAccount(name, bank, description, workspaceId, createdBy);
 
  Iif (newAccount instanceof Error) {
    throw newAccount;
  }
 
  return {
    status: 200,
    body: {
      message: "Account created successfully",
      account: newAccount,
    },
  };
};
export const newAccount = {
  middleware: [authenticate, validateWorkspace],
  handler: newAccountHandler,
};
 
const updateAccountHandler: AppRouteImplementation<
  typeof contract.protected.budgeting.workspaces.byId.accounts.byId.updateAccount
> = async function ({ req, res, body }) {
  const db = req.app.locals.database;
 
  const accountId = guard(res.account).accountId;
  const createdBy = guard(res.session).username;
  const { description, name, bank } = body;
 
  const updatedAccount = db.budgeting.accounts.updateAccount(accountId, name, bank, description, createdBy);
 
  Iif (updatedAccount instanceof Error) {
    throw updatedAccount;
  }
 
  return {
    status: 200,
    body: {
      message: "Account updated successfully",
    },
  };
};
export const updateAccount = {
  middleware: [authenticate, validateWorkspace, validateAccount],
  handler: updateAccountHandler,
};
 
const deleteAccountHandler: AppRouteImplementation<
  typeof contract.protected.budgeting.workspaces.byId.accounts.byId.deleteAccount
> = async function ({ req, res }) {
  const db = req.app.locals.database;
 
  const accountId = guard(res.account).accountId;
 
  db.budgeting.accounts.deleteAccount(accountId);
 
  return {
    status: 200,
    body: {
      message: "Account deleted successfully",
    },
  };
};
export const deleteAccount = {
  middleware: [authenticate, validateWorkspace, validateAccount],
  handler: deleteAccountHandler,
};