11import { Component , Inject , OnInit } from "@angular/core" ;
22import { UntypedFormControl } from "@angular/forms" ;
33import { MAT_DIALOG_DATA } from "@angular/material/dialog" ;
4- import { ActivatedRoute } from "@angular/router" ;
4+ import { Organisation } from "@app/admin/organisation/organisation.model" ;
5+ import { OrganisationService } from "@app/admin/organisation/organisation.service" ;
56import { PermissionResponse } from "@app/admin/permission/permission.model" ;
67import { PermissionService } from "@app/admin/permission/permission.service" ;
7- import { Application , ApplicationRequest } from "@applications/application.model" ;
8+ import { Application , UpdateApplicationOrganization } from "@applications/application.model" ;
89import { TranslateService } from "@ngx-translate/core" ;
910import { ApplicationDialogModel } from "@shared/models/dialog.model" ;
11+ import { MeService } from "@shared/services/me.service" ;
1012import { RestService } from "@shared/services/rest.service" ;
1113import { SharedVariableService } from "@shared/shared-variable/shared-variable.service" ;
1214import { ReplaySubject , Subscription } from "rxjs" ;
@@ -19,20 +21,29 @@ import { ReplaySubject, Subscription } from "rxjs";
1921export class ChangeOrganizationDialogComponent implements OnInit {
2022 public applicationsSubscription : Subscription ;
2123 public permissionsSubscription : Subscription ;
24+ public organizationsSubscription : Subscription ;
2225 public permissionMultiCtrl : UntypedFormControl = new UntypedFormControl ( ) ;
2326 public permissionMultiFilterCtrl : UntypedFormControl = new UntypedFormControl ( ) ;
24- application = new ApplicationRequest ( ) ;
27+ public application : UpdateApplicationOrganization ;
2528 public permissions : PermissionResponse [ ] ;
29+ public organizations : Organisation [ ] ;
2630 public filteredPermissionsMulti : ReplaySubject < PermissionResponse [ ] > = new ReplaySubject < PermissionResponse [ ] > ( 1 ) ;
31+ public filteredOrganizationsMulti : ReplaySubject < Organisation [ ] > = new ReplaySubject < Organisation [ ] > ( 1 ) ;
2732
2833 constructor (
2934 private restService : RestService ,
30- private route : ActivatedRoute ,
3135 public translate : TranslateService ,
3236 private permissionService : PermissionService ,
37+ private organizationService : OrganisationService ,
3338 private sharedVariableService : SharedVariableService ,
39+ private meService : MeService ,
3440 @Inject ( MAT_DIALOG_DATA ) public dialogModel : ApplicationDialogModel
3541 ) {
42+ this . application = {
43+ applicationId : this . dialogModel . id ,
44+ organizationId : this . dialogModel . organizationId ?? this . sharedVariableService . getSelectedOrganisationId ( ) ,
45+ permissionIds : [ ] ,
46+ } ;
3647 this . permissionMultiCtrl . setValue ( this . application . permissionIds ) ;
3748 }
3849
@@ -41,7 +52,8 @@ export class ChangeOrganizationDialogComponent implements OnInit {
4152 if ( this . dialogModel . id ) {
4253 this . getApplication ( this . dialogModel . id ) ;
4354 }
44- this . getPermissions ( this . sharedVariableService . getUserInfo ( ) . user . id ) ;
55+ this . getOrganizations ( ) ;
56+ this . getPermissions ( ) ;
4557 }
4658
4759 public compare ( o1 : any , o2 : any ) : boolean {
@@ -52,38 +64,47 @@ export class ChangeOrganizationDialogComponent implements OnInit {
5264 this . applicationsSubscription = this . restService
5365 . get ( "application" , { } , id )
5466 . subscribe ( ( application : Application ) => {
55- this . application = new ApplicationRequest ( ) ;
56- this . application . name = application . name ;
57- this . application . description = application . description ;
58- this . application . organizationId = application . belongsTo . id ;
59- this . application . status = application . status ;
60- this . application . startDate = application . startDate ;
61- this . application . endDate = application . endDate ;
62-
63- this . application . category = application . category ;
64- this . application . owner = application . owner ;
65- this . application . contactPerson = application . contactPerson ;
66- this . application . contactEmail = application . contactEmail ;
67- this . application . contactPhone = application . contactPhone ;
68- this . application . personalData = application . personalData ;
69- this . application . hardware = application . hardware ;
70- this . application . controlledProperties = application . controlledProperties . map ( ctrlProperty => ctrlProperty . type ) ;
71- this . application . deviceTypes = application . deviceTypes . map ( deviceType => deviceType . type ) ;
7267 this . application . permissionIds = application . permissionIds ;
7368 this . permissionMultiCtrl . setValue ( this . application . permissionIds ) ;
7469 } ) ;
7570 }
7671
77- getPermissions ( userId : number ) {
72+ getOrganizations ( ) {
73+ this . organizationsSubscription = this . organizationService . getMinimal ( ) . subscribe ( res => {
74+ this . organizations = res . data ;
75+ this . filteredOrganizationsMulti . next ( this . organizations . slice ( ) ) ;
76+ } ) ;
77+ }
78+
79+ getPermissions ( ) {
7880 this . permissionsSubscription = this . permissionService
79- . getPermissions ( 1000 , 0 , undefined , undefined , userId , this . sharedVariableService . getSelectedOrganisationId ( ) )
81+ . getPermissions (
82+ 1000 ,
83+ 0 ,
84+ undefined ,
85+ undefined ,
86+ this . meService . hasGlobalAdmin ( ) ? undefined : this . sharedVariableService . getUserInfo ( ) . user . id
87+ )
8088 . subscribe ( res => {
8189 this . permissions = res . data . sort ( ( a , b ) => a . name . localeCompare ( b . name , "da-DK" , { numeric : true } ) ) ;
82- this . filteredPermissionsMulti . next ( this . permissions . slice ( ) ) ;
90+ this . filteredPermissionsMulti . next (
91+ this . permissions . filter ( p => p ?. organization ?. id === this ?. application ?. organizationId )
92+ ) ;
8393 if ( ! this . dialogModel . id ) {
8494 this . application . permissionIds = [ this . permissions [ 0 ] . id ] ;
8595 this . permissionMultiCtrl . setValue ( this . application . permissionIds ) ;
8696 }
8797 } ) ;
8898 }
99+
100+ onOrganizationChange ( ) {
101+ this . filteredPermissionsMulti . next (
102+ this . permissions . filter ( p => p ?. organization ?. id === this ?. application ?. organizationId )
103+ ) ;
104+ this . filteredPermissionsMulti . subscribe ( res => {
105+ this . permissionMultiCtrl . setValue (
106+ res . filter ( permission => permission . automaticallyAddNewApplications ) . map ( permission => permission . id )
107+ ) ;
108+ } ) ;
109+ }
89110}
0 commit comments