Skip to content

Commit

Permalink
Merge pull request #459 from johanbook/active-organization-cache-issue
Browse files Browse the repository at this point in the history
fix(api): properly update active organization cache
  • Loading branch information
johanbook authored Oct 12, 2023
2 parents 64fdadb + 48c9ac3 commit a653047
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { Repository } from "typeorm";

import { CurrentProfileService } from "src/features/profiles";
import { createMockRepository } from "src/test/mocks";

import { ActiveOrganization } from "../../infrastructure/entities/active-organization.entity";
import { ActiveOrganizationService } from "./active-organization.service";

describe(ActiveOrganizationService.name, () => {
let activeOrganizations: Repository<ActiveOrganization>;
let activeOrganizationService: ActiveOrganizationService;
let currentProfileService: CurrentProfileService;

beforeEach(() => {
activeOrganizations = createMockRepository<ActiveOrganization>([
{
id: "my-id",
organizationId: 1,
} as any,
]);
currentProfileService = {
fetchCurrentProfileId: jest.fn(() => "my-profile-id"),
} as any;

activeOrganizationService = new ActiveOrganizationService(
activeOrganizations,
currentProfileService,
);
});

describe("cache", () => {
it("should update cache when switching organizations", async () => {
const initialValue =
await activeOrganizationService.fetchCurrentActiveOrganization();

expect(initialValue?.organizationId).toBe(1);

await activeOrganizationService.switchCurrentOrganization(2);

const updatedValue =
await activeOrganizationService.fetchCurrentActiveOrganization();

expect(updatedValue?.organizationId).toBe(2);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { CurrentProfileService } from "src/features/profiles";

import { ActiveOrganization } from "../../infrastructure/entities/active-organization.entity";

const ACTIVE_ORGANIZATION_CACHE_PERIOD_MS = 5000;
const ACTIVE_ORGANIZATION_CACHE_PERIOD_MS = 10_000;

@Injectable()
export class ActiveOrganizationService {
Expand Down Expand Up @@ -38,8 +38,6 @@ export class ActiveOrganizationService {
const currentProfileId =
await this.currentProfileService.fetchCurrentProfileId();

await this.cache.delete(currentProfileId);

let activeOrganization = await this.fetchCurrentActiveOrganization();

if (!activeOrganization) {
Expand All @@ -50,5 +48,7 @@ export class ActiveOrganizationService {
activeOrganization.organizationId = organizationId;

await this.activeOrganizations.save(activeOrganization);

await this.cache.set(currentProfileId, activeOrganization);
}
}

0 comments on commit a653047

Please sign in to comment.