|
|
@@ -3,38 +3,25 @@ import {
|
|
|
Product
|
|
|
} from "../../../models/Product";
|
|
|
import {
|
|
|
+ UpdateProductInput,
|
|
|
UpdateProductResult
|
|
|
} from "./types";
|
|
|
|
|
|
-const updateProduct = async (body: any, context: { userID: string }): Promise<UpdateProductResult> => {
|
|
|
+const updateProduct = async (userID: string, input: UpdateProductInput): Promise<UpdateProductResult> => {
|
|
|
try {
|
|
|
const {
|
|
|
- description,
|
|
|
- isAvailable,
|
|
|
categoryIDs,
|
|
|
productID,
|
|
|
- isActive,
|
|
|
- photos,
|
|
|
- price,
|
|
|
- title
|
|
|
- } = body;
|
|
|
-
|
|
|
- const {
|
|
|
- userID
|
|
|
- } = context;
|
|
|
-
|
|
|
+ ...rest
|
|
|
+ } = input;
|
|
|
+
|
|
|
const updateData: any = {
|
|
|
+ ...rest
|
|
|
};
|
|
|
-
|
|
|
- if (categoryIDs !== undefined) updateData.categoryIDs = Array.isArray(categoryIDs)
|
|
|
- ? categoryIDs
|
|
|
- : [categoryIDs];
|
|
|
- if (title !== undefined) updateData.title = title;
|
|
|
- if (description !== undefined) updateData.description = description;
|
|
|
- if (price !== undefined) updateData.price = price;
|
|
|
- if (photos !== undefined) updateData.photos = photos;
|
|
|
- if (isActive !== undefined) updateData.isActive = isActive;
|
|
|
- if (isAvailable !== undefined) updateData.isAvailable = isAvailable;
|
|
|
+
|
|
|
+ if (categoryIDs !== undefined) {
|
|
|
+ updateData.categoryIDs = Array.isArray(categoryIDs) ? categoryIDs : [categoryIDs];
|
|
|
+ }
|
|
|
|
|
|
const updatedDoc = await Product.findOneAndUpdate(
|
|
|
{
|
|
|
@@ -76,17 +63,8 @@ const updateProduct = async (body: any, context: { userID: string }): Promise<Up
|
|
|
message: "product-updated-successfully",
|
|
|
code: 200,
|
|
|
payload: {
|
|
|
- _id: aggregatedProduct[0]._id,
|
|
|
- categoryIDs: aggregatedProduct[0].categoryIDs.map((cat: any) => cat._id.toString()),
|
|
|
- userID: aggregatedProduct[0].userID,
|
|
|
- title: aggregatedProduct[0].title,
|
|
|
- description: aggregatedProduct[0].description,
|
|
|
- price: aggregatedProduct[0].price,
|
|
|
- photos: aggregatedProduct[0].photos,
|
|
|
- isActive: aggregatedProduct[0].isActive,
|
|
|
- isAvailable: aggregatedProduct[0].isAvailable,
|
|
|
- createdAt: aggregatedProduct[0].createdAt,
|
|
|
- updatedAt: aggregatedProduct[0].updatedAt
|
|
|
+ ...aggregatedProduct[0],
|
|
|
+ categoryIDs: aggregatedProduct[0].categoryIDs.map((cat: any) => cat._id.toString())
|
|
|
}
|
|
|
};
|
|
|
|