index.ts 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. coverPhotoPath,
  11. description,
  12. isAvailable,
  13. categoryID,
  14. isActive,
  15. price,
  16. title
  17. } = body;
  18. const {
  19. userID
  20. } = context;
  21. const newProduct = await Product.create({
  22. coverPhotoPath,
  23. userID: userID,
  24. description,
  25. isAvailable,
  26. categoryID,
  27. isActive,
  28. title,
  29. price
  30. });
  31. return {
  32. message: "product-created-successfully",
  33. code: 201,
  34. payload: {
  35. product: newProduct
  36. }
  37. };
  38. } catch (error) {
  39. console.error("AddProduct action error:", error);
  40. return {
  41. message: "internal-server-error",
  42. code: 500
  43. };
  44. }
  45. };
  46. export default addProduct;