From 645d56441ea59c79ee822478d65740dbef7ce8a2 Mon Sep 17 00:00:00 2001 From: Mohammed S Date: Thu, 30 Nov 2023 13:10:03 +0530 Subject: [PATCH] fix: participant high level functions (#896) --- .../restapi/src/lib/chat/getGroupMembers.ts | 23 +++++-- packages/restapi/src/lib/pushapi/chat.ts | 65 ++++++++++++++----- .../restapi/src/lib/pushapi/pushAPITypes.ts | 12 +++- packages/restapi/src/lib/types/index.ts | 14 ++++ .../tests/lib/notification/base.test.ts | 2 +- 5 files changed, 92 insertions(+), 24 deletions(-) diff --git a/packages/restapi/src/lib/chat/getGroupMembers.ts b/packages/restapi/src/lib/chat/getGroupMembers.ts index 30bc7d2cd..ed4587cfd 100644 --- a/packages/restapi/src/lib/chat/getGroupMembers.ts +++ b/packages/restapi/src/lib/chat/getGroupMembers.ts @@ -1,7 +1,7 @@ import axios from 'axios'; import { getAPIBaseUrls } from '../helpers'; import Constants, { ENV } from '../constants'; -import {ChatMemberProfile } from '../types'; +import { ChatMemberProfile } from '../types'; /** * GET /v1/chat/:chatId/members @@ -11,13 +11,22 @@ export interface FetchChatGroupInfoType { chatId: string; page?: number; limit?: number; + pending?: boolean; + role?: string; env?: ENV; } export const getGroupMembers = async ( options: FetchChatGroupInfoType ): Promise => { - const { chatId, page = 1, limit = 20, env = Constants.ENV.PROD } = options; + const { + chatId, + page = 1, + limit = 20, + env = Constants.ENV.PROD, + pending, + role, + } = options; try { if (!chatId) { @@ -25,8 +34,13 @@ export const getGroupMembers = async ( } const API_BASE_URL = getAPIBaseUrls(env); - const requestUrl = `${API_BASE_URL}/v1/chat/groups/${chatId}/members?pageNumber=${page}&pageSize=${limit}`; - + let requestUrl = `${API_BASE_URL}/v1/chat/groups/${chatId}/members?pageNumber=${page}&pageSize=${limit}`; + if (pending !== undefined) { + requestUrl += `&pending=${pending}`; + } + if (role) { + requestUrl += `&role=${encodeURIComponent(role)}`; + } const response = await axios.get(requestUrl); return response.data.members; } catch (error) { @@ -39,4 +53,3 @@ export const getGroupMembers = async ( ); } }; - diff --git a/packages/restapi/src/lib/pushapi/chat.ts b/packages/restapi/src/lib/pushapi/chat.ts index cddfcd7a9..3e1651245 100644 --- a/packages/restapi/src/lib/pushapi/chat.ts +++ b/packages/restapi/src/lib/pushapi/chat.ts @@ -13,6 +13,7 @@ import { GroupInfoDTO, ChatMemberProfile, ChatMemberCounts, + GroupParticipantCounts, } from '../types'; import { GroupUpdateOptions, @@ -21,6 +22,7 @@ import { ManageGroupOptions, RemoveFromGroupOptions, GetGroupParticipantsOptions, + ParticipantStatus, } from './pushAPITypes'; import * as PUSH_USER from '../user'; import * as PUSH_CHAT from '../chat'; @@ -33,6 +35,8 @@ import { import { User } from './user'; import { updateGroupConfig } from '../chat/updateGroupConfig'; import { PushAPI } from './PushAPI'; +import { ParticipantsType } from './participant'; + export class Chat { private userInstance: User; private scalabilityV2Feature: boolean; @@ -317,23 +321,50 @@ export class Chat { } }, - participants: async ( - chatId: string, - options?: GetGroupParticipantsOptions - ): Promise<{ count: ChatMemberCounts; members: ChatMemberProfile[] }> => { - const { page = 1, limit = 20 } = options ?? {}; - const getGroupMembersOptions: PUSH_CHAT.FetchChatGroupInfoType = { - chatId, - page, - limit, - env: this.env, - }; - const count = await PUSH_CHAT.getGroupMemberCount({ - chatId, - env: this.env, - }); - const members = await PUSH_CHAT.getGroupMembers(getGroupMembersOptions); - return { count, members }; + participants: { + list: async ( + chatId: string, + options?: GetGroupParticipantsOptions + ): Promise<{ members: ChatMemberProfile[] }> => { + const { page = 1, limit = 20 } = options ?? {}; + const getGroupMembersOptions: PUSH_CHAT.FetchChatGroupInfoType = { + chatId, + page, + limit, + env: this.env, + }; + + const members = await PUSH_CHAT.getGroupMembers(getGroupMembersOptions); + return { members }; + }, + + count: async (chatId: string): Promise => { + const count = await PUSH_CHAT.getGroupMemberCount({ + chatId, + env: this.env, + }); + return { + participants: count.overallCount - count.pendingCount, + pending: count.pendingCount, + }; + }, + + status: async ( + chatId: string, + accountId: string + ): Promise => { + const status = await PUSH_CHAT.getGroupMemberStatus({ + chatId: chatId, + did: accountId, + env: this.env, + }); + + return { + pending: status.isPending, + role: status.isAdmin ? 'ADMIN' : 'MEMBER', + participant: status.isMember, + }; + }, }, permissions: async (chatId: string): Promise => { diff --git a/packages/restapi/src/lib/pushapi/pushAPITypes.ts b/packages/restapi/src/lib/pushapi/pushAPITypes.ts index b17cf8fbc..278e43250 100644 --- a/packages/restapi/src/lib/pushapi/pushAPITypes.ts +++ b/packages/restapi/src/lib/pushapi/pushAPITypes.ts @@ -1,5 +1,5 @@ import Constants, { ENV } from '../constants'; -import { ChatStatus, ProgressHookType, Rules } from '../types'; +import { ChatMemberCounts, ChatMemberProfile, ChatStatus, ProgressHookType, Rules } from '../types'; export enum ChatListType { CHATS = 'CHATS', @@ -40,6 +40,10 @@ export interface RemoveFromGroupOptions { export interface GetGroupParticipantsOptions { page?: number; limit?: number; + filter?: { + pending?: boolean; + role?: string; + }; } export interface GroupUpdateOptions { @@ -57,3 +61,9 @@ export interface InfoOptions { overrideAccount?: string; } + +export interface ParticipantStatus { + pending: boolean; + role: 'ADMIN' | 'MEMBER'; + participant: boolean; +} \ No newline at end of file diff --git a/packages/restapi/src/lib/types/index.ts b/packages/restapi/src/lib/types/index.ts index d0f2a045b..d091d909f 100644 --- a/packages/restapi/src/lib/types/index.ts +++ b/packages/restapi/src/lib/types/index.ts @@ -394,12 +394,26 @@ export interface SpaceAccess { rules?: SpaceRules; } +export interface RoleCounts { + total: number; + pending: number; +} + export interface ChatMemberCounts { overallCount: number; adminsCount: number; membersCount: number; pendingCount: number; approvedCount: number; + roles: { + ADMIN: RoleCounts; + MEMBER: RoleCounts; + }; +} + +export interface GroupParticipantCounts { + participants: number; + pending: number; } export interface ChatMemberProfile { diff --git a/packages/restapi/tests/lib/notification/base.test.ts b/packages/restapi/tests/lib/notification/base.test.ts index 4374aaa22..e37391a72 100644 --- a/packages/restapi/tests/lib/notification/base.test.ts +++ b/packages/restapi/tests/lib/notification/base.test.ts @@ -24,7 +24,7 @@ enum ENV { */ LOCAL = 'local', } -describe.only('test', () => { +describe('test', () => { // const signer = createWalletClient({ // account: privateKeyToAccount(`0x${process.env['WALLET_PRIVATE_KEY']}`), // chain: goerli,