Skip to content

Commit

Permalink
WIP: Restrict current events #723
Browse files Browse the repository at this point in the history
  • Loading branch information
hupf committed Nov 26, 2024
1 parent 46a4de4 commit 951eda2
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 2 deletions.
27 changes: 26 additions & 1 deletion src/app/events/services/events-state.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,21 @@ import {
} from "rxjs";
import { SETTINGS, Settings } from "src/app/settings";
import { Course } from "src/app/shared/models/course.model";
import { EventLeadership } from "src/app/shared/models/event-leadership.model";
import { Event } from "src/app/shared/models/event.model";
import { StudyClass } from "src/app/shared/models/study-class.model";
import { CoursesRestService } from "src/app/shared/services/courses-rest.service";
import { EventLeadershipsRestService } from "src/app/shared/services/event-leaderships-rest.service";
import { EventsRestService } from "src/app/shared/services/events-rest.service";
import { LoadingService } from "src/app/shared/services/loading-service";
import { StorageService } from "src/app/shared/services/storage.service";
import { StudyClassesRestService } from "src/app/shared/services/study-classes-rest.service";
import { spread } from "src/app/shared/utils/function";
import { hasRole } from "src/app/shared/utils/roles";
import { searchEntries } from "src/app/shared/utils/search";
import {
EventStateWithLabel,
filterAccessibleEvents,
getCourseDesignation,
getEventState,
isRated,
Expand Down Expand Up @@ -85,8 +89,14 @@ export class EventsStateService {
);

private events$ = this.getEvents().pipe(shareReplay(1));
private filteredEvents$ = combineLatest([
private eventLeaderships$ = this.loadEventLeaderships().pipe(shareReplay(1));
private accessibleEvents$ = combineLatest([
this.events$,
this.eventLeaderships$,
]).pipe(map(spread(filterAccessibleEvents)));

private filteredEvents$ = combineLatest([
this.accessibleEvents$,
this.searchFields$,
this.search$,
]).pipe(map(spread(searchEntries)));
Expand All @@ -95,7 +105,9 @@ export class EventsStateService {
private coursesRestService: CoursesRestService,
private eventsRestService: EventsRestService,
private studyClassRestService: StudyClassesRestService,
private eventLeadershipsRestService: EventLeadershipsRestService,
private loadingService: LoadingService,
private storageService: StorageService,
private translate: TranslateService,
@Inject(SETTINGS) private settings: Settings,
) {}
Expand Down Expand Up @@ -140,6 +152,19 @@ export class EventsStateService {
.pipe(map(spread(this.createAndSortEvents.bind(this))));
}

private loadEventLeaderships(): Observable<ReadonlyArray<EventLeadership>> {
const userId = this.storageService.getPayload()?.id_person;
if (!userId) return of([]);

return this.loadingService.load(
this.eventLeadershipsRestService.getList({
params: {
"filter.PersonId": `=${userId}`,
},
}),
);
}

private loadUnratedCourses(
roles: Option<string>,
): Observable<ReadonlyArray<Course>> {
Expand Down
13 changes: 12 additions & 1 deletion src/app/events/utils/events.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { EventLeadership } from "src/app/shared/models/event-leadership.model";
import { Course } from "../../shared/models/course.model";
import { EventState } from "../services/events-state.service";
import { EventEntry, EventState } from "../services/events-state.service";

export type EventStateWithLabel = {
value: EventState;
Expand Down Expand Up @@ -94,3 +95,13 @@ export function getCourseDesignation(course: Course): string {

return classes ? course.Designation + ", " + classes : course.Designation;
}

export function filterAccessibleEvents(
events: ReadonlyArray<EventEntry>,
leaderships: ReadonlyArray<EventLeadership>,
): ReadonlyArray<EventEntry> {
const accessibleEventIds = leaderships.map(
(leadership) => leadership.EventId,
);
return events.filter((event) => accessibleEventIds.includes(event.id));
}
9 changes: 9 additions & 0 deletions src/app/shared/models/event-leadership.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import * as t from "io-ts";

const EventLeadership = t.type({
EventId: t.number,
PersonId: t.number,
});

type EventLeadership = t.TypeOf<typeof EventLeadership>;
export { EventLeadership };
16 changes: 16 additions & 0 deletions src/app/shared/services/event-leaderships-rest.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { TestBed } from "@angular/core/testing";
import { buildTestModuleMetadata } from "src/spec-helpers";
import { EventLeadershipsRestService } from "./event-leaderships-rest.service";

describe("EventLeadershipsRestService", () => {
let service: EventLeadershipsRestService;

beforeEach(() => {
TestBed.configureTestingModule(buildTestModuleMetadata({}));
service = TestBed.inject(EventLeadershipsRestService);
});

it("should be created", () => {
expect(service).toBeTruthy();
});
});
32 changes: 32 additions & 0 deletions src/app/shared/services/event-leaderships-rest.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { HttpClient, HttpHeaders, HttpParams } from "@angular/common/http";
import { Inject, Injectable } from "@angular/core";
import { Observable } from "rxjs";
import { SETTINGS, Settings } from "src/app/settings";
import { EventLeadership } from "../models/event-leadership.model";
import { RestService } from "./rest.service";

@Injectable({
providedIn: "root",
})
export class EventLeadershipsRestService extends RestService<
typeof EventLeadership
> {
constructor(http: HttpClient, @Inject(SETTINGS) settings: Settings) {
super(http, settings, EventLeadership, "EventLeaderships");
}

override getList({
headers,
params: customParams,
}: {
headers?: HttpHeaders | Dict<string>;
params?: HttpParams | Dict<string>;
} = {}): Observable<ReadonlyArray<EventLeadership>> {
let params =
customParams instanceof HttpParams
? customParams
: new HttpParams({ fromObject: customParams });
params = params.set("fields", "EventId,PersonId");
return super.getList({ headers, params });
}
}

0 comments on commit 951eda2

Please sign in to comment.