| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- import {
- Category
- } from "../../../models/Category";
- import {
- AddCategoryInput,
- AddCategoryResult
- } from "./types";
- const addCategory = async (userID: string, input: AddCategoryInput): Promise<AddCategoryResult> => {
- try {
- const {
- title,
- index
- } = input;
- /* const categoryCount = await Category.countDocuments({
- userID
- });
- if (categoryCount >= context.categoryLimit) {
- return {
- message: "category-limit-reached",
- code: 403
- };
- } */
- const existing = await Category.findOne({
- userID,
- index
- });
- if (existing) {
- return {
- message: "index-already-in-use",
- code: 409
- };
- }
- const newCategory = await Category.create({
- userID,
- title,
- index
- });
- return {
- message: "category-created",
- code: 201,
- payload: {
- category: {
- _id: newCategory._id.toString(),
- title: newCategory.title,
- index: newCategory.index
- }
- }
- };
- } catch (error) {
- console.error("CreateCategory error:", error);
- return {
- message: "internal-server-error",
- code: 500
- };
- }
- };
- export default addCategory;
|