Skip to content

Conversation

@hdiniz
Copy link
Contributor

@hdiniz hdiniz commented Jan 21, 2026

@hdiniz hdiniz self-assigned this Jan 21, 2026
@hdiniz hdiniz marked this pull request as ready for review January 21, 2026 13:19
@hdiniz hdiniz requested a review from gustavlrsn January 21, 2026 13:19
Copy link
Member

@gustavlrsn gustavlrsn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good! Added a question about possible performance issues

Comment on lines 557 to 579
if (args.chargedDateFrom || args.chargedDateTo) {
where[Op.and].push(
sequelize.where(sequelize.literal(`COALESCE("Subscription"."lastChargedAt", "Order"."createdAt")`), {
[Op.gte]: args.chargedDateFrom,
}),
);
}
if (args.chargedDateTo) {
where[Op.and].push(
sequelize.where(sequelize.literal(`COALESCE("Subscription"."lastChargedAt", "Order"."createdAt")`), {
[Op.lte]: args.chargedDateTo,
}),
sequelize.literal(
SequelizeUtils.formatNamedParameters(
`EXISTS (
SELECT 1 FROM "Transactions" t
WHERE t."OrderId" = "Order"."id" AND
${args.chargedDateFrom ? `COALESCE(t."clearedAt", t."createdAt") >= :chargedDateFrom AND` : ''}
${args.chargedDateTo ? `COALESCE(t."clearedAt", t."createdAt") <= :chargedDateTo AND` : ''}
t."kind" in ('CONTRIBUTION', 'ADDED_FUNDS') AND
t."type" = 'CREDIT' AND
NOT t."isRefund" AND
t."RefundTransactionId" IS NULL AND
t."deletedAt" IS NULL
LIMIT 1
)`,
{
chargedDateFrom: args.chargedDateFrom,
chargedDateTo: args.chargedDateTo,
},
'postgres',
),
),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the performance impact of this subquery?

If there is not already an index for this, maybe we could add one, or maybe use the existing condition when you only supply chargedDateFrom without chargedDateTo

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested the generated query on prod and the result is not great (~5s), I am trying something else I will update this PR with a better query strategy.

Comment on lines 655 to 656
dateTo: moment(args.dateTo).utc().toISOString(),
dateFrom: moment(args.dateFrom).utc().toISOString(),

This comment was marked as outdated.

Comment on lines 662 to 665
{
chargedDateFrom: args.chargedDateFrom,
chargedDateTo: args.chargedDateTo,
},

This comment was marked as outdated.

identifierField: leftKey,
} as unknown as Association<Model, Model>,
_pseudo: true,
required: true,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be optional, the filtering nature of this include should be kept within the main query and not implicitly defined by the helper function.

Comment on lines 655 to 656
${args.chargedDateFrom ? `COALESCE(t."clearedAt", t."createdAt") >= :chargedDateFrom AND` : ''}
${args.chargedDateTo ? `COALESCE(t."clearedAt", t."createdAt") <= :chargedDateTo AND` : ''}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use the helper ifStr.

},
models.Order,
'id',
{ required: true },

This comment was marked as outdated.

Comment on lines 656 to 658
dateTo: moment(args.dateTo).utc().toISOString(),
dateFrom: moment(args.dateFrom).utc().toISOString(),
},

This comment was marked as outdated.

Comment on lines +656 to +662
${ifStr(args.chargedDateFrom, `COALESCE(t."clearedAt", t."createdAt") >= :chargedDateFrom AND`)}
${ifStr(args.chargedDateTo, `COALESCE(t."clearedAt", t."createdAt") <= :chargedDateTo AND`)}
t."type" = 'CREDIT' AND
NOT t."isRefund" AND
t."RefundTransactionId" IS NULL AND
t."deletedAt" IS NULL
${ifStr(host?.id, `AND t."HostCollectiveId" = :hostCollectiveId`)}

This comment was marked as outdated.

Comment on lines +665 to +666
chargedDateFrom: args.chargedDateFrom,
chargedDateTo: args.chargedDateTo,

This comment was marked as outdated.

@hdiniz hdiniz requested a review from gustavlrsn February 10, 2026 17:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow filtering contributions by their charge date history Create graphql query returning contribution report time series

3 participants