From 6d5c474f80f87273cbb9220ea73d23d26a2ce9f3 Mon Sep 17 00:00:00 2001
From: pawan-dot <71133473+pawan-dot@users.noreply.github.com>
Date: Wed, 29 Mar 2023 12:17:00 +0530
Subject: [PATCH] business update
---
resources/Businesses/BusinessController.js | 149 +++++++++------------
1 file changed, 60 insertions(+), 89 deletions(-)
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}