Merge pull request #4225 from 4Science/task/main/DURACOM-353

[DURACOM-353] fix orejime callbacks
This commit is contained in:
Tim Donohue
2025-04-28 11:22:36 -05:00
committed by GitHub
2 changed files with 58 additions and 3 deletions

View File

@@ -433,4 +433,31 @@ describe('BrowserOrejimeService', () => {
expect(service.orejimeConfig.apps).not.toContain(jasmine.objectContaining({ name: googleAnalytics })); 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);
});
});
}); });

View File

@@ -192,12 +192,39 @@ export class BrowserOrejimeService extends OrejimeService {
this.translateConfiguration(); this.translateConfiguration();
this.orejimeConfig.apps = this.filterConfigApps(appsToHide); this.orejimeConfig.apps = this.filterConfigApps(appsToHide);
this.lazyOrejime.then(({ init }) => {
this.applyUpdateSettingsCallbackToApps(user);
void this.lazyOrejime.then(({ init }) => {
this.orejimeInstance = init(this.orejimeConfig); 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 * Return saved preferences stored in the orejime cookie
*/ */
@@ -220,7 +247,6 @@ export class BrowserOrejimeService extends OrejimeService {
* @param user The authenticated user * @param user The authenticated user
*/ */
private initializeUser(user: EPerson) { private initializeUser(user: EPerson) {
this.orejimeConfig.callback = debounce((consent, app) => this.updateSettingsForUsers(user), updateDebounce);
this.orejimeConfig.cookieName = this.getStorageName(user.uuid); this.orejimeConfig.cookieName = this.getStorageName(user.uuid);
const anonCookie = this.cookieService.get(ANONYMOUS_STORAGE_NAME_OREJIME); const anonCookie = this.cookieService.get(ANONYMOUS_STORAGE_NAME_OREJIME);
@@ -387,8 +413,10 @@ export class BrowserOrejimeService extends OrejimeService {
* @param user * @param user
*/ */
updateSettingsForUsers(user: EPerson) { updateSettingsForUsers(user: EPerson) {
if (user) {
this.setSettingsForUser(user, this.cookieService.get(this.getStorageName(user.uuid))); this.setSettingsForUser(user, this.cookieService.get(this.getStorageName(user.uuid)));
} }
}
/** /**
* Create the storage name for orejime cookies based on the user's identifier * Create the storage name for orejime cookies based on the user's identifier