Skip to content

Commit

Permalink
Merge branch 'main' into refactor/signature-controller-remove-message…
Browse files Browse the repository at this point in the history
…-managers
  • Loading branch information
matthewwalsh0 authored Oct 15, 2024
2 parents 8e2b937 + 3f1d4c1 commit 5fcf6d0
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<INotification[]> {
public async fetchAndUpdateMetamaskNotifications(
previewToken?: string,
): Promise<INotification[]> {
try {
this.#setIsFetchingMetamaskNotifications(true);

Expand All @@ -1065,6 +1068,7 @@ export default class NotificationServicesController extends BaseController<
.isFeatureAnnouncementsEnabled
? await FeatureNotifications.getFeatureAnnouncementNotifications(
this.#featureAnnouncementEnv,
previewToken,
).catch(() => [])
: [];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -766,7 +766,7 @@ export function createMockNotificationNotionalLoanExpiration(): OnChainRawNotifi
chainId: 1,
loans: [
{
amount: '100',
amount: '1.1234',
symbol: 'ETH',
maturityDateIso: '2024-01-01T00:00:00Z',
},
Expand Down Expand Up @@ -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',
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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`,
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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<FeatureAnnouncementRawNotification[]> => {
const url = getFeatureAnnouncementUrl(env);
const url = getFeatureAnnouncementUrl(env, previewToken);

const data = await fetch(url)
.then((r) => r.json())
Expand Down Expand Up @@ -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<INotification[]> {
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),
);
Expand Down

0 comments on commit 5fcf6d0

Please sign in to comment.