@@ -23,19 +23,16 @@ exports.getAllConversation = (req, res, next) => {
23
23
24
24
exports . createConversation = ( req , res , next ) => {
25
25
try {
26
- console . log ( "createConversation" )
27
- //création de la nouvelle conversation
28
26
const newConversation = new Conversation ( {
29
27
messages : [ ] ,
30
28
} ) ;
31
29
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
33
31
User . findOneAndUpdate (
34
32
{ _id : req . body . user1 } ,
35
33
{ $push : { matches : [ newConversation ] } }
36
34
) . then ( ( User1 ) => {
37
35
38
- //ajout de la conversation dans la liste des conversations de l'utilisateur 2
39
36
User . findOneAndUpdate (
40
37
{
41
38
_id :
@@ -83,6 +80,72 @@ exports.createConversation = (req, res, next) => {
83
80
}
84
81
} ;
85
82
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
+
86
149
exports . deleteConversation = async ( req , res , next ) => {
87
150
try {
88
151
res . send ( "endpoint wip" ) ;
0 commit comments