Skip to content

Commit 546619c

Browse files
Allow finding chats by member's id or username
1 parent 0dcfb76 commit 546619c

File tree

3 files changed

+42
-6
lines changed

3 files changed

+42
-6
lines changed

src/api/v1/chats/chats.router.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ chatsRouter.get('/', Middlewares.authValidator, async (req, res) => {
3333

3434
chatsRouter.get('/members/:profileId', Middlewares.authValidator, async (req, res) => {
3535
const userId = Utils.getCurrentUserIdFromReq(req)!;
36-
const chats = await Service.getUserChatsByMemberProfileId(userId, req.params.profileId);
36+
const chats = await Service.getUserChatsByMember(userId, req.params.profileId);
3737
res.json(chats);
3838
});
3939

src/api/v1/chats/chats.service.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -241,15 +241,20 @@ export const getUserChatById = async (userId: User['id'], chatId: Chat['id']) =>
241241
);
242242
};
243243

244-
export const getUserChatsByMemberProfileId = async (
245-
userId: User['id'],
246-
profileId: Profile['id'],
247-
) => {
244+
export const getUserChatsByMember = async (userId: User['id'], memberIdOrUsername: string) => {
248245
return await Utils.handleDBKnownErrors(
249246
db.$transaction(async (tx) => {
247+
let profileId: Profile['id'];
250248
try {
251-
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 } }));
252256
if (!memberProfile) throw new AppNotFoundError('Chat member profile not found');
257+
profileId = memberProfile.id;
253258
} catch {
254259
throw new AppNotFoundError('Chat member profile not found');
255260
}

src/tests/api/v1/chats.int.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,37 @@ describe('Chats endpoints', async () => {
322322
}
323323
assertReceivedDateUpdated(resChat1, dbChat1, userOneData.username);
324324
assertReceivedDateUpdated(resChat2, dbChat2, userOneData.username);
325+
await db.chat.delete({ where: { id: dbChat2.id } });
326+
});
327+
328+
it('should respond all the current user chats that include the given member`s', async () => {
329+
const memberUsername = dbUserTwo.username;
330+
const dbChatId1 = dbChats.find((c) =>
331+
c.profiles.some((p) => p.profileName === memberUsername),
332+
)!.id;
333+
const dbChat1 = (await db.chat.findUnique({
334+
where: { id: dbChatId1 },
335+
include: { profiles: true },
336+
}))!;
337+
const dbChat2 = await createChat('Hello #2', [dbUserOne, dbUserTwo, dbXUser]);
338+
const { authorizedApi } = await prepForAuthorizedTest(userOneData);
339+
const res = await authorizedApi.get(`${CHATS_URL}/members/${memberUsername}`);
340+
const resBody = res.body as ChatFullData[];
341+
const resChat1 = resBody.find((c) => c.id === dbChat1.id)!;
342+
const resChat2 = resBody.find((c) => c.id === dbChat2.id)!;
343+
expect(res.statusCode).toBe(200);
344+
expect(res.type).toMatch(/json/);
345+
expect(resBody).toBeInstanceOf(Array);
346+
expect(resBody).toHaveLength(2);
347+
expect(resBody.every((c) => c.id === dbChat1.id || c.id === dbChat2.id)).toBe(true);
348+
for (const c of resBody) {
349+
if (c.id === dbChat2.id) assertChat(c, c.id, 1, 3);
350+
else assertChat(c, c.id);
351+
assertChatMembersTangibility(c);
352+
}
353+
assertReceivedDateUpdated(resChat1, dbChat1, userOneData.username);
354+
assertReceivedDateUpdated(resChat2, dbChat2, userOneData.username);
355+
await db.chat.delete({ where: { id: dbChat2.id } });
325356
});
326357
});
327358

0 commit comments

Comments
 (0)