diff --git a/resources/Businesses/BusinessController.js b/resources/Businesses/BusinessController.js index ee74168..0915716 100644 --- a/resources/Businesses/BusinessController.js +++ b/resources/Businesses/BusinessController.js @@ -5,6 +5,8 @@ import sendEmail from "../../Utils/sendEmail.js" import cloudinary from "../../Utils/cloudinary.js"; import { Business } from './BusinessModel.js' import password from 'secure-random-password' +import bcrypt from "bcryptjs" + import fs from "fs"; @@ -178,17 +180,67 @@ export const updateBusiness = async (req, res) => { try { if (!req?.user) return res.status(400).json({ message: "please login !" }); if (!req?.params.id) return res.status(400).json({ message: "please Provide Business ID !" }); + const BusinessWithURL = await Business.findOne({ + short_url: req.body?.short_url, + }); + if ( + BusinessWithURL?._id && + BusinessWithURL?._id?.toString() !== req.params.id + ) { + if (req?.files?.image?.tempFilePath) + fs.unlinkSync(image_file?.tempFilePath); + return res.status(400).json({ message: "Business URL is not available!" }); + } + const getBusiness = await Business.findById(req.params.id); + if (req?.files?.image?.tempFilePath) { + if (getBusiness?.banner) { + const imageId = getBusiness?.banner?.public_id; + + await cloudinary.uploader.destroy(imageId) + } + const result = await cloudinary.v2.uploader.upload( + image_file?.tempFilePath, + { + folder: "Bolo/business_Image", + } + ); + const image = { url: result?.secure_url, public_id: result?.public_id }; + req.body.banner = image; + fs.unlinkSync(image_file?.tempFilePath); + await cloudinary.v2.uploader.destroy(getBusiness.banner.public_id); + } + //generate password + const passwords = password.randomPassword({ + length: 10, + characters: [ + { characters: password.upper, exactly: 1 }, + { characters: password.symbols, exactly: 1 }, + password.lower, + password.digits] + }) + + req.body.password = await bcrypt.hash(passwords, 12) req.body.added_by = req.user._id - const businesses = await Business.findByIdAndUpdate(req.params.id, { ...req.body }) + const business = await Business.findByIdAndUpdate(req.params.id, { ...req.body }) + await sendEmail({ + + to: `${req.body.email}`, // Change to your recipient + + from: `${process.env.SEND_EMAIL_FROM}`, // Change to your verified sender + + subject: `ATP Business Updated`, + html: `your business Url is:${req.body.url}

your login email is: ${req.body.email}
and password is: ${passwords}

Thank You

` - res.status(201).send({ - success: true, - message: "Business Updated Successfully", - businesses, }); + return res.status(200).json({ + success: true, + data: business, + message: `Business Updated successfully and Email sent to ${req.body.email} successfully`, + }); + } catch (error) { console.log(error); res.status(500).send({ @@ -199,6 +251,7 @@ export const updateBusiness = async (req, res) => { } } + //delete export const deleteBusinessById = async (req, res) => { try { @@ -206,6 +259,8 @@ export const deleteBusinessById = async (req, res) => { if (!req?.user) return res.status(400).json({ message: "please login !" }); if (!req?.params.id) return res.status(400).json({ message: "please Provide Business ID !" }); + + const business = await Business.findByIdAndDelete(req.params.id) if (!business) { return res.status(400).json({ message: 'business Not Found' }); @@ -240,89 +295,5 @@ export const deleteBusinessById = async (req, res) => { - - - - -const addBusiness = async (req, res) => { - const image_file = req?.files?.image; - try { - const { email } = req.body - let business = await Business.findOne({ email }); - if (business) { - return res - .status(400) - .json({ success: false, message: "business already exists" }); - } - const BusinessWithURL = await Business.findOne({ - short_url: req.body?.short_url, - }); - if (BusinessWithURL?._id) { - if (req?.files?.image?.tempFilePath) - fs.unlinkSync(image_file?.tempFilePath); - return res.status(400).json({ message: "Business URL is not available!" }); - } - if (image_file?.tempFilePath) { - const result = await cloudinary.v2.uploader.upload( - image_file?.tempFilePath, - { - folder: "ATP/Business_banners", - } - ); - const image = { url: result?.secure_url, public_id: result?.public_id }; - req.body.banner = image; - fs.unlinkSync(image_file?.tempFilePath); - } - //generate password - const passwords = password.randomPassword({ - length: 10, - characters: [ - { characters: password.upper, exactly: 1 }, - { characters: password.symbols, exactly: 1 }, - password.lower, - password.digits] - }) - - req.body.password = passwords; - req.user.role === 'admin' ? req.body.verify = true : req.body.verify = false - const entity = await Business.create(req.body); - await sendEmail({ - - to: `${req.body.email}`, // Change to your recipient - - from: `${process.env.SEND_EMAIL_FROM}`, // Change to your verified sender - - subject: `ATP Business Created`, - html: `your business Url is:${req.body.url}

your login email is: ${req.body.email}
and password is: ${passwords}

Thank You

` - - }); - return res.status(200).json({ - success: true, - data: entity, - message: `Business added successfully and Email sent to ${req.body.email} successfully`, - }); - } catch (err) { - // console.log(err) - fs.unlinkSync(image_file?.tempFilePath); - return res.status(500).json({ message: err.message ? err.message : "Unable to create." }); - } -}; - -const addProductToBusiness = async (req, res) => { - try { - const Business = await Business.findByIdAndUpdate( - req.params.id, - { - $push: { products: req.body.product_id }, - }, - { new: true } - ); - res - .status(200) - .json({ status: "ok", message: "Product added to Business successfully" }); - } catch (err) { - return res.status(500).json({ message: "Unable to get ID." }); - } -};