Skip to content

Commit

Permalink
refactor valid ticker validation checks
Browse files Browse the repository at this point in the history
  • Loading branch information
Daven-L committed Nov 24, 2023
1 parent 2f3b527 commit 7b530c8
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 9 deletions.
15 changes: 8 additions & 7 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,35 @@
import { isMarketOpen, logError } from './utils';
import { isMarketOpen, isValidMarketSymbol, logError } from './utils';
import dotenv from 'dotenv';
import { invokeWebhook } from './invoke_webhook';
import { MarketSymbol } from './types';

dotenv.config();

const WEBHOOK_URL = process.env.WEBHOOK_URL;
const TICKER: unknown = process.argv[2];
const validTickers: MarketSymbol[] = ['usd-jpy', 'spy', 'spy-futures'];
const TICKER: string | undefined = process.argv[2];

if (!WEBHOOK_URL) {
logError(new Error(`invalid webhook URL: ${WEBHOOK_URL}`));
process.exit(1);
}
// If TICKER doesn't exist or it's not part of the valid ticker list, throw usage guide
if (!(typeof TICKER === 'string' && validTickers.includes(TICKER))) {
if (!isValidMarketSymbol(TICKER)) {
logError(
new Error(
`Usage: npx ts-node src/app.js usd-jpy|spy|spy-futures - received ${TICKER}`
)
);
}
// TICKER is now guaranteed to be usd-jpy|spy|spy-futures
const validatedTicker = TICKER as MarketSymbol;

const date = new Date();
const day = date.getDate();
const month = date.getMonth() + 1;

const marketStatus = isMarketOpen(new Date()) ? 'Market Open' : 'Market Closed';

const metadata: Record<
const symbolWebhookMetadata: Record<
MarketSymbol,
{ title: string; description: string; filename: string }
> = {
Expand All @@ -49,7 +50,7 @@ const metadata: Record<
},
};

const marketSymbolMetaData = metadata[TICKER as MarketSymbol];
const marketSymbolMetaData = symbolWebhookMetadata[validatedTicker];

console.log('Invoking webhook for financial instrument:', TICKER);
console.log('Invoking webhook for financial instrument:', validatedTicker);
invokeWebhook(WEBHOOK_URL, marketSymbolMetaData);
7 changes: 6 additions & 1 deletion src/invoke_webhook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@ import FormData from 'form-data';
// if we do not explicitly import FormData from "form-data"

import axios from 'axios';
import { MarketMetadata } from './types';
import { isMarketOpen } from './utils';

type MarketMetadata = {
title: string;
description: string;
filename: string;
};

export const invokeWebhook = async (
URL: string,
{ title, description, filename }: MarketMetadata
Expand Down
2 changes: 1 addition & 1 deletion src/types.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
type MarketSymbol = 'usd-jpy' | 'spy' | 'spy-futures';
export type MarketSymbol = 'usd-jpy' | 'spy' | 'spy-futures';
5 changes: 5 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { MarketSymbol } from 'types';

export const isMarketOpen = (time: Date) =>
time.getHours() < 5 ||
(time.getHours() >= 22 && time.getMinutes() >= 30) ||
Expand All @@ -6,3 +8,6 @@ export const isMarketOpen = (time: Date) =>
export const logError = (error: Error) => {
console.error(error);
};

export const isValidMarketSymbol = (symbol: string): symbol is MarketSymbol =>
['usd-jpy', 'spy', 'spy-futures'].includes(symbol);

0 comments on commit 7b530c8

Please sign in to comment.