Skip to content

Commit 9e2048a

Browse files
VIA-629 SB Allow override of current datetime via a "x-e2e-datetime" header.
1 parent df86ee6 commit 9e2048a

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

src/services/content-api/parsers/custom/covid-19.test.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ import { Campaigns } from "@src/utils/campaigns/types";
55
import config from "@src/utils/config";
66
import { ConfigMock, configBuilder } from "@test-data/config/builders";
77
import { genericVaccineContentAPIResponse } from "@test-data/content-api/data";
8+
import { headers } from "next/headers";
89

910
jest.mock("sanitize-data", () => ({ sanitize: jest.fn() }));
1011
jest.mock("@src/services/nbs/nbs-service", () => ({ buildNbsUrl: jest.fn() }));
12+
jest.mock("next/headers");
1113

1214
describe("buildFilteredContentForCovid19Vaccine", () => {
1315
const mockedConfig = config as ConfigMock;
@@ -28,6 +30,11 @@ describe("buildFilteredContentForCovid19Vaccine", () => {
2830
Object.assign(mockedConfig, defaultConfig);
2931

3032
(buildNbsUrl as jest.Mock).mockResolvedValue(new URL("https://test-nbs-url.example.com/sausages"));
33+
34+
const mockHeaders = {
35+
get: jest.fn(),
36+
};
37+
(headers as jest.Mock).mockResolvedValue(mockHeaders);
3138
});
3239

3340
jest.useFakeTimers();
@@ -112,4 +119,36 @@ describe("buildFilteredContentForCovid19Vaccine", () => {
112119
expect(pageCopy).toEqual(expect.objectContaining(expected));
113120
expect(pageCopy.callout).toBeUndefined();
114121
});
122+
123+
describe("with x-e2e-datetime header set", () => {
124+
it("should return a callout but no actions when no campaign is active", async () => {
125+
const mockHeaders = {
126+
get: jest.fn((key: string) => {
127+
if (key === "x-e2e-datetime") return "2025-10-01T12:00:00Z";
128+
return null;
129+
}),
130+
};
131+
(headers as jest.Mock).mockResolvedValue(mockHeaders);
132+
133+
const pageCopy = await buildFilteredContentForCovid19Vaccine(JSON.stringify(genericVaccineContentAPIResponse));
134+
135+
expect(pageCopy.callout).not.toBeUndefined();
136+
expect(pageCopy.actions).toHaveLength(0);
137+
});
138+
139+
it("should return actions but no callout when no campaign is active", async () => {
140+
const mockHeaders = {
141+
get: jest.fn((key: string) => {
142+
if (key === "x-e2e-datetime") return "2025-12-01T12:00:00Z";
143+
return null;
144+
}),
145+
};
146+
(headers as jest.Mock).mockResolvedValue(mockHeaders);
147+
148+
const pageCopy = await buildFilteredContentForCovid19Vaccine(JSON.stringify(genericVaccineContentAPIResponse));
149+
150+
expect(pageCopy.callout).toBeUndefined();
151+
expect(pageCopy.actions).not.toHaveLength(0);
152+
});
153+
});
115154
});

src/services/content-api/parsers/custom/covid-19.tsx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,22 @@ import {
1111
} from "@src/services/eligibility-api/types";
1212
import { buildNbsUrl } from "@src/services/nbs/nbs-service";
1313
import config from "@src/utils/config";
14+
import { UtcDateTimeFromStringSchema } from "@src/utils/date";
1415
import { logger } from "@src/utils/logger";
16+
import { headers } from "next/headers";
1517
import { Logger } from "pino";
1618

17-
const log: Logger = logger.child({ module: "covid-19" });
19+
const log: Logger = logger.child({ module: "content-api-parsers-custom-covid-19" });
1820

1921
export const buildFilteredContentForCovid19Vaccine = async (apiContent: string): Promise<VaccinePageContent> => {
2022
const campaigns = await config.CAMPAIGNS;
23+
const now = await _getNow();
2124

2225
const standardFilteredContent = await buildFilteredContentForStandardVaccine(apiContent);
2326

2427
let callout: HeadingWithTypedContent | undefined;
2528
const actions: Action[] = [];
26-
if (campaigns.isActive(VaccineType.COVID_19)) {
29+
if (campaigns.isActive(VaccineType.COVID_19, now)) {
2730
log.debug({ context: { campaigns, vaccineType: VaccineType.COVID_19 } }, "Campaign active");
2831
callout = undefined;
2932
actions.push(...(await _buildActions()));
@@ -51,6 +54,13 @@ export const buildFilteredContentForCovid19Vaccine = async (apiContent: string):
5154
return { ...standardFilteredContent, callout, recommendation, actions };
5255
};
5356

57+
async function _getNow() {
58+
const headersList = await headers();
59+
const now = UtcDateTimeFromStringSchema.safeParse(headersList.get("x-e2e-datetime")).data ?? new Date();
60+
log.debug({ context: { headersList, now: now.toDateString() } }, "headers");
61+
return now;
62+
}
63+
5464
async function _buildActions(): Promise<Action[]> {
5565
const nbsURl = (await buildNbsUrl(VaccineType.COVID_19)) as ButtonUrl;
5666

0 commit comments

Comments
 (0)