index.ts 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import {
  2. Product
  3. } from "../../../models/Product";
  4. import {
  5. AddProductResult
  6. } from "./types";
  7. const addProduct = async (body: any, context: { userID: string }): Promise<AddProductResult> => {
  8. try {
  9. const {
  10. description,
  11. isAvailable,
  12. categoryID,
  13. isActive,
  14. photos,
  15. price,
  16. title
  17. } = body;
  18. const {
  19. userID
  20. } = context;
  21. const normalizedCategoryID = Array.isArray(categoryID)
  22. ? categoryID
  23. : categoryID ? [categoryID] : [];
  24. const newProduct = await Product.create({
  25. categoryID: normalizedCategoryID,
  26. userID: userID,
  27. description,
  28. isAvailable,
  29. isActive,
  30. photos,
  31. title,
  32. price
  33. });
  34. return {
  35. message: "product-created-successfully",
  36. code: 201,
  37. payload: {
  38. product: newProduct
  39. }
  40. };
  41. } catch (error) {
  42. console.error("AddProduct action error:", error);
  43. return {
  44. message: "internal-server-error",
  45. code: 500
  46. };
  47. }
  48. };
  49. export default addProduct;