From 88d89b4ca8ed366b0cb16aaacd941322cc663060 Mon Sep 17 00:00:00 2001 From: Maurizio Vitale Date: Wed, 11 Dec 2019 11:22:50 +0000 Subject: [PATCH] [AAE-1152] Making sure the people and group widget is working fine as part of form (#5322) * Making sure the people and group widget is working fine as part of the form * Be able to save a form with people and group * Fix tslint * Fix html error * Fix unit test --- .../widgets/core/form-field.model.ts | 19 +++++---- .../services/identity-group.service.spec.ts | 26 ++++--------- lib/core/services/identity-group.service.ts | 13 +++++-- lib/core/services/identity-user.service.ts | 39 ++++++++++++++----- .../form/components/form-cloud.component.ts | 1 + .../widgets/group/group-cloud.widget.html | 7 ++-- .../widgets/group/group-cloud.widget.ts | 24 +++++++++--- .../widgets/people/people-cloud.widget.html | 3 ++ .../widgets/people/people-cloud.widget.ts | 19 +++++++-- .../components/people-cloud.component.ts | 2 +- 10 files changed, 98 insertions(+), 55 deletions(-) diff --git a/lib/core/form/components/widgets/core/form-field.model.ts b/lib/core/form/components/widgets/core/form-field.model.ts index f44661ae4f..f3ebed421a 100644 --- a/lib/core/form/components/widgets/core/form-field.model.ts +++ b/lib/core/form/components/widgets/core/form-field.model.ts @@ -55,6 +55,7 @@ export class FormFieldModel extends FormWidgetModel { regexPattern: string; options: FormFieldOption[] = []; restUrl: string; + roles: string[]; restResponsePath: string; restIdProperty: string; restLabelProperty: string; @@ -79,11 +80,6 @@ export class FormFieldModel extends FormWidgetModel { emptyOption: FormFieldOption; validationSummary: ErrorMessageModel; - // People and Group Options - appName: string; - roles: string[]; - mode: string; - get value(): any { return this._value; } @@ -145,6 +141,8 @@ export class FormFieldModel extends FormWidgetModel { this.id = json.id; this.name = json.name; this.type = json.type; + this.roles = json.roles; + this.optionType = json.optionType; this._required = json.required; this._readOnly = json.readOnly || json.type === 'readonly'; this.overrideId = json.overrideId; @@ -173,11 +171,6 @@ export class FormFieldModel extends FormWidgetModel { this._value = this.parseValue(json); this.validationSummary = new ErrorMessageModel(); - // People and Group Options - this.appName = json.appName; - this.roles = json.roles; - this.mode = json.mode; - if (json.placeholder && json.placeholder !== '' && json.placeholder !== 'null') { this.placeholder = json.placeholder; } @@ -403,6 +396,12 @@ export class FormFieldModel extends FormWidgetModel { case FormFieldTypes.BOOLEAN: this.form.values[this.id] = (this.value !== null && this.value !== undefined) ? this.value : false; break; + case FormFieldTypes.PEOPLE: + this.form.values[this.id] = (this.value !== null && this.value !== undefined) ? this.value : []; + break; + case FormFieldTypes.FUNCTIONAL_GROUP: + this.form.values[this.id] = (this.value !== null && this.value !== undefined) ? this.value : []; + break; default: if (!FormFieldTypes.isReadOnlyType(this.type) && !this.isInvalidFieldType(this.type)) { this.form.values[this.id] = this.value; diff --git a/lib/core/services/identity-group.service.spec.ts b/lib/core/services/identity-group.service.spec.ts index eb3cdf9cb7..9371da6b75 100644 --- a/lib/core/services/identity-group.service.spec.ts +++ b/lib/core/services/identity-group.service.spec.ts @@ -34,8 +34,6 @@ import { groupsMockApi, roleMappingApi, clientRoles, - returnCallQueryParameters, - returnCallUrl, applicationDetailsMockApi, mockApiError, mockIdentityGroup1, @@ -233,22 +231,14 @@ describe('IdentityGroupService', () => { ); }); - it('should append to the call all the parameters', (done) => { - spyOn(apiService, 'getInstance').and.returnValue(returnCallQueryParameters); - service.findGroupsByName( {name: 'mock'}).subscribe((res) => { - expect(res).toBeDefined(); - expect(res).not.toBeNull(); - expect(res.search).toBe('mock'); - done(); - }); - }); - - it('should request groups api url', (done) => { - spyOn(apiService, 'getInstance').and.returnValue(returnCallUrl); - service.findGroupsByName( {name: 'mock'}).subscribe((requestUrl) => { - expect(requestUrl).toBeDefined(); - expect(requestUrl).not.toBeNull(); - expect(requestUrl).toContain('/groups'); + it('should return only the properties of IdentityGroupSearchParam', (done) => { + spyOn(apiService, 'getInstance').and.returnValue(groupsMockApi); + service.findGroupsByName( {name: 'mock'}).subscribe((groups) => { + expect(groups).toBeDefined(); + expect(groups).toBeDefined(); + expect(groups[0].id).toEqual('mock-group-id-1'); + expect(groups[0].name).toEqual('Mock Group 1'); + expect(groups[0]['subGroups']).not.toBeDefined(); done(); }); }); diff --git a/lib/core/services/identity-group.service.ts b/lib/core/services/identity-group.service.ts index 239386f86a..3475ede66d 100644 --- a/lib/core/services/identity-group.service.ts +++ b/lib/core/services/identity-group.service.ts @@ -173,7 +173,7 @@ export class IdentityGroupService { * @param searchParams Object containing the name filter string * @returns List of group information */ - findGroupsByName(searchParams: IdentityGroupSearchParam): Observable { + findGroupsByName(searchParams: IdentityGroupSearchParam): Observable { if (searchParams.name === '') { return of([]); } @@ -181,12 +181,17 @@ export class IdentityGroupService { const httpMethod = 'GET', pathParams = {}, queryParams = {search: searchParams.name}, bodyParam = {}, headerParams = {}, formParams = {}, contentTypes = ['application/json'], accepts = ['application/json']; - return (from(this.alfrescoApiService.getInstance().oauth2Auth.callCustomApi( + return from(this.alfrescoApiService.getInstance().oauth2Auth.callCustomApi( url, httpMethod, pathParams, queryParams, headerParams, formParams, bodyParam, contentTypes, accepts, Object, null, null) - )).pipe( - catchError((error) => this.handleError(error)) + ).pipe( + map((response: []) => { + return response.map( (group: IdentityGroupModel) => { + return {id: group.id, name: group.name}; + }); + }), + catchError((err) => this.handleError(err)) ); } diff --git a/lib/core/services/identity-user.service.ts b/lib/core/services/identity-user.service.ts index 519175178d..ac28fe2965 100644 --- a/lib/core/services/identity-user.service.ts +++ b/lib/core/services/identity-user.service.ts @@ -78,7 +78,7 @@ export class IdentityUserService { * @param search Search query string * @returns List of users */ - findUsersByName(search: string): Observable { + findUsersByName(search: string): Observable { if (search === '') { return of([]); } @@ -86,11 +86,18 @@ export class IdentityUserService { const httpMethod = 'GET', pathParams = {}, queryParams = { search: search }, bodyParam = {}, headerParams = {}, formParams = {}, contentTypes = ['application/json'], accepts = ['application/json']; - return (from(this.alfrescoApiService.getInstance().oauth2Auth.callCustomApi( + return from(this.alfrescoApiService.getInstance().oauth2Auth.callCustomApi( url, httpMethod, pathParams, queryParams, headerParams, formParams, bodyParam, contentTypes, accepts, Object, null, null) - )); + ).pipe( + map((response: []) => { + return response.map( (user: IdentityUserModel) => { + return {id: user.id, firstName: user.firstName, lastName: user.lastName, email: user.email, username: user.username}; + }); + }), + catchError((err) => this.handleError(err)) + ); } /** @@ -98,7 +105,7 @@ export class IdentityUserService { * @param username Search query string * @returns List of users */ - findUserByUsername(username: string): Observable { + findUserByUsername(username: string): Observable { if (username === '') { return of([]); } @@ -106,11 +113,18 @@ export class IdentityUserService { const httpMethod = 'GET', pathParams = {}, queryParams = { username: username }, bodyParam = {}, headerParams = {}, formParams = {}, contentTypes = ['application/json'], accepts = ['application/json']; - return (from(this.alfrescoApiService.getInstance().oauth2Auth.callCustomApi( + return from(this.alfrescoApiService.getInstance().oauth2Auth.callCustomApi( url, httpMethod, pathParams, queryParams, headerParams, formParams, bodyParam, contentTypes, accepts, Object, null, null) - )); + ).pipe( + map((response: []) => { + return response.map( (user: IdentityUserModel) => { + return {id: user.id, firstName: user.firstName, lastName: user.lastName, email: user.email, username: user.username}; + }); + }), + catchError((err) => this.handleError(err)) + ); } /** @@ -118,7 +132,7 @@ export class IdentityUserService { * @param email Search query string * @returns List of users */ - findUserByEmail(email: string): Observable { + findUserByEmail(email: string): Observable { if (email === '') { return of([]); } @@ -126,11 +140,18 @@ export class IdentityUserService { const httpMethod = 'GET', pathParams = {}, queryParams = { email: email }, bodyParam = {}, headerParams = {}, formParams = {}, contentTypes = ['application/json'], accepts = ['application/json']; - return (from(this.alfrescoApiService.getInstance().oauth2Auth.callCustomApi( + return from(this.alfrescoApiService.getInstance().oauth2Auth.callCustomApi( url, httpMethod, pathParams, queryParams, headerParams, formParams, bodyParam, contentTypes, accepts, Object, null, null) - )); + ).pipe( + map((response: []) => { + return response.map( (user: IdentityUserModel) => { + return {id: user.id, firstName: user.firstName, lastName: user.lastName, email: user.email, username: user.username}; + }); + }), + catchError((err) => this.handleError(err)) + ); } /** diff --git a/lib/process-services-cloud/src/lib/form/components/form-cloud.component.ts b/lib/process-services-cloud/src/lib/form/components/form-cloud.component.ts index 8a8ee1054d..8f6a44707d 100644 --- a/lib/process-services-cloud/src/lib/form/components/form-cloud.component.ts +++ b/lib/process-services-cloud/src/lib/form/components/form-cloud.component.ts @@ -202,6 +202,7 @@ export class FormCloudComponent extends FormBaseComponent implements OnChanges, .subscribe( (data) => { this.formCloudRepresentationJSON = data[0]; + this.formCloudRepresentationJSON.processVariables = data[1]; this.data = data[1]; diff --git a/lib/process-services-cloud/src/lib/form/components/widgets/group/group-cloud.widget.html b/lib/process-services-cloud/src/lib/form/components/widgets/group/group-cloud.widget.html index dda0795e75..af8bb1f59f 100644 --- a/lib/process-services-cloud/src/lib/form/components/widgets/group/group-cloud.widget.html +++ b/lib/process-services-cloud/src/lib/form/components/widgets/group/group-cloud.widget.html @@ -3,12 +3,11 @@ - - diff --git a/lib/process-services-cloud/src/lib/form/components/widgets/group/group-cloud.widget.ts b/lib/process-services-cloud/src/lib/form/components/widgets/group/group-cloud.widget.ts index b4bc20b95b..d07736a0d0 100644 --- a/lib/process-services-cloud/src/lib/form/components/widgets/group/group-cloud.widget.ts +++ b/lib/process-services-cloud/src/lib/form/components/widgets/group/group-cloud.widget.ts @@ -16,7 +16,7 @@ */ import { Component, OnInit, ViewEncapsulation } from '@angular/core'; -import { baseHost, WidgetComponent, IdentityGroupCountModel } from '@alfresco/adf-core'; +import { baseHost, WidgetComponent, IdentityGroupModel } from '@alfresco/adf-core'; /* tslint:disable:component-selector */ @@ -28,17 +28,29 @@ import { baseHost, WidgetComponent, IdentityGroupCountModel } from '@alfresco/ad }) export class GroupCloudWidgetComponent extends WidgetComponent implements OnInit { - appName: string; roles: string[]; mode: string; - preSelectGroup: IdentityGroupCountModel[]; + title: string; + preSelectGroup: IdentityGroupModel[]; ngOnInit() { if (this.field) { - this.appName = this.field.appName; this.roles = this.field.roles; - this.mode = this.field.mode; - this.preSelectGroup = this.field.value; + this.mode = this.field.optionType; + this.title = this.field.placeholder; + this.preSelectGroup = this.field.value ? this.field.value : []; } } + + onSelectGroup(group: IdentityGroupModel) { + this.field.value = [...this.field.value, group]; + this.onFieldChanged(this.field); + } + + onRemoveGroup(group: IdentityGroupModel) { + const indexToRemove = this.field.value.findIndex((selected) => { return selected.id === group.id; }); + this.field.value.splice(indexToRemove, 1); + this.field.value = [...this.field.value]; + this.onFieldChanged(this.field); + } } diff --git a/lib/process-services-cloud/src/lib/form/components/widgets/people/people-cloud.widget.html b/lib/process-services-cloud/src/lib/form/components/widgets/people/people-cloud.widget.html index 6671e4ebb0..a106006784 100644 --- a/lib/process-services-cloud/src/lib/form/components/widgets/people/people-cloud.widget.html +++ b/lib/process-services-cloud/src/lib/form/components/widgets/people/people-cloud.widget.html @@ -5,6 +5,9 @@ [preSelectUsers]="preSelectUsers" [validate]="true" [appName]="appName" + [title]="title" + (selectUser)="onSelectUser($event)" + (removeUser)="onRemoveUser($event)" [roles]="roles" [mode]="mode"> diff --git a/lib/process-services-cloud/src/lib/form/components/widgets/people/people-cloud.widget.ts b/lib/process-services-cloud/src/lib/form/components/widgets/people/people-cloud.widget.ts index e733c628f2..a79549e48e 100644 --- a/lib/process-services-cloud/src/lib/form/components/widgets/people/people-cloud.widget.ts +++ b/lib/process-services-cloud/src/lib/form/components/widgets/people/people-cloud.widget.ts @@ -31,14 +31,27 @@ export class PeopleCloudWidgetComponent extends WidgetComponent implements OnIni appName: string; roles: string[]; mode: string; + title: string; preSelectUsers: IdentityUserModel[]; ngOnInit() { if (this.field) { - this.appName = this.field.appName; this.roles = this.field.roles; - this.mode = this.field.mode; - this.preSelectUsers = this.field.value; + this.mode = this.field.optionType; + this.title = this.field.placeholder; + this.preSelectUsers = this.field.value ? this.field.value : []; } } + + onSelectUser(user: IdentityUserModel) { + this.field.value = [...this.field.value, user]; + this.onFieldChanged(this.field); + } + + onRemoveUser(user: IdentityUserModel) { + const indexToRemove = this.field.value.findIndex((selectedUser) => { return selectedUser.id === user.id; }); + this.field.value.splice(indexToRemove, 1); + this.field.value = [...this.field.value]; + this.onFieldChanged(this.field); + } } diff --git a/lib/process-services-cloud/src/lib/people/components/people-cloud.component.ts b/lib/process-services-cloud/src/lib/people/components/people-cloud.component.ts index c64b5f2632..470d026396 100644 --- a/lib/process-services-cloud/src/lib/people/components/people-cloud.component.ts +++ b/lib/process-services-cloud/src/lib/people/components/people-cloud.component.ts @@ -69,7 +69,7 @@ export class PeopleCloudComponent implements OnInit, OnChanges, OnDestroy { * Mandatory properties are: id, email, username */ @Input() - preSelectUsers: IdentityUserModel[]; + preSelectUsers: IdentityUserModel[] = []; /** FormControl to search the user */ @Input()