Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/app/shared/cookies/browser-orejime.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -433,4 +433,31 @@ describe('BrowserOrejimeService', () => {
expect(service.orejimeConfig.apps).not.toContain(jasmine.objectContaining({ name: googleAnalytics }));
});
});

describe('applyUpdateSettingsCallbackToApps', () => {
let user2: EPerson;
let mockApp1, mockApp2;
let updateSettingsSpy;

beforeEach(() => {
user2 = Object.assign(new EPerson(), { uuid: 'test-user' });
mockApp1 = { name: 'app1', callback: jasmine.createSpy('originalCallback1') };
mockApp2 = { name: 'app2', callback: jasmine.createSpy('originalCallback2') };
service.orejimeConfig.apps = [mockApp1, mockApp2];
updateSettingsSpy = spyOn(service, 'updateSettingsForUsers');
});

it('calls updateSettingsForUsers in a debounced manner when a callback is triggered', (done) => {
service.applyUpdateSettingsCallbackToApps(user2);

mockApp1.callback(true);
mockApp2.callback(false);

setTimeout(() => {
expect(updateSettingsSpy).toHaveBeenCalledTimes(1);
expect(updateSettingsSpy).toHaveBeenCalledWith(user2);
done();
}, 400);
});
});
});
34 changes: 31 additions & 3 deletions src/app/shared/cookies/browser-orejime.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,12 +192,39 @@
this.translateConfiguration();

this.orejimeConfig.apps = this.filterConfigApps(appsToHide);
this.lazyOrejime.then(({ init }) => {

this.applyUpdateSettingsCallbackToApps(user);

void this.lazyOrejime.then(({ init }) => {
this.orejimeInstance = init(this.orejimeConfig);
});
});
}

/**
* Applies a debounced callback to update user settings for all apps in the Orejime configuration.
*
* This method modifies the `callback` property of each app in the `orejimeConfig.apps` array.
* It ensures that the `updateSettingsForUsers` method is called in a debounced manner whenever
* a consent change occurs for any app. Additionally, it preserves and invokes the original
* callback for each app if one is defined.
*
* @param {EPerson} user - The authenticated user whose settings are being updated.
*/
applyUpdateSettingsCallbackToApps(user: EPerson) {
const updateSettingsCallback = debounce(() => this.updateSettingsForUsers(user), updateDebounce);

this.orejimeConfig.apps.forEach((app) => {
const originalCallback = app.callback;
app.callback = (consent: boolean) => {
updateSettingsCallback();
if (originalCallback) {
originalCallback(consent);
}
};
});
}

/**
* Return saved preferences stored in the orejime cookie
*/
Expand All @@ -220,7 +247,6 @@
* @param user The authenticated user
*/
private initializeUser(user: EPerson) {
this.orejimeConfig.callback = debounce((consent, app) => this.updateSettingsForUsers(user), updateDebounce);
this.orejimeConfig.cookieName = this.getStorageName(user.uuid);

const anonCookie = this.cookieService.get(ANONYMOUS_STORAGE_NAME_OREJIME);
Expand Down Expand Up @@ -387,7 +413,9 @@
* @param user
*/
updateSettingsForUsers(user: EPerson) {
this.setSettingsForUser(user, this.cookieService.get(this.getStorageName(user.uuid)));
if (user) {
this.setSettingsForUser(user, this.cookieService.get(this.getStorageName(user.uuid)));

Check warning on line 417 in src/app/shared/cookies/browser-orejime.service.ts

View check run for this annotation

Codecov / codecov/patch

src/app/shared/cookies/browser-orejime.service.ts#L417

Added line #L417 was not covered by tests
}
}

/**
Expand Down
Loading