Skip to content

Commit

Permalink
Handle meeting user user id mapping in meeting user repo
Browse files Browse the repository at this point in the history
  • Loading branch information
bastianjoel committed Apr 23, 2024
1 parent 12e1e38 commit ef68a0e
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Injectable } from '@angular/core';
import { Id } from 'src/app/domain/definitions/key-types';
import { MeetingUser } from 'src/app/domain/models/meeting-users/meeting-user';
import { toDecimal } from 'src/app/infrastructure/utils';
import { ViewMeetingUser } from 'src/app/site/pages/meetings/view-models/view-meeting-user';
Expand All @@ -17,6 +18,8 @@ export type MeetingUserPatchFn =
export class MeetingUserRepositoryService extends BaseMeetingRelatedRepository<ViewMeetingUser, MeetingUser> {
public override readonly resetOnMeetingChange = false;

private meetingUserIdMap: Map<Id, Map<Id, Id>> = new Map();

public constructor(repositoryServiceCollector: RepositoryMeetingServiceCollectorService) {
super(repositoryServiceCollector, MeetingUser);
}
Expand Down Expand Up @@ -66,4 +69,40 @@ export class MeetingUserRepositoryService extends BaseMeetingRelatedRepository<V
public getTitle = (viewUser: ViewMeetingUser) => viewUser.user?.getTitle() ?? `Unknown`;

public getVerboseName = (plural = false): string => this.translate.instant(plural ? `Participants` : `Participant`);

public getMeetingUserId(userId: Id, meetingId: Id): Id | null {
if (this.meetingUserIdMap.has(userId) && this.meetingUserIdMap.get(userId).has(meetingId)) {
return this.meetingUserIdMap.get(userId).get(meetingId);
}

return null;
}

public override deleteModels(ids: Id[]): void {
ids.forEach(id => {
const user = this.viewModelStore[id];
if (user && user.user_id && user.meeting_id) {
const meetingUserId = this.meetingUserIdMap.get(user.user_id)?.get(user.meeting_id);
if (meetingUserId === user.id) {
this.meetingUserIdMap.get(user.user_id).delete(user.meeting_id);
}
}
});
super.deleteModels(ids);
}

protected override clearViewModelStore(): void {
super.clearViewModelStore();
this.meetingUserIdMap = new Map();
}

protected override onCreateViewModel(meetingUser: ViewMeetingUser): void {
if (meetingUser.user_id && meetingUser.meeting_id) {
if (!this.meetingUserIdMap.has(meetingUser.user_id)) {
this.meetingUserIdMap.set(meetingUser.user_id, new Map());
}

this.meetingUserIdMap.get(meetingUser.user_id).set(meetingUser.meeting_id, meetingUser.id);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -338,37 +338,17 @@ export class UserRepositoryService extends BaseRepository<ViewUser, User> {
protected override createViewModel(model: User): ViewUser {
const viewModel = super.createViewModel(model);

const meetingUserIdMap = new Map<Id, Id>();
const getMeetingUserId = (meetingId: Id) => {
if (!meetingUserIdMap.has(meetingId)) {
const meetingUsers = this.relationManager.handleRelation(
viewModel.getModel(),
this.relationsByKey[`meeting_users`]
);

if (meetingUsers?.length) {
for (const meetingUser of meetingUsers) {
meetingUserIdMap.set(meetingUser.meeting_id, meetingUser.id);
}
} else {
for (const meetingUser of this.meetingUserRepo.getViewModelList()) {
if (meetingUser.user_id === model.id) {
meetingUserIdMap.set(meetingUser.meeting_id, meetingUser.id);
return meetingUser.id;
}
}
}
}

return meetingUserIdMap.get(meetingId);
};

viewModel.getName = () => this.getName(viewModel);
viewModel.getShortName = () => this.getShortName(viewModel);
viewModel.getFullName = (structureLevel?: ViewStructureLevel) => this.getFullName(viewModel, structureLevel);
viewModel.getMeetingUser = (meetingId: Id) => this.getMeetingUser(getMeetingUserId, meetingId);
viewModel.getLevelAndNumber = () => this.getLevelAndNumber(viewModel);
viewModel.getEnsuredActiveMeetingId = () => this.activeMeetingIdService.meetingId;
viewModel.getName = (): string => this.getName(viewModel);
viewModel.getShortName = (): string => this.getShortName(viewModel);
viewModel.getFullName = (structureLevel?: ViewStructureLevel): string =>
this.getFullName(viewModel, structureLevel);
viewModel.getMeetingUser = (meetingId: Id): ViewMeetingUser =>
this.getMeetingUser(
(meetingId: Id): Id => this.meetingUserRepo.getMeetingUserId(model.id, meetingId),
meetingId
);
viewModel.getLevelAndNumber = (): string => this.getLevelAndNumber(viewModel);
viewModel.getEnsuredActiveMeetingId = (): Id => this.activeMeetingIdService.meetingId;
return viewModel;
}

Expand Down

0 comments on commit ef68a0e

Please sign in to comment.