-
Notifications
You must be signed in to change notification settings - Fork 0
Fix CSV downloads for challenge reports #50
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,7 +5,6 @@ const path = require("path"); | |
| const { Pool } = require("pg"); | ||
|
|
||
| const CSV_COLUMNS = [ | ||
| "payment.payment_id", | ||
| "payee.handle", | ||
| "payee.email", | ||
| "payee.first_name", | ||
|
|
@@ -336,6 +335,11 @@ function writeCsv(outputPath, rows) { | |
| fs.writeFileSync(outputPath, `${lines.join("\n")}\n`, "utf8"); | ||
| } | ||
|
|
||
| function toNumberOrZero(value) { | ||
| const parsed = Number(value); | ||
| return Number.isFinite(parsed) ? parsed : 0; | ||
| } | ||
|
|
||
| async function run() { | ||
| const options = parseArgs(process.argv.slice(2)); | ||
| if (options.help) { | ||
|
|
@@ -436,7 +440,45 @@ async function run() { | |
| }; | ||
| }); | ||
|
|
||
| writeCsv(outputPath, mergedRows); | ||
| const aggregatedByUser = new Map(); | ||
| for (const row of mergedRows) { | ||
| const userId = String(row.__user_id); | ||
| const existing = aggregatedByUser.get(userId); | ||
|
|
||
| if (!existing) { | ||
| aggregatedByUser.set(userId, { | ||
| ...row, | ||
| "user_payment.gross_amount": toNumberOrZero( | ||
| row["user_payment.gross_amount"], | ||
| ), | ||
| "user_payment.net_amount": toNumberOrZero(row["user_payment.net_amount"]), | ||
| }); | ||
| continue; | ||
| } | ||
|
|
||
| existing["user_payment.gross_amount"] = | ||
| toNumberOrZero(existing["user_payment.gross_amount"]) + | ||
| toNumberOrZero(row["user_payment.gross_amount"]); | ||
| existing["user_payment.net_amount"] = | ||
| toNumberOrZero(existing["user_payment.net_amount"]) + | ||
| toNumberOrZero(row["user_payment.net_amount"]); | ||
| } | ||
|
|
||
| const aggregatedRows = Array.from(aggregatedByUser.values()) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [💡 |
||
| .map((row) => { | ||
| const normalized = { ...row }; | ||
| delete normalized.__user_id; | ||
| delete normalized.__payment_id; | ||
| delete normalized["payment.payment_id"]; | ||
| return normalized; | ||
| }) | ||
| .sort((a, b) => | ||
| String(a["payee.handle"] ?? "").localeCompare( | ||
| String(b["payee.handle"] ?? ""), | ||
| ), | ||
| ); | ||
|
|
||
| writeCsv(outputPath, aggregatedRows); | ||
| console.log(`[member-tax-export] Wrote CSV: ${outputPath}`); | ||
| } finally { | ||
| await Promise.allSettled([mainPool.end(), oldPaymentsPool.end()]); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,14 @@ | ||
| import { Controller, Get, Query, UseGuards } from "@nestjs/common"; | ||
| import { | ||
| Controller, | ||
| Get, | ||
| Query, | ||
| UseGuards, | ||
| UseInterceptors, | ||
| } from "@nestjs/common"; | ||
| import { | ||
| ApiBearerAuth, | ||
| ApiOperation, | ||
| ApiProduces, | ||
| ApiResponse, | ||
| ApiTags, | ||
| } from "@nestjs/swagger"; | ||
|
|
@@ -11,6 +18,7 @@ import { PaymentsReportResponse } from "../sfdc/sfdc-reports.dto"; | |
| import { Scopes } from "../../auth/decorators/scopes.decorator"; | ||
| import { Scopes as AppScopes } from "../../app-constants"; | ||
| import { ChallengesReportsService } from "./challenges-reports.service"; | ||
| import { CsvResponseInterceptor } from "../../common/interceptors/csv-response.interceptor"; | ||
| import { ChallengeRegistrantsQueryDto } from "./dtos/registrants.dto"; | ||
| import { ChallengesReportQueryDto } from "./dtos/challenge.dto"; | ||
| import { | ||
|
|
@@ -19,6 +27,8 @@ import { | |
| } from "./dtos/submission-links.dto"; | ||
|
|
||
| @ApiTags("Challenges Reports") | ||
| @ApiProduces("application/json", "text/csv") | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [ |
||
| @UseInterceptors(CsvResponseInterceptor) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [ |
||
| @Controller("/challenges") | ||
| export class ChallengesReportsController { | ||
| constructor(private readonly reportsService: ChallengesReportsService) {} | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,17 @@ | ||
| import { Module } from "@nestjs/common"; | ||
| import { CsvSerializer } from "src/common/csv/csv-serializer"; | ||
| import { SqlLoaderService } from "src/common/sql-loader.service"; | ||
| import { CsvResponseInterceptor } from "src/common/interceptors/csv-response.interceptor"; | ||
| import { ChallengesReportsController } from "./challenges-reports.controller"; | ||
| import { ChallengesReportsService } from "./challenges-reports.service"; | ||
|
|
||
| @Module({ | ||
| controllers: [ChallengesReportsController], | ||
| providers: [ChallengesReportsService, SqlLoaderService], | ||
| providers: [ | ||
| ChallengesReportsService, | ||
| SqlLoaderService, | ||
| CsvSerializer, | ||
| CsvResponseInterceptor, | ||
| ], | ||
| }) | ||
| export class ChallengesReportsModule {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[⚠️
maintainability]The
toNumberOrZerofunction is introduced to handle conversion of values to numbers with a fallback to zero. Ensure that this function is used consistently throughout the codebase wherever similar conversions are needed to maintain uniformity and prevent potential errors from inconsistent handling.