@@ -10,7 +10,7 @@ import db from '@/lib/db';
1010
1111const createPaginationArgs = (
1212 filters : Types . BasePaginationFilters & { orderBy : 'createdAt' | 'updatedAt' } ,
13- limit = 10
13+ limit = 10 ,
1414) => {
1515 return {
1616 ...( filters . cursor ? { cursor : { id : filters . cursor } , skip : 1 } : { } ) ,
@@ -36,7 +36,7 @@ const createChatAggregation = () => {
3636
3737const createCurrentProfileChatArgs = (
3838 currentProfile : Prisma . ProfileGetPayload < { include : { user : true } } > ,
39- now : Date
39+ now : Date ,
4040) => {
4141 return {
4242 profileName : currentProfile . user . username ,
@@ -48,7 +48,7 @@ const createCurrentProfileChatArgs = (
4848
4949const prepareChat = (
5050 chat : Prisma . ChatGetPayload < { include : ReturnType < typeof createChatAggregation > } > ,
51- currentProfile : Profile
51+ currentProfile : Profile ,
5252) => {
5353 if ( currentProfile . tangible ) {
5454 chat . profiles = chat . profiles . map ( ( p ) => {
@@ -59,7 +59,7 @@ const prepareChat = (
5959 } ) ;
6060 } else {
6161 chat . profiles = chat . profiles . map ( ( p ) =>
62- p . profileId === currentProfile . id ? p : { ...p , lastSeenAt : null }
62+ p . profileId === currentProfile . id ? p : { ...p , lastSeenAt : null } ,
6363 ) ;
6464 }
6565 return chat ;
@@ -69,23 +69,22 @@ export const createChat = async (
6969 user : Types . PublicUser ,
7070 data : Schema . ValidChat ,
7171 imageData ?: Types . ImageFullData ,
72- uploadedImage ?: Storage . UploadedImageData
72+ uploadedImage ?: Storage . UploadedImageData ,
7373) => {
7474 return await Utils . handleDBKnownErrors (
7575 db . $transaction ( async ( tx ) => {
76- // Get current user's profile
77- const currentProfile = await tx . profile . findUnique ( {
78- where : { userId : user . id } ,
76+ const allProfiles = await tx . profile . findMany ( {
77+ where : { OR : [ { id : { in : data . profiles } } , { userId : user . id } ] } ,
7978 include : { user : true } ,
79+ distinct : 'id' ,
8080 } ) ;
81+ const otherProfiles : typeof allProfiles = [ ] ;
82+ const currentProfile = allProfiles . reduce < ( typeof allProfiles ) [ number ] | null > (
83+ ( acc , curr ) => ( curr . user . id === user . id ? curr : ( otherProfiles . push ( curr ) , acc ) ) ,
84+ null ,
85+ ) ;
8186 if ( ! currentProfile ) throw new AppNotFoundError ( 'Profile not found' ) ;
8287 const currentProfileChatArgs = createCurrentProfileChatArgs ( currentProfile , new Date ( ) ) ;
83- // Get all the other profiles
84- const otherProfiles = await tx . profile . findMany ( {
85- where : { id : { in : data . profiles } } ,
86- include : { user : true } ,
87- distinct : 'id' ,
88- } ) ;
8988 const chatAggregation = createChatAggregation ( ) ;
9089 const messageArgs = {
9190 profileName : currentProfile . user . username ,
@@ -101,19 +100,15 @@ export const createChat = async (
101100 profileId : currentProfile . id ,
102101 } ;
103102 // Find chats with the same owner and same group of profiles (there should be at most one)
104- const existentChats = await tx . chat . findMany ( {
105- where : {
106- managers : { some : { profileId : currentProfile . id , role : 'OWNER' } } ,
107- profiles : {
108- every : {
109- profileName : {
110- in : [ currentProfile . user . username , ...otherProfiles . map ( ( p ) => p . user . username ) ] ,
111- } ,
112- } ,
103+ const existentChats = (
104+ await tx . chat . findMany ( {
105+ where : {
106+ managers : { some : { profileId : currentProfile . id , role : 'OWNER' } } ,
107+ profiles : { every : { profileName : { in : allProfiles . map ( ( p ) => p . user . username ) } } } ,
113108 } ,
114- } ,
115- include : chatAggregation ,
116- } ) ;
109+ include : { ... chatAggregation , _count : { select : { profiles : true } } } ,
110+ } )
111+ ) . filter ( ( c ) => c . _count . profiles === allProfiles . length ) ; // Filter out the profiles with a mismatched count.
117112 // Upsert a chat
118113 let chat : Prisma . ChatGetPayload < { include : typeof chatAggregation } > ;
119114 if ( existentChats . length > 0 ) {
@@ -163,7 +158,7 @@ export const createChat = async (
163158 } ) ;
164159 }
165160 return prepareChat ( chat , currentProfile ) ;
166- } )
161+ } ) ,
167162 ) ;
168163} ;
169164
@@ -187,13 +182,13 @@ export const deleteChat = async (userId: User['id'], chatId: Chat['id']) => {
187182 }
188183 }
189184 }
190- } )
185+ } ) ,
191186 ) ;
192187} ;
193188
194189export const getUserChats = async (
195190 userId : User [ 'id' ] ,
196- filters : Types . BasePaginationFilters = { }
191+ filters : Types . BasePaginationFilters = { } ,
197192) => {
198193 return await Utils . handleDBKnownErrors (
199194 db . $transaction ( async ( tx ) => {
@@ -214,7 +209,7 @@ export const getUserChats = async (
214209 include : createChatAggregation ( ) ,
215210 } ) ;
216211 return chats . map ( ( chat ) => prepareChat ( chat , currentProfile ) ) ;
217- } )
212+ } ) ,
218213 ) ;
219214} ;
220215
@@ -242,19 +237,24 @@ export const getUserChatById = async (userId: User['id'], chatId: Chat['id']) =>
242237 throw new AppNotFoundError ( 'Chat not found' ) ;
243238 }
244239 return prepareChat ( chat , currentProfile ) ;
245- } )
240+ } ) ,
246241 ) ;
247242} ;
248243
249- export const getUserChatsByMemberProfileId = async (
250- userId : User [ 'id' ] ,
251- profileId : Profile [ 'id' ]
252- ) => {
244+ export const getUserChatsByMember = async ( userId : User [ 'id' ] , memberIdOrUsername : string ) => {
253245 return await Utils . handleDBKnownErrors (
254246 db . $transaction ( async ( tx ) => {
247+ let profileId : Profile [ 'id' ] ;
255248 try {
256- const memberProfile = await tx . profile . findUnique ( { where : { id : profileId } } ) ;
249+ const memberUser = await tx . user . findUnique ( {
250+ where : { username : memberIdOrUsername } ,
251+ include : { profile : true } ,
252+ } ) ;
253+ const memberProfile =
254+ memberUser ?. profile ??
255+ ( await tx . profile . findUnique ( { where : { id : memberIdOrUsername } } ) ) ;
257256 if ( ! memberProfile ) throw new AppNotFoundError ( 'Chat member profile not found' ) ;
257+ profileId = memberProfile . id ;
258258 } catch {
259259 throw new AppNotFoundError ( 'Chat member profile not found' ) ;
260260 }
@@ -281,14 +281,14 @@ export const getUserChatsByMemberProfileId = async (
281281 } ) ;
282282 if ( ! currentProfileWithChats ) throw new AppNotFoundError ( 'Profile not found' ) ;
283283 return currentProfileWithChats . chats . map ( ( c ) => prepareChat ( c . chat , currentProfileWithChats ) ) ;
284- } )
284+ } ) ,
285285 ) ;
286286} ;
287287
288288export const getUserChatMessages = async (
289289 userId : User [ 'id' ] ,
290290 chatId : Chat [ 'id' ] ,
291- filters : Types . BasePaginationFilters = { }
291+ filters : Types . BasePaginationFilters = { } ,
292292) => {
293293 return await Utils . handleDBKnownErrors (
294294 db . $transaction ( async ( tx ) => {
@@ -315,14 +315,14 @@ export const getUserChatMessages = async (
315315 where : { chat : { id : chatId , profiles : { some : { profileId } } } } ,
316316 include : createMessageAggregation ( ) ,
317317 } ) ;
318- } )
318+ } ) ,
319319 ) ;
320320} ;
321321
322322export const getUserChatMessageById = async (
323323 userId : User [ 'id' ] ,
324324 chatId : Chat [ 'id' ] ,
325- msgId : Message [ 'id' ]
325+ msgId : Message [ 'id' ] ,
326326) => {
327327 return await Utils . handleDBKnownErrors (
328328 db . $transaction ( async ( tx ) => {
@@ -343,7 +343,7 @@ export const getUserChatMessageById = async (
343343 } ) ;
344344 if ( ! message ) throw new AppNotFoundError ( 'Message not found' ) ;
345345 return message ;
346- } )
346+ } ) ,
347347 ) ;
348348} ;
349349
@@ -352,7 +352,7 @@ export const createUserChatMessage = async (
352352 chatId : Chat [ 'id' ] ,
353353 { body } : Schema . ValidMessage ,
354354 imageData ?: Types . ImageFullData ,
355- uploadedImage ?: Storage . UploadedImageData
355+ uploadedImage ?: Storage . UploadedImageData ,
356356) => {
357357 return await Utils . handleDBKnownErrors (
358358 db . $transaction ( async ( tx ) => {
@@ -393,7 +393,7 @@ export const createUserChatMessage = async (
393393 } ,
394394 include : createMessageAggregation ( ) ,
395395 } ) ;
396- } )
396+ } ) ,
397397 ) ;
398398} ;
399399
@@ -411,7 +411,7 @@ export const updateProfileChatLastSeenDate = async (userId: User['id'], chatId:
411411 where : { profileName_chatId : { profileName, chatId } } ,
412412 data : { lastSeenAt : now } ,
413413 } ) ;
414- } )
414+ } ) ,
415415 ) ;
416416 return now ;
417417} ;
0 commit comments