Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hasDebt parameter on Transactions resolver #10530

Merged
merged 1 commit into from
Dec 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions server/graphql/v2/query/collection/TransactionsCollectionQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { cloneDeep, flatten, intersection, isEmpty, isNil, pick, uniq } from 'lo
import type { Order as SequelizeOrder } from 'sequelize';

import { CollectiveType } from '../../../../constants/collectives';
import { TransactionKind } from '../../../../constants/transaction-kind';
import cache, { memoize } from '../../../../lib/cache';
import { buildSearchConditions } from '../../../../lib/sql-search';
import { parseToBoolean } from '../../../../lib/utils';
Expand Down Expand Up @@ -45,6 +46,8 @@ const LEDGER_ORDERED_TRANSACTIONS_FIELDS = {
),
};

const { PLATFORM_TIP, HOST_FEE_SHARE } = TransactionKind;

export const getTransactionKindPriorityCase = tableName => `
CASE
WHEN "${tableName}"."kind" IN ('CONTRIBUTION', 'EXPENSE', 'ADDED_FUNDS', 'BALANCE_TRANSFER', 'PREPAID_PAYMENT_METHOD') THEN 1
Expand Down Expand Up @@ -127,6 +130,10 @@ export const TransactionsCollectionArgs = {
type: GraphQLString,
description: 'The term to search',
},
hasDebt: {
type: GraphQLBoolean,
description: 'If true, return transactions with debt attached, if false transactions without debt attached.',
},
hasExpense: {
type: GraphQLBoolean,
description: 'Only return transactions with an Expense attached',
Expand Down Expand Up @@ -497,6 +504,25 @@ export const TransactionsCollectionResolver = async (
where.push({ isRefund: args.isRefund });
}

if (args.hasDebt !== undefined) {
const hasDebtSubquery = `SELECT id FROM "Transactions" as "DebtTransaction"
WHERE "DebtTransaction"."TransactionGroup" = "Transaction"."TransactionGroup"
AND ("DebtTransaction".kind)::text = CONCAT(("Transaction"."kind")::text, '_DEBT')
AND "DebtTransaction"."type" != "Transaction"."type"
AND "DebtTransaction"."deletedAt" IS NULL`;
if (args.hasDebt === true) {
where.push({ kind: [PLATFORM_TIP, HOST_FEE_SHARE] }); // Need to be this kind to have debt
where.push(sequelize.literal(`EXISTS (${hasDebtSubquery})`));
} else if (args.hasDebt === false) {
where.push(
sequelize.or(
{ kind: { [Op.not]: [PLATFORM_TIP, HOST_FEE_SHARE] } }, // No Debt if not this kind
sequelize.literal(`NOT EXISTS (${hasDebtSubquery})`),
),
);
}
}

/*
Ordering of transactions by
- createdAt (rounded by a 10s interval): to treat very close timestamps as the same to defer ordering to transaction group, kind and type
Expand Down
Loading