@@ -226,17 +226,21 @@ export class Chats {
226226 }
227227
228228 async addChat ( { userId, recipientsId } : { userId : string , recipientsId : string [ ] } ) {
229- if ( recipientsId . length === 1 ) {
230- const { rows } = await this . db . query ( sql `
231- SELECT chats.* FROM chats, (SELECT * FROM chats_users WHERE user_id = ${ userId } ) AS chats_of_current_user, chats_users
232- WHERE chats_users.chat_id = chats_of_current_user.chat_id
233- AND chats.id = chats_users.chat_id AND chats_users.user_id IN (${ recipientsId . toString ( ) } )
234- ` ) ;
235- // If there is already a chat between these two users, return it
236- if ( rows [ 0 ] ) {
237- return rows [ 0 ] ;
238- }
239- }
229+ // TEMPORARILY REMOVING CHECK FOR EXISTING CHAT DUE TO PROBLEMS WITH SQL QUERY
230+ // if (recipientsId.length === 1) {
231+ // const recipient = recipientsId[0];
232+ // console.log("inside if 1 found loop")
233+ // const { rows } = await this.db.query(sql`
234+ // SELECT chats.* FROM chats, (SELECT * FROM chats_users WHERE user_id = ${userId}) AS chats_of_current_user, chats_users
235+ // WHERE chats_users.chat_id = chats_of_current_user.chat_id
236+ // AND chats.id = chats_users.chat_id AND chats_users.user_id = ${recipient.toString()}
237+ // `);
238+ // // If there is already a chat between these two users, return it
239+ // if (rows[0]) {
240+ // console.log("found a match returning existing chat")
241+ // return rows[0];
242+ // }
243+ // }
240244 try {
241245 await this . db . query ( "BEGIN" ) ;
242246 const { rows } = await this . db . query ( sql `
@@ -249,13 +253,8 @@ export class Chats {
249253 INSERT INTO chats_users(chat_id, user_id)
250254 VALUES(${ chatAdded . id } , ${ userId } )
251255 ` ) ;
252- for ( var i = 0 ; i < recipientsId . length ; i ++ ) {
253- const groupOfRecipients = recipientsId [ i ]
254- await this . db . query ( sql `
255- INSERT INTO chats_users(chat_id, user_id)
256- VALUES(${ chatAdded . id } , ${ groupOfRecipients } )
257- ` ) ;
258- }
256+
257+ await this . addRecipientsToChat ( recipientsId , chatAdded )
259258 await this . db . query ( "COMMIT" ) ;
260259 this . pubsub . publish ( "chatAdded" , {
261260 chatAdded
@@ -265,7 +264,16 @@ export class Chats {
265264 await this . db . query ( "ROLLBACK" ) ;
266265 throw e ;
267266 }
267+ }
268268
269+ async addRecipientsToChat ( recipientsId : any [ ] , chatAdded : any ) {
270+ for ( var i = 0 ; i < recipientsId . length ; i ++ ) {
271+ const groupOfRecipients = recipientsId [ i ]
272+ await this . db . query ( sql `
273+ INSERT INTO chats_users(chat_id, user_id)
274+ VALUES(${ chatAdded . id } , ${ groupOfRecipients } )
275+ ` ) ;
276+ }
269277 }
270278
271279 async removeChat ( { chatId, userId } : { chatId : string ; userId : string } ) {
0 commit comments