Skip to content

Commit

Permalink
Updates to new Org Members route
Browse files Browse the repository at this point in the history
  • Loading branch information
d13 committed Feb 3, 2025
1 parent c982edb commit 7ef3a72
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 28 deletions.
11 changes: 11 additions & 0 deletions src/plus/gk/models/organization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,18 @@ export enum OrganizationType {

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 status: OrganizationMemberStatus;
}

export interface OrganizationMemberOld {
readonly id: string;
readonly email: string;
readonly name: string;
Expand Down
51 changes: 23 additions & 28 deletions src/plus/gk/organizationService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,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 _organizationMembers: Map<Organization['id'], OrganizationMember[]> | undefined;

constructor(
private readonly container: Container,
Expand Down Expand Up @@ -147,43 +147,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 +236,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));
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { Event } from 'vscode';
import { EventEmitter } from 'vscode';
import type { IntegrationId } from '../../../constants.integrations';
import { HostingIntegrationId } from '../../../constants.integrations';
import type { StoredConfiguredIntegrationDescriptor } from '../../../constants.storage';
Expand Down Expand Up @@ -27,6 +29,11 @@ export type ConfiguredIntegrationType = 'cloud' | 'local';
export class ConfiguredIntegrationService {
private _configured?: Map<IntegrationId, ConfiguredIntegrationDescriptor[]>;

private readonly _onDidChangeConfiguredIntegrations = new EventEmitter<void>();
get onDidChangeConfiguredIntegrations(): Event<void> {
return this._onDidChangeConfiguredIntegrations.event;
}

constructor(private readonly container: Container) {}

private get configured(): Map<IntegrationId, ConfiguredIntegrationDescriptor[]> {
Expand Down

0 comments on commit 7ef3a72

Please sign in to comment.