1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-08-09 07:25:19 +02:00

import fixes

This commit is contained in:
Tyler Myracle 2024-01-19 19:17:36 -06:00
parent 4bb856ffbf
commit bc4ff9bd7f
6 changed files with 50 additions and 18 deletions

View file

@ -1,5 +1,5 @@
import { InvestmentTransactionCategory, type User } from '@prisma/client' import type { User } from '@prisma/client'
import { PrismaClient } from '@prisma/client' import { PrismaClient, InvestmentTransactionCategory } from '@prisma/client'
import { createLogger, transports } from 'winston' import { createLogger, transports } from 'winston'
import { DateTime } from 'luxon' import { DateTime } from 'luxon'
import { import {

View file

@ -1,18 +1,22 @@
import type { AxiosInstance } from 'axios' import type { AxiosInstance } from 'axios'
import type { SharedType } from '@maybe-finance/shared' import {
import type { Prisma, AccountConnection, User } from '@prisma/client' type Prisma,
type AccountConnection,
type User,
AccountConnectionType,
AccountSyncStatus,
} from '@prisma/client'
import { startServer, stopServer } from './utils/server' import { startServer, stopServer } from './utils/server'
import { getAxiosClient } from './utils/axios' import { getAxiosClient } from './utils/axios'
import prisma from '../lib/prisma' import prisma from '../lib/prisma'
import { InMemoryQueue } from '@maybe-finance/server/shared' import { InMemoryQueue } from '@maybe-finance/server/shared'
import { default as _teller } from '../lib/teller'
import nock from 'nock' import nock from 'nock'
import { resetUser } from './utils/user' import { resetUser } from './utils/user'
jest.mock('../lib/teller.ts') jest.mock('../lib/teller.ts')
// For TypeScript support // For TypeScript support
const teller = jest.mocked(_teller) //const teller = jest.mocked(_teller)
const authId = '__TEST_USER_ID__' const authId = '__TEST_USER_ID__'
let axios: AxiosInstance let axios: AxiosInstance
@ -46,13 +50,13 @@ beforeEach(async () => {
connectionData = { connectionData = {
data: { data: {
name: 'Chase Test', name: 'Chase Test',
type: 'teller' as SharedType.AccountConnectionType, type: AccountConnectionType.teller,
tellerEnrollmentId: 'test-teller-item-workers', tellerEnrollmentId: 'test-teller-item-workers',
tellerInstitutionId: 'chase_test', tellerInstitutionId: 'chase_test',
tellerAccessToken: tellerAccessToken:
'U2FsdGVkX1+WMq9lfTS9Zkbgrn41+XT1hvSK5ain/udRPujzjVCAx/lyPG7EumVZA+nVKXPauGwI+d7GZgtqTA9R3iCZNusU6LFPnmFOCE4=', // need correct encoding here 'U2FsdGVkX1+WMq9lfTS9Zkbgrn41+XT1hvSK5ain/udRPujzjVCAx/lyPG7EumVZA+nVKXPauGwI+d7GZgtqTA9R3iCZNusU6LFPnmFOCE4=', // need correct encoding here
userId: user.id, userId: user.id,
syncStatus: 'PENDING', syncStatus: AccountSyncStatus.PENDING,
}, },
} }

View file

@ -1,5 +1,5 @@
import { InvestmentTransactionCategory, type User } from '@prisma/client' import type { User } from '@prisma/client'
import { Prisma, PrismaClient } from '@prisma/client' import { Prisma, PrismaClient, InvestmentTransactionCategory } from '@prisma/client'
import { createLogger, transports } from 'winston' import { createLogger, transports } from 'winston'
import { DateTime } from 'luxon' import { DateTime } from 'luxon'
import type { import type {

View file

@ -7,6 +7,7 @@ import type { AccountConnection, User } from '@prisma/client'
import prisma from '../lib/prisma' import prisma from '../lib/prisma'
import { default as _teller } from '../lib/teller' import { default as _teller } from '../lib/teller'
import { resetUser } from './helpers/user.test-helper' import { resetUser } from './helpers/user.test-helper'
import { Interval } from 'luxon'
// Import the workers process // Import the workers process
import '../../main' import '../../main'
@ -131,13 +132,24 @@ describe('Message queue tests', () => {
expect(item.accounts).toHaveLength(1) expect(item.accounts).toHaveLength(1)
const [account] = item.accounts const [account] = item.accounts
const transactionBalance = mockTransactions.reduce(
(acc, t) => acc + t.amount, const intervalDates = Interval.fromDateTimes(
mockAccounts[0].balance.available TellerGenerator.lowerBound,
TellerGenerator.now
)
.splitBy({ day: 1 })
.map((date: Interval) => date.start.toISODate())
const startingBalance = Number(mockAccounts[0].balance.available)
const balances = TellerGenerator.calculateDailyBalances(
startingBalance,
mockTransactions,
intervalDates
) )
expect(account.transactions).toHaveLength(10) expect(account.transactions).toHaveLength(10)
expect(account.balances.map((b) => b.balance)).toEqual(transactionBalance) expect(account.balances.map((b) => b.balance)).toEqual(balances)
expect(account.holdings).toHaveLength(0) expect(account.holdings).toHaveLength(0)
expect(account.valuations).toHaveLength(0) expect(account.valuations).toHaveLength(0)
expect(account.investmentTransactions).toHaveLength(0) expect(account.investmentTransactions).toHaveLength(0)

View file

@ -15,7 +15,7 @@ async function main() {
name: 'Capital One', name: 'Capital One',
providers: [ providers: [
{ {
provider: 'TELLER', provider: Provider.TELLER,
providerId: 'capital_one', providerId: 'capital_one',
logoUrl: 'https://teller.io/images/banks/capital_one.jpg', logoUrl: 'https://teller.io/images/banks/capital_one.jpg',
rank: 1, rank: 1,
@ -27,7 +27,7 @@ async function main() {
name: 'Wells Fargo', name: 'Wells Fargo',
providers: [ providers: [
{ {
provider: 'TELLER', provider: Provider.TELLER,
providerId: 'wells_fargo', providerId: 'wells_fargo',
logoUrl: 'https://teller.io/images/banks/wells_fargo.jpg', logoUrl: 'https://teller.io/images/banks/wells_fargo.jpg',
}, },

View file

@ -263,9 +263,9 @@ export function generateConnection(): GenerateConnectionsResponse {
} }
} }
const now = DateTime.fromISO('2022-01-03', { zone: 'utc' }) export const now = DateTime.fromISO('2022-01-03', { zone: 'utc' })
const lowerBound = DateTime.fromISO('2021-12-01', { zone: 'utc' }) export const lowerBound = DateTime.fromISO('2021-12-01', { zone: 'utc' })
export const testDates = { export const testDates = {
now, now,
@ -278,3 +278,19 @@ export const testDates = {
}, },
} as Prisma.AccountBalanceWhereInput, } as Prisma.AccountBalanceWhereInput,
} }
export function calculateDailyBalances(startingBalance, transactions, dateInterval) {
transactions.sort((a, b) => new Date(a.date).getTime() - new Date(b.date).getTime())
const balanceChanges = {}
transactions.forEach((transaction) => {
const date = new Date(transaction.date).toISOString().split('T')[0]
balanceChanges[date] = (balanceChanges[date] || 0) + Number(transaction.amount)
})
return dateInterval.map((date) => {
return Object.keys(balanceChanges)
.filter((d) => d <= date)
.reduce((acc, d) => acc + balanceChanges[d], startingBalance)
})
}