72541: user-agreement update to patch

This commit is contained in:
Kristof De Langhe
2020-09-17 11:31:16 +02:00
parent abab73b909
commit 4e10db8a53
3 changed files with 16 additions and 19 deletions

View File

@@ -3,7 +3,7 @@ import { Store } from '@ngrx/store';
import { Operation } from 'fast-json-patch'; import { Operation } from 'fast-json-patch';
import { Observable } from 'rxjs'; import { Observable } from 'rxjs';
import { distinctUntilChanged, filter, find, first, map, mergeMap, switchMap, take } from 'rxjs/operators'; import { distinctUntilChanged, filter, find, first, map, mergeMap, switchMap, take } from 'rxjs/operators';
import { hasValue, isNotEmpty, isNotEmptyOperator } from '../../shared/empty.util'; import { hasValue, hasValueOperator, isNotEmpty, isNotEmptyOperator } from '../../shared/empty.util';
import { NotificationOptions } from '../../shared/notifications/models/notification-options.model'; import { NotificationOptions } from '../../shared/notifications/models/notification-options.model';
import { NotificationsService } from '../../shared/notifications/notifications.service'; import { NotificationsService } from '../../shared/notifications/notifications.service';
import { FollowLinkConfig } from '../../shared/utils/follow-link-config.model'; import { FollowLinkConfig } from '../../shared/utils/follow-link-config.model';
@@ -376,6 +376,7 @@ export abstract class DataService<T extends CacheableObject> implements UpdateDa
).subscribe(); ).subscribe();
return this.requestService.getByUUID(requestId).pipe( return this.requestService.getByUUID(requestId).pipe(
hasValueOperator(),
find((request: RequestEntry) => request.completed), find((request: RequestEntry) => request.completed),
map((request: RequestEntry) => request.response) map((request: RequestEntry) => request.response)
); );

View File

@@ -7,6 +7,7 @@ import { CookieServiceMock } from '../../shared/mocks/cookie.service.mock';
import { of as observableOf } from 'rxjs'; import { of as observableOf } from 'rxjs';
import { EPerson } from '../eperson/models/eperson.model'; import { EPerson } from '../eperson/models/eperson.model';
import { createSuccessfulRemoteDataObject$ } from '../../shared/remote-data.utils'; import { createSuccessfulRemoteDataObject$ } from '../../shared/remote-data.utils';
import { RestResponse } from '../cache/response.models';
describe('EndUserAgreementService', () => { describe('EndUserAgreementService', () => {
let service: EndUserAgreementService; let service: EndUserAgreementService;
@@ -36,7 +37,8 @@ describe('EndUserAgreementService', () => {
getAuthenticatedUserFromStore: observableOf(userWithMetadata) getAuthenticatedUserFromStore: observableOf(userWithMetadata)
}); });
ePersonService = jasmine.createSpyObj('ePersonService', { ePersonService = jasmine.createSpyObj('ePersonService', {
update: createSuccessfulRemoteDataObject$(userWithMetadata) update: createSuccessfulRemoteDataObject$(userWithMetadata),
patch: observableOf(new RestResponse(true, 200, 'OK'))
}); });
service = new EndUserAgreementService(cookie, authService, ePersonService); service = new EndUserAgreementService(cookie, authService, ePersonService);
@@ -98,15 +100,7 @@ describe('EndUserAgreementService', () => {
it('setUserAcceptedAgreement should update the user with new metadata', (done) => { it('setUserAcceptedAgreement should update the user with new metadata', (done) => {
service.setUserAcceptedAgreement(true).subscribe(() => { service.setUserAcceptedAgreement(true).subscribe(() => {
expect(ePersonService.update).toHaveBeenCalledWith(jasmine.objectContaining({ expect(ePersonService.patch).toHaveBeenCalled();
metadata: jasmine.objectContaining({
[END_USER_AGREEMENT_METADATA_FIELD]: [
{
value: 'true'
}
]
})
}));
done(); done();
}); });
}); });

View File

@@ -5,10 +5,7 @@ import { Observable } from 'rxjs/internal/Observable';
import { of as observableOf } from 'rxjs'; import { of as observableOf } from 'rxjs';
import { map, switchMap, take } from 'rxjs/operators'; import { map, switchMap, take } from 'rxjs/operators';
import { hasValue } from '../../shared/empty.util'; import { hasValue } from '../../shared/empty.util';
import { cloneDeep } from 'lodash';
import { Metadata } from '../shared/metadata.utils';
import { EPersonDataService } from '../eperson/eperson-data.service'; import { EPersonDataService } from '../eperson/eperson-data.service';
import { getSucceededRemoteData } from '../shared/operators';
export const END_USER_AGREEMENT_COOKIE = 'hasAgreedEndUser'; export const END_USER_AGREEMENT_COOKIE = 'hasAgreedEndUser';
export const END_USER_AGREEMENT_METADATA_FIELD = 'dspace.agreements.end-user'; export const END_USER_AGREEMENT_METADATA_FIELD = 'dspace.agreements.end-user';
@@ -67,13 +64,18 @@ export class EndUserAgreementService {
switchMap((authenticated) => { switchMap((authenticated) => {
if (authenticated) { if (authenticated) {
return this.authService.getAuthenticatedUserFromStore().pipe( return this.authService.getAuthenticatedUserFromStore().pipe(
take(1),
switchMap((user) => { switchMap((user) => {
const updatedUser = cloneDeep(user); const newValue = { value: String(accepted) };
Metadata.setFirstValue(updatedUser.metadata, END_USER_AGREEMENT_METADATA_FIELD, String(accepted)); let operation;
return this.ePersonService.update(updatedUser); if (user.hasMetadata(END_USER_AGREEMENT_METADATA_FIELD)) {
operation = { op: 'replace', path: `/metadata/${END_USER_AGREEMENT_METADATA_FIELD}/0`, value: newValue };
} else {
operation = { op: 'add', path: `/metadata/${END_USER_AGREEMENT_METADATA_FIELD}`, value: [ newValue ] };
}
return this.ePersonService.patch(user, [operation]);
}), }),
getSucceededRemoteData(), map((response) => response.isSuccessful)
map((rd) => hasValue(rd.payload))
); );
} else { } else {
this.setCookieAccepted(accepted); this.setCookieAccepted(accepted);