diff --git a/packages/notification-services-controller/src/NotificationServicesController/NotificationServicesController.ts b/packages/notification-services-controller/src/NotificationServicesController/NotificationServicesController.ts index 2ffc48d63c..f8f64613b7 100644 --- a/packages/notification-services-controller/src/NotificationServicesController/NotificationServicesController.ts +++ b/packages/notification-services-controller/src/NotificationServicesController/NotificationServicesController.ts @@ -1054,9 +1054,12 @@ export default class NotificationServicesController extends BaseController< * * **Action** - When a user views the notification list page/dropdown * + * @param previewToken - the preview token to use if needed * @throws {Error} Throws an error if unauthenticated or from other operations. */ - public async fetchAndUpdateMetamaskNotifications(): Promise { + public async fetchAndUpdateMetamaskNotifications( + previewToken?: string, + ): Promise { try { this.#setIsFetchingMetamaskNotifications(true); @@ -1065,6 +1068,7 @@ export default class NotificationServicesController extends BaseController< .isFeatureAnnouncementsEnabled ? await FeatureNotifications.getFeatureAnnouncementNotifications( this.#featureAnnouncementEnv, + previewToken, ).catch(() => []) : []; diff --git a/packages/notification-services-controller/src/NotificationServicesController/__fixtures__/mock-raw-notifications.ts b/packages/notification-services-controller/src/NotificationServicesController/__fixtures__/mock-raw-notifications.ts index 57591334f7..7d41fa3e7a 100644 --- a/packages/notification-services-controller/src/NotificationServicesController/__fixtures__/mock-raw-notifications.ts +++ b/packages/notification-services-controller/src/NotificationServicesController/__fixtures__/mock-raw-notifications.ts @@ -704,7 +704,7 @@ export function createMockNotificationEnsExpiration(): OnChainRawNotification { data: { kind: 'ens_expiration', chainId: 1, - reverseEnsName: 'example.eth', + reverseEnsName: 'vitalik.eth', expirationDateIso: '2024-01-01T00:00:00Z', reminderDelayInSeconds: 86400, }, @@ -733,11 +733,11 @@ export function createMockNotificationLidoStakingRewards(): OnChainRawNotificati data: { kind: 'lido_staking_rewards', chainId: 1, - currentStethBalance: '100', - currentEthValue: '10000.00', - estimatedTotalRewardInPeriod: '10000.00', - daysSinceLastNotification: 1, - notificationIntervalDays: 1, + currentStethBalance: '10', + currentEthValue: '10.5', + estimatedTotalRewardInPeriod: '0.5', + daysSinceLastNotification: 30, + notificationIntervalDays: 30, }, id: 'd73df14d-ce73-4f38-bad3-ab028154042l', trigger_id: 'd73df14d-ce73-4f38-bad3-ab028154042l', @@ -766,7 +766,7 @@ export function createMockNotificationNotionalLoanExpiration(): OnChainRawNotifi chainId: 1, loans: [ { - amount: '100', + amount: '1.1234', symbol: 'ETH', maturityDateIso: '2024-01-01T00:00:00Z', }, @@ -798,11 +798,11 @@ export function createMockNotificationRocketpoolStakingRewards(): OnChainRawNoti data: { kind: 'rocketpool_staking_rewards', chainId: 1, - currentRethBalance: '100', - currentEthValue: '10000.00', - estimatedTotalRewardInPeriod: '10000.00', - daysSinceLastNotification: 1, - notificationIntervalDays: 1, + currentRethBalance: '10', + currentEthValue: '10.5', + estimatedTotalRewardInPeriod: '0.5', + daysSinceLastNotification: 30, + notificationIntervalDays: 30, }, id: 'd73df14d-ce73-4f38-bad3-ab028154042r', trigger_id: 'd73df14d-ce73-4f38-bad3-ab028154042r', diff --git a/packages/notification-services-controller/src/NotificationServicesController/services/feature-announcements.test.ts b/packages/notification-services-controller/src/NotificationServicesController/services/feature-announcements.test.ts index 55b1b85183..b866545205 100644 --- a/packages/notification-services-controller/src/NotificationServicesController/services/feature-announcements.test.ts +++ b/packages/notification-services-controller/src/NotificationServicesController/services/feature-announcements.test.ts @@ -1,7 +1,10 @@ import { createMockFeatureAnnouncementAPIResult } from '../__fixtures__/mock-feature-announcements'; import { mockFetchFeatureAnnouncementNotifications } from '../__fixtures__/mockServices'; import { TRIGGER_TYPES } from '../constants/notification-schema'; -import { getFeatureAnnouncementNotifications } from './feature-announcements'; +import { + getFeatureAnnouncementNotifications, + getFeatureAnnouncementUrl, +} from './feature-announcements'; // Mocked type for testing, allows overwriting TS to test erroneous values // eslint-disable-next-line @typescript-eslint/no-explicit-any @@ -95,3 +98,22 @@ describe('Feature Announcement Notifications', () => { expect(resultNotification.data).toBeDefined(); }); }); + +describe('getFeatureAnnouncementUrl', () => { + it('should construct the correct URL for the default domain', () => { + const url = getFeatureAnnouncementUrl(featureAnnouncementsEnv); + expect(url).toBe( + `https://cdn.contentful.com/spaces/:space_id/environments/master/entries?access_token=:access_token&content_type=productAnnouncement&include=10&fields.clients=extension`, + ); + }); + + it('should construct the correct URL for the preview domain', () => { + const url = getFeatureAnnouncementUrl( + featureAnnouncementsEnv, + ':preview_token', + ); + expect(url).toBe( + `https://preview.contentful.com/spaces/:space_id/environments/master/entries?access_token=:preview_token&content_type=productAnnouncement&include=10&fields.clients=extension`, + ); + }); +}); diff --git a/packages/notification-services-controller/src/NotificationServicesController/services/feature-announcements.ts b/packages/notification-services-controller/src/NotificationServicesController/services/feature-announcements.ts index fdfa40a824..8ed2963e7c 100644 --- a/packages/notification-services-controller/src/NotificationServicesController/services/feature-announcements.ts +++ b/packages/notification-services-controller/src/NotificationServicesController/services/feature-announcements.ts @@ -19,7 +19,9 @@ import type { INotification } from '../types/notification/notification'; const DEFAULT_SPACE_ID = ':space_id'; const DEFAULT_ACCESS_TOKEN = ':access_token'; const DEFAULT_CLIENT_ID = ':client_id'; -export const FEATURE_ANNOUNCEMENT_API = `https://cdn.contentful.com/spaces/${DEFAULT_SPACE_ID}/environments/master/entries`; +const DEFAULT_DOMAIN = 'cdn.contentful.com'; +const PREVIEW_DOMAIN = 'preview.contentful.com'; +export const FEATURE_ANNOUNCEMENT_API = `https://${DEFAULT_DOMAIN}/spaces/${DEFAULT_SPACE_ID}/environments/master/entries`; export const FEATURE_ANNOUNCEMENT_URL = `${FEATURE_ANNOUNCEMENT_API}?access_token=${DEFAULT_ACCESS_TOKEN}&content_type=productAnnouncement&include=10&fields.clients=${DEFAULT_CLIENT_ID}`; type Env = { @@ -41,15 +43,19 @@ export type ContentfulResult = { items?: TypeFeatureAnnouncement[]; }; -const getFeatureAnnouncementUrl = (env: Env) => - FEATURE_ANNOUNCEMENT_URL.replace(DEFAULT_SPACE_ID, env.spaceId) - .replace(DEFAULT_ACCESS_TOKEN, env.accessToken) - .replace(DEFAULT_CLIENT_ID, env.platform); +export const getFeatureAnnouncementUrl = (env: Env, previewToken?: string) => { + const domain = previewToken ? PREVIEW_DOMAIN : DEFAULT_DOMAIN; + return FEATURE_ANNOUNCEMENT_URL.replace(DEFAULT_SPACE_ID, env.spaceId) + .replace(DEFAULT_ACCESS_TOKEN, previewToken || env.accessToken) + .replace(DEFAULT_CLIENT_ID, env.platform) + .replace(DEFAULT_DOMAIN, domain); +}; const fetchFeatureAnnouncementNotifications = async ( env: Env, + previewToken?: string, ): Promise => { - const url = getFeatureAnnouncementUrl(env); + const url = getFeatureAnnouncementUrl(env, previewToken); const data = await fetch(url) .then((r) => r.json()) @@ -144,13 +150,18 @@ const fetchFeatureAnnouncementNotifications = async ( /** * Gets Feature Announcement from our services * @param env - environment for feature announcements + * @param previewToken - the preview token to use if needed * @returns Raw Feature Announcements */ export async function getFeatureAnnouncementNotifications( env: Env, + previewToken?: string, ): Promise { if (env?.accessToken && env?.spaceId && env?.platform) { - const rawNotifications = await fetchFeatureAnnouncementNotifications(env); + const rawNotifications = await fetchFeatureAnnouncementNotifications( + env, + previewToken, + ); const notifications = rawNotifications.map((notification) => processFeatureAnnouncement(notification), );