-
Notifications
You must be signed in to change notification settings - Fork 32
/
committee-repository.service.ts
128 lines (116 loc) · 5.56 KB
/
committee-repository.service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import { Injectable } from '@angular/core';
import { ORGANIZATION_ID } from 'src/app/site/pages/organization/services/organization.service';
import { Id } from '../../domain/definitions/key-types';
import { CML, OML } from '../../domain/definitions/organization-permission';
import { Identifiable } from '../../domain/interfaces';
import { Committee } from '../../domain/models/comittees/committee';
import { ViewCommittee } from '../../site/pages/organization/pages/committees';
import { Fieldsets, TypedFieldset } from '../../site/services/model-request-builder';
import { OperatorService } from '../../site/services/operator.service';
import { Action } from '../actions';
import { BaseRepository } from './base-repository';
import { CommitteeAction } from './committees/committee.action';
import { RepositoryServiceCollectorService } from './repository-service-collector.service';
import { UserRepositoryService } from './users/user-repository.service';
@Injectable({
providedIn: `root`
})
export class CommitteeRepositoryService extends BaseRepository<ViewCommittee, Committee> {
public constructor(
repositoryServiceCollector: RepositoryServiceCollectorService,
private operator: OperatorService,
private userRepo: UserRepositoryService
) {
super(repositoryServiceCollector, Committee);
}
public getTitle = (viewCommittee: ViewCommittee) => viewCommittee.name;
public getVerboseName = (plural: boolean = false) => this.translate.instant(plural ? `Committees` : `Committee`);
public override getFieldsets(): Fieldsets<Committee> {
const nameFields: TypedFieldset<Committee> = [`name`];
const listFields: TypedFieldset<Committee> = nameFields.concat([
`description`,
`meeting_ids`,
`forward_to_committee_ids`,
`receive_forwardings_from_committee_ids`,
`organization_tag_ids`,
`user_ids`,
{ templateField: `user_$_management_level` },
`external_id`
]);
return {
...super.getFieldsets(),
list: listFields,
name: nameFields
};
}
public create(...committees: any[]): Promise<Identifiable[]> {
const payload: any[] = committees.map(committee => ({
name: committee.name,
organization_id: ORGANIZATION_ID,
...this.getPartialCommitteePayload(committee)
}));
return this.sendBulkActionToBackend(CommitteeAction.CREATE, payload);
}
public update(update?: any, ...committees: ViewCommittee[]): Action<void> {
const createPayload = (id: Id, model: Partial<Committee>) => ({
id,
name: model.name,
default_meeting_id: model.default_meeting_id,
...this.getPartialCommitteePayload(model)
});
const payload: any[] = committees.map(committee => createPayload(committee.id, update ?? committee));
return this.createAction(CommitteeAction.UPDATE, payload);
}
public delete(...committees: ViewCommittee[]): Promise<void> {
const payload: any[] = committees.map(committee => ({ id: committee.id }));
return this.sendBulkActionToBackend(CommitteeAction.DELETE, payload);
}
public bulkForwardToCommittees(committees: ViewCommittee[], committeeIds: Id[]): Promise<void> {
const payload: any[] = committees.map(committee => {
const forwardToIds = new Set(committeeIds.concat(committee.forward_to_committee_ids || []));
return {
id: committee.id,
forward_to_committee_ids: Array.from(forwardToIds)
};
});
return this.sendBulkActionToBackend(CommitteeAction.UPDATE, payload);
}
public bulkUnforwardToCommittees(committees: ViewCommittee[], committeeIds: Id[]): Promise<void> {
const payload: any[] = committees.map(committee => ({
id: committee.id,
forward_to_committee_ids: (committee.forward_to_committee_ids || []).filter(
id => !committeeIds.includes(id)
)
}));
return this.sendBulkActionToBackend(CommitteeAction.UPDATE, payload);
}
protected override createViewModel(model: Committee): ViewCommittee {
const viewModel = super.createViewModel(model);
viewModel.getViewUser = (id: Id) => this.userRepo.getViewModel(id);
viewModel.canAccess = () =>
this.operator.hasCommitteePermissions(model.id, CML.can_manage) ||
this.operator.hasOrganizationPermissions(OML.can_manage_users) ||
this.operator.isInCommitteesNonAdminCheck(model);
return viewModel;
}
/**
* TODO CLEANUP: This seems unnecessary.
* removing all entries that are not fit to a class can be done using
* decorators
* https://www.typescriptlang.org/docs/handbook/decorators.html#metadata
*/
private getPartialCommitteePayload(committee: any): any {
return {
description: committee.description,
organization_tag_ids: committee.organization_tag_ids === null ? [] : committee.organization_tag_ids,
user_$_management_level: committee.user_$_management_level,
forward_to_committee_ids:
committee.forward_to_committee_ids === null ? [] : committee.forward_to_committee_ids,
receive_forwardings_from_committee_ids:
committee.receive_forwardings_from_committee_ids === null
? []
: committee.receive_forwardings_from_committee_ids,
external_id: committee.external_id
};
}
}