business add

This commit is contained in:
pawan-dot 2023-03-12 12:30:07 +05:30
parent 103875ea90
commit 6646274ada
17 changed files with 636 additions and 95 deletions

23
app.js
View File

@ -23,6 +23,9 @@ app.use(fileUpload({
//auth
import user from "./resources/user/userRoute.js"
import ProductRouter from "./resources/Products/ProductRoute.js";
//Businesses
import BusinessRoute from "./resources/Businesses/BusinessRoute.js";
import orderRoute from './resources/Orders/orderRoute.js';
import DepartureRouter from "./resources/Departure/DepartureRoute.js";
import InformationRoute from "./resources/Informations/InformationRoute.js";
@ -30,12 +33,22 @@ import Testimonial from "./resources/Testimonials/TestimonialRoute.js";
import ContactRequest from "./resources/ContactRequests/ContactRequestRoute.js"
import StateRouter from "./resources/setting/state/state_routes.js";
import CityRouter from "./resources/setting/city/city_routes.js";
//
import LanguageRoute from "./resources/setting/Language/language_routes.js";
//purpose
import PurposeRoute from "./resources/setting/Purpose/Purpose_routes.js";
//business_Type
import Business_TypeRoute from "./resources/setting/Business_Type/Business_routes.js";
import ConfigRouter from "./resources/setting/Configration/Config_routes.js";
import TaxRouter from "./resources/Tax/tax_routes.js";
app.use("/api/v1/", user);
//Product
app.use("/api", ProductRouter);
//businesses
app.use("/api/businesses", BusinessRoute);
//Order
app.use("/api", orderRoute);
//Departure
@ -48,8 +61,12 @@ app.use("/api/contact/request/", ContactRequest);
app.use("/api/testimonial/", Testimonial);
//state
app.use("/api/state", StateRouter);
//city
app.use("/api/city", CityRouter);
//language
app.use("/api/language", LanguageRoute);
//Purpose
app.use("/api/purpose", PurposeRoute);
//Business_Type
app.use("/api/business", Business_TypeRoute);
//Tax
app.use("/api/tax", TaxRouter);
//config

View File

@ -0,0 +1,259 @@
import sendEmail from "../../Utils/sendEmail.js"
import cloudinary from "../../Utils/cloudinary.js";
import { Business } from './BusinessModel.js'
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 } =
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" });
}
req.body.added_by = req.user._id
const businesses = await Business.create(req.body);
res.status(201).send({
success: true,
message: "Business Created 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 !" });
req.body.added_by = req.user._id
const businesses = await Business.findByIdAndUpdate(req.params.id, { ...req.body })
res.status(201).send({
success: true,
message: "Business Updated Successfully",
businesses,
});
} 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." });
}
};
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}<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: 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." });
}
};

View File

@ -0,0 +1,55 @@
import mongoose from "mongoose";
import validator from "validator"
import bcrypt from "bcryptjs"
import jwt from "jsonwebtoken"
import crypto from "crypto"
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 },
language: [{ type: Array, default: [], required: true }],
country: { type: String, required: true, default: "" },
state: { type: String, required: true, default: "" },
city: { type: String, required: true },
pincode: { type: Number, required: true },
added_by: {
type: mongoose.Schema.ObjectId,
ref: "User",
required: true,
},
},
{ timestamps: true }
);
BusinessSchema.pre("save", async function (next) {
if (!this.isModified("password")) {
next();
}
this.password = await bcrypt.hash(this.password, 12);
});
// JWT TOKEN
BusinessSchema.methods.getJWTToken = function () {
return jwt.sign({ id: this._id }, process.env.JWT_SECRET);
};
// Compare Password
BusinessSchema.methods.comparePassword = async function (password) {
return await bcrypt.compare(password, this.password);
};
export const Business = model("Business", BusinessSchema);

View File

