import { Category } from "../../../models/Category"; import { AddCategoryInput, AddCategoryResult } from "./types"; const addCategory = async (userID: string, input: AddCategoryInput): Promise => { try { const { title, index } = input; 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(), isActive: newCategory.isActive, title: newCategory.title, index: newCategory.index } } }; } catch (error) { console.error("CreateCategory error:", error); return { message: "internal-server-error", code: 500 }; } }; export default addCategory;