Skip to content

Commit

Permalink
feat: Remove member-finding aliases from Org instances and simplify t…
Browse files Browse the repository at this point in the history
…he names of said methods in the Client. BREAKING
  • Loading branch information
adam-coster committed Aug 19, 2021
1 parent 01b1537 commit b3e8a0f
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 44 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const bravoClient = new BravoClient({
});

async function doFavroStuff() {
const org = await bravoClient.getCurrentOrganization();
await bravoClient.getCurrentOrganization();

// Find a Widget (a.k.a. Board)
const widget = await bravoClient.findWidgetByName('My To Do List');
Expand All @@ -59,7 +59,7 @@ async function doFavroStuff() {
});

// Find the userId for an assignee
const assignee = await org.findMemberByName('Scam Likely');
const assignee = await bravoClient.findMemberByName('Scam Likely');

// Use the update-builder to create and send an update
// that covers multiple fields.
Expand Down
26 changes: 12 additions & 14 deletions src/lib/BravoClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ import { basename } from 'path';
* The `BravoClient` class should be singly-instanced for a given
* set of credentials and a target organization. Once the organizationId
* is set (either out of the gate via env var or construct args, or
* by using `client.setOrganizationIdByName` when the org name is known
* by using `client.chooseOrganizationByName` when the org name is known
* but the id is not).
*
* All Favro API fetching and caching is centralized and managed in
Expand Down Expand Up @@ -140,7 +140,7 @@ export class BravoClient extends FavroClient {
/**
* Full user info for the org (includes emails and names).
*/
async listOrganizationMembers() {
async listMembers() {
const org = await this.getCurrentOrganization();
assertBravoClaim(org, 'Organization not set');
if (!this.cache.users) {
Expand All @@ -154,30 +154,28 @@ export class BravoClient extends FavroClient {
return this.cache.users;
}

async findOrganizationMember(match: (user: BravoUser) => any) {
const members = await this.listOrganizationMembers();
async findMember(match: (user: BravoUser) => any) {
const members = await this.listMembers();
return members.find((user) => match(user));
}

async findOrganizationMemberByEmail(email: string | RegExp) {
return await this.findOrganizationMemberByField('email', email);
async findMemberByEmail(email: string | RegExp) {
return await this.findMemberByField('email', email);
}

async findOrganizationMemberByName(name: string | RegExp) {
return await this.findOrganizationMemberByField('name', name);
async findMemberByName(name: string | RegExp) {
return await this.findMemberByField('name', name);
}

async findOrganizationMemberByUserId(userId: string) {
return await this.findOrganizationMemberByField('userId', userId);
async findMemberByUserId(userId: string) {
return await this.findMemberByField('userId', userId);
}

private async findOrganizationMemberByField(
private async findMemberByField(
field: 'email' | 'name' | 'userId',
value: string | RegExp,
) {
return await this.findOrganizationMember((user) =>
user[field]?.match(value),
);
return await this.findMember((user) => user[field]?.match(value));
}

//#endregion
Expand Down
28 changes: 3 additions & 25 deletions src/lib/entities/BravoOrganization.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import type {
DataFavroOrganization,
DataFavroUser,
} from '$types/FavroApiTypes';
import type { DataFavroOrganization } from '$types/FavroApiTypes';
import { BravoEntity } from '$lib/BravoEntity.js';
import { BravoOrganizationMember, BravoUser } from './users.js';
import { BravoOrganizationMember } from './users.js';

/** Hydrated Favro Organization. */
export class BravoOrganization extends BravoEntity<DataFavroOrganization> {
get name() {
return this._data.name;
Expand All @@ -20,26 +18,6 @@ export class BravoOrganization extends BravoEntity<DataFavroOrganization> {
return this._data.organizationId;
}

async listMembers() {
return await this._client.listOrganizationMembers();
}

async findMember(match: (user: BravoUser) => any) {
return await this._client.findOrganizationMember(match);
}

async findMemberByEmail(email: string | RegExp) {
return await this._client.findOrganizationMemberByEmail(email);
}

async findMemberByName(name: string | RegExp) {
return await this._client.findOrganizationMemberByName(name);
}

async findMemberByUserId(userId: string) {
return await this._client.findOrganizationMemberByUserId(userId);
}

equals(org: BravoOrganization) {
return (
this.hasSameConstructor(org) && this.organizationId === org.organizationId
Expand Down
4 changes: 1 addition & 3 deletions src/test/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,7 @@ describe('BravoClient', function () {
});

it('can find a specific user by email', async function () {
const me = await (await client.getCurrentOrganization())!.findMemberByEmail(
myUserEmail,
)!;
const me = await client.findOrganizationMemberByEmail(myUserEmail)!;
expect(me).to.exist;
expect(me!.email).to.equal(myUserEmail);
});
Expand Down

0 comments on commit b3e8a0f

Please sign in to comment.