business added and send email
This commit is contained in:
parent
6646274ada
commit
b8ab99cf9a
@ -4,6 +4,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 fs from "fs";
|
||||
|
||||
|
||||
@ -12,7 +14,8 @@ 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 } =
|
||||
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) {
|
||||
@ -34,16 +37,82 @@ export const createBusiness = async (req, res) => {
|
||||
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 Created Successfully",
|
||||
|
||||
message: `business added successfully and Email sent to ${req.body.email} successfully`,
|
||||
|
||||
businesses,
|
||||
});
|
||||
} catch (error) {
|
||||
|
@ -8,12 +8,43 @@ const { Schema, model } = mongoose;
|
||||
const BusinessSchema = new Schema(
|
||||
{
|
||||
|
||||
address_Line_1: { type: String, required: true },
|
||||
address_Line_2: { type: String, required: true },
|
||||
purpose: { type: String, required: true },
|
||||
|
||||
business: { type: String, required: true },
|
||||
purpose: { type: String, required: true },
|
||||
|
||||
language: [{ type: Array, default: [], required: true }],
|
||||
//contacts
|
||||
business_name: {
|
||||
type: String,
|
||||
required: [true, "Please Enter Your business_name"],
|
||||
maxLength: [50, "business_name cannot exceed 30 characters"],
|
||||
minLength: [3, "business_name should have more than 3 characters"],
|
||||
}, email: {
|
||||
type: String,
|
||||
required: [true, "Please Enter Your Email"],
|
||||
|
||||
unique: [true, "Email already exist ! please try with diffent email"],
|
||||
|
||||
|
||||
validate: [validator.isEmail, "Please Enter a valid Email"],
|
||||
},
|
||||
password: {
|
||||
type: String,
|
||||
required: [true, "Please Enter Your Password"],
|
||||
minLength: [6, "Password should be greater than 6 characters"],
|
||||
select: false,//find not got passpord
|
||||
},
|
||||
|
||||
contact_Number: { type: Number, required: true },
|
||||
contact_Person_Name: { type: String, required: true },
|
||||
|
||||
url: { type: String, default: "" },
|
||||
short_url: { type: String, default: "" },
|
||||
banner: { type: Object, default: { url: "", public_id: "" } },
|
||||
///
|
||||
//address
|
||||
address_Line_1: { type: String, required: true },
|
||||
address_Line_2: { type: String, required: true },
|
||||
country: { type: String, required: true, default: "" },
|
||||
state: { type: String, required: true, default: "" },
|
||||
city: { type: String, required: true },
|
||||
|
@ -11,3 +11,4 @@ router.route("/get/:id").get(isAuthenticatedUser, getSingleBusiness);
|
||||
router.route("/getall").get(isAuthenticatedUser, getAllBusiness);
|
||||
|
||||
export default router;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user