Skip to content

Commit

Permalink
refact: rewrite some files in ts
Browse files Browse the repository at this point in the history
  • Loading branch information
kewitz committed Oct 3, 2024
1 parent f7e0214 commit c76adb8
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 13 deletions.
96 changes: 96 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@
"devDependencies": {
"@babel/cli": "^7.23.4",
"@graphql-eslint/eslint-plugin": "^3.20.1",
"@types/express": "^5.0.0",
"@types/lodash": "^4.17.9",
"@typescript-eslint/eslint-plugin": "^7.10.0",
"@typescript-eslint/parser": "^7.10.0",
"commitizen": "^4.3.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Parser } from '@json2csv/plainjs';
import type { RequestHandler } from 'express';
import gqlV2 from 'graphql-tag';
import { difference, get, head, intersection, isNil, pick, toUpper, trim } from 'lodash';
import moment from 'moment';
Expand Down Expand Up @@ -454,16 +455,21 @@ const applyMapping = (mapping, row) => {
return res;
};

/**
* @param {import('express').Request} req
* @param {import('express').Response} res
*/
const accountTransactions = async (req, res) => {
type Params = {
slug: string;
reportType: 'hostTransactions' | 'transactions';
type?: 'credit' | 'debit';
kind?: string;
format: 'json' | 'csv' | 'txt';
};

const accountTransactions: RequestHandler<Params> = async (req, res) => {
if (!['HEAD', 'GET'].includes(req.method)) {
return res.status(405).send({ error: { message: 'Method not allowed' } });
res.status(405).send({ error: { message: 'Method not allowed' } });
return;
}

const variables = pick({ ...req.params, ...req.query }, [
const variables: any = pick({ ...req.params, ...req.query }, [
'account',
'accountingCategory',
'dateFrom',
Expand Down Expand Up @@ -627,18 +633,18 @@ const accountTransactions = async (req, res) => {
variables.fullDescription = req.params.reportType === 'hostTransactions' ? true : false;
}

let fields = get(req.query, 'fields', '')
let fields = (get(req.query, 'fields', '') as string)
.split(',')
.map(trim)
.filter((v) => !!v);

if (fields.length === 0) {
const remove = get(req.query, 'remove', '')
const remove = (get(req.query, 'remove', '') as string)
.split(',')
.map(trim)
.filter((v) => !!v);

const add = get(req.query, 'add', '')
const add = (get(req.query, 'add', '') as string)
.split(',')
.map(trim)
.filter((v) => !!v);
Expand Down Expand Up @@ -714,7 +720,8 @@ const accountTransactions = async (req, res) => {
res.append('Access-Control-Expose-Headers', 'X-Exported-Rows');
res.append('X-Exported-Rows', result.transactions.totalCount);
if (req.method === 'HEAD') {
return res.status(200).end();
res.status(200).end();
return;
}

if (result.transactions.totalCount === 0) {
Expand All @@ -725,7 +732,7 @@ const accountTransactions = async (req, res) => {
const mapping = pick(csvMapping, fields);

const mappedTransactions = result.transactions.nodes.map((t) => applyMapping(mapping, t));
res.write(json2csv(mappedTransactions));
res.write(json2csv(mappedTransactions, {}));
res.write(`\n`);

if (result.transactions.totalCount > result.transactions.limit) {
Expand Down
File renamed without changes.
3 changes: 2 additions & 1 deletion src/server/routes.js → src/server/routes.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import cors from 'cors';
import type { Express } from 'express';

import { idOrUuid } from './lib/utils';
import controllers from './controllers';
Expand All @@ -8,7 +9,7 @@ const requireApiKey = (req, res, next) => {
next();
};

export const loadRoutes = (app) => {
export const loadRoutes = (app: Express) => {
app.use(cors());

app.get('/', (req, res) => {
Expand Down

0 comments on commit c76adb8

Please sign in to comment.