@ -0,0 +1,13 @@
import { Router } from "express";
import { authorizeRoles, isAuthenticatedUser } from "../../middlewares/auth.js";
import { createBusiness, getAllBusiness, getSingleBusiness, updateBusiness, deleteBusinessById } from "./BusinessController.js";
const router = Router();
router.route("/add").post(isAuthenticatedUser, authorizeRoles("admin"), createBusiness);
router.route("/update/:id").patch(isAuthenticatedUser, authorizeRoles("admin"), updateBusiness);
router.route("/delete/:id").delete(isAuthenticatedUser, authorizeRoles("admin"), deleteBusinessById);
router.route("/get/:id").get(isAuthenticatedUser, getSingleBusiness);
router.route("/getall").get(isAuthenticatedUser, getAllBusiness);
export default router;

View File

@ -0,0 +1,61 @@
import mongoose from "mongoose";
import { Business_Type } from "./Business_model.js";
import {
addEntity,
deleteEntity,
getEntity,
updateEntity,
} from "../../../Utils/reusableApi.js";
const getNewId = async (req, res) => {
try {
const newId = new mongoose.Types.ObjectId();
res.status(200).json({ status: "OK", data: { _id: newId } });
} catch (err) {
return res.status(500).json({ message: "Unable to get ID." });
}
};
const addBusiness_Type = async (req, res) => {
await addEntity(req, res, Business_Type);
};
const getBusiness_TypeById = async (req, res) => {
await getEntity(req, res, Business_Type);
};
const getBusiness_TypeByIdWithState = async (req, res) => {
try {
const business = await Business_Type.findById(req.params.id).populate("state");
res.status(200).json(business);
} catch (e) {
res.status(500).json({ message: "Something went wrong!" });
}
};
const getAllBusiness_Types = async (req, res) => {
try {
const business = await Business_Type.find().sort({ createdAt: -1 });
res.status(200).json({ data: business });
} catch (e) {
res.status(500).json({ message: "Something went wrong!" });
}
};
const updateBusiness_Type = async (req, res) => {
await updateEntity(req, res, Business_Type);
};
const deleteBusiness_TypeById = async (req, res) => {
await deleteEntity(req, res, Business_Type);
};
export {
getNewId,
addBusiness_Type,
getAllBusiness_Types,
getBusiness_TypeById,
updateBusiness_Type,
deleteBusiness_TypeById,
getBusiness_TypeByIdWithState,
};

View File

@ -0,0 +1,16 @@
import mongoose from "mongoose";
const { Schema, model } = mongoose;
const Business_TypeSchema = new Schema(
{
// _id: { type: Schema.Types.ObjectId },
business: { type: String, default: "" },
// state: { type: Schema.Types.ObjectId, ref: "State" },
// createdAt: { type: Date, default: new Date() },
},
// { timestamps: { createdAt: false, updatedAt: true } }
{ timestamps: true }
);
export const Business_Type = model("Business_Type", Business_TypeSchema);

View File

@ -0,0 +1,21 @@
import { Router } from "express";
const router = Router();
import {
getNewId,
addBusiness_Type,
getAllBusiness_Types,
getBusiness_TypeById,
updateBusiness_Type,
deleteBusiness_TypeById,
getBusiness_TypeByIdWithState,
} from "./business_controller.js";
router.get("/newid", getNewId);
router.get("/", getAllBusiness_Types);
router.get("/:id", getBusiness_TypeById);
router.get("/withstate/:id", getBusiness_TypeByIdWithState);
router.post("/", addBusiness_Type);
router.patch("/:id", updateBusiness_Type);
router.delete("/:id", deleteBusiness_TypeById);
export default router;

View File

