Skip to content

Commit 3a952d7

Browse files
authored
Merge fd50422 into 01d73e2
2 parents 01d73e2 + fd50422 commit 3a952d7

File tree

3 files changed

+69
-4
lines changed

3 files changed

+69
-4
lines changed

src/controllers/conversation.controller.js

+67-4
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,16 @@ exports.getAllConversation = (req, res, next) => {
2323

2424
exports.createConversation = (req, res, next) => {
2525
try {
26-
console.log("createConversation")
27-
//création de la nouvelle conversation
2826
const newConversation = new Conversation({
2927
messages: [],
3028
});
3129

32-
//ajout de la conversation dans la liste des conversations de l'utilisateur 1
30+
//adding the conversation to the user's list of conversations
3331
User.findOneAndUpdate(
3432
{ _id: req.body.user1 },
3533
{ $push: { matches: [newConversation] } }
3634
).then((User1) => {
3735

38-
//ajout de la conversation dans la liste des conversations de l'utilisateur 2
3936
User.findOneAndUpdate(
4037
{
4138
_id:
@@ -83,6 +80,72 @@ exports.createConversation = (req, res, next) => {
8380
}
8481
};
8582

83+
exports.createInstantConversation = (req, res, next) => {
84+
const newConversation = new Conversation({
85+
messages: [],
86+
});
87+
88+
User.findOne({ _id: req.userToken.id })
89+
.then((user) => {
90+
if (user.nbInstantConversationPossibilities > 0) {
91+
user.nbInstantConversationPossibilities -= 1;
92+
user.save();
93+
User.findOneAndUpdate(
94+
{ _id: req.userToken.id },
95+
{ $push: { matches: [newConversation] } }
96+
)
97+
.then((User1) => {
98+
User.findOneAndUpdate(
99+
{ _id: req.body.user2 },
100+
{ $push: { matches: [newConversation] } }
101+
)
102+
.then((User2) => {
103+
// Check if the conversation with the given members already exists
104+
Conversation.findOne({ members: { $all: [User1, User2] } })
105+
.then((existingConversation) => {
106+
if (existingConversation) {
107+
// Conversation already exists
108+
res.send({
109+
message: "Conversation already exists",
110+
conversation: existingConversation,
111+
});
112+
} else {
113+
// Conversation doesn't exist, so add new conversation
114+
newConversation.members.push(User1);
115+
newConversation.members.push(User2);
116+
newConversation.save().then((conversation) => {
117+
res.send({
118+
message:
119+
"conversation " + conversation._id + " successfully added", conversation: conversation,
120+
});
121+
})
122+
.catch((error) => {
123+
next(error);
124+
});
125+
}
126+
})
127+
.catch((error) => {
128+
next(error);
129+
});
130+
})
131+
.catch((error) => {
132+
next(error);
133+
});
134+
})
135+
.catch((error) => {
136+
next(error);
137+
});
138+
} else {
139+
res.status(400).send({
140+
message: "This user has no more instant conversation possibilities available",
141+
});
142+
}
143+
})
144+
.catch((error) => {
145+
next(error);
146+
});
147+
};
148+
86149
exports.deleteConversation = async (req, res, next) => {
87150
try {
88151
res.send("endpoint wip");

src/models/user.js

+1
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ const userSchema = new mongoose.Schema({
155155
default: "questions",
156156
},
157157
},
158+
nbInstantConversationPossibilities: { type: Number, default: 1 },
158159
});
159160

160161
module.exports = mongoose.model("User", userSchema);

src/routes/conversation.route.js

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const verifyAdmin = require('../middlewares/verifyAdmin');
99
router.get('/', verifyToken, conversationController.getAllConversation);
1010
router.post('/', conversationController.createConversation);
1111
// router.delete('/:id', verifyAdmin, conversationController.deleteConversation);
12+
router.post('/instant', verifyToken, conversationController.createInstantConversation);
1213
router.post('/block', verifyToken, conversationController.BlockConversation);
1314
router.post('/unblock', verifyToken, conversationController.UnblockConversation);
1415
router.post('/report', verifyToken, conversationController.ReportConversation);

0 commit comments

Comments
 (0)