This commit is contained in:
MD ARIF KHAN 2023-07-28 17:18:10 +05:30
parent ac12d5dfd4
commit 258b3e48f1
6 changed files with 99 additions and 3 deletions

4
app.js
View File

@ -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;

View File

@ -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)

View File

@ -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();

View File

@ -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,
});
}
};

View File

@ -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);

View File

@ -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;