diff --git a/app.js b/app.js index db2359b..fd9d80e 100644 --- a/app.js +++ b/app.js @@ -49,6 +49,8 @@ import SpecialtiesRouter from "./resources/Specialties/SpecialtiesRoute.js"; import SpecialistRouter from "./resources/Specialist/SpecialistRoute.js"; //appointments import AppointmentRouter from "./resources/Appointments/AppointmentRoute.js"; +//short urls +import ShortUrlRouter from "./resources/Businesses/Short_Urls/ShortUrlRoute.js"; app.use("/api/v1/", user); @@ -84,5 +86,7 @@ app.use("/api/config/specialty", SpecialtiesRouter); app.use("/api/specialist", SpecialistRouter); //appointments app.use("/api/appointment", AppointmentRouter); +//short urls +app.use("/api/shorturl", ShortUrlRouter); export default app; diff --git a/resources/Businesses/BusinessController.js b/resources/Businesses/BusinessController.js index c7770bc..96f5ba0 100644 --- a/resources/Businesses/BusinessController.js +++ b/resources/Businesses/BusinessController.js @@ -1,6 +1,7 @@ import sendEmail from "../../Utils/sendEmail.js"; import cloudinary from "../../Utils/cloudinary.js"; import { Business } from "./BusinessModel.js"; +import { ShortUrl } from "./Short_Urls/ShortUrlModel.js"; import password from "secure-random-password"; import bcrypt from "bcryptjs"; @@ -62,6 +63,7 @@ export const createBusiness = async (req, res) => { slug.length > 0 ? `${req.body?.short_url}-${slug.length}` : req.body?.short_url; + req.body.short_url = uniqueSlug; req.body.url = `${url}${uniqueSlug}`; let businesse = await Business.findOne({ email }); @@ -102,6 +104,10 @@ export const createBusiness = async (req, res) => { req.body.added_by = req.user._id; const businesses = await Business.create(req.body); + const shortUrls = await ShortUrl.create({ + shortUrl: req.body?.short_url, + HealthCareProviderID: businesses._id, + }); await sendEmail({ to: `${req.body.email}`, // Change to your recipient @@ -162,7 +168,6 @@ export const getBusinessDetails = catchAsyncErrors(async (req, res, next) => { 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 !" }); @@ -293,13 +298,13 @@ export const deleteBusinessById = async (req, res) => { if (!req?.params.id) return res.status(400).json({ message: "please Provide Business ID !" }); - const business = await Business.findByIdAndDelete(req.params.id); + const business = await Business.findById(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" }); + res.status(200).json({ status: "OK", msg: "Deleted successfully" }); } catch (err) { return res .status(500) diff --git a/resources/Businesses/BusinessModel.js b/resources/Businesses/BusinessModel.js index 32cf1ed..676dca9 100644 --- a/resources/Businesses/BusinessModel.js +++ b/resources/Businesses/BusinessModel.js @@ -3,6 +3,9 @@ import validator from "validator"; import bcrypt from "bcryptjs"; import jwt from "jsonwebtoken"; import crypto from "crypto"; +import { ShortUrl } from "./Short_Urls/ShortUrlModel.js"; +import { Appointment } from "../Appointments/AppointmentModel.js"; +import { Specialist } from "../Specialist/SpecialistModel.js"; const { Schema, model } = mongoose; const BusinessSchema = new Schema( @@ -53,6 +56,26 @@ const BusinessSchema = new Schema( { timestamps: true } ); +BusinessSchema.pre("remove", async function (next) { + try { + // Delete all the short urls associated with the healthcare provider + await ShortUrl.deleteMany({ HealthCareProviderID: this._id }); + + // Delete all the appointments associated with the healthcare provider + await Appointment.deleteMany({ + HealthCareProviderID: this._id, + }); + + // Delete all the specialist associated with the healthcare provider + await Specialist.deleteMany({ + HealthCareProviderID: this._id, + }); + next(); + } catch (error) { + next(error); + } +}); + BusinessSchema.pre("save", async function (next) { if (!this.isModified("password")) { next(); diff --git a/resources/Businesses/Short_Urls/ShortUrlController.js b/resources/Businesses/Short_Urls/ShortUrlController.js new file mode 100644 index 0000000..a370fea --- /dev/null +++ b/resources/Businesses/Short_Urls/ShortUrlController.js @@ -0,0 +1,39 @@ +import { ShortUrl } from "./ShortUrlModel.js"; + +// get all short urls +export const getAllShortUrls = async (req, res) => { + try { + const shortUrls = await ShortUrl.find(); + res.status(200).json({ + success: true, + shortUrls, + }); + } catch (error) { + res.status(400).json({ + success: false, + message: error.message, + }); + } +}; + +// get a single short url +export const getSingleShortUrl = async (req, res) => { + try { + const shortUrl = await ShortUrl.findById(req.params.id); + if (!shortUrl) { + return res.status(404).json({ + success: false, + message: "Short url not found", + }); + } + res.status(200).json({ + success: true, + shortUrl, + }); + } catch (error) { + res.status(400).json({ + success: false, + message: error.message, + }); + } +}; diff --git a/resources/Businesses/Short_Urls/ShortUrlModel.js b/resources/Businesses/Short_Urls/ShortUrlModel.js new file mode 100644 index 0000000..ecc55cd --- /dev/null +++ b/resources/Businesses/Short_Urls/ShortUrlModel.js @@ -0,0 +1,16 @@ +import mongoose from "mongoose"; +const { Schema, model } = mongoose; + +const ShortUrlSchema = new Schema( + { + shortUrl: { type: String, required: true }, + HealthCareProviderID: { + type: mongoose.Schema.ObjectId, + ref: "Businesses", + required: true, + }, + }, + { timestamps: true } +); + +export const ShortUrl = model("ShortUrl", ShortUrlSchema); diff --git a/resources/Businesses/Short_Urls/ShortUrlRoute.js b/resources/Businesses/Short_Urls/ShortUrlRoute.js new file mode 100644 index 0000000..c402dde --- /dev/null +++ b/resources/Businesses/Short_Urls/ShortUrlRoute.js @@ -0,0 +1,9 @@ +import { Router } from "express"; +import { getAllShortUrls, getSingleShortUrl } from "./ShortUrlController.js"; + +const router = Router(); + +router.route("/getall").get(getAllShortUrls); +router.route("/get/:id").get(getSingleShortUrl); + +export default router;