All files / src/routes/protected/budgeting categories.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 103 10472x   1x 1x 1x 1x       1x 4x   4x   4x   4x             1x             1x 7x   7x 7x 7x   7x   7x       7x               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 { validateCategory } from "@cooper/backend/src/middleware/validateCategory";
import guard from "@cooper/backend/src/middleware/guard";
 
const getCategoriesHandler: AppRouteImplementation<
  typeof contract.protected.budgeting.workspaces.byId.categories.getCategories
> = async function ({ req, res }) {
  const db = req.app.locals.database;
 
  const workspaceId = guard(res.workspace).workspaceId;
 
  const categories = db.budgeting.categories.getWorkspaceCategories(workspaceId);
 
  return {
    status: 200,
    body: {
      categories,
    },
  };
};
export const getCategories = {
  middleware: [authenticate, validateWorkspace],
  handler: getCategoriesHandler,
};
 
const newCategoryHandler: AppRouteImplementation<
  typeof contract.protected.budgeting.workspaces.byId.categories.newCategory
> = async function ({ req, res, body }) {
  const db = req.app.locals.database;
 
  const workspaceId = guard(res.workspace).workspaceId;
  const createdBy = guard(res.session).username;
  const { name } = body;
 
  const newCategory = db.budgeting.categories.createCategory(name, createdBy, workspaceId);
 
  Iif (newCategory instanceof Error) {
    throw newCategory;
  }
 
  return {
    status: 200,
    body: {
      message: "Category created successfully",
      category: newCategory,
    },
  };
};
export const newCategory = {
  middleware: [authenticate, validateWorkspace],
  handler: newCategoryHandler,
};
 
const updateCategoryHandler: AppRouteImplementation<
  typeof contract.protected.budgeting.workspaces.byId.categories.byId.updateCategory
> = async function ({ req, res, body }) {
  const db = req.app.locals.database;
 
  const workspaceId = guard(res.workspace).workspaceId;
  const { categoryId, createdBy } = guard(res.category);
  const { name } = body;
 
  const updatedCategory = db.budgeting.categories.updateCategory(categoryId, workspaceId, name, createdBy);
 
  Iif (updatedCategory instanceof Error) {
    throw updatedCategory;
  }
 
  return {
    status: 200,
    body: {
      message: "Category updated successfully",
    },
  };
};
export const updateCategory = {
  middleware: [authenticate, validateWorkspace, validateCategory],
  handler: updateCategoryHandler,
};
 
const deleteCategoryHandler: AppRouteImplementation<
  typeof contract.protected.budgeting.workspaces.byId.categories.byId.deleteCategory
> = async function ({ req, res }) {
  const db = req.app.locals.database;
 
  const categoryId = guard(res.category).categoryId;
 
  db.budgeting.categories.deleteCategory(categoryId);
 
  return {
    status: 200,
    body: {
      message: "Category deleted successfully",
    },
  };
};
export const deleteCategory = {
  middleware: [authenticate, validateWorkspace, validateCategory],
  handler: deleteCategoryHandler,
};