AAE-35340 Replace deprecated toPromise() usages with firstValueFrom / lastValueFrom (#11161)

This commit is contained in:
Amedeo Lepore
2025-09-03 08:37:13 -04:00
committed by GitHub
parent 93c872b65d
commit 51c55674b4
27 changed files with 178 additions and 191 deletions
@@ -46,7 +46,7 @@ import { MatDialog } from '@angular/material/dialog';
import { MatDialogHarness } from '@angular/material/dialog/testing';
import { By } from '@angular/platform-browser';
import { TranslateLoader, TranslateService, provideTranslateService } from '@ngx-translate/core';
import { Observable, of, throwError } from 'rxjs';
import { firstValueFrom, Observable, of, throwError } from 'rxjs';
import {
cloudFormMock,
conditionalUploadWidgetsMock,
@@ -1616,7 +1616,7 @@ describe('Multilingual Form', () => {
formComponent.ngOnChanges({ appName: new SimpleChange(null, appName, true) });
expect(formCloudService.getForm).toHaveBeenCalledWith(appName, formId, 1);
await translateService.use('fr').toPromise();
await firstValueFrom(translateService.use('fr'));
fixture.detectChanges();
await fixture.whenStable();
@@ -1626,7 +1626,7 @@ describe('Multilingual Form', () => {
expect(getLabelValue('dateField')).toEqual('Champ de date (D-M-YYYY)');
expect(getLabelValue('amountField')).toEqual('Champ Montant');
await translateService.use('en').toPromise();
await firstValueFrom(translateService.use('en'));
fixture.detectChanges();
await fixture.whenStable();
@@ -20,7 +20,7 @@ import { NotificationService } from '@alfresco/adf-core';
import { MatDialog } from '@angular/material/dialog';
import { ContentNodeSelectorComponent, ContentNodeSelectorComponentData, NodeAction, AlfrescoApiService } from '@alfresco/adf-content-services';
import { Node, NodeEntry, NodesApi } from '@alfresco/js-api';
import { from, Observable, Subject, throwError } from 'rxjs';
import { firstValueFrom, from, Observable, Subject, throwError } from 'rxjs';
import { catchError, map, mapTo } from 'rxjs/operators';
import { DestinationFolderPathModel } from '../models/form-cloud-representation.model';
@@ -36,7 +36,11 @@ export class ContentCloudNodeSelectorService {
sourceNodeNotFound = false;
constructor(private apiService: AlfrescoApiService, private notificationService: NotificationService, private dialog: MatDialog) {}
constructor(
private apiService: AlfrescoApiService,
private notificationService: NotificationService,
private dialog: MatDialog
) {}
openUploadFileDialog(
currentFolderId?: string,
@@ -65,25 +69,25 @@ export class ContentCloudNodeSelectorService {
async getNodeIdFromPath(destinationFolderPath: DestinationFolderPathModel): Promise<string> {
if (destinationFolderPath.alias && destinationFolderPath.path) {
try {
return await this.getNodeId(destinationFolderPath.alias, destinationFolderPath.path).toPromise();
return await firstValueFrom(this.getNodeId(destinationFolderPath.alias, destinationFolderPath.path));
} catch {
/*empty*/
}
}
return this.getNodeId(destinationFolderPath.alias).toPromise();
return firstValueFrom(this.getNodeId(destinationFolderPath.alias));
}
async getNodeIdFromFolderVariableValue(variableValue: string, defaultAlias?: string): Promise<string> {
const isExistingNode = await this.isExistingNode(variableValue);
return isExistingNode ? variableValue : this.getNodeId(defaultAlias).toPromise();
return isExistingNode ? variableValue : firstValueFrom(this.getNodeId(defaultAlias));
}
async isExistingNode(nodeId: string): Promise<boolean> {
let isExistingNode = false;
if (nodeId) {
try {
isExistingNode = await this.getNodeId(nodeId).pipe(mapTo(true)).toPromise();
isExistingNode = await firstValueFrom(this.getNodeId(nodeId).pipe(mapTo(true)));
} catch {
/*empty*/
}
@@ -31,7 +31,7 @@ import {
} from '@angular/core';
import { ReactiveFormsModule, UntypedFormControl } from '@angular/forms';
import { animate, state, style, transition, trigger } from '@angular/animations';
import { BehaviorSubject, Observable } from 'rxjs';
import { BehaviorSubject, firstValueFrom, Observable } from 'rxjs';
import { debounceTime, distinctUntilChanged, filter, mergeMap, switchMap, tap } from 'rxjs/operators';
import { ComponentSelectionMode } from '../../types';
import { IdentityGroupModel } from '../models/identity-group.model';
@@ -248,7 +248,7 @@ export class GroupCloudComponent implements OnInit, OnChanges {
}
private async searchGroup(name: string): Promise<IdentityGroupModel> {
return (await this.identityGroupService.search(name).toPromise())[0];
return (await firstValueFrom(this.identityGroupService.search(name)))[0];
}
private getPreselectedGroups(): IdentityGroupModel[] {
@@ -31,7 +31,7 @@ import {
ViewChild,
ViewEncapsulation
} from '@angular/core';
import { BehaviorSubject, Observable } from 'rxjs';
import { BehaviorSubject, firstValueFrom, Observable } from 'rxjs';
import { debounceTime, distinctUntilChanged, filter, mergeMap, switchMap, tap } from 'rxjs/operators';
import { FullNamePipe, InitialUsernamePipe } from '@alfresco/adf-core';
import { animate, state, style, transition, trigger } from '@angular/animations';
@@ -368,9 +368,13 @@ export class PeopleCloudComponent implements OnInit, OnChanges, AfterViewInit {
for (const user of this.getPreselectedUsers()) {
try {
const validationResult = (
await this.identityUserService
.search(user.username, { roles: this.roles, withinApplication: this.appName, groups: this.groupsRestriction })
.toPromise()
await firstValueFrom(
this.identityUserService.search(user.username, {
roles: this.roles,
withinApplication: this.appName,
groups: this.groupsRestriction
})
)
)[0];
if (!this.equalsUsers(user, validationResult)) {
@@ -246,7 +246,7 @@ describe('ProcessFilterCloudService', () => {
changedFilter.processDefinitionKey = 'modifiedProcessDefinitionKey';
spyOn<any>(service, 'defaultProcessFilters').and.returnValue(fakeProcessCloudFilters);
await service.resetProcessFilterToDefaults('mock-appName', changedFilter).toPromise();
await firstValueFrom(service.resetProcessFilterToDefaults('mock-appName', changedFilter));
expect(updatePreferenceSpy).toHaveBeenCalledWith('mock-appName', 'process-filters-mock-appName-mock-username', fakeProcessCloudFilters);
});
@@ -182,7 +182,7 @@ describe('ProcessListCloudService', () => {
it('should append to the call all the parameters', async () => {
const processRequest = { appName: 'fakeName', skipCount: 0, maxItems: 20, service: 'fake-service' } as ProcessQueryCloudRequestModel;
requestSpy.and.callFake(returnCallQueryParameters);
const request = await service.getAdminProcessByRequest(processRequest).toPromise();
const request = await firstValueFrom(service.getAdminProcessByRequest(processRequest));
expect(request).toBeDefined();
expect(request).not.toBeNull();
@@ -194,7 +194,7 @@ describe('ProcessListCloudService', () => {
it('should concat the app name to the request url', async () => {
const processRequest = { appName: 'fakeName', skipCount: 0, maxItems: 20, service: 'fake-service' } as ProcessQueryCloudRequestModel;
requestSpy.and.callFake(returnCallUrl);
const requestUrl = await service.getAdminProcessByRequest(processRequest).toPromise();
const requestUrl = await firstValueFrom(service.getAdminProcessByRequest(processRequest));
expect(requestUrl).toBeDefined();
expect(requestUrl).not.toBeNull();
@@ -213,7 +213,7 @@ describe('ProcessListCloudService', () => {
]
} as ProcessQueryCloudRequestModel;
requestSpy.and.callFake(returnCallQueryParameters);
const request = await service.getAdminProcessByRequest(processRequest).toPromise();
const request = await firstValueFrom(service.getAdminProcessByRequest(processRequest));
expect(request).toBeDefined();
expect(request).not.toBeNull();
@@ -225,7 +225,7 @@ describe('ProcessListCloudService', () => {
requestSpy.and.callFake(returnCallUrl);
try {
await service.getAdminProcessByRequest(processRequest).toPromise();
await firstValueFrom(service.getAdminProcessByRequest(processRequest));
fail('Should have thrown error');
} catch (error) {
@@ -236,7 +236,7 @@ describe('ProcessListCloudService', () => {
it('should make post request', async () => {
const processRequest = { appName: 'fakeName', skipCount: 0, maxItems: 20, service: 'fake-service' } as ProcessQueryCloudRequestModel;
requestSpy.and.callFake(returnCallOperation);
const adminProcessResponse = await service.getAdminProcessByRequest(processRequest).toPromise();
const adminProcessResponse = await firstValueFrom(service.getAdminProcessByRequest(processRequest));
expect(adminProcessResponse).toBeDefined();
expect(adminProcessResponse).not.toBeNull();
expect(adminProcessResponse.httpMethod).toBe('POST');
@@ -251,7 +251,7 @@ describe('ProcessListCloudService', () => {
variableKeys: ['test-one', 'test-two']
} as ProcessQueryCloudRequestModel;
requestSpy.and.callFake(returnCallQueryParameters);
const requestParams = await service.getAdminProcessByRequest(processRequest).toPromise();
const requestParams = await firstValueFrom(service.getAdminProcessByRequest(processRequest));
expect(requestParams).toBeDefined();
expect(requestParams).not.toBeNull();
@@ -267,7 +267,7 @@ describe('ProcessListCloudService', () => {
variableKeys: ['test-one', 'test-two']
} as ProcessQueryCloudRequestModel;
requestSpy.and.callFake(returnCallBody);
const requestBodyParams = await service.getAdminProcessByRequest(processRequest).toPromise();
const requestBodyParams = await firstValueFrom(service.getAdminProcessByRequest(processRequest));
expect(requestBodyParams).toBeDefined();
expect(requestBodyParams).not.toBeNull();
@@ -16,7 +16,7 @@
*/
import { TestBed } from '@angular/core/testing';
import { of, throwError } from 'rxjs';
import { firstValueFrom, of, throwError } from 'rxjs';
import { StartProcessCloudService } from './start-process-cloud.service';
import { fakeProcessPayload } from '../mock/start-process.component.mock';
import { ProcessDefinitionCloud } from '../../../models/process-definition-cloud.model';
@@ -37,7 +37,7 @@ describe('StartProcessCloudService', () => {
it('should be able to create a new process', async () => {
spyOn(service, 'startProcess').and.returnValue(of({ id: 'fake-id', name: 'fake-name' }));
const result = await service.startProcess('appName1', fakeProcessPayload).toPromise();
const result = await firstValueFrom(service.startProcess('appName1', fakeProcessPayload));
expect(result).toBeDefined();
expect(result.id).toEqual('fake-id');
@@ -46,7 +46,7 @@ describe('StartProcessCloudService', () => {
it('should be able to create a new process with form', async () => {
spyOn(service, 'startProcessWithForm').and.returnValue(of({ id: 'fake-id', name: 'fake-name' }));
const result = await service.startProcessWithForm('appName1', 'mockFormId', 1, fakeProcessPayload).toPromise();
const result = await firstValueFrom(service.startProcessWithForm('appName1', 'mockFormId', 1, fakeProcessPayload));
expect(result).toBeDefined();
expect(result.id).toEqual('fake-id');
@@ -61,14 +61,11 @@ describe('StartProcessCloudService', () => {
});
spyOn(service, 'startProcess').and.returnValue(throwError(errorResponse));
const result = await service
.startProcess('appName1', fakeProcessPayload)
.toPromise()
.catch((error) => {
expect(error.status).toEqual(404);
expect(error.statusText).toEqual('Not Found');
expect(error.error).toEqual('Mock Error');
});
const result = await firstValueFrom(service.startProcess('appName1', fakeProcessPayload)).catch((error) => {
expect(error.status).toEqual(404);
expect(error.statusText).toEqual('Not Found');
expect(error.error).toEqual('Mock Error');
});
if (result) {
fail('expected an error, not applications');
@@ -77,7 +74,7 @@ describe('StartProcessCloudService', () => {
it('should be able to get all the process definitions', async () => {
spyOn(service, 'getProcessDefinitions').and.returnValue(of([new ProcessDefinitionCloud({ id: 'fake-id', name: 'fake-name' })]));
const result = await service.getProcessDefinitions('appName1').toPromise();
const result = await firstValueFrom(service.getProcessDefinitions('appName1'));
expect(result).toBeDefined();
expect(result[0].id).toEqual('fake-id');
@@ -91,14 +88,11 @@ describe('StartProcessCloudService', () => {
statusText: 'Not Found'
});
spyOn(service, 'getProcessDefinitions').and.returnValue(throwError(errorResponse));
const result = await service
.getProcessDefinitions('appName1')
.toPromise()
.catch((error) => {
expect(error.status).toEqual(404);
expect(error.statusText).toEqual('Not Found');
expect(error.error).toEqual('Mock Error');
});
const result = await firstValueFrom(service.getProcessDefinitions('appName1')).catch((error) => {
expect(error.status).toEqual(404);
expect(error.statusText).toEqual('Not Found');
expect(error.error).toEqual('Mock Error');
});
if (result) {
fail('expected an error, not applications');
@@ -111,7 +105,7 @@ describe('StartProcessCloudService', () => {
const requestSpy = spyOn(adfHttpClient, 'request');
requestSpy.and.returnValue(Promise.resolve({ static1: 'value', static2: 0, static3: true }));
const result = await service.getStartEventFormStaticValuesMapping(appName, processDefinitionId).toPromise();
const result = await firstValueFrom(service.getStartEventFormStaticValuesMapping(appName, processDefinitionId));
expect(result.length).toEqual(3);
expect(result[0].name).toEqual('static1');
expect(result[0].id).toEqual('static1');
@@ -132,7 +126,7 @@ describe('StartProcessCloudService', () => {
const requestSpy = spyOn(adfHttpClient, 'request');
requestSpy.and.returnValue(Promise.resolve({ constant1: 'value', constant2: '0', constant3: 'true' }));
const result = await service.getStartEventConstants(appName, processDefinitionId).toPromise();
const result = await firstValueFrom(service.getStartEventConstants(appName, processDefinitionId));
expect(result.length).toEqual(3);
expect(result[0].name).toEqual('constant1');
@@ -18,6 +18,7 @@
import { Directive, Input, HostListener, Output, EventEmitter, OnInit, ElementRef, Renderer2 } from '@angular/core';
import { IdentityUserService } from '../../../../../people/services/identity-user.service';
import { TaskCloudService } from '../../../../services/task-cloud.service';
import { firstValueFrom } from 'rxjs';
@Directive({
// eslint-disable-next-line @angular-eslint/directive-selector
@@ -86,7 +87,7 @@ export class ClaimTaskCloudDirective implements OnInit {
const currentUser: string = this.identityUserService.getCurrentUserInfo().username;
try {
this.renderer.setAttribute(this.el.nativeElement, 'disabled', 'true');
const result = await this.taskListService.claimTask(this.appName, this.taskId, currentUser).toPromise();
const result = await firstValueFrom(this.taskListService.claimTask(this.appName, this.taskId, currentUser));
if (result) {
this.success.emit(result);
}
@@ -17,6 +17,7 @@
import { Directive, Input, HostListener, Output, EventEmitter, OnInit, ElementRef, Renderer2 } from '@angular/core';
import { TaskCloudService } from '../../../../services/task-cloud.service';
import { firstValueFrom } from 'rxjs';
@Directive({
// eslint-disable-next-line @angular-eslint/directive-selector
@@ -41,7 +42,11 @@ export class UnClaimTaskCloudDirective implements OnInit {
invalidParams: string[] = [];
constructor(private readonly el: ElementRef, private readonly renderer: Renderer2, private taskListService: TaskCloudService) {}
constructor(
private readonly el: ElementRef,
private readonly renderer: Renderer2,
private taskListService: TaskCloudService
) {}
ngOnInit() {
this.validateInputs();
@@ -71,7 +76,7 @@ export class UnClaimTaskCloudDirective implements OnInit {
async onClick() {
try {
this.renderer.setAttribute(this.el.nativeElement, 'disabled', 'true');
await this.taskListService.unclaimTask(this.appName, this.taskId).toPromise();
await firstValueFrom(this.taskListService.unclaimTask(this.appName, this.taskId));
this.success.emit(this.taskId);
} catch (error) {
this.renderer.removeAttribute(this.el.nativeElement, 'disabled');
@@ -17,6 +17,7 @@
import { Directive, Input, HostListener, Output, EventEmitter, OnInit, ElementRef, Renderer2 } from '@angular/core';
import { TaskCloudService } from '../../../../services/task-cloud.service';
import { firstValueFrom } from 'rxjs';
@Directive({
// eslint-disable-next-line @angular-eslint/directive-selector
@@ -41,7 +42,11 @@ export class CompleteTaskDirective implements OnInit {
invalidParams: string[] = [];
constructor(private readonly el: ElementRef, private readonly renderer: Renderer2, private readonly taskListService: TaskCloudService) {}
constructor(
private readonly el: ElementRef,
private readonly renderer: Renderer2,
private readonly taskListService: TaskCloudService
) {}
ngOnInit() {
this.validateInputs();
@@ -71,7 +76,7 @@ export class CompleteTaskDirective implements OnInit {
async onClick() {
try {
this.renderer.setAttribute(this.el.nativeElement, 'disabled', 'true');
const result = await this.taskListService.completeTask(this.appName, this.taskId).toPromise();
const result = await firstValueFrom(this.taskListService.completeTask(this.appName, this.taskId));
if (result) {
this.success.emit(result);
}
@@ -18,7 +18,7 @@
import { TestBed } from '@angular/core/testing';
import { ServiceTaskListCloudService } from './service-task-list-cloud.service';
import { ServiceTaskQueryCloudRequestModel } from '../models/service-task-cloud.model';
import { of } from 'rxjs';
import { firstValueFrom, of } from 'rxjs';
import { AdfHttpClient } from '@alfresco/adf-core/api';
import { NoopTranslateModule } from '@alfresco/adf-core';
@@ -112,56 +112,44 @@ describe('Activiti ServiceTaskList Cloud Service', () => {
const spyOnPost = spyOn<any>(service, 'post').and.returnValue(of({}));
const params = ['fakeName', 'executionId_1', 'flowNodeId_1'] as const;
await service.replayServiceTaskRequest(...params).toPromise();
await firstValueFrom(service.replayServiceTaskRequest(...params));
expect(spyOnPost).toHaveBeenCalledWith(expected.expectedQueryUrl, expected.expectedPayload);
});
it('should throw an exeption and execute logService error if appName is null', (done) => {
const expectedErrorMessage = 'Appname/executionId/flowNodeId not configured';
const params = [null, 'executionId_1', 'flowNodeId_1'] as const;
service
.replayServiceTaskRequest(...params)
.toPromise()
.catch((error) => {
expect(error).toEqual(expectedErrorMessage);
done();
});
firstValueFrom(service.replayServiceTaskRequest(...params)).catch((error) => {
expect(error).toEqual(expectedErrorMessage);
done();
});
});
it('should throw an exeption and execute logService error if executionId is null', (done) => {
const expectedErrorMessage = 'Appname/executionId/flowNodeId not configured';
const params = ['fakeName', null, 'flowNodeId_1'] as const;
service
.replayServiceTaskRequest(...params)
.toPromise()
.catch((error) => {
expect(error).toEqual(expectedErrorMessage);
done();
});
firstValueFrom(service.replayServiceTaskRequest(...params)).catch((error) => {
expect(error).toEqual(expectedErrorMessage);
done();
});
});
it('should throw an exeption and execute logService error if flowNodeId is null', (done) => {
const expectedErrorMessage = 'Appname/executionId/flowNodeId not configured';
const params = ['fakeName', 'executionId_1', null] as const;
service
.replayServiceTaskRequest(...params)
.toPromise()
.catch((error) => {
expect(error).toEqual(expectedErrorMessage);
done();
});
firstValueFrom(service.replayServiceTaskRequest(...params)).catch((error) => {
expect(error).toEqual(expectedErrorMessage);
done();
});
});
it('should throw an exeption and execute logService error if appName, executionId and flowNodeId are null', (done) => {
const expectedErrorMessage = 'Appname/executionId/flowNodeId not configured';
const params = [null, null, null] as const;
service
.replayServiceTaskRequest(...params)
.toPromise()
.catch((error) => {
expect(error).toEqual(expectedErrorMessage);
done();
});
firstValueFrom(service.replayServiceTaskRequest(...params)).catch((error) => {
expect(error).toEqual(expectedErrorMessage);
done();
});
});
});
});