Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 2 additions & 21 deletions src/plus/gk/models/organization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,15 @@ export type OrganizationRole = 'owner' | 'admin' | 'billing' | 'user';

export type OrganizationsResponse = Organization[];

export interface FullOrganization {
readonly id: string;
readonly name: string;
readonly domain: string;
readonly updatedToNewRoles: boolean;
readonly memberCount: number;
readonly members: OrganizationMember[];
readonly connections: OrganizationConnection[];
readonly type: OrganizationType;
readonly isOnChargebee: boolean;
}

export enum OrganizationType {
Enterprise = 'ENTERPRISE',
Individual = 'INDIVIDUAL',
Pro = 'PRO',
Teams = 'TEAMS',
}

export type OrganizationConnection = Record<string, unknown>;
export type OrganizationMemberStatus = 'activated' | 'pending';

export interface OrganizationMember {
readonly id: string;
readonly email: string;
readonly name: string;
readonly username: string;
readonly role: OrganizationRole;
readonly licenseConsumption: Record<string, boolean>;
readonly status: OrganizationMemberStatus;
}

export interface OrganizationSettings {
Expand Down
54 changes: 24 additions & 30 deletions src/plus/gk/organizationService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { once } from '../../system/function';
import { Logger } from '../../system/logger';
import { getLogScope } from '../../system/logger.scope';
import type {
FullOrganization,
Organization,
OrganizationMember,
OrganizationSettings,
Expand All @@ -20,8 +19,8 @@ const organizationsCacheExpiration = 24 * 60 * 60 * 1000; // 1 day
export class OrganizationService implements Disposable {
private _disposable: Disposable;
private _organizations: Organization[] | null | undefined;
private _fullOrganizations: Map<FullOrganization['id'], FullOrganization> | undefined;
private _organizationSettings: Map<FullOrganization['id'], OrganizationSettings> | undefined;
private _organizationSettings: Map<Organization['id'], OrganizationSettings> | undefined;
private _organizationMembers: Map<Organization['id'], OrganizationMember[]> | undefined;

constructor(
private readonly container: Container,
Expand Down Expand Up @@ -147,43 +146,34 @@ export class OrganizationService implements Disposable {
}

@gate()
private async getFullOrganization(
id: string,
options?: { force?: boolean },
): Promise<FullOrganization | undefined> {
if (!this._fullOrganizations?.has(id) || options?.force === true) {
const rsp = await this.connection.fetchGkApi(`organization/${id}`, { method: 'GET' });
async getMembers(id?: string | undefined, options?: { force?: boolean }): Promise<OrganizationMember[]> {
if (id == null) {
id = await this.getActiveOrganizationId();
if (id == null) return [];
}

if (!this._organizationMembers?.has(id) || options?.force === true) {
type MemberResponse = {
members: OrganizationMember[];
};
const rsp = await this.connection.fetchGkApi(`organization/${id}/members`, { method: 'GET' });
if (!rsp.ok) {
Logger.error(
'',
getLogScope(),
`Unable to get organization; status=(${rsp.status}): ${rsp.statusText}`,
`Unable to get organization members; status=(${rsp.status}): ${rsp.statusText}`,
);
return undefined;
return [];
}

const organization = (await rsp.json()) as FullOrganization;
if (this._fullOrganizations == null) {
this._fullOrganizations = new Map();
}
organization.members.sort((a, b) => (a.name ?? a.username).localeCompare(b.name ?? b.username));
this._fullOrganizations.set(id, organization);
}
return this._fullOrganizations.get(id);
}
const members: OrganizationMember[] = ((await rsp.json()) as MemberResponse).members;
sortOrgMembers(members);

@gate()
async getMembers(
organizationId?: string | undefined,
options?: { force?: boolean },
): Promise<OrganizationMember[]> {
if (organizationId == null) {
organizationId = await this.getActiveOrganizationId();
if (organizationId == null) return [];
this._organizationMembers ??= new Map();
this._organizationMembers.set(id, members);
}

const organization = await this.getFullOrganization(organizationId, options);
return organization?.members ?? [];
return this._organizationMembers.get(id) ?? [];
}

async getMemberById(id: string, organizationId: string): Promise<OrganizationMember | undefined> {
Expand Down Expand Up @@ -245,3 +235,7 @@ export class OrganizationService implements Disposable {
return this._organizationSettings.get(id);
}
}

function sortOrgMembers(members: OrganizationMember[]): OrganizationMember[] {
return members.sort((a, b) => (a.name ?? a.username).localeCompare(b.name ?? b.username));
}