Skip to content

Commit

Permalink
Latest changes
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinLemon112 committed Jun 23, 2024
1 parent 8e34e98 commit a4ad7ea
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 12 deletions.
2 changes: 0 additions & 2 deletions src/controllers/publishController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import { v1 as uuidv1 } from 'uuid';
import mongoose from 'mongoose';
import dotenv from 'dotenv';
dotenv.config();
import db from '../notification/publish/db'; // Import the db.ts file to access the dbURI variable


const options = {
username: process.env.ACTIVE_MQ_USERNAME,
Expand Down
22 changes: 19 additions & 3 deletions src/controllers/userController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export const registerUser = async (req: Request, res: Response): Promise<void> =
const { email, name, deletionDate, devicesLinked } = req.body;
const currentDate = new Date();

// Create a new user instance
const newUser: UserInterface = new User({
email,
name,
Expand All @@ -17,12 +18,27 @@ export const registerUser = async (req: Request, res: Response): Promise<void> =
devicesLinked: [], // Initialize as an empty array
});

// Check if devicesLinked is provided and not empty
if (devicesLinked && devicesLinked.length > 0) {
// Assuming devicesLinked is an array of objects that match DeviceInterface
const devices: any[] = await Device.insertMany(devicesLinked); // Cast to any[] temporarily
newUser.devicesLinked = devices.map((device: any) => device._id); // Map to _id
const savedDevices: DeviceInterface[] = [];

// Iterate through devicesLinked array to create and save devices
for (const deviceData of devicesLinked) {
const newDeviceData: DeviceInterface = {
...deviceData,
lastUpdated: currentDate,
};

const newDevice = new Device(newDeviceData);
const savedDevice = await newDevice.save();
savedDevices.push(savedDevice);
}

// Map the saved device IDs to newUser.devicesLinked
newUser.devicesLinked = savedDevices.map((device: DeviceInterface) => device._id);
}

// Save the user with linked devices
const savedUser: UserInterface = await newUser.save();
res.status(201).json(savedUser);
} catch (error) {
Expand Down
21 changes: 20 additions & 1 deletion src/notification/publish/publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@ import mqtt, { MqttClient } from 'mqtt';
import { v1 as uuidv1 } from 'uuid';
import dotenv from 'dotenv';
import express from 'express';
import mongoose, { ConnectOptions } from 'mongoose'; // Import mongoose for MongoDB connection
import dbConfig from './db'; // Import MongoDB configuration from db.ts

dotenv.config();

const app = express();
const port = process.env.PORT || 4000;

const options = {
username: process.env.ACTIVE_MQ_USERNAME,
password: process.env.ACTIVE_MQ_PASSWORD,
Expand All @@ -23,6 +28,16 @@ client.on('error', (error) => {
console.error(error);
});

mongoose.connect(dbConfig.dbURI, {
useNewUrlParser: true,
useUnifiedTopology: true,
} as ConnectOptions).then(() => {
console.log('MongoDB connected');
}).catch((err) => {
console.error('MongoDB connection error:', err);
process.exit(1); // Exit process on connection error
});

const publishRouter = express.Router();

publishRouter.post('/publish', (req, res) => {
Expand All @@ -42,4 +57,8 @@ publishRouter.post('/publish', (req, res) => {
});
});

export { publishRouter };
app.use('/publish', publishRouter);

app.listen(port, () => {
console.log(`Publish service is running on port ${port}`);
});
10 changes: 5 additions & 5 deletions src/routes/userRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import * as userController from '../controllers/userController';

const router = express.Router();

// Define routes for users (formerly both user and account routes)
router.post('/users/register', userController.registerUser);
router.get('/users/all', userController.getAllUsers);
router.get('/users/:id', userController.getUserById);
router.delete('/users/:id/delete', userController.deleteUserById);
// Define routes for users
router.post('/register', userController.registerUser);
router.get('/all', userController.getAllUsers);
router.get('/:id', userController.getUserById);
router.delete('/:id/delete', userController.deleteUserById);

export default router;
2 changes: 1 addition & 1 deletion src/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ app.use(cors());

// app.use("/app", routeURLs); // Existing routes <- DEPRECATED

app.use("/user", userRouter); // Account service routes
app.use("/users", userRouter); // User service routes
app.use("/publish", publishRouter); // Publish service routes
app.use("/subscribe", subscribeRouter); // Subscribe service routes

Expand Down

0 comments on commit a4ad7ea

Please sign in to comment.