Untitled
unknown
plain_text
a year ago
16 kB
4
Indexable
const mongoose = require('mongoose');
const Product = require('../Models/productModel.js');
const User = require('../Models/userModel.js');
const Salesman = require('../Models/salesmanModel.js');
const Image = require('../Models/imageModel.js');
const catchAsync = require('../utils/catchAsync.js');
const AppError = require('../utils/appError.js');
const Layout = require('../Models/layoutModel.js');
const Category = require('../Models/categoryModel.js');
// exports.addDesktopBanner = async (req, res, next) => {
// const images = req.body.images;
// if (images) {
// if (images.length > 0) {
// if (images.length > 2) {
// return next(new AppError('The number of images is greater than 2', 400));
// }
// const imageDocuments = images.map((base64Str) => ({ image: new Buffer.from(base64Str, 'base64') }));
// const imageInsertionResults = await Image.insertMany(imageDocuments);
// const imgs = imageInsertionResults.map((doc) => doc._id);
// return imgs;
// } else {
// return next(new AppError('At least one image should be provided', 400));
// }
// } else {
// return next(new AppError('desktop banner Images are required', 500));
// }
// };
// exports.addMobileBanner = async (req, res, next) => {
// const images = req.body.images;
// if (images) {
// if (images.length > 0) {
// if (images.length > 2) {
// return next(new AppError('The number of images is greater than 2', 400));
// }
// const imageDocuments = images.map((base64Str) => ({ image: new Buffer.from(base64Str, 'base64') }));
// const imageInsertionResults = await Image.insertMany(imageDocuments);
// const imgs = imageInsertionResults.map((doc) => doc._id);
// return imgs;
// } else {
// return next(new AppError('At least one image should be provided', 400));
// }
// } else {
// return next(new AppError('mobile banner Images are required', 500));
// }
// };
const addDesktopBanner = async (images, next) => {
if (images) {
if (images.length > 0) {
if (images.length > 2) {
return next(new AppError('The number of images is greater than 2', 400));
}
const imageDocuments = images.map((base64Str) => ({ image: new Buffer.from(base64Str, 'base64') }));
const imageInsertionResults = await Image.insertMany(imageDocuments);
const imgs = imageInsertionResults.map((doc) => doc._id);
return imgs;
} else {
return next(new AppError('At least one image should be provided', 400));
}
} else {
return next(new AppError('desktop banner Images are required', 500));
}
};
const addMobileBanner = async (images, next) => {
if (images) {
if (images.length > 0) {
if (images.length > 2) {
return next(new AppError('The number of images is greater than 2', 400));
}
const imageDocuments = images.map((base64Str) => ({ image: new Buffer.from(base64Str, 'base64') }));
const imageInsertionResults = await Image.insertMany(imageDocuments);
const imgs = imageInsertionResults.map((doc) => doc._id);
return imgs;
} else {
return next(new AppError('At least one image should be provided', 400));
}
} else {
return next(new AppError('mobile banner Images are required', 500));
}
};
exports.searchbyname = catchAsync(async (req, res, next) => {
try {
// Extract name from query parameters
const { name } = req.query;
const { lang } = req.query;
// Check if name is provided
if (!name) {
return next(new AppError('name is required', 400));
}
// Search for products in the dataabase based on name
const products = await Product.find({ [`name.${lang}`]: { $regex: name, $options: 'i' } })
.select('name')
.limit(10);
// Handle case where no products are found with the specified SKU
if (products.length === 0) {
return next(new AppError('No products found with the specified name', 400));
}
// Return the found products
res.json({ products });
} catch (error) {
console.error(error);
return next(new AppError('Internal Server Error', 500));
}
});
exports.searchbycategory = catchAsync(async (req, res, next) => {
try {
// Extract SKU from query parameters
const { category } = req.query;
// Check if SKU is provided
if (!category) {
return next(new AppError('category is required', 400));
}
// Search for products in the dataabase based on SKU
const categories = await Category.find({ category: { $regex: category, $options: 'i' } })
.select('category')
.limit(10);
// Handle case where no products are found with the specified SKU
if (categories.length === 0) {
return next(new AppError('No categories found with the specified name', 400));
}
// Return the found products
res.json({ categories });
} catch (error) {
console.error(error);
return next(new AppError('Internal Server Error', 500));
}
});
exports.addSection1 = catchAsync(async (req, res, next) => {
let { products } = req.body;
console.log(products);
if (products) {
if (products.length > 0) {
if (products.length > 10) {
return next(new AppError('The number of products is greater than 10', 400));
}
let duplicate = new Set(products);
if (duplicate.size !== products.length) {
console.log(duplicate);
return next(new AppError('Duplicate product in list', 400));
}
for (const product_id of products) {
try {
var Id = new mongoose.Types.ObjectId(product_id);
} catch (e) {
return next(new AppError('Invalid id.', 401));
}
const prod = await Product.findById(Id);
if (!prod) {
return next(new AppError('product not found', 400));
}
}
const result = await Layout.updateOne({}, { section1: products });
if (result) {
res.status(200).json({
message: 'Products have been added to section 1 successfully',
result
});
} else {
return next(new AppError('Internal Server Error', 500));
}
} else {
return next(new AppError('min of products to add is 1', 400));
}
} else {
return next(new AppError('products is required', 500));
}
});
exports.addSection2 = catchAsync(async (req, res, next) => {
let { products } = req.body;
if (products) {
if (products.length > 0) {
if (products.length > 20) {
return next(new AppError('The number of products is greater than 20', 400));
}
let duplicate = new Set(products);
if (duplicate.size !== products.length) {
console.log(duplicate);
return next(new AppError('Duplicate product in list', 400));
}
for (const product_id of products) {
try {
var Id = new mongoose.Types.ObjectId(product_id);
} catch (e) {
return next(new AppError('Invalid id.', 401));
}
const prod = await Product.findById(Id);
if (!prod) {
return next(new AppError('product not found', 400));
}
}
const result = await Layout.updateOne({}, { section2: products });
if (result) {
res.status(200).json({
message: 'Products have been added to section 2 successfully',
result
});
} else {
return next(new AppError('Internal Server Error', 500));
}
} else {
return next(new AppError('min of products to add is 1', 400));
}
} else {
return next(new AppError('products is required', 500));
}
});
exports.addcategories = catchAsync(async (req, res, next) => {
let { categories } = req.body;
if (categories) {
if (categories.length > 0) {
if (categories.length > 6) {
return next(new AppError('The number of categories is greater than 6', 400));
}
let duplicate = new Set(categories);
if (duplicate.size !== categories.length) {
console.log(duplicate);
return next(new AppError('Duplicate categories in list', 400));
}
for (const category_id of categories) {
try {
var Id = new mongoose.Types.ObjectId(category_id);
} catch (e) {
return next(new AppError('Invalid id.', 401));
}
const cat = await Category.findById(Id);
if (!cat) {
return next(new AppError('category not found', 400));
}
}
const result = await Layout.updateOne({}, { categories: categories });
if (result) {
res.status(200).json({
message: 'Categories have been added successfully',
result
});
} else {
return next(new AppError('Internal Server Error', 500));
}
} else {
return next(new AppError('min of products to add is 1', 400));
}
} else {
return next(new AppError('products is required', 500));
}
});
const addsec1 = async (products, next) => {
if (products) {
if (products.length > 0) {
if (products.length > 20) {
return next(new AppError('The number of products of section 1 is greater than 20', 400));
}
let duplicate = new Set(products);
if (duplicate.size !== products.length) {
console.log(duplicate);
return next(new AppError('Duplicate product of section 1 in list', 400));
}
for (const product_id of products) {
try {
var Id = new mongoose.Types.ObjectId(product_id);
} catch (e) {
return next(new AppError('Invalid id.', 401));
}
const prod = await Product.findById(Id);
if (!prod) {
return next(new AppError('product of section 1 not found', 400));
}
}
return products;
} else {
return next(new AppError('min of products of section 1 to add is 1', 400));
}
} else {
return next(new AppError('products of section 1 is required', 500));
}
};
const addSec2 = async (products, next) => {
if (products) {
if (products.length > 0) {
if (products.length > 20) {
return next(new AppError('The number of products of section 2 is greater than 20', 400));
}
let duplicate = new Set(products);
if (duplicate.size !== products.length) {
return next(new AppError('Duplicate product of section 2 in list', 400));
}
for (const product_id of products) {
try {
var Id = new mongoose.Types.ObjectId(product_id);
} catch (e) {
return next(new AppError('Invalid id.', 401));
}
const prod = await Product.findById(Id);
if (!prod) {
return next(new AppError('product of section 2 not found', 400));
}
}
return products;
} else {
return next(new AppError('min of products of section 2 to add is 1', 400));
}
} else {
return next(new AppError('products of section 2 is required', 500));
}
};
const addCats = async (categories, next) => {
if (categories) {
if (categories.length > 0) {
if (categories.length > 6) {
return next(new AppError('The number of categories is greater than 6', 400));
}
let duplicate = new Set(categories.map((item) => item));
if (duplicate.size !== categories.length) {
return next(new AppError('Duplicate categories in list', 400));
}
for (const category_id of categories) {
// = categ.category_id
try {
var Id = new mongoose.Types.ObjectId(category_id);
} catch (e) {
return next(new AppError('Invalid id.', 401));
}
const cat = await Category.findById(Id);
if (!cat) {
return next(new AppError('category not found', 400));
}
}
return categories;
} else {
return next(new AppError('min of products to add is 1', 400));
}
} else {
return next(new AppError('products is required', 500));
}
};
exports.addAll = catchAsync(async (req, res, next) => {
let { categories, products_section1, products_section2, imagesmobile, imagesdesktop } = req.body;
let obj = {};
if (products_section1) {
const prod_s1 = await addsec1(products_section1, next);
obj.section1 = prod_s1;
}
if (products_section2) {
const prod_s2 = await addSec2(products_section2, next);
obj.section2 = prod_s2;
}
if (categories) {
const cats = await addCats(categories, next);
obj.categories = cats;
}
if (imagesmobile) {
const imagesformobile = await addMobileBanner(imagesmobile, next);
obj.mobileimages = imagesformobile;
}
if (imagesdesktop) {
const imagesfordesktop = await addDesktopBanner(imagesdesktop, next);
obj.desktopimages = imagesfordesktop;
}
const result = await Layout.updateOne({}, obj);
if (result) {
res.status(200).json({
message: 'Layout is successfully updated',
result
});
} else {
return next(new AppError('Internal Server Error', 500));
}
});
exports.getAllLayoutData = catchAsync(async (req, res, next) => {
const pathsAndFields = [
{ path: 'section1', select: ['name', 'images', 'pricing', 'quantity'],populate: { path: 'offer', select: ['discount','status'] }},
{ path: 'section2', select: ['name', 'images', 'pricing', 'quantity'],populate: { path: 'offer', select: ['discount','status'] }},
{ path: 'categories', select: 'category' },
];
const layout = await Layout.findOne({}).populate(pathsAndFields);
if (layout) {
res.status(200).json({
layout
});
} else {
return next(new AppError('Internal Server Error', 500));
}
});
exports.getProductsSection1 = catchAsync(async (req, res, next) => {
try {
const layout = await Layout.findOne();
console.log(layout);
const productsSection1 = await Product.find({ _id: { $in: layout.section1 } })
.populate({ path: 'offer', select: ['discount','status'] });;
res.status(200).json({
status: 'success',
data: {
Section1: productsSection1
}
});
} catch (error) {}
});
// Get Products from Section 2
exports.getProductsSection2 = catchAsync(async (req, res, next) => {
try {
const layout = await Layout.findOne();
const productsSection2 = await Product.find({ _id: { $in: layout.section2 } })
.populate({ path: 'offer', select: ['discount','status'] });;
res.status(200).json({
status: 'success',
data: {
Section2: productsSection2
}
});
} catch (error) {
return next(new AppError('Error retrieving products from section 2'));
}
});
// Get Categories
exports.getCategories = catchAsync(async (req, res, next) => {
try {
const layout = await Layout.findOne();
const categories = await Category.find({ _id: { $in: layout.categories } });
res.status(200).json({
status: 'success',
data: {
categories: categories
}
});
} catch (error) {
return next(new AppError('Error retrieving categories'));
}
});
const addIcons = async (icon, next) => {
const imageDocuments = { image: new Buffer.from(base64Str, 'base64') };
const imageResult = await Image.insert(imageDocuments);
return imageResult._id;
};
Editor is loading...
Leave a Comment