300 lines
9.8 KiB
JavaScript
300 lines
9.8 KiB
JavaScript
|
|
|
|
|
|
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";
|
|
|
|
|
|
|
|
export const createBusiness = async (req, res) => {
|
|
try {
|
|
if (!req?.user) return res.status(400).json({ message: "please login !" });
|
|
|
|
const { business, purpose, country, language, state, city, address_Line_1, address_Line_2, pincode,
|
|
business_name, email, contact_Number, contact_Person_Name, url, short_url } =
|
|
req.body;
|
|
//validation
|
|
switch (true) {
|
|
case !business:
|
|
return res.status(500).send({ error: "Business is Required" });
|
|
case !purpose:
|
|
return res.status(500).send({ error: "Purpose is Required" });
|
|
case !language:
|
|
return res.status(500).send({ error: "Language is Required" });
|
|
case !address_Line_1:
|
|
return res.status(500).send({ error: "address_Line_1 is Required" });
|
|
case !address_Line_2:
|
|
return res.status(500).send({ error: "address_Line_2 is Required" });
|
|
case !state:
|
|
return res.status(500).send({ error: "state is Required" });
|
|
case !pincode:
|
|
return res.status(500).send({ error: "pincode is Required" });
|
|
case !city:
|
|
return res.status(500).send({ error: "city is Required" });
|
|
case !country:
|
|
return res.status(500).send({ error: "country is Required" });
|
|
case !business_name:
|
|
return res.status(500).send({ error: "business_name is Required" });
|
|
case !email:
|
|
return res.status(500).send({ error: "email is Required" });
|
|
case !contact_Number:
|
|
return res.status(500).send({ error: "contact_Number is Required" });
|
|
case !contact_Person_Name:
|
|
return res.status(500).send({ error: "contact_Person_Name is Required" });
|
|
case !url:
|
|
return res.status(500).send({ error: " Business url is Required" });
|
|
case !short_url:
|
|
return res.status(500).send({ error: "short_url is Required" });
|
|
|
|
|
|
}
|
|
|
|
|
|
let businesse = await Business.findOne({ email });
|
|
if (businesse) {
|
|
return res
|
|
.status(400)
|
|
.json({ success: false, message: " THis Email already exists Please try another Email!" });
|
|
}
|
|
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 (req?.files?.image?.tempFilePath) {
|
|
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);
|
|
}
|
|
//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
|
|
|
|
|
|
|
|
|
|
req.body.added_by = req.user._id
|
|
const businesses = 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: `Bolo.Ai business Created`,
|
|
html: `your Business Url is:${req.body.url}<br/><br/>your login email is: <strong> ${req.body.email}</strong><br/>and password is: <strong> ${passwords}</strong><br/><br/><h3>Thank You</h3>`
|
|
|
|
});
|
|
|
|
res.status(201).send({
|
|
success: true,
|
|
|
|
message: `business added successfully and Email sent to ${req.body.email} successfully`,
|
|
|
|
businesses,
|
|
});
|
|
} catch (error) {
|
|
console.log(error);
|
|
res.status(500).send({
|
|
success: false,
|
|
error,
|
|
message: error.message ? error.message : "Unable to create."
|
|
});
|
|
|
|
}
|
|
}
|
|
|
|
export const getAllBusiness = async (req, res) => {
|
|
try {
|
|
if (!req?.user) return res.status(400).json({ message: "please login !" });
|
|
|
|
|
|
const businesses = await Business.find().sort({ createdAt: -1 })
|
|
if (businesses) {
|
|
res.status(201).send({
|
|
success: true,
|
|
message: "Business Fetched Successfully",
|
|
businesses,
|
|
});
|
|
}
|
|
|
|
} catch (error) {
|
|
// console.log(error);
|
|
res.status(500).send({
|
|
success: false,
|
|
error,
|
|
message: error.message ? error.message : "Unable to fetch."
|
|
});
|
|
|
|
}
|
|
}
|
|
export const getSingleBusiness = 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 businesses = await Business.findById(req.params.id)
|
|
if (businesses) {
|
|
res.status(201).send({
|
|
success: true,
|
|
message: "Business Fetched Successfully",
|
|
businesses,
|
|
});
|
|
}
|
|
|
|
} catch (error) {
|
|
// console.log(error);
|
|
res.status(500).send({
|
|
success: false,
|
|
error,
|
|
message: error.message ? error.message : "Unable to fetch."
|
|
});
|
|
|
|
}
|
|
}
|
|
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 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}<br/><br/>your login email is: <strong> ${req.body.email}</strong><br/>and password is: <strong> ${passwords}</strong><br/><br/><h3>Thank You</h3>`
|
|
|
|
});
|
|
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({
|
|
success: false,
|
|
error,
|
|
message: error.message ? error.message : "Unable to Update."
|
|
});
|
|
|
|
}
|
|
}
|
|
|
|
//delete
|
|
export const deleteBusinessById = 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 business = await Business.findByIdAndDelete(req.params.id)
|
|
if (!business) {
|
|
return res.status(400).json({ message: 'business Not Found' });
|
|
}
|
|
await business.remove();
|
|
|
|
res.status(200).json({ status: "OK", msg: 'Deteted successfully' });
|
|
} catch (err) {
|
|
return res.status(500).json({ message: err.message ? err.message : "Unable to delete." });
|
|
}
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|