Skip to content

Commit

Permalink
Implement an upsert method that uses StateProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
addisonbeck committed Feb 12, 2024
1 parent 254daa2 commit b7d5776
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
Expand Up @@ -149,6 +149,34 @@ describe("Organization Service", () => {
]);
});

describe("upsertActiveUserOrganization", () => {
it("Replaces an existing organization in state", async () => {
await organizationService.upsertActiveUserOrganization(organizationData("1", "UPDATED"));
expect(await firstValueFrom(organizationService.activeUserOrganizations$)).toEqual([
{
id: "1",
name: "UPDATED",
identifier: "test",
},
]);
});
it("Adds a new organization to state if input does not exist", async () => {
await organizationService.upsertActiveUserOrganization(organizationData("2", "Test 2"));
expect(await firstValueFrom(organizationService.activeUserOrganizations$)).toEqual([
{
id: "1",
name: "Test Org",
identifier: "test",
},
{
id: "2",
name: "Test 2",
identifier: "test",
},
]);
});
});

describe("getByIdentifier", () => {
it("exists", async () => {
const result = organizationService.getByIdentifier("test");
Expand Down
Expand Up @@ -76,6 +76,7 @@ export class OrganizationService implements InternalOrganizationServiceAbstracti
return organizations.length > 0;
}

// marked for removal during AC-2009
async upsert(organization: OrganizationData): Promise<void> {
let organizations = await this.stateService.getOrganizations();
if (organizations == null) {
Expand All @@ -85,6 +86,17 @@ export class OrganizationService implements InternalOrganizationServiceAbstracti
organizations[organization.id] = organization;

await this.replace(organizations);
await this.upsertActiveUserOrganization(organization);
}

async upsertActiveUserOrganization(organization: OrganizationData): Promise<void> {
await this.stateProvider.getActive(ORGANIZATIONS).update((organizations) => {
if (organizations == null) {
organizations = {};

Check warning on line 95 in libs/common/src/admin-console/services/organization/organization.service.ts

View check run for this annotation

Codecov / codecov/patch

libs/common/src/admin-console/services/organization/organization.service.ts#L95

Added line #L95 was not covered by tests
}
organizations[organization.id] = organization;
return organizations;
});
}

// marked for removal during AC-2009
Expand Down

0 comments on commit b7d5776

Please sign in to comment.