Skip to content

Commit

Permalink
Make ticker URLs stage-aware
Browse files Browse the repository at this point in the history
I had trouble deploying this to CODE, because it was requesting the
ticker data from PROD, which doesn’t exist yet. This change uses the
code URL if the stage is CODE or DEV, and the PROD URL
otherwise (including for unrecognised stages).
  • Loading branch information
emdash-ie authored and Emily Bourke committed Oct 13, 2023
1 parent e0f063f commit 26ab393
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
25 changes: 17 additions & 8 deletions packages/server/src/lib/fetchTickerData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,17 @@ import { Response } from 'node-fetch';
import fetch from 'node-fetch';
import { buildReloader, ValueProvider } from '../utils/valueReloader';
import { logError } from '../utils/logging';
import { TickerName } from '@sdc/shared/dist/types';
import { TickerName, Stage } from '@sdc/shared/dist/types';

const tickerUrl = (name: TickerName): string =>
`https://support.theguardian.com/ticker/${name}.json`;
const tickerUrl = (stage: Stage, name: TickerName): string => {
switch (stage) {
case 'PROD':
return `https://support.theguardian.com/ticker/${name}.json`;
case 'CODE':
case 'DEV':
return `https://support.code.dev-theguardian.com/ticker/${name}.json`;
}
};

const checkForErrors = (response: Response): Promise<Response> => {
if (!response.ok) {
Expand All @@ -32,8 +39,10 @@ const parse = (json: any): Promise<TickerData> => {
}
};

const getTickerDataForTickerTypeFetcher = (name: TickerName) => (): Promise<TickerData> => {
return fetch(tickerUrl(name), {
const getTickerDataForTickerTypeFetcher = (stage: Stage, name: TickerName) => (): Promise<
TickerData
> => {
return fetch(tickerUrl(stage, name), {
timeout: 1000 * 20,
})
.then((response) => checkForErrors(response))
Expand Down Expand Up @@ -75,10 +84,10 @@ export class TickerDataProvider {
}
}

export const buildTickerDataReloader = async (): Promise<TickerDataProvider> => {
export const buildTickerDataReloader = async (stage: Stage): Promise<TickerDataProvider> => {
const reloaders: TickerDataProviders = await Promise.all([
buildReloader(getTickerDataForTickerTypeFetcher('US'), 60),
buildReloader(getTickerDataForTickerTypeFetcher('AU'), 60),
buildReloader(getTickerDataForTickerTypeFetcher(stage, 'US'), 60),
buildReloader(getTickerDataForTickerTypeFetcher(stage, 'AU'), 60),
]).then(([US, AU]) => ({
US,
AU,
Expand Down
5 changes: 4 additions & 1 deletion packages/server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ const buildApp = async (): Promise<Express> => {
app.use(loggingMiddleware);
app.use(bodyParser.urlencoded({ extended: true }));

const stage =
process.env.stage === 'CODE' ? 'CODE' : process.env.stage === 'DEV' ? 'DEV' : 'PROD';

// Initialise dependencies
const [
channelSwitches,
Expand All @@ -71,7 +74,7 @@ const buildApp = async (): Promise<Express> => {
buildEpicLiveblogTestsReloader(),
buildAmpEpicTestsReloader(),
buildChoiceCardAmountsReloader(),
buildTickerDataReloader(),
buildTickerDataReloader(stage),
buildProductPricesReloader(),
buildBannerTestsReloader(),
buildBannerDeployTimesReloader(),
Expand Down

0 comments on commit 26ab393

Please sign in to comment.