generated from Arquisoft/wiq_0
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from Arquisoft/groupsService1
Group Service
- Loading branch information
Showing
19 changed files
with
6,423 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
node_modules | ||
coverage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Use an official Node.js runtime as a parent image | ||
FROM node:20 | ||
|
||
# Set the working directory in the container | ||
WORKDIR /usr/src/groupservice | ||
|
||
# Copy package.json and package-lock.json to the working directory | ||
COPY package*.json ./ | ||
|
||
# Install app dependencies | ||
RUN npm install | ||
|
||
# Copy the app source code to the working directory | ||
COPY . . | ||
|
||
# Expose the port the app runs on | ||
EXPOSE 8005 | ||
|
||
# Define the command to run your app | ||
CMD ["node", "group-service.js"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
const uuid = require('uuid'); | ||
const generateJoinCode = require('./util/GenerateJoinCode'); | ||
const Group = require('./group-model'); | ||
|
||
const maxNumUsers = 30; | ||
|
||
let GroupController = { | ||
joinGroup: async (req,res) => { | ||
try{ | ||
requiredFields = ['uuid','groupName'] | ||
validateRequiredFields(req, requiredFields); | ||
const group = await getGroupByName(req.body.groupName); | ||
|
||
if(group.members.includes(req.body.uuid)){ | ||
res.json({ message: 'User is already in this group' }); | ||
return; | ||
} | ||
|
||
if(group.members.length == maxNumUsers){ | ||
res.json({ message: 'This group is full' }); | ||
return; | ||
} | ||
|
||
if(!group.isPublic){ | ||
if(group.joinCode != req.body.joinCode){ | ||
res.json({ message: 'Invalid join code' }); | ||
return; | ||
} | ||
} | ||
|
||
group.members.push(req.body.uuid); | ||
|
||
const response = await group.save() | ||
|
||
res.json(response); | ||
|
||
}catch(error){ | ||
console.log(error) | ||
res.status(400).json({error: error.message}) | ||
} | ||
|
||
}, | ||
leaveGroup: async (req,res) => { | ||
try{ | ||
console.log(req.body) | ||
requiredFields = ['expelledUUID','groupName', 'adminUUID'] | ||
validateRequiredFields(req, requiredFields); | ||
const group = await getGroupByName(req.body.groupName); | ||
console.log(req.body.adminUUID +" - "+ req.body.expelledUUID) | ||
if(req.body.adminUUID != group.admin && req.body.adminUUID != req.body.expelledUUID){ | ||
console.log("entra en la condicion") | ||
res.json({ message: 'User is unable to perform this operation' }); | ||
return; | ||
} | ||
|
||
group.members = group.members.filter(member => member != req.body.expelledUUID) | ||
|
||
if(group.members.length == 0){ | ||
await group.deleteOne() | ||
res.json({ message: 'Group deleted' }); | ||
return; | ||
} | ||
|
||
if(group.admin == req.body.expelledUUID){ | ||
group.admin = group.members.at(0); | ||
} | ||
|
||
const response = await group.save() | ||
res.json(response); | ||
|
||
}catch(error){ | ||
console.log(error) | ||
res.status(500).json({error: error.message}) | ||
} | ||
}, | ||
createGroup: async (req,res) =>{ | ||
try{ | ||
|
||
requiredFields =['groupName','creatorUUID','description','isPublic'] | ||
validateRequiredFields(req,requiredFields); | ||
|
||
let newGroup; | ||
if(req.body.isPublic){ | ||
|
||
newGroup = new Group({ | ||
admin: req.body.creatorUUID, | ||
members: [req.body.creatorUUID], | ||
maxNumUsers: maxNumUsers, | ||
description: req.body.description, | ||
isPublic: true, | ||
creationDate: Date(), | ||
groupName: req.body.groupName, | ||
uuid: uuid.v4(), | ||
}) | ||
await newGroup.save(); | ||
|
||
} else { | ||
const joinCode = generateJoinCode(); | ||
|
||
newGroup = new Group({ | ||
groupName: req.body.groupName, | ||
admin: req.body.creatorUUID, | ||
members: [req.body.creatorUUID], | ||
maxNumUsers: maxNumUsers, | ||
description: req.body.description, | ||
isPublic: false, | ||
joinCode: joinCode, | ||
creationDate: Date(), | ||
uuid: uuid.v4(), | ||
}); | ||
await newGroup.save(); | ||
} | ||
res.json(newGroup); | ||
|
||
} catch(error){ | ||
res.status(500).json({error: error.message}) | ||
} | ||
|
||
}, | ||
getGroup: async (req, res) => { | ||
try{ | ||
const uuid = req.params.uuid | ||
const group = await Group.findOne({uuid: uuid}) | ||
if(!group){ | ||
res.status(404).json({error: 'Group not found'}) | ||
return; | ||
} | ||
res.json(group) | ||
} catch(error){ | ||
res.status(500).json({error: error.message}) | ||
} | ||
} | ||
} | ||
|
||
async function getGroupByName(name) { | ||
try { | ||
const group = await Group.findOne({ groupName: name }); | ||
|
||
if (!group) { | ||
throw new Error('This group does not exist'); | ||
} | ||
|
||
return group; | ||
} catch (error) { | ||
// Handle the error here | ||
throw new Error(error.message); | ||
} | ||
} | ||
|
||
function validateRequiredFields(req, requiredFields) { | ||
for (const field of requiredFields) { | ||
if (!(field in req.body)) { | ||
throw new Error(`Missing required field: ${field}`); | ||
} | ||
} | ||
} | ||
|
||
module.exports = GroupController; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
const mongoose = require('mongoose'); | ||
|
||
const groupSchema = new mongoose.Schema({ | ||
uuid: { | ||
type: String, | ||
required: true, | ||
}, | ||
groupName: { | ||
type: String, | ||
required: true, | ||
unique: true, | ||
}, | ||
admin: { | ||
type: String, | ||
required: true, | ||
}, | ||
members:[{ | ||
type: String, | ||
required: true, | ||
}], | ||
maxNumUsers: { | ||
type: Number, | ||
required: true, | ||
}, | ||
description: { | ||
type: String, | ||
required: false, | ||
}, | ||
isPublic: { | ||
type: Boolean, | ||
required: true, | ||
}, | ||
joinCode: { | ||
type: String, | ||
required: false, | ||
}, | ||
creationDate: { | ||
type: Date, | ||
default: Date.now, | ||
} | ||
}); | ||
|
||
const Group = mongoose.model('Group', groupSchema); | ||
|
||
module.exports = Group; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
const express = require('express'); | ||
const mongoose = require('mongoose'); | ||
const GroupController = require('./GroupController'); | ||
|
||
const app = express(); | ||
const port = 8005; | ||
|
||
app.use(express.json()); | ||
|
||
// Connect to MongoDB | ||
const mongoUri = process.env.MONGODB_URI || 'mongodb://localhost:27017/userdb'; | ||
mongoose.connect(mongoUri); | ||
|
||
app.get('/', (req, res) => { | ||
res.json({ message: 'Welcome to group service module' }); | ||
}); | ||
|
||
app.post('/joinGroup', GroupController.joinGroup); | ||
app.post('/leaveGroup', GroupController.leaveGroup); | ||
app.post('/createGroup', GroupController.createGroup); | ||
app.get('/getGroup/:uuid', GroupController.getGroup); | ||
|
||
const server = app.listen(port, () => { | ||
console.log(`Group Service listening at http://localhost:${port}`); | ||
}); | ||
|
||
server.on('close', () => { | ||
// Close the Mongoose connection | ||
mongoose.connection.close(); | ||
}); | ||
|
||
module.exports = server |
Oops, something went wrong.