From a8a9a57a90361409e6a6b6c188978f0d6924b177 Mon Sep 17 00:00:00 2001 From: tahmid-saj Date: Sat, 21 Sep 2024 15:13:20 -0400 Subject: [PATCH] migrating savings utils to ts --- .../market-data-graph.component.jsx | 2 +- .../savings-goal-calculator.context.tsx | 2 +- .../savings-goal-calculator.types.ts | 3 +- .../signed-in/savings/savings.context.tsx | 6 +-- .../signed-in/savings/savings.types.ts | 13 +++++++ .../signed-out/savings/savings.types.ts | 8 ++++ .../savings-goal-calculator.action.ts | 4 +- ...ta.requests.js => market-data.requests.ts} | 9 +++-- ...avings.requests.js => savings.requests.ts} | 22 +++++++---- .../calculations/savings.calculations.ts | 38 +++++++++++++++---- src/utils/constants/investments.constants.ts | 2 - src/utils/constants/market-data.constants.js | 22 ----------- src/utils/constants/market-data.constants.ts | 22 +++++++++++ ...{regex.constants.js => regex.constants.ts} | 0 src/utils/constants/savings.constants.js | 22 ----------- src/utils/constants/savings.constants.ts | 22 +++++++++++ ...t-data.errors.js => market-data.errors.ts} | 0 .../{savings.errors.js => savings.errors.ts} | 0 ...{savings.graphql.js => savings.graphql.ts} | 16 ++++---- ...alidation.js => market-data.validation.ts} | 3 +- ...gs.validation.js => savings.validation.ts} | 8 ++-- 21 files changed, 138 insertions(+), 86 deletions(-) rename src/utils/api-requests/{market-data.requests.js => market-data.requests.ts} (81%) rename src/utils/api-requests/{savings.requests.js => savings.requests.ts} (66%) delete mode 100644 src/utils/constants/market-data.constants.js create mode 100644 src/utils/constants/market-data.constants.ts rename src/utils/constants/{regex.constants.js => regex.constants.ts} (100%) delete mode 100644 src/utils/constants/savings.constants.js create mode 100644 src/utils/constants/savings.constants.ts rename src/utils/errors/{market-data.errors.js => market-data.errors.ts} (100%) rename src/utils/errors/{savings.errors.js => savings.errors.ts} (100%) rename src/utils/graphql/{savings.graphql.js => savings.graphql.ts} (78%) rename src/utils/validations/{market-data.validation.js => market-data.validation.ts} (70%) rename src/utils/validations/{savings.validation.js => savings.validation.ts} (84%) diff --git a/src/components/shared/market-view/market-data-graph/market-data-graph.component.jsx b/src/components/shared/market-view/market-data-graph/market-data-graph.component.jsx index 2fe1ebe..5e93bb5 100644 --- a/src/components/shared/market-view/market-data-graph/market-data-graph.component.jsx +++ b/src/components/shared/market-view/market-data-graph/market-data-graph.component.jsx @@ -5,7 +5,7 @@ import ReactApexChart from 'react-apexcharts'; import { MarketDataContext } from "../../../../contexts/shared/market-data/market-data.context"; import { Fragment, useContext } from "react"; -import { MARKET_DATA_INTERVALS } from "../../../../utils/constants/market-data.constants"; +import { MARKET_DATA_INTERVALS } from "../../../../utils/constants/market-data.constants.js"; import { Divider } from "@mui/material"; import { COLOR_CODES, COMMON_SPACING } from "../../../../utils/constants/shared.constants.js"; import SimplePaper from "../../mui/paper/paper.component.jsx"; diff --git a/src/contexts/shared/savings-goal-calculator/savings-goal-calculator.context.tsx b/src/contexts/shared/savings-goal-calculator/savings-goal-calculator.context.tsx index e598501..7b5e4e4 100644 --- a/src/contexts/shared/savings-goal-calculator/savings-goal-calculator.context.tsx +++ b/src/contexts/shared/savings-goal-calculator/savings-goal-calculator.context.tsx @@ -21,7 +21,7 @@ const calculateSavingsGoalHelper = (savingsGoalResult: SavingsGoalResult | undef }) } -const calculateSavingsGoalScheduleHelper = (savingsGoalResult: SavingsGoalResult | undefined): SavingsGoalScheduleResult[] | undefined => { +const calculateSavingsGoalScheduleHelper = (savingsGoalResult: SavingsGoalResult): SavingsGoalScheduleResult[] | undefined => { return calculateSavingsGoalSchedule(savingsGoalResult) } diff --git a/src/contexts/shared/savings-goal-calculator/savings-goal-calculator.types.ts b/src/contexts/shared/savings-goal-calculator/savings-goal-calculator.types.ts index 446ae2c..bb75ca3 100644 --- a/src/contexts/shared/savings-goal-calculator/savings-goal-calculator.types.ts +++ b/src/contexts/shared/savings-goal-calculator/savings-goal-calculator.types.ts @@ -41,4 +41,5 @@ export type SavingsGoalScheduleResult = { interestEarned: number, totalInterestEarned: number, balance: number -} \ No newline at end of file +} + diff --git a/src/contexts/signed-in/savings/savings.context.tsx b/src/contexts/signed-in/savings/savings.context.tsx index 209b8b4..bea86eb 100644 --- a/src/contexts/signed-in/savings/savings.context.tsx +++ b/src/contexts/signed-in/savings/savings.context.tsx @@ -11,7 +11,7 @@ import { DEFAULT_SAVINGS_ACCOUNTS, DEFAULT_SAVINGS_ACCOUNTS_SUMMARY } from "../. import { useSelector } from "react-redux"; import { selectCurrentUser } from "../../../store/shared/user/user.selector"; -import { SavingsAccount, SavingsAccountsSummary, SavingsContextType, SavingsProviderProps } from "./savings.types"; +import { SavingsAccount, SavingsAccountInfo, SavingsAccountsSummary, SavingsContextType, SavingsProviderProps } from "./savings.types"; // helper functions @@ -89,8 +89,8 @@ const updateSavingsAccountHelper = (savingsAccounts: SavingsAccount[], originalS return savingsAccount.savingsAccountName === originalSavingsAccountName; }); - const savingsAccountInfo = { - originalSavingsAccountInfo: originalSavingsAccount, + const savingsAccountInfo: SavingsAccountInfo = { + originalSavingsAccountInfo: originalSavingsAccount as SavingsAccount, updatedSavingsAccountInfo: { ...updatedSavingsAccount, diff --git a/src/contexts/signed-in/savings/savings.types.ts b/src/contexts/signed-in/savings/savings.types.ts index 7f30e0b..193a0db 100644 --- a/src/contexts/signed-in/savings/savings.types.ts +++ b/src/contexts/signed-in/savings/savings.types.ts @@ -18,6 +18,14 @@ export type SavingsAccount = { savings: SavingsCalculationRecord[]; } +export type SavingsAccountCalculated = { + totalSavings: number; + totalContribution: number; + totalInterest: number; + + savings: SavingsCalculationRecord[]; +} + export interface SavingsContextType { savingsAccounts: SavingsAccount[]; @@ -48,4 +56,9 @@ export type SavingsAccountsSummary = { currentAllSavingsAccountsBalance?: number; totalAllContribution?: number; totalAllInterest?: number; +} + +export type SavingsAccountInfo = { + originalSavingsAccountInfo: SavingsAccount; + updatedSavingsAccountInfo: SavingsAccount; } \ No newline at end of file diff --git a/src/contexts/signed-out/savings/savings.types.ts b/src/contexts/signed-out/savings/savings.types.ts index 6ef0230..2b95bcb 100644 --- a/src/contexts/signed-out/savings/savings.types.ts +++ b/src/contexts/signed-out/savings/savings.types.ts @@ -18,6 +18,14 @@ export type SavingsAccount = { savings: SavingsCalculationRecord[]; } +export type SavingsAccountCalculated = { + totalSavings: number; + totalContribution: number; + totalInterest: number; + + savings: SavingsCalculationRecord[]; +} + export interface SavingsContextType { savingsAccounts: SavingsAccount[]; diff --git a/src/store/shared/savings-goal-calculator/savings-goal-calculator.action.ts b/src/store/shared/savings-goal-calculator/savings-goal-calculator.action.ts index bd92947..e2f95ff 100644 --- a/src/store/shared/savings-goal-calculator/savings-goal-calculator.action.ts +++ b/src/store/shared/savings-goal-calculator/savings-goal-calculator.action.ts @@ -6,7 +6,7 @@ import { calculateSavingsGoal as _calculateSavingsGoal, import { ActionWithPayload, createAction, withMatcher } from "../../../utils/reducer/reducer.utils"; import { SAVINGS_GOAL_CALCULATOR_ACTION_TYPES, SavingsGoalResult, SavingsGoalScheduleResult } from "./savings-goal-calculator.types"; -export type CalculateSavingsGoal = ActionWithPayload +export type CalculateSavingsGoal = ActionWithPayload export type CalculateSavingsGoalSchedule = ActionWithPayload type SavingsGoalInput = { @@ -19,7 +19,7 @@ type SavingsGoalInput = { } // helper functions -const calculateSavingsGoalHelper = (savingsGoalResult: SavingsGoalResult, savingsGoalInput: SavingsGoalInput): SavingsGoalResult => { +const calculateSavingsGoalHelper = (savingsGoalResult: SavingsGoalResult, savingsGoalInput: SavingsGoalInput): SavingsGoalResult | undefined => { if (validateSavingsGoalInput(savingsGoalInput)) { return savingsGoalResult } diff --git a/src/utils/api-requests/market-data.requests.js b/src/utils/api-requests/market-data.requests.ts similarity index 81% rename from src/utils/api-requests/market-data.requests.js rename to src/utils/api-requests/market-data.requests.ts index e15ea34..bc632c1 100644 --- a/src/utils/api-requests/market-data.requests.js +++ b/src/utils/api-requests/market-data.requests.ts @@ -1,8 +1,9 @@ +import { MarketDataQuery } from "../../contexts/shared/market-data/market-data.types"; import { errorOnGetStocksMarketData, errorOnGetIndicesMarketData, errorOnGetCryptoMarketData, errorOnGetForexMarketData } from "../errors/market-data.errors"; // stocks -export async function getStocksMarketData(marketDataQuery) { +export async function getStocksMarketData(marketDataQuery: MarketDataQuery): Promise { try { const response = await fetch(`${process.env.REACT_APP_API_URL_MARKET_DATA}${process.env.REACT_APP_API_URL_MARKET_DATA_STOCKS}`, { @@ -22,7 +23,7 @@ export async function getStocksMarketData(marketDataQuery) { } // indices -export async function getIndicesMarketData(marketDataQuery) { +export async function getIndicesMarketData(marketDataQuery: MarketDataQuery): Promise { try { const response = await fetch(`${process.env.REACT_APP_API_URL_MARKET_DATA}${process.env.REACT_APP_API_URL_MARKET_DATA_INDICES}`, { @@ -42,7 +43,7 @@ export async function getIndicesMarketData(marketDataQuery) { } // crypto -export async function getCryptoMarketData(marketDataQuery) { +export async function getCryptoMarketData(marketDataQuery: MarketDataQuery): Promise { try { const response = await fetch(`${process.env.REACT_APP_API_URL_MARKET_DATA}${process.env.REACT_APP_API_URL_MARKET_DATA_CRYPTO}`, { @@ -62,7 +63,7 @@ export async function getCryptoMarketData(marketDataQuery) { } // forex -export async function getForexMarketData(marketDataQuery) { +export async function getForexMarketData(marketDataQuery: MarketDataQuery): Promise { try { const response = await fetch(`${process.env.REACT_APP_API_URL_MARKET_DATA}${process.env.REACT_APP_API_URL_MARKET_DATA_FOREX}`, { diff --git a/src/utils/api-requests/savings.requests.js b/src/utils/api-requests/savings.requests.ts similarity index 66% rename from src/utils/api-requests/savings.requests.js rename to src/utils/api-requests/savings.requests.ts index ebc7951..563045d 100644 --- a/src/utils/api-requests/savings.requests.js +++ b/src/utils/api-requests/savings.requests.ts @@ -1,3 +1,4 @@ +import { SavingsAccount, SavingsAccountInfo, SavingsAccountsSummary } from "../../contexts/signed-in/savings/savings.types"; import { errorOnGetSavingsAccountsData, errorOnGetSavingsAccountsSummaryData, errorOnPostSavingsAccountCreate, errorOnPutSavingsAccountData, errorOnSavingsAccountInvestment, errorOnPutSavingsAccountsData, errorOnPutSavingsAccountsSummaryData } from "../errors/savings.errors"; @@ -5,7 +6,7 @@ import { errorOnGetSavingsAccountsData, errorOnGetSavingsAccountsSummaryData, // savings accounts api requests // getting savings accounts and summary data on sign in -export const getSavingsAccountsData = async (userId, email) => { +export const getSavingsAccountsData = async (userId: string | null | undefined, email: string | null | undefined): Promise => { try { const response = await fetch(`${process.env.REACT_APP_API_URL_SAVINGS_ACCOUNTS}/${userId}/${email}`); @@ -17,7 +18,7 @@ export const getSavingsAccountsData = async (userId, email) => { } }; -export const getSavingsAccountsSummaryData = async (userId, email) => { +export const getSavingsAccountsSummaryData = async (userId: string | null | undefined, email: string | null | undefined): Promise => { try { const response = await fetch(`${process.env.REACT_APP_API_URL_SAVINGS_ACCOUNTS_SUMMARY}/${userId}/${email}`); @@ -30,7 +31,8 @@ export const getSavingsAccountsSummaryData = async (userId, email) => { }; // saving accounts operations -export const postSavingsAccountCreate = async (userId, email, savingsAccountInfo) => { +export const postSavingsAccountCreate = async (userId: string | null | undefined, email: string | null | undefined, + savingsAccountInfo: SavingsAccount): Promise => { try { const response = await fetch(`${process.env.REACT_APP_API_URL_SAVINGS_ACCOUNTS}/${userId}/${email}`, { @@ -48,7 +50,8 @@ export const postSavingsAccountCreate = async (userId, email, savingsAccountInfo } }; -export const putSavingsAccountData = async (userId, email, savingsAccountInfo) => { +export const putSavingsAccountData = async (userId: string | null | undefined, email: string | null | undefined, + savingsAccountInfo: SavingsAccountInfo): Promise => { try { const response = await fetch(`${process.env.REACT_APP_API_URL_SAVINGS_ACCOUNTS}/${userId}/${email}`, { @@ -66,7 +69,8 @@ export const putSavingsAccountData = async (userId, email, savingsAccountInfo) = } }; -export const deleteSavingsAccount = async (userId, email, closingSavingsAccountName) => { +export const deleteSavingsAccount = async (userId: string | null | undefined, email: string | null | undefined, + closingSavingsAccountName: string): Promise => { try { const response = await fetch(`${process.env.REACT_APP_API_URL_SAVINGS_ACCOUNTS}/${userId}/${email}`, { @@ -77,7 +81,7 @@ export const deleteSavingsAccount = async (userId, email, closingSavingsAccountN body: String(closingSavingsAccountName) }); - return response.statusCode; + return response.status; } catch (error) { errorOnSavingsAccountInvestment(); @@ -85,7 +89,8 @@ export const deleteSavingsAccount = async (userId, email, closingSavingsAccountN }; // updating savings accounts and summary data on sign out -export const putSavingsAccountsData = async (userId, email, savingsAccounts) => { +export const putSavingsAccountsData = async (userId: string | null | undefined, email: string | null | undefined, + savingsAccounts: SavingsAccount[]): Promise => { try { const response = await fetch(`${process.env.REACT_APP_API_URL_SAVINGS_ACCOUNTS}/${userId}/${email}`, { @@ -105,7 +110,8 @@ export const putSavingsAccountsData = async (userId, email, savingsAccounts) => } }; -export const putSavingsAccountsSummaryData = async (userId, email, savingsAccountsSummary) => { +export const putSavingsAccountsSummaryData = async (userId: string | null | undefined, email: string | null | undefined, + savingsAccountsSummary: SavingsAccountsSummary): Promise => { try { const response = await fetch(`${process.env.REACT_APP_API_URL_SAVINGS_ACCOUNTS}/${userId}/${email}`, { diff --git a/src/utils/calculations/savings.calculations.ts b/src/utils/calculations/savings.calculations.ts index 9ed4b69..9e2341c 100644 --- a/src/utils/calculations/savings.calculations.ts +++ b/src/utils/calculations/savings.calculations.ts @@ -1,10 +1,30 @@ +import { SavingsGoalInput, SavingsGoalResult, SavingsGoalScheduleResult } from "../../contexts/shared/savings-goal-calculator/savings-goal-calculator.types"; +import { SavingsAccount } from "../../contexts/signed-in/savings/savings.types"; +import { SavingsAccountCalculated } from "../../contexts/signed-out/savings/savings.types"; import { SAVINGS_CONTRIBUTION_INTERVALS, SAVINGS_GOAL_COMPOUNDED, AVERAGE_DAYS_IN_MONTH } from "../constants/savings.constants"; import date from 'date-and-time' +type CompoundedInterestInput = { + A: number; + P: number; + r: number; + t: number; + n: number; +} + +type SavingsAccountCalculationInput = { + initialDeposit: number; + startDate: string; + monthlyContribution: number; + contributionPeriod: number; + contributionInterval: string; + apy: number; +} + // helper functions -const calculateCompoundedInterestPMT = (compoundedInterestInput) => { +const calculateCompoundedInterestPMT = (compoundedInterestInput: CompoundedInterestInput): number => { let res = compoundedInterestInput.A res -= compoundedInterestInput.P * (Math.pow(1 + (compoundedInterestInput.r / compoundedInterestInput.n), compoundedInterestInput.n * compoundedInterestInput.t)) res /= (Math.pow(1 + (compoundedInterestInput.r / compoundedInterestInput.n), compoundedInterestInput.n * compoundedInterestInput.t) - 1) / (compoundedInterestInput.r / compoundedInterestInput.n) @@ -12,7 +32,7 @@ const calculateCompoundedInterestPMT = (compoundedInterestInput) => { return res } -const calculateCompoundedInterestA = (compoundedInterestInput, PMT) => { +const calculateCompoundedInterestA = (compoundedInterestInput: CompoundedInterestInput, PMT: number): { interestEarned: number, balance: number } => { const interestEarned = compoundedInterestInput.P * Math.pow(compoundedInterestInput.r / compoundedInterestInput.n, compoundedInterestInput.n * compoundedInterestInput.t) const compoundedResult = compoundedInterestInput.P * Math.pow(1 + (compoundedInterestInput.r / compoundedInterestInput.n), compoundedInterestInput.n * compoundedInterestInput.t) const balance = compoundedResult + PMT @@ -23,7 +43,7 @@ const calculateCompoundedInterestA = (compoundedInterestInput, PMT) => { } } -const calculatePMTCompoundedInterestA = (compoundedInterestInput, PMT) => { +const calculatePMTCompoundedInterestA = (compoundedInterestInput: CompoundedInterestInput, PMT: number): number => { let res = compoundedInterestInput.P res *= Math.pow(1 + (compoundedInterestInput.r / compoundedInterestInput.n), compoundedInterestInput.n * compoundedInterestInput.t) res += PMT * ((Math.pow(1 + (compoundedInterestInput.r / compoundedInterestInput.n), compoundedInterestInput.n * compoundedInterestInput.t) - 1) / (compoundedInterestInput.r / compoundedInterestInput.n)) @@ -33,13 +53,14 @@ const calculatePMTCompoundedInterestA = (compoundedInterestInput, PMT) => { // savings calculations -export const calculateSavings = (savingsAccount) => { +export const calculateSavings = (savingsAccount: SavingsAccountCalculationInput): SavingsAccountCalculated => { // return calculated totalSavings, totalContribution, totalInterest let resSavings = [] const startDate = new Date(savingsAccount.startDate) - let compoundedInterestInput = { + let compoundedInterestInput: CompoundedInterestInput = { + A: 0, P: savingsAccount.initialDeposit, r: savingsAccount.apy / 100, t: 0, @@ -96,7 +117,7 @@ export const calculateSavings = (savingsAccount) => { // savings goal -export const calculateSavingsGoal = (savingsGoalInput) => { +export const calculateSavingsGoal = (savingsGoalInput: SavingsGoalInput): SavingsGoalResult | undefined => { let compoundedInterestInput = { @@ -132,12 +153,13 @@ export const calculateSavingsGoal = (savingsGoalInput) => { } } -export const calculateSavingsGoalSchedule = (savingsGoalResult) => { +export const calculateSavingsGoalSchedule = (savingsGoalResult: SavingsGoalResult): SavingsGoalScheduleResult[] => { let res = [] let totalInterest = 0 const startDate = new Date(savingsGoalResult.dateFirstDeposit) - let compoundedInterestInput = { + let compoundedInterestInput: CompoundedInterestInput = { + A: 0, P: savingsGoalResult.amountFirstDeposit, r: savingsGoalResult.interestRatePerYear / 100, t: (1 / 12), diff --git a/src/utils/constants/investments.constants.ts b/src/utils/constants/investments.constants.ts index 781293c..605f70e 100644 --- a/src/utils/constants/investments.constants.ts +++ b/src/utils/constants/investments.constants.ts @@ -82,8 +82,6 @@ export enum INVESTMENT_COMPOUNDING_PERIOD_MONTHS { // }, // } -const AVERAGE_DAYS_IN_MONTH = 30.437; // Example value for AVERAGE_DAYS_IN_MONTH, replace with actual value if needed - // Define enum for compounding types export enum InvestmentCompoundingType { Annually = "Annually", diff --git a/src/utils/constants/market-data.constants.js b/src/utils/constants/market-data.constants.js deleted file mode 100644 index 04aef9a..0000000 --- a/src/utils/constants/market-data.constants.js +++ /dev/null @@ -1,22 +0,0 @@ -// market data constants - -export const MARKET_DATA_TYPES = { - stocks: "Stocks", - indices: "Indices", - crypto: "Crypto", - currencies: "Currencies" -} - -export const DEFAULT_MARKET_DATA = undefined - -export const MARKET_DATA_SEARCH_QUERY_MULTIPLIER = 1 - -export const MARKET_DATA_CRYPTO_PREFIX = "X:" -export const MARKET_DATA_FOREX_PREFIX = "C:" - -export const MARKET_DATA_INTERVALS = { - "Hour": "Hourly", - "Day": "Daily", - "Week": "Weekly", - "Month": "Monthly" -} \ No newline at end of file diff --git a/src/utils/constants/market-data.constants.ts b/src/utils/constants/market-data.constants.ts new file mode 100644 index 0000000..fee276e --- /dev/null +++ b/src/utils/constants/market-data.constants.ts @@ -0,0 +1,22 @@ +// market data constants + +export enum MARKET_DATA_TYPES { + stocks = "Stocks", + indices = "Indices", + crypto = "Crypto", + currencies = "Currencies" +} + +export const DEFAULT_MARKET_DATA = undefined + +export const MARKET_DATA_SEARCH_QUERY_MULTIPLIER = 1 + +export const MARKET_DATA_CRYPTO_PREFIX = "X:" +export const MARKET_DATA_FOREX_PREFIX = "C:" + +export enum MARKET_DATA_INTERVALS { + "Hour" = "Hourly", + "Day" = "Daily", + "Week" = "Weekly", + "Month" = "Monthly" +} \ No newline at end of file diff --git a/src/utils/constants/regex.constants.js b/src/utils/constants/regex.constants.ts similarity index 100% rename from src/utils/constants/regex.constants.js rename to src/utils/constants/regex.constants.ts diff --git a/src/utils/constants/savings.constants.js b/src/utils/constants/savings.constants.js deleted file mode 100644 index 4391e30..0000000 --- a/src/utils/constants/savings.constants.js +++ /dev/null @@ -1,22 +0,0 @@ -// savings constants - -export const SAVINGS_CONTRIBUTION_INTERVALS = { - months: "Months", - years: "Years", -}; - -export const DEFAULT_SAVINGS_ACCOUNTS = []; - -export const DEFAULT_SAVINGS_ACCOUNTS_SUMMARY = {}; - -export const SAVINGS_CONFIRM_CLOSE = { - yes: "Yes", - no: "No", -}; - -export const SAVINGS_GOAL_COMPOUNDED = { - daily: "Daily", - monthly: "Monthly" -} - -export const AVERAGE_DAYS_IN_MONTH = 30.437 \ No newline at end of file diff --git a/src/utils/constants/savings.constants.ts b/src/utils/constants/savings.constants.ts new file mode 100644 index 0000000..ef0db54 --- /dev/null +++ b/src/utils/constants/savings.constants.ts @@ -0,0 +1,22 @@ +// savings constants + +export enum SAVINGS_CONTRIBUTION_INTERVALS { + months = "Months", + years = "Years", +}; + +export const DEFAULT_SAVINGS_ACCOUNTS = []; + +export const DEFAULT_SAVINGS_ACCOUNTS_SUMMARY = {}; + +export enum SAVINGS_CONFIRM_CLOSE { + yes = "Yes", + no = "No", +}; + +export enum SAVINGS_GOAL_COMPOUNDED { + daily ="Daily", + monthly = "Monthly" +} + +export const AVERAGE_DAYS_IN_MONTH = 30.437 \ No newline at end of file diff --git a/src/utils/errors/market-data.errors.js b/src/utils/errors/market-data.errors.ts similarity index 100% rename from src/utils/errors/market-data.errors.js rename to src/utils/errors/market-data.errors.ts diff --git a/src/utils/errors/savings.errors.js b/src/utils/errors/savings.errors.ts similarity index 100% rename from src/utils/errors/savings.errors.js rename to src/utils/errors/savings.errors.ts diff --git a/src/utils/graphql/savings.graphql.js b/src/utils/graphql/savings.graphql.ts similarity index 78% rename from src/utils/graphql/savings.graphql.js rename to src/utils/graphql/savings.graphql.ts index 518fb19..38fe9cf 100644 --- a/src/utils/graphql/savings.graphql.js +++ b/src/utils/graphql/savings.graphql.ts @@ -1,7 +1,7 @@ -import { gql } from "@apollo/client"; +import { DocumentNode, gql } from "@apollo/client"; // graphql savings queries -export const SAVINGS_ACCOUNTS_BY_USER = gql` +export const SAVINGS_ACCOUNTS_BY_USER: DocumentNode = gql` query SavingsAccountsByUser($userId: String!, $email: String!) { savingsAccountsByUser(userId: $userId, email: $email) { savingsAccountName @@ -26,7 +26,7 @@ export const SAVINGS_ACCOUNTS_BY_USER = gql` } ` -export const SAVINGS_ACCOUNTS_SUMMARY_BY_USER = gql` +export const SAVINGS_ACCOUNTS_SUMMARY_BY_USER: DocumentNode = gql` query SavingsAccountsSummaryByUser($userId: String!, $email: String!) { currentAllSavingsAccountsBalance totalAllContribution @@ -35,31 +35,31 @@ export const SAVINGS_ACCOUNTS_SUMMARY_BY_USER = gql` ` // graphql savings mutations -export const CREATE_USER_SAVINGS_ACCOUNT = gql` +export const CREATE_USER_SAVINGS_ACCOUNT: DocumentNode = gql` mutation CreateUserSavingsAccount($userId: String!, $email: String!, $savingsAccountInfo: SavingsAccountInfo!) { CreateUserSavingsAccount(userId: $userId, email: $email, savingsAccountInfo: $savingsAccountInfo) } ` -export const UPDATE_USER_SAVINGS_ACCOUNT = gql` +export const UPDATE_USER_SAVINGS_ACCOUNT: DocumentNode = gql` mutation UpdateUserSavingsAccount($userId: String!, $email: String!, $savingsAccountInfoUpdate: SavingsAccountInfoUpdate!) { updateUserSavingsAccount(userId: $userId, email: $email, savingsAccountInfoUpdate: $savingsAccountInfoUpdate) } ` -export const DELETE_USER_SAVINGS_ACCOUNT = gql` +export const DELETE_USER_SAVINGS_ACCOUNT: DocumentNode = gql` mutation DeleteUserSavingsAccount($userId: String!, $email: String!, $closingSavingsAccountName: String!) { deleteUserSavingsAccount(userId: $userId, email: $email, closingSavingsAccountName: $closingSavingsAccountName) } ` -export const UPDATE_USER_SAVINGS_ACCOUNTS = gql` +export const UPDATE_USER_SAVINGS_ACCOUNTS: DocumentNode = gql` mutation UpdateUserSavingsAccounts($userId: String!, $email: String!, $savingsAccounts: [SavingsAccountInfo]!) { updateUserSavingsAccounts(userId: $userId, email: $email, savingsAccounts: $savingsAccounts) } ` -export const UPDATE_USER_SAVINGS_ACCOUNTS_SUMMARY = gql` +export const UPDATE_USER_SAVINGS_ACCOUNTS_SUMMARY: DocumentNode = gql` mutation UpdateUserSavingsAccountsSummary($userId: String!, $email: String!, $savingsAccountsSummary: SavingsAccountSummaryInfo!) { updateUserSavingsAccountsSummary(userId: $userId, email: $email, savingsAccountsSummary: $savingsAccountsSummary) } diff --git a/src/utils/validations/market-data.validation.js b/src/utils/validations/market-data.validation.ts similarity index 70% rename from src/utils/validations/market-data.validation.js rename to src/utils/validations/market-data.validation.ts index 215bf51..9460bba 100644 --- a/src/utils/validations/market-data.validation.js +++ b/src/utils/validations/market-data.validation.ts @@ -1,8 +1,9 @@ +import { MarketDataQuery } from "../../contexts/shared/market-data/market-data.types" import { errorOnMarketDataQuery, errorOnStartDateBeforeEndDate } from "../errors/market-data.errors" // market data validation functions -export const validateMarketDataQuery = (marketDataQuery) => { +export const validateMarketDataQuery = (marketDataQuery: MarketDataQuery): boolean => { // validating if startDate > endDate if (marketDataQuery.marketDataStartDate && marketDataQuery.marketDataEndDate && marketDataQuery.marketDataStartDate > marketDataQuery.marketDataEndDate) { errorOnStartDateBeforeEndDate() diff --git a/src/utils/validations/savings.validation.js b/src/utils/validations/savings.validation.ts similarity index 84% rename from src/utils/validations/savings.validation.js rename to src/utils/validations/savings.validation.ts index 835e6b0..cbfd1eb 100644 --- a/src/utils/validations/savings.validation.js +++ b/src/utils/validations/savings.validation.ts @@ -1,3 +1,5 @@ +import { SavingsGoalInput } from "../../contexts/shared/savings-goal-calculator/savings-goal-calculator.types"; +import { SavingsAccount } from "../../contexts/signed-in/savings/savings.types"; import { REGEX_PATTERNS } from "../constants/regex.constants"; import { errorOnSavingsAccountExists, errorOnInvalidSavingsAccountName, errorOnInvalidSavingsAccountInputs, @@ -5,7 +7,7 @@ import { errorOnSavingsAccountExists, errorOnInvalidSavingsAccountName, // savings validation functions -export const validateSavingsAccountCreation = (savingsAccounts, savingsAccount) => { +export const validateSavingsAccountCreation = (savingsAccounts: SavingsAccount[], savingsAccount: SavingsAccount): boolean => { const savingsAccountExists = savingsAccounts.find((account) => account.savingsAccountName === savingsAccount.savingsAccountName); if (savingsAccountExists) { @@ -33,7 +35,7 @@ export const validateSavingsAccountCreation = (savingsAccounts, savingsAccount) return false; }; -export const validateSavingsAccountUpdate = (savingsAccounts, originalSavingsAccountName, updatedSavingsAccount) => { +export const validateSavingsAccountUpdate = (savingsAccounts: SavingsAccount[], originalSavingsAccountName: string, updatedSavingsAccount: SavingsAccount): boolean => { // validate fields of updatedInvestment // strings @@ -63,7 +65,7 @@ export const validateSavingsAccountUpdate = (savingsAccounts, originalSavingsAcc // savings goal -export const validateSavingsGoalInput = (savingsGoalInput) => { +export const validateSavingsGoalInput = (savingsGoalInput: SavingsGoalInput): boolean => { // number