Skip to content

Commit

Permalink
fix(users): load config before initializing user entity (#2323)
Browse files Browse the repository at this point in the history
to ensure the required entity type is available

fixes #2306
  • Loading branch information
sleidig authored Mar 26, 2024
1 parent a0c57d5 commit 0c9e923
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import { mockEntityMapper } from "../../entity/entity-mapper/mock-entity-mapper-
import { User } from "../../user/user";
import { TEST_USER } from "../../user/demo-user-generator.service";
import { Child } from "../../../child-dev-project/children/model/child";
import { Config } from "../../config/config";

describe("SessionManagerService", () => {
let service: SessionManagerService;
Expand All @@ -65,7 +66,10 @@ describe("SessionManagerService", () => {
LoginStateSubject,
SessionSubject,
CurrentUserSubject,
{ provide: EntityMapperService, useValue: mockEntityMapper() },
{
provide: EntityMapperService,
useValue: mockEntityMapper(),
},
{ provide: Database, useClass: PouchDatabase },
{ provide: KeycloakAuthService, useValue: mockKeycloak },
{ provide: NAVIGATOR_TOKEN, useValue: mockNavigator },
Expand All @@ -85,7 +89,11 @@ describe("SessionManagerService", () => {
const db = TestBed.inject(Database) as PouchDatabase;
initInMemorySpy = spyOn(db, "initInMemoryDB").and.callThrough();
initIndexedSpy = spyOn(db, "initIndexedDB").and.callThrough();
spyOn(TestBed.inject(SyncService), "startSync");
spyOn(TestBed.inject(SyncService), "startSync").and.callFake(() =>
TestBed.inject(EntityMapperService).save(
new Config(Config.CONFIG_KEY, {}),
),
);

TestBed.inject(LocalAuthService).saveUser(dbUser);
environment.session_type = SessionType.mock;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ import { Database } from "../../database/database";
import { NAVIGATOR_TOKEN } from "../../../utils/di-tokens";
import { CurrentUserSubject } from "../current-user-subject";
import { EntityMapperService } from "../../entity/entity-mapper/entity-mapper.service";
import { filter } from "rxjs/operators";
import { filter, take } from "rxjs/operators";
import { Subscription } from "rxjs";
import { Entity } from "../../entity/model/entity";
import { ConfigService } from "../../config/config.service";

/**
* This service handles the user session.
Expand All @@ -58,6 +59,7 @@ export class SessionManagerService {
private loginStateSubject: LoginStateSubject,
private router: Router,
@Inject(NAVIGATOR_TOKEN) private navigator: Navigator,
private configService: ConfigService,
database: Database,
) {
if (database instanceof PouchDatabase) {
Expand Down Expand Up @@ -101,7 +103,10 @@ export class SessionManagerService {
this.sessionInfo.next(session);
this.loginStateSubject.next(LoginState.LOGGED_IN);
if (session.entityId) {
this.initUserEntity(session.entityId);
this.configService.configUpdates.pipe(take(1)).subscribe(() =>
// requires initial config to be loaded first!
this.initUserEntity(session.entityId),
);
}
}

Expand Down

0 comments on commit 0c9e923

Please sign in to comment.