diff --git a/libs/client/shared/src/api/useTellerApi.ts b/libs/client/shared/src/api/useTellerApi.ts index a59287a1..d67f4e69 100644 --- a/libs/client/shared/src/api/useTellerApi.ts +++ b/libs/client/shared/src/api/useTellerApi.ts @@ -3,7 +3,6 @@ import toast from 'react-hot-toast' import { useAxiosWithAuth } from '../hooks/useAxiosWithAuth' import { useMutation, useQueryClient } from '@tanstack/react-query' import type { SharedType } from '@maybe-finance/shared' -import { invalidateAccountQueries } from '../utils' import type { AxiosInstance } from 'axios' import type { TellerTypes } from '@maybe-finance/teller-api' import { useAccountConnectionApi } from './useAccountConnectionApi' @@ -57,9 +56,6 @@ export function useTellerApi() { syncConnection.mutate(_connection.id) toast.success(`Account connection added!`) }, - onSettled: () => { - invalidateAccountQueries(queryClient, false) - }, }) return { diff --git a/libs/server/features/src/providers/teller/teller.etl.ts b/libs/server/features/src/providers/teller/teller.etl.ts index 524659c9..8b6d8762 100644 --- a/libs/server/features/src/providers/teller/teller.etl.ts +++ b/libs/server/features/src/providers/teller/teller.etl.ts @@ -1,6 +1,6 @@ import type { AccountConnection, PrismaClient } from '@prisma/client' import type { Logger } from 'winston' -import { SharedUtil, type SharedType } from '@maybe-finance/shared' +import { AccountUtil, SharedUtil, type SharedType } from '@maybe-finance/shared' import type { TellerApi, TellerTypes } from '@maybe-finance/teller-api' import { DbUtil, TellerUtil, type IETL, type ICryptoService } from '@maybe-finance/server/shared' import { Prisma } from '@prisma/client' @@ -117,6 +117,9 @@ export class TellerETL implements IETL { return [ // upsert accounts ...accounts.map((tellerAccount) => { + const type = TellerUtil.getType(tellerAccount.type) + const classification = AccountUtil.getClassification(type) + return this.prisma.account.upsert({ where: { accountConnectionId_tellerAccountId: { @@ -136,7 +139,7 @@ export class TellerETL implements IETL { tellerType: tellerAccount.type, tellerSubtype: tellerAccount.subtype, mask: tellerAccount.last_four, - ...TellerUtil.getAccountBalanceData(tellerAccount), + ...TellerUtil.getAccountBalanceData(tellerAccount, classification), }, update: { type: TellerUtil.getType(tellerAccount.type), @@ -144,7 +147,7 @@ export class TellerETL implements IETL { subcategoryProvider: tellerAccount.subtype ?? 'other', tellerType: tellerAccount.type, tellerSubtype: tellerAccount.subtype, - ..._.omit(TellerUtil.getAccountBalanceData(tellerAccount), [ + ..._.omit(TellerUtil.getAccountBalanceData(tellerAccount, classification), [ 'currentBalanceStrategy', 'availableBalanceStrategy', ]), diff --git a/libs/server/features/src/providers/teller/teller.service.ts b/libs/server/features/src/providers/teller/teller.service.ts index defa153d..3059f42a 100644 --- a/libs/server/features/src/providers/teller/teller.service.ts +++ b/libs/server/features/src/providers/teller/teller.service.ts @@ -168,16 +168,6 @@ export class TellerService implements IAccountConnectionProvider, IInstitutionPr }, }) - await this.sync(accountConnection, { type: 'teller', initialSync: true }) - - await this.prisma.accountConnection.update({ - where: { id: accountConnection.id }, - data: { - status: 'OK', - syncStatus: 'IDLE', - }, - }) - return accountConnection } } diff --git a/libs/server/shared/src/utils/teller-utils.ts b/libs/server/shared/src/utils/teller-utils.ts index 0b741583..ec44700a 100644 --- a/libs/server/shared/src/utils/teller-utils.ts +++ b/libs/server/shared/src/utils/teller-utils.ts @@ -1,4 +1,10 @@ -import { Prisma, AccountCategory, AccountType, type Account } from '@prisma/client' +import { + Prisma, + AccountCategory, + AccountType, + type Account, + type AccountClassification, +} from '@prisma/client' import type { TellerTypes } from '@maybe-finance/teller-api' import { Duration } from 'luxon' @@ -7,10 +13,10 @@ import { Duration } from 'luxon' */ export const TELLER_WINDOW_MAX = Duration.fromObject({ years: 1 }) -export function getAccountBalanceData({ - balance, - currency, -}: Pick): Pick< +export function getAccountBalanceData( + { balance, currency }: Pick, + classification: AccountClassification +): Pick< Account, | 'currentBalanceProvider' | 'currentBalanceStrategy' @@ -18,11 +24,14 @@ export function getAccountBalanceData({ | 'availableBalanceStrategy' | 'currencyCode' > { + const sign = classification === 'liability' ? -1 : 1 return { - currentBalanceProvider: new Prisma.Decimal(balance.ledger ? Number(balance.ledger) : 0), + currentBalanceProvider: new Prisma.Decimal( + balance.ledger ? sign * Number(balance.ledger) : 0 + ), currentBalanceStrategy: 'current', availableBalanceProvider: new Prisma.Decimal( - balance.available ? Number(balance.available) : 0 + balance.available ? sign * Number(balance.available) : 0 ), availableBalanceStrategy: 'available', currencyCode: currency,