index.ts 893 B

12345678910111213141516171819202122232425262728293031323334353637
  1. import {
  2. Category
  3. } from "../../../models/Category";
  4. import {
  5. GetCategoriesResult
  6. } from "./types";
  7. const getCategories = async (userID: string): Promise<GetCategoriesResult> => {
  8. try {
  9. const categories = await Category.find({
  10. userID
  11. }).sort({
  12. index: 1
  13. });
  14. return {
  15. message: "categories-retrieved",
  16. code: 200,
  17. payload: {
  18. categories: categories.map(c => ({
  19. _id: c._id.toString(),
  20. isActive: c.isActive,
  21. title: c.title,
  22. index: c.index
  23. }))
  24. }
  25. };
  26. } catch (error) {
  27. console.error("GetCategories error:", error);
  28. return {
  29. message: "internal-server-error",
  30. code: 500
  31. };
  32. }
  33. };
  34. export default getCategories;