-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8bb472c
commit 910bee3
Showing
10 changed files
with
110 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from "./get-price-history" | ||
export * from "./update-price-history" | ||
export * from "./get-price-history"; | ||
export * from "./update-price-history"; | ||
export * from "./notify-price-change"; | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,8 @@ | ||
type GetPriceHistoryArgs = { | ||
currency: string | ||
range?: string | ||
} | ||
currency: string; | ||
range?: string; | ||
}; | ||
|
||
type NotifyPriceChangeArgs = { | ||
range?: PriceRange; | ||
}; | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { defaultBaseCurrency } from "@config"; | ||
import { PriceRange } from "@domain/price"; | ||
import { checkedToCurrency } from "@domain/primitives"; | ||
import { PriceRepository } from "@services/database"; | ||
import { NotificationsService } from "@services/notifications"; | ||
|
||
export const notifyPriceChange = async ({ range }: NotifyPriceChangeArgs) => { | ||
const quote = checkedToCurrency("USD"); | ||
|
||
if (quote instanceof Error) { | ||
return quote; | ||
} | ||
|
||
const rangeToQuery = range || PriceRange.OneDay; | ||
const prices = await PriceRepository().listPrices({ | ||
base: defaultBaseCurrency, | ||
quote, | ||
range: rangeToQuery, | ||
}); | ||
|
||
if (prices instanceof Error) { | ||
return prices; | ||
} | ||
|
||
const notificationsService = NotificationsService(); | ||
|
||
return notificationsService.priceChanged({ | ||
initialPrice: prices[0], | ||
finalPrice: prices[prices.length - 1], | ||
range: rangeToQuery, | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,18 @@ | ||
import { wrapAsyncToRunInSpan } from "@services/tracing" | ||
import { wrapAsyncToRunInSpan } from "@services/tracing"; | ||
|
||
import * as HistoryMod from "./history" | ||
import * as HistoryMod from "./history"; | ||
|
||
const allFunctions = { | ||
History: { ...HistoryMod }, | ||
} | ||
}; | ||
|
||
for (const subModule in allFunctions) { | ||
for (const fn in allFunctions[subModule]) { | ||
allFunctions[subModule][fn] = wrapAsyncToRunInSpan({ | ||
namespace: `app.${subModule.toLowerCase()}`, | ||
fn: allFunctions[subModule][fn], | ||
}) | ||
}); | ||
} | ||
} | ||
|
||
export const { History } = allFunctions | ||
export const { History } = allFunctions; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { ErrorLevel, ServiceError } from "../errors"; | ||
|
||
export class NotificationsServiceError extends ServiceError {} | ||
export class UnknownNotificationServiceError extends NotificationsServiceError { | ||
level = ErrorLevel.Critical; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./errors"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
type NotificationsServiceError = import("./errors").NotificationsServiceError; | ||
|
||
interface INotificationsService { | ||
priceChanged( | ||
args: PriceChangedArgs | ||
): Promise<void | NotificationsServiceError>; | ||
} | ||
|
||
type PriceChangedArgs = { | ||
range: PriceRange; | ||
initialPrice: Tick; | ||
finalPrice: Tick; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,17 @@ | ||
import dotenv from "dotenv" | ||
import dotenv from "dotenv"; | ||
|
||
import { History } from "@app" | ||
import { History } from "@app"; | ||
|
||
import { closeDbConnections } from "@services/database" | ||
import { closeDbConnections } from "@services/database"; | ||
|
||
dotenv.config() | ||
dotenv.config(); | ||
|
||
const startServer = async () => { | ||
await History.updatePriceHistory() | ||
await closeDbConnections() | ||
} | ||
await History.updatePriceHistory(); | ||
await History.notifyPriceChange({}); | ||
await closeDbConnections(); | ||
}; | ||
|
||
if (require.main === module) { | ||
startServer() | ||
startServer(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
export const NotificationsService = (): INotificationsService => { | ||
const priceChanged = async ( | ||
args: PriceChangedArgs | ||
): Promise<void | NotificationsServiceError> => { | ||
const priceChangedEvent = createPriceChangedEvent(args); | ||
|
||
// Send event to notification service | ||
}; | ||
return { | ||
priceChanged, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
const PriceDirection = { | ||
Increase: "Increase", | ||
Decrease: "Decrease", | ||
} as const; | ||
|
||
type PriceDirection = (typeof PriceDirection)[keyof typeof PriceDirection]; | ||
|
||
type PriceChangedEvent = { | ||
currentPriceInUsd: string; | ||
priceChangeDirection: PriceDirection; | ||
priceChangeInBips: string; | ||
timeRange: PriceRange; | ||
timestamp: string; | ||
}; | ||
|
||
const createPriceChangedEvent = ({ | ||
range, | ||
initialPrice, | ||
finalPrice, | ||
}: PriceChangedArgs): PriceChangedEvent => { | ||
throw new Error("Method not implemented."); | ||
}; |