mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-08-09 07:25:19 +02:00
revert changes to schema/db and undo changes to account integration spec
This commit is contained in:
parent
4b6a441c61
commit
d4984fba31
4 changed files with 11 additions and 69 deletions
|
@ -15,11 +15,17 @@ import { startServer, stopServer } from './utils/server'
|
||||||
import { getAxiosClient } from './utils/axios'
|
import { getAxiosClient } from './utils/axios'
|
||||||
import { resetUser } from './utils/user'
|
import { resetUser } from './utils/user'
|
||||||
import { createTestInvestmentAccount } from './utils/account'
|
import { createTestInvestmentAccount } from './utils/account'
|
||||||
|
import { default as _plaid } from '../lib/plaid'
|
||||||
|
|
||||||
|
jest.mock('../middleware/validate-plaid-jwt.ts')
|
||||||
jest.mock('bull')
|
jest.mock('bull')
|
||||||
|
jest.mock('plaid')
|
||||||
|
|
||||||
const prisma = new PrismaClient()
|
const prisma = new PrismaClient()
|
||||||
|
|
||||||
|
// For TypeScript support
|
||||||
|
const plaid = jest.mocked(_plaid) // eslint-disable-line
|
||||||
|
|
||||||
const authId = '__TEST_USER_ID__'
|
const authId = '__TEST_USER_ID__'
|
||||||
let axios: AxiosInstance
|
let axios: AxiosInstance
|
||||||
let user: User
|
let user: User
|
||||||
|
@ -101,6 +107,10 @@ describe('/v1/accounts API', () => {
|
||||||
mask: null,
|
mask: null,
|
||||||
isActive: true,
|
isActive: true,
|
||||||
syncStatus: 'IDLE',
|
syncStatus: 'IDLE',
|
||||||
|
plaidType: null,
|
||||||
|
plaidSubtype: null,
|
||||||
|
plaidAccountId: null,
|
||||||
|
plaidLiability: null,
|
||||||
currencyCode: 'USD',
|
currencyCode: 'USD',
|
||||||
currentBalance: new Decimal(21_000),
|
currentBalance: new Decimal(21_000),
|
||||||
availableBalance: null,
|
availableBalance: null,
|
||||||
|
|
|
@ -1,7 +0,0 @@
|
||||||
ALTER TABLE
|
|
||||||
"investment_transaction" DROP COLUMN "category";
|
|
||||||
|
|
||||||
ALTER TABLE
|
|
||||||
"investment_transaction"
|
|
||||||
ADD
|
|
||||||
COLUMN "category" "InvestmentTransactionCategory" DEFAULT 'other' :: "InvestmentTransactionCategory" NOT NULL;
|
|
|
@ -1,61 +0,0 @@
|
||||||
CREATE OR REPLACE FUNCTION calculate_return_dietz(p_account_id account.id%type, p_start date, p_end date, out percentage numeric, out amount numeric) AS $$
|
|
||||||
DECLARE
|
|
||||||
v_start date := GREATEST(p_start, (SELECT MIN(date) FROM account_balance WHERE account_id = p_account_id));
|
|
||||||
v_end date := p_end;
|
|
||||||
v_days int := v_end - v_start;
|
|
||||||
BEGIN
|
|
||||||
SELECT
|
|
||||||
ROUND((b1.balance - b0.balance - flows.net) / NULLIF(b0.balance + flows.weighted, 0), 4) AS "percentage",
|
|
||||||
b1.balance - b0.balance - flows.net AS "amount"
|
|
||||||
INTO
|
|
||||||
percentage, amount
|
|
||||||
FROM
|
|
||||||
account a
|
|
||||||
LEFT JOIN LATERAL (
|
|
||||||
SELECT
|
|
||||||
COALESCE(SUM(-fw.flow), 0) AS "net",
|
|
||||||
COALESCE(SUM(-fw.flow * fw.weight), 0) AS "weighted"
|
|
||||||
FROM (
|
|
||||||
SELECT
|
|
||||||
SUM(it.amount) AS flow,
|
|
||||||
(v_days - (it.date - v_start))::numeric / v_days AS weight
|
|
||||||
FROM
|
|
||||||
investment_transaction it
|
|
||||||
WHERE
|
|
||||||
it.account_id = a.id
|
|
||||||
AND it.date BETWEEN v_start AND v_end
|
|
||||||
-- filter for investment_transactions that represent external flows
|
|
||||||
AND (
|
|
||||||
it.category = 'transfer'
|
|
||||||
)
|
|
||||||
GROUP BY
|
|
||||||
it.date
|
|
||||||
) fw
|
|
||||||
) flows ON TRUE
|
|
||||||
LEFT JOIN LATERAL (
|
|
||||||
SELECT
|
|
||||||
ab.balance AS "balance"
|
|
||||||
FROM
|
|
||||||
account_balance ab
|
|
||||||
WHERE
|
|
||||||
ab.account_id = a.id AND ab.date <= v_start
|
|
||||||
ORDER BY
|
|
||||||
ab.date DESC
|
|
||||||
LIMIT 1
|
|
||||||
) b0 ON TRUE
|
|
||||||
LEFT JOIN LATERAL (
|
|
||||||
SELECT
|
|
||||||
COALESCE(ab.balance, a.current_balance) AS "balance"
|
|
||||||
FROM
|
|
||||||
account_balance ab
|
|
||||||
WHERE
|
|
||||||
ab.account_id = a.id AND ab.date <= v_end
|
|
||||||
ORDER BY
|
|
||||||
ab.date DESC
|
|
||||||
LIMIT 1
|
|
||||||
) b1 ON TRUE
|
|
||||||
WHERE
|
|
||||||
a.id = p_account_id;
|
|
||||||
END;
|
|
||||||
$$ LANGUAGE plpgsql STABLE;
|
|
||||||
|
|
|
@ -250,7 +250,7 @@ model InvestmentTransaction {
|
||||||
currencyCode String @default("USD") @map("currency_code")
|
currencyCode String @default("USD") @map("currency_code")
|
||||||
|
|
||||||
// Derived from provider types
|
// Derived from provider types
|
||||||
category InvestmentTransactionCategory @default(other)
|
category InvestmentTransactionCategory @default(dbgenerated("\nCASE\n WHEN (plaid_type = 'buy'::text) THEN 'buy'::\"InvestmentTransactionCategory\"\n WHEN (plaid_type = 'sell'::text) THEN 'sell'::\"InvestmentTransactionCategory\"\n WHEN (plaid_subtype = ANY (ARRAY['dividend'::text, 'qualified dividend'::text, 'non-qualified dividend'::text])) THEN 'dividend'::\"InvestmentTransactionCategory\"\n WHEN (plaid_subtype = ANY (ARRAY['non-resident tax'::text, 'tax'::text, 'tax withheld'::text])) THEN 'tax'::\"InvestmentTransactionCategory\"\n WHEN ((plaid_type = 'fee'::text) OR (plaid_subtype = ANY (ARRAY['account fee'::text, 'legal fee'::text, 'management fee'::text, 'margin expense'::text, 'transfer fee'::text, 'trust fee'::text]))) THEN 'fee'::\"InvestmentTransactionCategory\"\n WHEN (plaid_type = 'cash'::text) THEN 'transfer'::\"InvestmentTransactionCategory\"\n WHEN (plaid_type = 'cancel'::text) THEN 'cancel'::\"InvestmentTransactionCategory\"\n WHEN (finicity_investment_transaction_type = ANY (ARRAY['purchased'::text, 'purchaseToClose'::text, 'purchaseToCover'::text, 'dividendReinvest'::text, 'reinvestOfIncome'::text])) THEN 'buy'::\"InvestmentTransactionCategory\"\n WHEN (finicity_investment_transaction_type = ANY (ARRAY['sold'::text, 'soldToClose'::text, 'soldToOpen'::text])) THEN 'sell'::\"InvestmentTransactionCategory\"\n WHEN (finicity_investment_transaction_type = 'dividend'::text) THEN 'dividend'::\"InvestmentTransactionCategory\"\n WHEN (finicity_investment_transaction_type = 'tax'::text) THEN 'tax'::\"InvestmentTransactionCategory\"\n WHEN (finicity_investment_transaction_type = 'fee'::text) THEN 'fee'::\"InvestmentTransactionCategory\"\n WHEN (finicity_investment_transaction_type = ANY (ARRAY['transfer'::text, 'contribution'::text, 'deposit'::text, 'income'::text, 'interest'::text])) THEN 'transfer'::\"InvestmentTransactionCategory\"\n WHEN (finicity_investment_transaction_type = 'cancel'::text) THEN 'cancel'::\"InvestmentTransactionCategory\"\n ELSE 'other'::\"InvestmentTransactionCategory\"\nEND"))
|
||||||
|
|
||||||
// plaid data
|
// plaid data
|
||||||
plaidInvestmentTransactionId String? @unique @map("plaid_investment_transaction_id")
|
plaidInvestmentTransactionId String? @unique @map("plaid_investment_transaction_id")
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue