Skip to content

Commit 051aa7c

Browse files
committed
Updates to new Org Members route
1 parent 94781a1 commit 051aa7c

File tree

2 files changed

+26
-51
lines changed

2 files changed

+26
-51
lines changed

src/plus/gk/models/organization.ts

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,15 @@ export type OrganizationRole = 'owner' | 'admin' | 'billing' | 'user';
88

99
export type OrganizationsResponse = Organization[];
1010

11-
export interface FullOrganization {
12-
readonly id: string;
13-
readonly name: string;
14-
readonly domain: string;
15-
readonly updatedToNewRoles: boolean;
16-
readonly memberCount: number;
17-
readonly members: OrganizationMember[];
18-
readonly connections: OrganizationConnection[];
19-
readonly type: OrganizationType;
20-
readonly isOnChargebee: boolean;
21-
}
22-
23-
export enum OrganizationType {
24-
Enterprise = 'ENTERPRISE',
25-
Individual = 'INDIVIDUAL',
26-
Pro = 'PRO',
27-
Teams = 'TEAMS',
28-
}
29-
30-
export type OrganizationConnection = Record<string, unknown>;
11+
export type OrganizationMemberStatus = 'activated' | 'pending';
3112

3213
export interface OrganizationMember {
3314
readonly id: string;
3415
readonly email: string;
3516
readonly name: string;
3617
readonly username: string;
3718
readonly role: OrganizationRole;
38-
readonly licenseConsumption: Record<string, boolean>;
19+
readonly status: OrganizationMemberStatus;
3920
}
4021

4122
export interface OrganizationSettings {

src/plus/gk/organizationService.ts

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import { once } from '../../system/function';
66
import { Logger } from '../../system/logger';
77
import { getLogScope } from '../../system/logger.scope';
88
import type {
9-
FullOrganization,
109
Organization,
1110
OrganizationMember,
1211
OrganizationSettings,
@@ -20,8 +19,8 @@ const organizationsCacheExpiration = 24 * 60 * 60 * 1000; // 1 day
2019
export class OrganizationService implements Disposable {
2120
private _disposable: Disposable;
2221
private _organizations: Organization[] | null | undefined;
23-
private _fullOrganizations: Map<FullOrganization['id'], FullOrganization> | undefined;
24-
private _organizationSettings: Map<FullOrganization['id'], OrganizationSettings> | undefined;
22+
private _organizationSettings: Map<Organization['id'], OrganizationSettings> | undefined;
23+
private _organizationMembers: Map<Organization['id'], OrganizationMember[]> | undefined;
2524

2625
constructor(
2726
private readonly container: Container,
@@ -147,43 +146,34 @@ export class OrganizationService implements Disposable {
147146
}
148147

149148
@gate()
150-
private async getFullOrganization(
151-
id: string,
152-
options?: { force?: boolean },
153-
): Promise<FullOrganization | undefined> {
154-
if (!this._fullOrganizations?.has(id) || options?.force === true) {
155-
const rsp = await this.connection.fetchGkApi(`organization/${id}`, { method: 'GET' });
149+
async getMembers(id?: string | undefined, options?: { force?: boolean }): Promise<OrganizationMember[]> {
150+
if (id == null) {
151+
id = await this.getActiveOrganizationId();
152+
if (id == null) return [];
153+
}
154+
155+
if (!this._organizationMembers?.has(id) || options?.force === true) {
156+
type MemberResponse = {
157+
members: OrganizationMember[];
158+
};
159+
const rsp = await this.connection.fetchGkApi(`organization/${id}/members`, { method: 'GET' });
156160
if (!rsp.ok) {
157161
Logger.error(
158162
'',
159163
getLogScope(),
160-
`Unable to get organization; status=(${rsp.status}): ${rsp.statusText}`,
164+
`Unable to get organization members; status=(${rsp.status}): ${rsp.statusText}`,
161165
);
162-
return undefined;
166+
return [];
163167
}
164168

165-
const organization = (await rsp.json()) as FullOrganization;
166-
if (this._fullOrganizations == null) {
167-
this._fullOrganizations = new Map();
168-
}
169-
organization.members.sort((a, b) => (a.name ?? a.username).localeCompare(b.name ?? b.username));
170-
this._fullOrganizations.set(id, organization);
171-
}
172-
return this._fullOrganizations.get(id);
173-
}
169+
const members: OrganizationMember[] = ((await rsp.json()) as MemberResponse).members;
170+
sortOrgMembers(members);
174171

175-
@gate()
176-
async getMembers(
177-
organizationId?: string | undefined,
178-
options?: { force?: boolean },
179-
): Promise<OrganizationMember[]> {
180-
if (organizationId == null) {
181-
organizationId = await this.getActiveOrganizationId();
182-
if (organizationId == null) return [];
172+
this._organizationMembers ??= new Map();
173+
this._organizationMembers.set(id, members);
183174
}
184175

185-
const organization = await this.getFullOrganization(organizationId, options);
186-
return organization?.members ?? [];
176+
return this._organizationMembers.get(id) ?? [];
187177
}
188178

189179
async getMemberById(id: string, organizationId: string): Promise<OrganizationMember | undefined> {
@@ -245,3 +235,7 @@ export class OrganizationService implements Disposable {
245235
return this._organizationSettings.get(id);
246236
}
247237
}
238+
239+
function sortOrgMembers(members: OrganizationMember[]): OrganizationMember[] {
240+
return members.sort((a, b) => (a.name ?? a.username).localeCompare(b.name ?? b.username));
241+
}

0 commit comments

Comments
 (0)