Merge branch 'master' of http://128.199.30.231/possibillion/smellika-api
This commit is contained in:
commit
9aa54da47d
@ -4,59 +4,123 @@ import { v4 as uuidv4 } from "uuid";
|
|||||||
import { CategoryModel } from "../Category/CategoryModel.js";
|
import { CategoryModel } from "../Category/CategoryModel.js";
|
||||||
export const createProduct = async (req, res) => {
|
export const createProduct = async (req, res) => {
|
||||||
try {
|
try {
|
||||||
if (!req.files) {
|
let findProduct = "";
|
||||||
return res.status(400).json({
|
let product = { _id: "" };
|
||||||
msg: " PLease Provide Product image",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
let images = [];
|
|
||||||
let Allfiles = req.files.image;
|
|
||||||
if (typeof Allfiles.tempFilePath === "string") {
|
|
||||||
let filepath = Allfiles.tempFilePath;
|
|
||||||
|
|
||||||
images.push(filepath);
|
if (req.body?.product_id) {
|
||||||
|
findProduct = await Product.findById(req.body.product_id);
|
||||||
|
}
|
||||||
|
const name = req.body?.name;
|
||||||
|
if (!findProduct) {
|
||||||
|
const data = await Product.findOne({
|
||||||
|
name: { $regex: new RegExp(`^${name}$`, "ig") },
|
||||||
|
}).exec();
|
||||||
|
if (data)
|
||||||
|
return res
|
||||||
|
.status(400)
|
||||||
|
.json({ message: "Product name already exists!" });
|
||||||
|
req.body.addedBy = req.user._id;
|
||||||
|
product = await Product.create(req.body);
|
||||||
} else {
|
} else {
|
||||||
Allfiles.map((item) => {
|
const data = await Product.findOne({
|
||||||
images.push(item.tempFilePath);
|
_id: { $ne: findProduct._id },
|
||||||
});
|
name: { $regex: new RegExp(`^${name}$`, "ig") },
|
||||||
|
}).exec();
|
||||||
|
if (data)
|
||||||
|
return res
|
||||||
|
.status(400)
|
||||||
|
.json({ message: "Product name already exists!" });
|
||||||
|
product = await Product.findByIdAndUpdate(req.body.product_id, req.body);
|
||||||
}
|
}
|
||||||
|
|
||||||
const imagesLinks = [];
|
|
||||||
for (let i = 0; i < images.length; i++) {
|
|
||||||
const result = await cloudinary.v2.uploader.upload(images[i], {
|
|
||||||
folder: "smellica/product",
|
|
||||||
});
|
|
||||||
|
|
||||||
imagesLinks.push({
|
|
||||||
public_id: result.public_id,
|
|
||||||
url: result.secure_url,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
req.body.image = imagesLinks;
|
|
||||||
req.body.addedBy = req.user._id;
|
|
||||||
const newUniquid = uuidv4();
|
|
||||||
req.body.uniqueId = newUniquid.replace(/-/g, "").substring(0, 10);
|
|
||||||
|
|
||||||
const data = await Product.create({ ...req.body });
|
|
||||||
res.status(201).json({
|
res.status(201).json({
|
||||||
success: true,
|
message: "Product details added successfully!",
|
||||||
data,
|
product_id: product._id,
|
||||||
msg: " create Product Successfully!!",
|
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
res.status(500).json({
|
res.status(500).json({
|
||||||
success: false,
|
message: error.message ? error.message : "Something went wrong!",
|
||||||
msg: error.message,
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
export const updateProduct = async (req, res) => {
|
||||||
|
try {
|
||||||
|
if (req.body?.variants) {
|
||||||
|
const vars = req.body.variants || []; // Default to an empty array if req.body.variants is undefined or null
|
||||||
|
const product = await Product.findByIdAndUpdate(
|
||||||
|
req.params.id,
|
||||||
|
{ variants: vars },
|
||||||
|
{ new: true } // Return the updated document
|
||||||
|
);
|
||||||
|
|
||||||
|
// Send a JSON response back to the client
|
||||||
|
return res.status(201).json({
|
||||||
|
message: "Product variant saved successfully",
|
||||||
|
variants: product?.variants || [], // Return the updated variants or an empty array if product is undefined
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (req.files) {
|
||||||
|
// req.body.addedBy = req.user.id;
|
||||||
|
// const image_file = req.files.image;
|
||||||
|
// console.log("req.files", req.files);
|
||||||
|
const getProduct = await Product.findById(req.params.id);
|
||||||
|
|
||||||
|
if (getProduct.image?.length > 0) {
|
||||||
|
// Deleting Images From Cloudinary
|
||||||
|
for (let i = 0; i < getProduct.image.length; i++) {
|
||||||
|
await cloudinary.v2.uploader.destroy(getProduct.image[i].public_id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let images = [];
|
||||||
|
let Allfiles = req.files.image;
|
||||||
|
if (typeof Allfiles.tempFilePath === "string") {
|
||||||
|
let filepath = Allfiles.tempFilePath;
|
||||||
|
|
||||||
|
images.push(filepath);
|
||||||
|
} else {
|
||||||
|
Allfiles.map((item) => {
|
||||||
|
images.push(item.tempFilePath);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const imagesLinks = [];
|
||||||
|
for (let i = 0; i < images.length; i++) {
|
||||||
|
const result = await cloudinary.v2.uploader.upload(images[i], {
|
||||||
|
folder: "smellica/product",
|
||||||
|
});
|
||||||
|
|
||||||
|
imagesLinks.push({
|
||||||
|
public_id: result.public_id,
|
||||||
|
url: result.secure_url,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
let product = await Product.findByIdAndUpdate(
|
||||||
|
req.params.id,
|
||||||
|
{ image: imagesLinks },
|
||||||
|
{ new: true } // Return the updated document
|
||||||
|
);
|
||||||
|
return res.status(201).json({
|
||||||
|
message: "Product image saved successfully",
|
||||||
|
images: product?.image || [], // Return the updated variants or an empty array if product is undefined
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.log(error);
|
||||||
|
res.status(500).json({
|
||||||
|
message: error.message ? error.message : "Something went wrong!",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////
|
||||||
//get All Product
|
//get All Product
|
||||||
export const getAllProduct = async (req, res) => {
|
export const getAllProduct = async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const product = await Product.find()
|
const product = await Product.find()
|
||||||
.populate({
|
.populate({
|
||||||
path: "category gst addedBy",
|
path: "category addedBy variants.gst_Id",
|
||||||
select: "name categoryName tax",
|
select: "name categoryName tax",
|
||||||
})
|
})
|
||||||
.sort({
|
.sort({
|
||||||
@ -75,6 +139,28 @@ export const getAllProduct = async (req, res) => {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
//get One Product
|
||||||
|
export const getOneProduct = async (req, res) => {
|
||||||
|
try {
|
||||||
|
const data = await Product.findById(req.params.id).populate({
|
||||||
|
path: "category addedBy variants.gst_Id",
|
||||||
|
select: "name categoryName tax",
|
||||||
|
});
|
||||||
|
if (data) {
|
||||||
|
return res.status(200).json({
|
||||||
|
success: true,
|
||||||
|
data,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
// console.log(error)
|
||||||
|
res.status(500).json({
|
||||||
|
success: false,
|
||||||
|
msg: error.message ? error.message : "Something went wrong!",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
// get all product with device products first
|
// get all product with device products first
|
||||||
export const getAllProductsDevicesFirst = async (req, res) => {
|
export const getAllProductsDevicesFirst = async (req, res) => {
|
||||||
try {
|
try {
|
||||||
@ -121,27 +207,6 @@ export const getAllProductsDevicesFirst = async (req, res) => {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
//get One Product
|
|
||||||
export const getOneProduct = async (req, res) => {
|
|
||||||
try {
|
|
||||||
const product = await Product.findById(req.params.id).populate({
|
|
||||||
path: "category gst addedBy",
|
|
||||||
select: "name categoryName tax",
|
|
||||||
});
|
|
||||||
if (product) {
|
|
||||||
return res.status(200).json({
|
|
||||||
success: true,
|
|
||||||
product,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
// console.log(error)
|
|
||||||
res.status(500).json({
|
|
||||||
success: false,
|
|
||||||
msg: error.message ? error.message : "Something went wrong!",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// 3.update Product
|
// 3.update Product
|
||||||
// export const updateProduct = async (req, res) => {
|
// export const updateProduct = async (req, res) => {
|
||||||
@ -236,88 +301,88 @@ export const getOneProduct = async (req, res) => {
|
|||||||
// });
|
// });
|
||||||
// }
|
// }
|
||||||
// };
|
// };
|
||||||
export const updateProduct = async (req, res) => {
|
// export const updateProduct = async (req, res) => {
|
||||||
const {
|
// const {
|
||||||
name,
|
// name,
|
||||||
description,
|
// description,
|
||||||
price,
|
// price,
|
||||||
category,
|
// category,
|
||||||
image,
|
// image,
|
||||||
gst_amount,
|
// gst_amount,
|
||||||
product_Status,
|
// product_Status,
|
||||||
gst,
|
// gst,
|
||||||
total_amount,
|
// total_amount,
|
||||||
} = req.body;
|
// } = req.body;
|
||||||
try {
|
// try {
|
||||||
// Prepare an array for the images
|
// // Prepare an array for the images
|
||||||
const jsonArray = JSON.parse(image);
|
// const jsonArray = JSON.parse(image);
|
||||||
const AllImages = jsonArray.map(({ public_id, url }) => ({
|
// const AllImages = jsonArray.map(({ public_id, url }) => ({
|
||||||
public_id,
|
// public_id,
|
||||||
url,
|
// url,
|
||||||
}));
|
// }));
|
||||||
|
|
||||||
let updatedImages = AllImages;
|
// let updatedImages = AllImages;
|
||||||
|
|
||||||
if (req.files && req.files.newImages) {
|
// if (req.files && req.files.newImages) {
|
||||||
const newUploadImages = Array.isArray(req.files.newImages)
|
// const newUploadImages = Array.isArray(req.files.newImages)
|
||||||
? req.files.newImages
|
// ? req.files.newImages
|
||||||
: [req.files.newImages];
|
// : [req.files.newImages];
|
||||||
|
|
||||||
const imagesLinks = [];
|
// const imagesLinks = [];
|
||||||
|
|
||||||
for (let i = 0; i < newUploadImages.length; i++) {
|
// for (let i = 0; i < newUploadImages.length; i++) {
|
||||||
const result = await cloudinary.v2.uploader.upload(
|
// const result = await cloudinary.v2.uploader.upload(
|
||||||
newUploadImages[i].tempFilePath,
|
// newUploadImages[i].tempFilePath,
|
||||||
{
|
// {
|
||||||
folder: "smellica/product",
|
// folder: "smellica/product",
|
||||||
}
|
// }
|
||||||
);
|
// );
|
||||||
|
|
||||||
imagesLinks.push({
|
// imagesLinks.push({
|
||||||
public_id: result.public_id,
|
// public_id: result.public_id,
|
||||||
url: result.secure_url,
|
// url: result.secure_url,
|
||||||
});
|
// });
|
||||||
}
|
// }
|
||||||
|
|
||||||
// Combine the existing images and the newly uploaded images
|
// // Combine the existing images and the newly uploaded images
|
||||||
updatedImages = [...AllImages, ...imagesLinks];
|
// updatedImages = [...AllImages, ...imagesLinks];
|
||||||
}
|
// }
|
||||||
|
|
||||||
// Perform the product update
|
// // Perform the product update
|
||||||
const updatedProduct = await Product.findOneAndUpdate(
|
// const updatedProduct = await Product.findOneAndUpdate(
|
||||||
{ _id: req.params.id },
|
// { _id: req.params.id },
|
||||||
{
|
// {
|
||||||
$set: {
|
// $set: {
|
||||||
name,
|
// name,
|
||||||
description,
|
// description,
|
||||||
product_Status,
|
// product_Status,
|
||||||
price,
|
// price,
|
||||||
category,
|
// category,
|
||||||
image: updatedImages,
|
// image: updatedImages,
|
||||||
gst,
|
// gst,
|
||||||
gst_amount,
|
// gst_amount,
|
||||||
total_amount,
|
// total_amount,
|
||||||
},
|
// },
|
||||||
},
|
// },
|
||||||
{ new: true }
|
// { new: true }
|
||||||
);
|
// );
|
||||||
|
|
||||||
if (!updatedProduct) {
|
// if (!updatedProduct) {
|
||||||
return res.status(404).json({ success: false, msg: "Product not found" });
|
// return res.status(404).json({ success: false, msg: "Product not found" });
|
||||||
}
|
// }
|
||||||
|
|
||||||
return res.status(200).json({
|
// return res.status(200).json({
|
||||||
success: true,
|
// success: true,
|
||||||
updatedProduct,
|
// updatedProduct,
|
||||||
});
|
// });
|
||||||
} catch (error) {
|
// } catch (error) {
|
||||||
console.error("Error updating product:", error);
|
// console.error("Error updating product:", error);
|
||||||
res.status(500).json({
|
// res.status(500).json({
|
||||||
success: false,
|
// success: false,
|
||||||
msg: error.message ? error.message : "Something went wrong!",
|
// msg: error.message ? error.message : "Something went wrong!",
|
||||||
});
|
// });
|
||||||
}
|
// }
|
||||||
};
|
// };
|
||||||
|
|
||||||
export const deleteImageFromCloudinary = async (req, res) => {
|
export const deleteImageFromCloudinary = async (req, res) => {
|
||||||
const { public_id } = req.params;
|
const { public_id } = req.params;
|
||||||
@ -360,8 +425,10 @@ export const deleteProduct = async (req, res) => {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
// Deleting Images From Cloudinary
|
// Deleting Images From Cloudinary
|
||||||
for (let i = 0; i < getProduct.image.length; i++) {
|
if (getProduct?.image?.length > 0) {
|
||||||
await cloudinary.v2.uploader.destroy(getProduct.image[i].public_id);
|
for (let i = 0; i < getProduct.image.length; i++) {
|
||||||
|
await cloudinary.v2.uploader.destroy(getProduct.image[i].public_id);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//-------------------------//
|
//-------------------------//
|
||||||
|
@ -9,45 +9,58 @@ const productSchema = new Schema(
|
|||||||
required: [true, "Please Enter product Name"],
|
required: [true, "Please Enter product Name"],
|
||||||
trim: true,
|
trim: true,
|
||||||
},
|
},
|
||||||
uniqueId: {
|
// uniqueId: {
|
||||||
type: String,
|
// type: String,
|
||||||
required: true,
|
// required: true,
|
||||||
},
|
// },
|
||||||
description: {
|
description: {
|
||||||
type: String,
|
type: String,
|
||||||
maxLength: [400, "description cannot exceed 100 characters"],
|
maxLength: [400, "description cannot exceed 100 characters"],
|
||||||
required: [true, "Please Enter product Description"],
|
required: [true, "Please Enter product Description"],
|
||||||
},
|
},
|
||||||
price: {
|
|
||||||
type: Number,
|
|
||||||
required: [true, "Please Enter product Price"],
|
|
||||||
maxLength: [8, "Price can not exceed 8 characters"],
|
|
||||||
},
|
|
||||||
category: {
|
category: {
|
||||||
type: Schema.Types.ObjectId,
|
type: Schema.Types.ObjectId,
|
||||||
ref: "CategoryModel",
|
ref: "CategoryModel",
|
||||||
},
|
},
|
||||||
gst: {
|
// gst: {
|
||||||
type: Schema.Types.ObjectId,
|
// type: Schema.Types.ObjectId,
|
||||||
ref: "Tax",
|
// ref: "Tax",
|
||||||
},
|
// },
|
||||||
total_amount: {
|
// total_amount: {
|
||||||
type: Number,
|
// type: Number,
|
||||||
required: true,
|
// required: true,
|
||||||
},
|
// },
|
||||||
gst_amount: {
|
// gst_amount: {
|
||||||
type: Number,
|
// type: Number,
|
||||||
required: true,
|
// required: true,
|
||||||
|
// },
|
||||||
|
special_instructions: {
|
||||||
|
type: String,
|
||||||
},
|
},
|
||||||
|
variants: [
|
||||||
|
{
|
||||||
|
variant_Name: { type: String, default: "" },
|
||||||
|
weight: { type: Number, default: 0 },
|
||||||
|
volume: { type: Number, default: 0 },
|
||||||
|
price: { type: String, default: "" },
|
||||||
|
// gst_Name: { type: String, default: "" },
|
||||||
|
// gst_Rate: { type: String, default: "" },
|
||||||
|
gst_Id: {
|
||||||
|
type: mongoose.Schema.ObjectId,
|
||||||
|
ref: "Tax",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
image: [
|
image: [
|
||||||
{
|
{
|
||||||
public_id: {
|
public_id: {
|
||||||
type: String,
|
type: String,
|
||||||
required: true,
|
// required: true,
|
||||||
},
|
},
|
||||||
url: {
|
url: {
|
||||||
type: String,
|
type: String,
|
||||||
required: true,
|
// required: true,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
@ -58,6 +71,7 @@ const productSchema = new Schema(
|
|||||||
},
|
},
|
||||||
addedBy: {
|
addedBy: {
|
||||||
type: Schema.Types.ObjectId,
|
type: Schema.Types.ObjectId,
|
||||||
|
required: true,
|
||||||
ref: "User",
|
ref: "User",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -15,7 +15,9 @@ router
|
|||||||
.route("/product/create/")
|
.route("/product/create/")
|
||||||
.post(isAuthenticatedUser, authorizeRoles("admin"), createProduct);
|
.post(isAuthenticatedUser, authorizeRoles("admin"), createProduct);
|
||||||
router.route("/product/getAll/").get(getAllProduct);
|
router.route("/product/getAll/").get(getAllProduct);
|
||||||
router.route("/product/getAllProductsDevicesFrist/").get(getAllProductsDevicesFirst);
|
router
|
||||||
|
.route("/product/getAllProductsDevicesFrist/")
|
||||||
|
.get(getAllProductsDevicesFirst);
|
||||||
router.route("/product/getOne/:id").get(getOneProduct);
|
router.route("/product/getOne/:id").get(getOneProduct);
|
||||||
router
|
router
|
||||||
.route("/product/update/:id")
|
.route("/product/update/:id")
|
||||||
|
Loading…
Reference in New Issue
Block a user