Skip to content

Commit 9aa505d

Browse files
authored
[ENG-9758] fix(notifications): change settings page to default to correct notif (#761)
- Ticket: https://openscience.atlassian.net/browse/ENG-9758 - Feature flag: n/a ## Purpose Make the notification preferences default and change to correct values. Corrosponses with this PR: CenterForOpenScience/osf.io#11441 ## Summary of Changes Simple logic changes to make dropdowns functional
1 parent 6b78a4f commit 9aa505d

File tree

4 files changed

+69
-15
lines changed

4 files changed

+69
-15
lines changed

src/app/features/settings/notifications/constants/notifications-constants.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,14 @@ export const SUBSCRIPTION_EVENTS: SubscriptionEventModel[] = [
1212
labelKey: 'settings.notifications.notificationPreferences.items.preprints',
1313
},
1414
];
15+
16+
export const FORM_EVENT_TO_API_EVENT: Record<string, string> = {
17+
new_pending_submissions: 'new_pending_submissions',
18+
files_updated: 'files_updated',
19+
global_file_updated: 'global_file_updated',
20+
};
21+
22+
export const API_EVENT_TO_FORM_EVENT: Record<string, string> = {
23+
new_pending_submissions: 'new_pending_submissions',
24+
files_updated: 'global_file_updated',
25+
};

src/app/features/settings/notifications/notifications.component.spec.ts

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,19 @@ describe('NotificationsComponent', () => {
4141
subscribeOsfHelpEmail: false,
4242
};
4343

44+
// new_pending_submissions → global_reviews
45+
// files_updated → global_file_updated
4446
const mockNotificationSubscriptions = [
45-
{ id: 'id1', event: SubscriptionEvent.GlobalFileUpdated, frequency: SubscriptionFrequency.Daily },
4647
{
47-
id: 'id2',
48-
event: SubscriptionEvent.GlobalFileUpdated,
48+
id: 'osf_new_pending_submissions',
49+
event: 'new_pending_submissions',
4950
frequency: SubscriptionFrequency.Instant,
5051
},
52+
{
53+
id: 'cuzg4_global_file_updated',
54+
event: 'files_updated',
55+
frequency: SubscriptionFrequency.Daily,
56+
},
5157
];
5258

5359
beforeEach(async () => {
@@ -77,7 +83,7 @@ describe('NotificationsComponent', () => {
7783
return signal(null);
7884
});
7985

80-
MOCK_STORE.dispatch.mockImplementation(() => of());
86+
MOCK_STORE.dispatch.mockReturnValue(of({}));
8187

8288
await TestBed.configureTestingModule({
8389
imports: [
@@ -116,9 +122,9 @@ describe('NotificationsComponent', () => {
116122

117123
return signal(null);
118124
});
119-
component.emailPreferencesFormSubmit();
120125

121-
expect(loaderService.hide).not.toHaveBeenCalled();
126+
component.emailPreferencesFormSubmit();
127+
expect(loaderService.hide).toHaveBeenCalledTimes(1);
122128
});
123129

124130
it('should handle subscription completion correctly', () => {
@@ -136,11 +142,15 @@ describe('NotificationsComponent', () => {
136142
it('should call dispatch only once per subscription change', () => {
137143
const mockDispatch = jest.fn().mockReturnValue(of({}));
138144
MOCK_STORE.dispatch.mockImplementation(mockDispatch);
139-
const event = SubscriptionEvent.GlobalFileUpdated;
140-
const frequency = SubscriptionFrequency.Daily;
141145

142-
component.onSubscriptionChange(event, frequency);
146+
component.onSubscriptionChange(SubscriptionEvent.GlobalFileUpdated, SubscriptionFrequency.Daily);
143147

144148
expect(mockDispatch).toHaveBeenCalledTimes(1);
145149
});
150+
151+
it('should default to API value', () => {
152+
const subs = component.notificationSubscriptionsForm.value;
153+
expect(subs.new_pending_submissions).toBe(SubscriptionFrequency.Instant);
154+
expect(subs.global_file_updated).toBe(SubscriptionFrequency.Daily);
155+
});
146156
});

src/app/features/settings/notifications/notifications.component.ts

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import { ToastService } from '@osf/shared/services/toast.service';
2121
import { AccountSettings } from '../account-settings/models';
2222
import { AccountSettingsSelectors, GetAccountSettings, UpdateAccountSettings } from '../account-settings/store';
2323

24-
import { SUBSCRIPTION_EVENTS } from './constants';
24+
import { API_EVENT_TO_FORM_EVENT, FORM_EVENT_TO_API_EVENT, SUBSCRIPTION_EVENTS } from './constants';
2525
import { EmailPreferencesForm, EmailPreferencesFormControls } from './models';
2626
import {
2727
GetAllGlobalNotificationSubscriptions,
@@ -80,7 +80,9 @@ export class NotificationsComponent implements OnInit {
8080
notificationSubscriptionsForm = this.fb.group(
8181
SUBSCRIPTION_EVENTS.reduce(
8282
(control, { event }) => {
83-
control[event] = this.fb.control<SubscriptionFrequency>(SubscriptionFrequency.Never, { nonNullable: true });
83+
control[event as string] = this.fb.control<SubscriptionFrequency>(SubscriptionFrequency.Never, {
84+
nonNullable: true,
85+
});
8486
return control;
8587
},
8688
{} as Record<string, FormControl<SubscriptionFrequency>>
@@ -128,7 +130,24 @@ export class NotificationsComponent implements OnInit {
128130
onSubscriptionChange(event: SubscriptionEvent, frequency: SubscriptionFrequency) {
129131
const user = this.currentUser();
130132
if (!user) return;
131-
const id = `${user.id}_${event}`;
133+
134+
const eventKey = event as string;
135+
136+
const apiEventName = FORM_EVENT_TO_API_EVENT[eventKey] ?? eventKey;
137+
138+
let id: string | undefined;
139+
140+
if (event === SubscriptionEvent.GlobalReviews) {
141+
const subs = this.notificationSubscriptions();
142+
const match = subs.find((s) => (s.event as string) === 'new_pending_submissions');
143+
if (match) {
144+
id = match.id;
145+
} else {
146+
return;
147+
}
148+
} else {
149+
id = `${user.id}_${apiEventName}`;
150+
}
132151

133152
this.loaderService.show();
134153
this.actions.updateNotificationSubscription({ id, frequency }).subscribe(() => {
@@ -145,10 +164,24 @@ export class NotificationsComponent implements OnInit {
145164
}
146165

147166
private updateNotificationSubscriptionsForm() {
167+
const subs = this.notificationSubscriptions();
168+
if (!subs?.length) {
169+
return;
170+
}
171+
148172
const patch: Record<string, SubscriptionFrequency> = {};
149173

150-
for (const sub of this.notificationSubscriptions()) {
151-
patch[sub.event] = sub.frequency;
174+
for (const sub of subs) {
175+
const apiEvent = sub.event as string | null;
176+
if (!apiEvent) {
177+
continue;
178+
}
179+
180+
const formEventKey = API_EVENT_TO_FORM_EVENT[apiEvent];
181+
182+
if (formEventKey) {
183+
patch[formEventKey] = sub.frequency;
184+
}
152185
}
153186

154187
this.notificationSubscriptionsForm.patchValue(patch);
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export enum SubscriptionEvent {
22
GlobalFileUpdated = 'global_file_updated',
3-
GlobalReviews = 'global_reviews',
3+
GlobalReviews = 'new_pending_submissions',
44
FileUpdated = 'file_updated',
55
}

0 commit comments

Comments
 (0)