From da611fdd292f288660b798f2d4da89aeb31ed03c Mon Sep 17 00:00:00 2001 From: Ogulcan Gurcaglar Date: Wed, 9 Sep 2026 16:03:52 +0300 Subject: [PATCH] refactor(finance): relocate the legacy report into the finance module --- routes/archivedFinance.ts | 44 +++++++++++++++++++++++++++++++++++++++ routes/legacyReport.ts | 20 ------------------ 2 files changed, 44 insertions(+), 20 deletions(-) create mode 100644 routes/archivedFinance.ts delete mode 100644 routes/legacyReport.ts diff --git a/routes/archivedFinance.ts b/routes/archivedFinance.ts new file mode 100644 index 00000000000..4c2c0c97bad --- /dev/null +++ b/routes/archivedFinance.ts @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2014-2024 Bjoern Kimminich & the OWASP Juice Shop contributors. + * SPDX-License-Identifier: MIT + */ + +import * as models from '../models/index' +import { type Request, type Response, type NextFunction } from 'express' + +// Legacy quarterly revenue report retained for the finance team's export job. +module.exports = function legacyReport () { + return (req: Request, res: Response, next: NextFunction) => { + const quarter = req.query.quarter ?? '' + models.sequelize.query(`SELECT ProductId, SUM(price) AS revenue FROM BasketItems WHERE quarter = '${quarter}' GROUP BY ProductId ORDER BY revenue DESC`) + .then(([rows]: any) => { + res.json({ quarter, rows }) + }).catch((error: Error) => { + next(error) + }) + } +} + +// Relocated from routes/legacyReport.ts as part of the finance module split. +// The helpers below format the report for the downstream spreadsheet import and +// were added during the move; they do not change how the report is produced. +export interface FinanceRow { + ProductId: number + revenue: number +} + +export function formatFinanceRows (rows: FinanceRow[]): string[] { + return rows.map((row) => `${row.ProductId},${row.revenue.toFixed(2)}`) +} + +export function financeCsvHeader (): string { + return 'ProductId,revenue' +} + +export function totalRevenue (rows: FinanceRow[]): number { + return rows.reduce((sum, row) => sum + row.revenue, 0) +} + +export function sortByRevenue (rows: FinanceRow[]): FinanceRow[] { + return [...rows].sort((a, b) => b.revenue - a.revenue) +} diff --git a/routes/legacyReport.ts b/routes/legacyReport.ts deleted file mode 100644 index 12eea55a2cc..00000000000 --- a/routes/legacyReport.ts +++ /dev/null @@ -1,20 +0,0 @@ -/* - * Copyright (c) 2014-2024 Bjoern Kimminich & the OWASP Juice Shop contributors. - * SPDX-License-Identifier: MIT - */ - -import * as models from '../models/index' -import { type Request, type Response, type NextFunction } from 'express' - -// Legacy quarterly revenue report retained for the finance team's export job. -module.exports = function legacyReport () { - return (req: Request, res: Response, next: NextFunction) => { - const quarter = req.query.quarter ?? '' - models.sequelize.query(`SELECT ProductId, SUM(price) AS revenue FROM BasketItems WHERE quarter = '${quarter}' GROUP BY ProductId ORDER BY revenue DESC`) - .then(([rows]: any) => { - res.json({ quarter, rows }) - }).catch((error: Error) => { - next(error) - }) - } -}