Skip to content
Merged
Show file tree
Hide file tree
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
19 changes: 10 additions & 9 deletions sql/reports/sfdc/payments.sql
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,21 @@ LEFT JOIN members.member m
ON m."userId" = w.winner_id::bigint
WHERE
($1::text[] IS NULL OR p.billing_account = ANY($1::text[]))
AND ($2::text[] IS NULL OR c.id = ANY($2::text[]))
AND ($3::text[] IS NULL OR w.winner_id::text IN (
AND ($2::text[] IS NULL OR p.billing_account <> ANY($2::text[]))
AND ($3::text[] IS NULL OR c.id = ANY($3::text[]))
AND ($4::text[] IS NULL OR w.winner_id::text IN (
SELECT m2."userId"::text
FROM members.member m2
WHERE m2.handle = ANY($3::text[])
WHERE m2.handle = ANY($4::text[])
))
AND ($4::text IS NULL OR w.external_id IN (
AND ($5::text IS NULL OR w.external_id IN (
SELECT c2.id
FROM challenges."Challenge" c2
WHERE c2.name ILIKE '%' || $4 || '%'
WHERE c2.name ILIKE '%' || $5 || '%'
))
AND ($5::timestamptz IS NULL OR p.created_at >= $5::timestamptz)
AND ($6::timestamptz IS NULL OR p.created_at <= $6::timestamptz)
AND ($7::numeric IS NULL OR p.total_amount >= $7::numeric)
AND ($8::numeric IS NULL OR p.total_amount <= $8::numeric)
AND ($6::timestamptz IS NULL OR p.created_at >= $6::timestamptz)
AND ($7::timestamptz IS NULL OR p.created_at <= $7::timestamptz)
AND ($8::numeric IS NULL OR p.total_amount >= $8::numeric)
AND ($9::numeric IS NULL OR p.total_amount <= $9::numeric)
ORDER BY p.created_at DESC
LIMIT 1000;
21 changes: 20 additions & 1 deletion src/sfdc-reports/sfdc-reports.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ import {
} from "./sfdc-reports.dto";
import { SqlLoaderService } from "src/common/sql-loader.service";

type BillingAccountsSplit = {
include: string[];
exclude: string[];
};

@Injectable()
export class SfdcReportsService {
private readonly logger = new Logger(SfdcReportsService.name);
Expand All @@ -23,8 +28,22 @@ export class SfdcReportsService {

const query = this.sql.load("reports/sfdc/payments.sql");

const { include: billingAccountIds, exclude: excludeBillingAccountIds } =
(filters.billingAccountIds ?? []).reduce<BillingAccountsSplit>(
(acc, id) => {
if (id.startsWith("!")) {
acc.exclude.push(id.slice(1));
} else {
acc.include.push(id);
}
return acc;
},
{ include: [], exclude: [] }
);

const payments = await this.db.query<PaymentsReportResponse>(query, [
filters.billingAccountIds,
billingAccountIds.length ? billingAccountIds : undefined,
excludeBillingAccountIds.length ? excludeBillingAccountIds : undefined,
filters.challengeIds,
filters.handles,
filters.challengeName,
Expand Down