@ -2,7 +2,6 @@ import { Config } from "./Config_model.js";
import cloudinary from "../../../Utils/cloudinary.js";
//Add app Name
// Add Social Media
export const addApplicationName = async (req, res) => {
@ -52,7 +51,7 @@ export const addApplicationName = async (req, res) => {
}
};
//
//add copyright msg
export const addCopyRightMessage = async (req, res) => {
@ -100,8 +99,10 @@ export const addCopyRightMessage = async (req, res) => {
console.log(error);
}
};
//add business name
//add purpose
//add language
// Add Social Media
const addSocialMedia = async (req, res) => {
const { facebook, twitter, instagram, linkedin, mail, youtube, pinterest } =
req.body;
@ -286,14 +287,14 @@ const addLogo = async (req, res) => {
const result = await cloudinary.v2.uploader.upload(
req.files.Headerlogo.tempFilePath,
{ folder: "Jatin/Logo" }
{ folder: "bolo/Logo" }
);
result1 = result.secure_url;
}
if (req.files.Footerlogo) {
const result = await cloudinary.v2.uploader.upload(
req.files.Footerlogo.tempFilePath,
{ folder: "Jatin/Logo" }
{ folder: "bolo/Logo" }
);
result2 = result.secure_url;
}
@ -301,7 +302,7 @@ const addLogo = async (req, res) => {
// console.log(req.files.Adminlogo.path)
const result = await cloudinary.v2.uploader.upload(
req.files.Adminlogo.tempFilePath,
{ folder: "Jatin/Logo" }
{ folder: "bolo/Logo" }
);
result3 = result.secure_url;
}

View File

@ -10,6 +10,9 @@ const configSchema = new Schema({
address: { type: Array, default: [] },
logo: { type: Array, default: [] },
terms_of_use: { type: String, default: "" },
purpose: { type: String, default: "" },
businessType: { type: String, default: "" },
language: { type: String, default: "" },
});
export const Config = model("Config", configSchema);

View File

@ -0,0 +1,61 @@
import mongoose from "mongoose";
import { Language } from "./language_model.js";
import {
addEntity,
deleteEntity,
getEntity,
updateEntity,
} from "../../../Utils/reusableApi.js";
const getNewId = async (req, res) => {
try {
const newId = new mongoose.Types.ObjectId();
res.status(200).json({ status: "OK", data: { _id: newId } });
} catch (err) {
return res.status(500).json({ message: "Unable to get ID." });
}
};
const addLanguage = async (req, res) => {
await addEntity(req, res, Language);
};
const getLanguageById = async (req, res) => {
await getEntity(req, res, Language);
};
const getLanguageByIdWithState = async (req, res) => {
try {
const language = await Language.findById(req.params.id).populate("state");
res.status(200).json(language);
} catch (e) {
res.status(500).json({ message: "Something went wrong!" });
}
};
const getAllLanguages = async (req, res) => {
try {
const language = await Language.find().sort({ createdAt: -1 });
res.status(200).json({ data: language });
} catch (e) {
res.status(500).json({ message: "Something went wrong!" });
}
};
const updateLanguage = async (req, res) => {
await updateEntity(req, res, Language);
};
const deleteLanguageById = async (req, res) => {
await deleteEntity(req, res, Language);
};
export {
getNewId,
addLanguage,
getAllLanguages,
getLanguageById,
updateLanguage,
deleteLanguageById,
getLanguageByIdWithState,
};

View File

@ -1,11 +1,11 @@
import mongoose from "mongoose";
const { Schema, model } = mongoose;
const CitySchema = new Schema(
const LanguageSchema = new Schema(
{
// _id: { type: Schema.Types.ObjectId },
city_name: { type: String, default: "" },
state: { type: Schema.Types.ObjectId, ref: "State" },
language: { type: String, default: "" },
// state: { type: Schema.Types.ObjectId, ref: "State" },
// createdAt: { type: Date, default: new Date() },
},
// { timestamps: { createdAt: false, updatedAt: true } }
@ -13,4 +13,4 @@ const CitySchema = new Schema(
);
export const City = model("City", CitySchema);
export const Language = model("Language", LanguageSchema);

View File

@ -0,0 +1,21 @@
import { Router } from "express";
const router = Router();
import {
getNewId,
addLanguage,
getAllLanguages,
getLanguageById,
updateLanguage,
deleteLanguageById,
getLanguageByIdWithState,
} from "./language_controller.js";
router.get("/newid", getNewId);
router.get("/", getAllLanguages);
router.get("/:id", getLanguageById);
router.get("/withstate/:id", getLanguageByIdWithState);
router.post("/", addLanguage);
router.patch("/:id", updateLanguage);
router.delete("/:id", deleteLanguageById);
export default router;

View File

@ -0,0 +1,61 @@
import mongoose from "mongoose";
import { Purpose } from "./purpose_model.js";
import {
addEntity,
deleteEntity,
getEntity,
updateEntity,
} from "../../../Utils/reusableApi.js";
const getNewId = async (req, res) => {
try {
const newId = new mongoose.Types.ObjectId();
res.status(200).json({ status: "OK", data: { _id: newId } });
} catch (err) {
return res.status(500).json({ message: "Unable to get ID." });
}
};
const addPurpose = async (req, res) => {
await addEntity(req, res, Purpose);
};
const getPurposeById = async (req, res) => {
await getEntity(req, res, Purpose);
};
const getPurposeByIdWithState = async (req, res) => {
try {
const purpose = await Purpose.findById(req.params.id).populate("state");
res.status(200).json(purpose);
} catch (e) {
res.status(500).json({ message: "Something went wrong!" });
}
};
const getAllPurposes = async (req, res) => {
try {
const purpose = await Purpose.find().sort({ createdAt: -1 });
res.status(200).json({ data: purpose });
} catch (e) {
res.status(500).json({ message: "Something went wrong!" });
}
};
const updatePurpose = async (req, res) => {
await updateEntity(req, res, Purpose);
};
const deletePurposeById = async (req, res) => {
await deleteEntity(req, res, Purpose);
};
export {
getNewId,
addPurpose,
getAllPurposes,
getPurposeById,
updatePurpose,
deletePurposeById,
getPurposeByIdWithState,
};

View File

@ -0,0 +1,13 @@
import mongoose from "mongoose";
const { Schema, model } = mongoose;
const PurposeSchema = new Schema(
{
purpose: { type: String, default: "" },
},
{ timestamps: true }
);
export const Purpose = model("Purpose", PurposeSchema);

View File

@ -0,0 +1,21 @@
import { Router } from "express";
const router = Router();
import {
getNewId,
addPurpose,
getAllPurposes,
getPurposeById,
updatePurpose,
deletePurposeById,
// getPurposeByIdWithState,
} from "./purpose_controller.js";
router.get("/newid", getNewId);
router.get("/", getAllPurposes);
router.get("/:id", getPurposeById);
// router.get("/withstate/:id", getPurposeByIdWithState)
router.post("/", addPurpose);
router.patch("/:id", updatePurpose);
router.delete("/:id", deletePurposeById);
export default router;

View File

@ -1,61 +0,0 @@
import mongoose from "mongoose";
import { City } from "./city_model.js";
import {
addEntity,
deleteEntity,
getEntity,
updateEntity,
} from "../../../Utils/reusableApi.js";
const getNewId = async (req, res) => {
try {
const newId = new mongoose.Types.ObjectId();
res.status(200).json({ status: "OK", data: { _id: newId } });
} catch (err) {
return res.status(500).json({ message: "Unable to get ID." });
}
};
const addCity = async (req, res) => {
await addEntity(req, res, City);
};
const getCityById = async (req, res) => {
await getEntity(req, res, City);
};
const getCityByIdWithState = async (req, res) => {
try {
const city = await City.findById(req.params.id).populate("state");
res.status(200).json(city);
} catch (e) {
res.status(500).json({ message: "Something went wrong!" });
}
};
const getAllCities = async (req, res) => {
try {
const cities = await City.find({}).populate("state");
res.status(200).json({ data: cities });
} catch (e) {
res.status(500).json({ message: "Something went wrong!" });
}
};
const updateCity = async (req, res) => {
await updateEntity(req, res, City);
};
const deleteCityById = async (req, res) => {
await deleteEntity(req, res, City);
};
export {
getNewId,
addCity,
getAllCities,
getCityById,
updateCity,
deleteCityById,
getCityByIdWithState,
};

View File

@ -1,21 +0,0 @@
import { Router } from "express";
const router = Router();
import {
getNewId,
addCity,
getAllCities,
getCityById,
updateCity,
deleteCityById,
getCityByIdWithState,
} from "./city_controller.js";
router.get("/newid", getNewId);
router.get("/", getAllCities);
router.get("/:id", getCityById);
router.get("/withstate/:id", getCityByIdWithState);
router.post("/", addCity);
router.patch("/:id", updateCity);
router.delete("/:id", deleteCityById);
export default router;