| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- import {
- Product
- } from "../../../models/Product";
- import {
- AddProductResult
- } from "./types";
- const addProduct = async (body: any, context: { userID: string }): Promise<AddProductResult> => {
- try {
- const {
- description,
- isAvailable,
- categoryID,
- isActive,
- photos,
- price,
- title
- } = body;
- const {
- userID
- } = context;
- const normalizedCategoryID = Array.isArray(categoryID)
- ? categoryID
- : categoryID ? [categoryID] : [];
-
- const newProduct = await Product.create({
- categoryID: normalizedCategoryID,
- userID: userID,
- description,
- isAvailable,
- isActive,
- photos,
- title,
- price
- });
- return {
- message: "product-created-successfully",
- code: 201,
- payload: {
- product: newProduct
- }
- };
- } catch (error) {
- console.error("AddProduct action error:", error);
- return {
- message: "internal-server-error",
- code: 500
- };
- }
- };
- export default addProduct;
|