Skip to content

Commit

Permalink
Remove lastOneOffContributionDate
Browse files Browse the repository at this point in the history
  • Loading branch information
rupertbates committed Dec 12, 2024
1 parent 4c77c6f commit 35efa61
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 96 deletions.
3 changes: 0 additions & 3 deletions dotcom-rendering/src/components/LiveBlogEpic.importable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { createPortal } from 'react-dom';
import { submitComponentEvent } from '../client/ophan/ophan';
import { useArticleCounts } from '../lib/articleCount';
import {
getLastOneOffContributionTimestamp,
shouldHideSupportMessaging,
useHasOptedOutOfArticleCount,
} from '../lib/contributions';
Expand Down Expand Up @@ -124,8 +123,6 @@ const usePayload = ({
isPaidContent,
tags,
showSupportMessaging: !hideSupportMessagingForUser,
lastOneOffContributionDate:
getLastOneOffContributionTimestamp() ?? undefined,
mvtId,
countryCode,
epicViewLog: getEpicViewLog(storage.local),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { css } from '@emotion/react';
import { cmp } from '@guardian/libs';
import { getCookie, startPerformanceMeasure, storage } from '@guardian/libs';
import {
cmp,
getCookie,
startPerformanceMeasure,
storage,
} from '@guardian/libs';
import type { ComponentEvent } from '@guardian/ophan-tracker-js';
import { getEpic, getEpicViewLog } from '@guardian/support-dotcom-components';
import type {
Expand All @@ -12,7 +16,6 @@ import type {
import { useEffect, useState } from 'react';
import { submitComponentEvent } from '../../client/ophan/ophan';
import {
getLastOneOffContributionTimestamp,
hasCmpConsentForBrowserId,
hasCmpConsentForWeeklyArticleCount,
hasOptedOutOfArticleCount,
Expand Down Expand Up @@ -69,7 +72,6 @@ const buildPayload = async (
isPaidContent: data.isPaidContent,
tags: data.tags,
showSupportMessaging: !data.hideSupportMessagingForUser,
lastOneOffContributionDate: getLastOneOffContributionTimestamp(),
epicViewLog: getEpicViewLog(storage.local),
weeklyArticleHistory: await data.asyncArticleCount,

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import { useEffect, useState } from 'react';
import { submitComponentEvent } from '../../client/ophan/ophan';
import type { ArticleCounts } from '../../lib/articleCount';
import {
getLastOneOffContributionDate,
getPurchaseInfo,
hasCmpConsentForBrowserId,
hasOptedOutOfArticleCount,
Expand Down Expand Up @@ -165,7 +164,6 @@ const buildPayload = async ({
: undefined,
purchaseInfo: getPurchaseInfo(),
isSignedIn,
lastOneOffContributionDate: getLastOneOffContributionDate(),
hasConsented: userConsent,
abandonedBasket: parseAbandonedBasket(
getCookie({ name: 'GU_CO_INCOMPLETE', shouldMemoize: true }),
Expand Down
2 changes: 0 additions & 2 deletions dotcom-rendering/src/components/TopBarSupport.importable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import type {
import { useEffect, useState } from 'react';
import { submitComponentEvent } from '../client/ophan/ophan';
import {
getLastOneOffContributionDate,
getPurchaseInfo,
shouldHideSupportMessaging,
} from '../lib/contributions';
Expand Down Expand Up @@ -81,7 +80,6 @@ const ReaderRevenueLinksRemote = ({
mvtId: Number(
getCookie({ name: 'GU_mvt_id', shouldMemoize: true }),
),
lastOneOffContributionDate: getLastOneOffContributionDate(),
purchaseInfo: getPurchaseInfo(),
isSignedIn,
},
Expand Down
58 changes: 12 additions & 46 deletions dotcom-rendering/src/lib/contributions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
HIDE_SUPPORT_MESSAGING_COOKIE,
isRecentOneOffContributor,
NO_RR_BANNER_KEY,
ONE_OFF_CONTRIBUTION_DATE_COOKIE,
recentlyClosedBanner,
setLocalNoBannerCachePeriod,
SUPPORT_ONE_OFF_CONTRIBUTION_COOKIE,
Expand All @@ -22,23 +21,10 @@ const clearAllCookies = () => {
}
};

describe('getLastOneOffContributionDate', () => {
describe('getLastOneOffContributionTimestamp', () => {
beforeEach(clearAllCookies);

it('returns date from attributes cookie if only cookie found', () => {
const somePastDate = '2020-01-28';
setCookie({
name: ONE_OFF_CONTRIBUTION_DATE_COOKIE,
value: somePastDate,
});
const lastOneOffContributionDate = getLastOneOffContributionTimestamp();

// Our function will convert YYYY-MM-DD into a timestamp
const somePastDateToTimestamp = Date.parse(somePastDate);
expect(lastOneOffContributionDate).toBe(somePastDateToTimestamp);
});

it('returns a support cookie date if only cookie found', () => {
it('returns a support cookie date if found', () => {
const somePastDate = 1582567969093;
setCookie({
name: SUPPORT_ONE_OFF_CONTRIBUTION_COOKIE,
Expand All @@ -48,31 +34,10 @@ describe('getLastOneOffContributionDate', () => {
expect(lastOneOffContributionDate).toBe(somePastDate);
});

it('returns the most recent date if both cookies present', () => {
const muchLongerAgo = '2020-01-28';
setCookie({
name: ONE_OFF_CONTRIBUTION_DATE_COOKIE,
value: muchLongerAgo,
});

const notSoLongAgo = 1582567969093;
it('returns undefined if the date cannot be parsed correctly', () => {
setCookie({
name: SUPPORT_ONE_OFF_CONTRIBUTION_COOKIE,
value: String(notSoLongAgo),
});

const lastOneOffContributionDate = getLastOneOffContributionTimestamp();
expect(lastOneOffContributionDate).toBe(notSoLongAgo);
});

it('returns an empty string if no dates can be parsed correctly', () => {
setCookie({
name: ONE_OFF_CONTRIBUTION_DATE_COOKIE,
value: 'CANT_TOUCH_THIS',
});
setCookie({
name: SUPPORT_ONE_OFF_CONTRIBUTION_COOKIE,
value: 'OR_THIS',
value: 'NOT_A_DATE',
});

const lastOneOffContributionDate = getLastOneOffContributionTimestamp();
Expand All @@ -97,27 +62,28 @@ describe('isRecentOneOffContributor', () => {

it('returns true if there are 5 days between the last contribution date and now', () => {
setCookie({
name: ONE_OFF_CONTRIBUTION_DATE_COOKIE,
value: '2018-08-01',
name: SUPPORT_ONE_OFF_CONTRIBUTION_COOKIE,
value: Date.parse('2018-08-01').toString(),
});

MockDate.set(Date.parse('2018-08-07T10:50:34'));
expect(isRecentOneOffContributor()).toBe(true);
});

it('returns true if there are 0 days between the last contribution date and now', () => {
const theDate = Date.parse('2018-08-01T13:00:30');
setCookie({
name: ONE_OFF_CONTRIBUTION_DATE_COOKIE,
value: '2018-08-01',
name: SUPPORT_ONE_OFF_CONTRIBUTION_COOKIE,
value: theDate.toString(),
});
MockDate.set(Date.parse('2018-08-01T13:00:30'));
MockDate.set(theDate);
expect(isRecentOneOffContributor()).toBe(true);
});

it('returns false if the one-off contribution was more than 3 months ago', () => {
setCookie({
name: ONE_OFF_CONTRIBUTION_DATE_COOKIE,
value: '2018-08-01',
name: SUPPORT_ONE_OFF_CONTRIBUTION_COOKIE,
value: Date.parse('2018-08-01').toString(),
});
MockDate.set(Date.parse('2019-08-01T13:00:30'));
expect(isRecentOneOffContributor()).toBe(false);
Expand Down
45 changes: 6 additions & 39 deletions dotcom-rendering/src/lib/contributions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,13 @@ import type { DCRTagPageType } from '../types/tagPage';
// User Attributes API cookies (created on sign-in)
export const HIDE_SUPPORT_MESSAGING_COOKIE = 'gu_hide_support_messaging';
export const RECURRING_CONTRIBUTOR_COOKIE = 'gu_recurring_contributor';
export const ONE_OFF_CONTRIBUTION_DATE_COOKIE = 'gu_one_off_contribution_date';
export const OPT_OUT_OF_ARTICLE_COUNT_COOKIE = 'gu_article_count_opt_out';

// Support Frontend cookies (created when a contribution is made)
export const SUPPORT_RECURRING_CONTRIBUTOR_MONTHLY_COOKIE =
'gu.contributions.recurring.contrib-timestamp.Monthly';
'gu.contributions.recurring.contrib-timestamp.Monthly'; // TODO: delete this, no longer needed
export const SUPPORT_RECURRING_CONTRIBUTOR_ANNUAL_COOKIE =
'gu.contributions.recurring.contrib-timestamp.Annual';
'gu.contributions.recurring.contrib-timestamp.Annual'; // TODO: delete this, no longer needed
export const SUPPORT_ONE_OFF_CONTRIBUTION_COOKIE =
'gu.contributions.contrib-timestamp';

Expand Down Expand Up @@ -61,58 +60,26 @@ export const hasSupporterCookie = (
}
};

// looks at attribute and support cookies
// ONE_OFF_CONTRIBUTION_DATE_COOKIE (attributes cookie, when loggin in)
// SUPPORT_ONE_OFF_CONTRIBUTION_COOKIE (support cookie, when making one-off contribution)
// Get the date of the latest one-off contribution by looking at the two relevant cookies
// and returning a Unix epoch string of the latest date found.
// looks at the SUPPORT_ONE_OFF_CONTRIBUTION_COOKIE (set by support-frontend when making one-off contribution)
// and returns a Unix epoch int of the date if it exists.
export const getLastOneOffContributionTimestamp = (): number | undefined => {
// Attributes cookie - expects YYYY-MM-DD
const contributionDateFromAttributes = getCookie({
name: ONE_OFF_CONTRIBUTION_DATE_COOKIE,
});

// Support cookies - expects Unix epoch
const contributionDateFromSupport = getCookie({
name: SUPPORT_ONE_OFF_CONTRIBUTION_COOKIE,
});

if (!contributionDateFromAttributes && !contributionDateFromSupport) {
if (!contributionDateFromSupport) {
return undefined;
}

// Parse dates into common format so they can be compared
const parsedDateFromAttributes = contributionDateFromAttributes
? Date.parse(contributionDateFromAttributes)
: 0;
// Parse dates into common a number
const parsedDateFromSupport = contributionDateFromSupport
? parseInt(contributionDateFromSupport, 10)
: 0;

// Return most recent date
// Condition only passed if 'parsedDateFromAttributes' is NOT NaN
if (parsedDateFromAttributes > parsedDateFromSupport) {
return parsedDateFromAttributes;
}

return parsedDateFromSupport || undefined; // This guards against 'parsedDateFromSupport' being NaN
};

export const getLastOneOffContributionDate = (): string | undefined => {
const timestamp = getLastOneOffContributionTimestamp();

if (isUndefined(timestamp)) {
return undefined;
}

const date = new Date(timestamp);
const year = date.getFullYear();
const month = (date.getMonth() + 1).toString().padStart(2, '0');
const day = date.getDate().toString().padStart(2, '0');

return `${year}-${month}-${day}`;
};

const dateDiffDays = (from: number, to: number): number => {
const oneDayMs = 1000 * 60 * 60 * 24;
const diffMs = to - from;
Expand Down

0 comments on commit 35efa61

Please sign in to comment.