Skip to content

Commit

Permalink
Add some resolvers to Export Host Collectives CSV (#10399)
Browse files Browse the repository at this point in the history
* feat: add unhostedAt and unfrozenAt resolvers

* feat: add HostedAccountSummary resolver

* chore: update schemas

* fix: hostedAccountSummary currency group
  • Loading branch information
kewitz authored Oct 22, 2024
1 parent 236ca9e commit 2a96d5b
Show file tree
Hide file tree
Showing 6 changed files with 468 additions and 19 deletions.
42 changes: 42 additions & 0 deletions server/graphql/loaders/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
sumCollectivesTransactions,
} from '../../lib/budget';
import { getFxRate } from '../../lib/currency';
import { ifStr } from '../../lib/utils';
import models, { Op, sequelize } from '../../models';

import { generateTotalAccountHostAgreementsLoader } from './agreements';
Expand Down Expand Up @@ -466,6 +467,47 @@ export const loaders = req => {

return sortResultsSimple(collectiveIds, stats, row => row.CollectiveId);
}),
hostedAccountSummary: {
buildLoader: ({ dateFrom, dateTo } = {}) =>
new DataLoader(async collectiveIds => {
const stats = await sequelize.query(
`
SELECT
t."CollectiveId",
t."hostCurrency",
COUNT(t.id) FILTER (WHERE t.kind = 'EXPENSE' AND t.type = 'DEBIT') AS "expenseCount",
SUM(ABS(t."amountInHostCurrency")) FILTER (WHERE t.kind = 'EXPENSE' AND t.type = 'DEBIT') AS "expenseTotal",
MAX(ABS(t."amountInHostCurrency")) FILTER (WHERE t.kind = 'EXPENSE' AND t.type = 'DEBIT') AS "expenseMaxValue",
COUNT(DISTINCT t."FromCollectiveId") FILTER (WHERE t.kind = 'EXPENSE' AND t.type = 'DEBIT') AS "expenseDistinctPayee",
COUNT(t.id) FILTER (WHERE t.kind IN ('CONTRIBUTION', 'ADDED_FUNDS') AND t.type = 'CREDIT') AS "contributionCount",
SUM(t."amountInHostCurrency") FILTER (WHERE t.kind IN ('CONTRIBUTION', 'ADDED_FUNDS') AND t.type = 'CREDIT') AS "contributionTotal",
SUM(ABS(t."amountInHostCurrency")) FILTER (WHERE t.kind IN ('HOST_FEE') AND t.type = 'DEBIT') AS "hostFeeTotal",
SUM(ABS(t."amountInHostCurrency")) FILTER (WHERE t.kind IN ('CONTRIBUTION', 'EXPENSE') AND t.type = 'DEBIT') AS "spentTotal"
FROM
"Transactions" t
INNER JOIN "Collectives" c ON t."CollectiveId" = c.id
WHERE t."CollectiveId" IN (:collectiveIds)
AND t."deletedAt" IS NULL
${ifStr(dateFrom, 'AND t."createdAt" > :dateFrom')}
${ifStr(dateTo, 'AND t."createdAt" <= :dateTo')}
AND t."HostCollectiveId" = c."HostCollectiveId"
GROUP BY
t."CollectiveId", t."hostCurrency"
`,
{
replacements: {
collectiveIds,
dateFrom,
dateTo,
},
type: sequelize.QueryTypes.SELECT,
raw: true,
},
);

return sortResultsSimple(collectiveIds, stats, row => row.CollectiveId);
}),
},
};

/** *** Tier *****/
Expand Down
Loading

0 comments on commit 2a96d5b

Please sign in to comment.