diff --git a/src/notification/publish/db.ts b/src/notification/publish/db.ts index 6ee8cd05..1d19dd0d 100644 --- a/src/notification/publish/db.ts +++ b/src/notification/publish/db.ts @@ -2,7 +2,7 @@ import mongoose from 'mongoose'; import dotenv from 'dotenv'; dotenv.config(); -const dbURI = `mongodb+srv://${process.env.MONGO_DB_USER}:${process.env.MONGO_DB_PASS}@luminositycluster-0.cgornhw.mongodb.net/test`; //MongoDb Connection String +const dbURI = `mongodb+srv://${process.env.MONGO_DB_USER}:${process.env.MONGO_DB_PASS}@luminositycluster-0.cgornhw.mongodb.net/Luminosity`; //MongoDb Connection String mongoose.connect(dbURI); diff --git a/src/notification/publish/publishRoutes.ts b/src/notification/publish/publishRoutes.ts deleted file mode 100644 index c603f7b8..00000000 --- a/src/notification/publish/publishRoutes.ts +++ /dev/null @@ -1,85 +0,0 @@ -// publishRoutes.ts -import express from 'express'; -import mqtt, { MqttClient } from 'mqtt'; -import db from './db'; -import { v1 as uuidv1 } from 'uuid'; -import mongoose, { Schema, Document } from 'mongoose'; -import dotenv from 'dotenv'; -dotenv.config(); - -const router = express.Router(); -const options = { - username: process.env.ACTIVE_MQ_USERNAME, - password: process.env.ACTIVE_MQ_PASSWORD, - clientId: `publish_${uuidv1()}`, - port: 1883, -}; -const topic = process.env.ACTIVE_MQ_TOPIC as string; // Type assertion - -// GET all events -router.get("/publish/events", async (_req, res) => { - try { - const EventModel = mongoose.model('Event'); - const events = await EventModel.find(); - res.json(events); - } catch (error) { - console.error(error); - res.status(500).json({ error: 'Internal Server Error' }); - } -}); - -// GET a specific event by ID -router.get("/publish/events/:id", async (req, res) => { - try { - const EventModel = mongoose.model('Event'); - const event = await EventModel.findById(req.params.id); - - if (!event) { - res.status(404).json({ error: 'Event not found' }); - return; - } - - res.json(event); - } catch (error) { - console.error(error); - res.status(500).json({ error: 'Internal Server Error' }); - } -}); - -// POST a new event -router.post("/publish/events", async (req, res) => { - try { - const EventModel = mongoose.model('Event'); - const event = { - id: uuidv1(), - message: req.body.message, - }; - - await EventModel.create(event); - - res.json(event); - } catch (error) { - console.error(error); - res.status(500).json({ error: 'Internal Server Error' }); - } -}); - -// DELETE a specific event by ID -router.delete("/publish/events/:id", async (req, res) => { - try { - const EventModel = mongoose.model('Event'); - const deletedEvent = await EventModel.findByIdAndDelete(req.params.id); - - if (!deletedEvent) { - res.status(404).json({ error: 'Event not found' }); - return; - } - - res.json({ message: 'Event deleted successfully' }); - } catch (error) { - console.error(error); - res.status(500).json({ error: 'Internal Server Error' }); - } -}); - -export default router; \ No newline at end of file diff --git a/src/notification/subscribe/subscribeRoutes.ts b/src/notification/subscribe/subscribeRoutes.ts deleted file mode 100644 index 111fe02d..00000000 --- a/src/notification/subscribe/subscribeRoutes.ts +++ /dev/null @@ -1,116 +0,0 @@ -import express from 'express'; -import mqtt, { MqttClient } from 'mqtt'; -import db from '../publish/db'; -import { v1 as uuidv1 } from 'uuid'; -import mongoose, { Schema, Document } from 'mongoose'; -import dotenv from 'dotenv'; -dotenv.config(); - -const options = { - username: process.env.ACTIVE_MQ_USERNAME, - password: process.env.ACTIVE_MQ_PASSWORD, - clientId: `subscribe_${uuidv1()}`, - port: 1883, -}; - -const topic = process.env.ACTIVE_MQ_TOPIC as string; - -router.get("/subscribe/:id", async (req: express.Request, res: express.Response) => { - try { - // Example MongoDB logic for retrieving a specific subscription - interface Subscription extends Document { - id: string; - message: string; - } - - const subscriptionSchema = new Schema({ - id: String, - message: String, - }); - - const SubscriptionModel = mongoose.model('Subscription', subscriptionSchema); - const subscriptionFromDB = await SubscriptionModel.findOne({ id: req.params.id }); - - if (!subscriptionFromDB) { - res.status(404).json({ error: 'Subscription not found' }); - return; - } - - const client: MqttClient = mqtt.connect(process.env.ACTIVE_MQ_ENDPOINT as string, options); // Type assertion - - const subscription = { - id: subscriptionFromDB.id, - message: subscriptionFromDB.message, - }; - - client.on('connect', () => { - console.log("Broker connected"); - client.publish(topic, JSON.stringify(subscription), {}, (err) => { - if (err) { - console.error(`Error publishing message: ${err}`); - res.status(500).json({ error: 'Internal Server Error' }); - } else { - client.end(); - res.json(subscription); - } - }); - }); - - } catch (error) { - console.error(error); - res.status(500).json({ error: 'Internal Server Error' }); - } -}); - -router.post("/subscribe", async (req: express.Request, res: express.Response) => { - try { - // Example MongoDB logic for handling POST request - interface Subscription extends Document { - id: string; - message: string; - } - - const subscriptionSchema = new Schema({ - id: String, - message: String, - }); - - const SubscriptionModel = mongoose.model('Subscription', subscriptionSchema); - const subscription = { - id: uuidv1(), - message: req.body.message, - }; - - await SubscriptionModel.create(subscription); - - res.json(subscription); - } catch (error) { - console.error(error); - res.status(500).json({ error: 'Internal Server Error' }); - } -}); - -router.delete("/subscribe/:id", async (req: express.Request, res: express.Response) => { - try { - // Example MongoDB logic for handling DELETE request - interface Subscription extends Document { - id: string; - message: string; - } - - const subscriptionSchema = new Schema({ - id: String, - message: String, - }); - - const SubscriptionModel = mongoose.model('Subscription', subscriptionSchema); - await SubscriptionModel.deleteOne({ id: req.params.id }); - - res.json({ message: 'Subscription deleted successfully' }); - } catch (error) { - console.error(error); - res.status(500).json({ error: 'Internal Server Error' }); - } -}); - -export default router; \ No newline at end of file