Skip to content

Instantly share code, notes, and snippets.

@Shankjbs571
Last active July 19, 2024 05:18
Show Gist options
  • Save Shankjbs571/0ad205422fa8959f7f2945335111666b to your computer and use it in GitHub Desktop.
Save Shankjbs571/0ad205422fa8959f7f2945335111666b to your computer and use it in GitHub Desktop.
updated model for product with no required fields except category
// Create product
export const createProduct = async (req, res) => {
const {
title,
description,
price,
discountedPrice,
discountPercent,
quantity,
brand,
category,
ratings,
reviews,
// FIELDS FOR OFFLINE COUNTER PURCHASES
BarCode,
stockType,
unit,
purchaseRate,
profitPercentage,
HSN,
GST,
retailPrice,
totalAmount,
amountPaid
} = req.body;
try {
let imageUrl = '';
if (req.file) {
try {
const result = await uploadImageOnCloudinary(req.file.path);
imageUrl = result.secure_url;
fs.unlinkSync(req.file.path);
} catch (uploadError) {
console.error('Error uploading image to Cloudinary:', uploadError);
return res.status(500).send({ message: "Internal server error", status: false, error: "Error uploading image to Cloudinary" });
}
}
const slug = slugify(title, { lower: true });
const productData = {};
if (title) productData.title = title;
if (description) productData.description = description;
if (price) productData.price = price;
if (discountedPrice) productData.discountedPrice = discountedPrice;
if (discountPercent) productData.discountPercent = discountPercent;
if (quantity) productData.quantity = quantity;
if (brand) productData.brand = brand;
if (imageUrl) productData.imageUrl = imageUrl;
if (category) productData.category = category;
if (slug) productData.slug = slug;
if (ratings) productData.ratings = ratings;
if (reviews) productData.reviews = reviews;
if (BarCode) productData.BarCode = BarCode;
if (stockType) productData.stockType = stockType;
if (unit) productData.unit = unit;
if (purchaseRate) productData.purchaseRate = purchaseRate;
if (profitPercentage) productData.profitPercentage = profitPercentage;
if (HSN) productData.HSN = HSN;
if (GST) productData.GST = GST;
if (retailPrice) productData.retailPrice = retailPrice;
if (totalAmount) productData.totalAmount = totalAmount;
if (amountPaid) productData.amountPaid = amountPaid;
const product = new Product(productData);
const savedProduct = await product.save();
return res.status(201).send({ message: "Product created successfully", status: true, data: savedProduct });
} catch (error) {
console.error('Error creating product:', error);
return res.status(500).send({ message: "Internal server error", status: false, error: error.message });
}
};
export const updateProduct = async (req, res) => {
const { id } = req.params;
const {
title,
description,
price,
discountedPrice,
discountPercent,
quantity,
brand,
category,
ratings,
reviews,
// FIELDS FOR OFFLINE COUNTER PURCHASES
BarCode,
stockType,
unit,
purchaseRate,
profitPercentage,
HSN,
GST,
retailPrice,
totalAmount,
amountPaid
} = req.body;
try {
let imageUrl = '';
if (req.file) {
try {
const result = await uploadImageOnCloudinary(req.file.path);
imageUrl = result.secure_url;
fs.unlinkSync(req.file.path);
} catch (uploadError) {
console.error('Error uploading image to Cloudinary:', uploadError);
return res.status(500).send({ message: "Internal server error", status: false, error: "Error uploading image to Cloudinary" });
}
} else {
const existingProduct = await Product.findById(id);
if (!existingProduct) {
return res.status(404).send({ message: "Product not found", status: false });
}
imageUrl = existingProduct.imageUrl;
}
if (title){
const slug = slugify(title, { lower: true });
}
const updatedProductData = {};
if (title) updatedProductData.title = title;
if (description) updatedProductData.description = description;
if (price) updatedProductData.price = price;
if (discountedPrice) updatedProductData.discountedPrice = discountedPrice;
if (discountPercent) updatedProductData.discountPercent = discountPercent;
if (quantity) updatedProductData.quantity = quantity;
if (brand) updatedProductData.brand = brand;
if (imageUrl) updatedProductData.imageUrl = imageUrl;
if (category) updatedProductData.category = category;
if (slug) updatedProductData.slug = slug;
if (ratings) updatedProductData.ratings = ratings;
if (reviews) updatedProductData.reviews = reviews;
if (BarCode) updatedProductData.BarCode = BarCode;
if (stockType) updatedProductData.stockType = stockType;
if (unit) updatedProductData.unit = unit;
if (purchaseRate) updatedProductData.purchaseRate = purchaseRate;
if (profitPercentage) updatedProductData.profitPercentage = profitPercentage;
if (HSN) updatedProductData.HSN = HSN;
if (GST) updatedProductData.GST = GST;
if (retailPrice) updatedProductData.retailPrice = retailPrice;
if (totalAmount) updatedProductData.totalAmount = totalAmount;
if (amountPaid) updatedProductData.amountPaid = amountPaid;
const updatedProduct = await Product.findByIdAndUpdate(
id,
updatedProductData,
{ new: true }
);
if (!updatedProduct) {
return res.status(404).send({ message: "Product not found", status: false });
}
return res.status(200).send({ message: "Product updated successfully", status: true, data: updatedProduct });
} catch (error) {
console.error('Error updating product:', error);
return res.status(500).send({ message: "Internal server error", status: false, error: error.message });
}
};
import mongoose from "mongoose";
const productSchema = new mongoose.Schema({
title: {
type: String,
default: 'TITLE' // required: true,
},
description: {
type: String,
default: 'TRUE' // required: true,
},
price: {
type: Number,
default: 0 // required: true,
},
discountedPrice: {
type: Number,
},
discountPercent: {
type: Number,
},
weight: {
type: Number,
},
quantity: {
type: Number,
},
brand: {
type: String,
},
imageUrl: {
type: String,
},
slug: {
type: String,
unique: true,
required: true,
},
ratings: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "ratings",
},
],
reviews: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "reviews",
},
],
numRatings: {
type: Number,
default: 0,
},
category: {
type: mongoose.Schema.Types.ObjectId,
ref: "Category",
},
createdAt: {
type: Date,
default: Date.now,
},
updatedAt: {
type: Date,
default: Date.now,
},
// New fields for offline counter sales
BarCode:{
type: Number,
default: 0,
},
stockType:{
type: String,
default: "TYPE NONE"
},
unit:{
type: String,
default: 'Unit A'
},
purchaseRate: {
type: Number,
default : 0
},
profitPercentage: {
type: Number,
default : 0
},
HSN: {
type: String,
default: 'HSN'
},
GST: {
type: Number,
default : 0
},
retailPrice: {
type: Number,
default : 0
},
totalAmount: {
type: Number,
default : 0
},
amountPaid: {
type: Number,
default : 0
}
});
const Product = mongoose.model("products", productSchema);
export default Product;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment