Skip to content

Commit

Permalink
chore: Investigate on circular dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
Betree committed Dec 12, 2024
1 parent 574b65d commit 38a6214
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 74 deletions.
17 changes: 17 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -395,3 +395,20 @@ jobs:
github-token: ${{ secrets.GITHUB_TOKEN }}
schema: 'main:server/graphql/schemaV2.graphql'
fail-on-breaking: false

circular-dependencies:
runs-on: ubuntu-latest
timeout-minutes: 15

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Setup node
uses: actions/setup-node@v4
with:
node-version-file: 'package.json'
cache: 'npm'

- name: Check circular dependencies
run: npm run dependencies:check-circular
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@
"db:seed": "./scripts/sequelize.sh db:seed",
"db:setup": "babel-node --extensions .js,.ts ./scripts/db_setup.js",
"depcheck": "npx @opencollective/depcheck .",
"dependencies:check-circular": "npx madge --circular --extensions ts,js server",
"deploy:production": "./scripts/pre-deploy.sh production && git push production main",
"deploy:staging": "./scripts/pre-deploy.sh staging && git push -f staging main",
"dev": "nodemon server/index.js -x \"babel-node --extensions .js,.ts\" . -e js,hbs,ts",
Expand Down
3 changes: 2 additions & 1 deletion reports/host-report.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ import { generateHostFeeAmountForTransactionLoader } from '../server/graphql/loa
import { getHostTransactionsCsvAsAdmin } from '../server/lib/csv';
import emailLib from '../server/lib/email';
import { getBackersStats, getHostedCollectives, sumTransactions } from '../server/lib/hostlib';
import { exportToPDF } from '../server/lib/pdf.js';

Check failure on line 13 in reports/host-report.js

View workflow job for this annotation

GitHub Actions / lint

Unable to resolve path to module '../server/lib/pdf.js'
import { stripHTML } from '../server/lib/sanitize-html';
import { reportErrorToSentry, reportMessageToSentry } from '../server/lib/sentry';
import { getPaidTaxTransactions, getTaxesSummary, getTransactions } from '../server/lib/transactions';
import { exportToPDF, sumByWhen } from '../server/lib/utils';
import { sumByWhen } from '../server/lib/utils';
import models, { Op, sequelize } from '../server/models';

const debug = debugLib('hostreport');
Expand Down
7 changes: 0 additions & 7 deletions server/graphql/v2/object/SearchResponse.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
import { GraphQLNonNull, GraphQLObjectType } from 'graphql';

import { getSearchResultFields } from '../../../lib/elastic-search/graphql-search';
import { GraphQLAccountTypeKeys } from '../enum/AccountType';

export type SearchQueryAccountsResolverArgs = {
type: GraphQLAccountTypeKeys;
isHost: boolean;
tags: string[];
};

const GraphQLSearchResults = new GraphQLObjectType({
name: 'SearchResults',
Expand Down
8 changes: 7 additions & 1 deletion server/lib/elastic-search/graphql-search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ import { GraphQLTierCollection } from '../../graphql/v2/collection/TierCollectio
import { GraphQLTransactionCollection } from '../../graphql/v2/collection/TransactionCollection';
import { GraphQLUpdateCollection } from '../../graphql/v2/collection/UpdateCollection';
import { AccountTypeToModelMapping, GraphQLAccountType } from '../../graphql/v2/enum';
import { GraphQLAccountTypeKeys } from '../../graphql/v2/enum/AccountType';
import { idEncode } from '../../graphql/v2/identifiers';
import type { SearchQueryAccountsResolverArgs } from '../../graphql/v2/object/SearchResponse';
import { Collective, User } from '../../models';

import { ElasticSearchIndexName, ElasticSearchIndexParams } from './constants';
Expand All @@ -40,6 +40,12 @@ type GraphQLSearchParams = {
host: Collective;
};

export type SearchQueryAccountsResolverArgs = {
type: GraphQLAccountTypeKeys;
isHost: boolean;
tags: string[];
};

/**
* Returns a unique identifier for the ElasticSearch query, which can be used to batch multiple queries together.
*/
Expand Down
64 changes: 64 additions & 0 deletions server/lib/pdf.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import fs from 'fs';
import path from 'path';

import config from 'config';
import pdf from 'html-pdf';
import { get } from 'lodash';
import moment from 'moment';

Expand All @@ -7,10 +11,70 @@ import { USTaxFormType } from '../models/LegalDocument';

import { TOKEN_EXPIRATION_PDF } from './auth';
import { fetchWithTimeout } from './fetch';
import handlebars from './handlebars';
import logger from './logger';
import { reportErrorToSentry, reportMessageToSentry } from './sentry';
import { parseToBoolean } from './utils';

/**
* export transactions to PDF
*/
export function exportToPDF(template, data, options) {
options = options || {};
options.paper = options.paper || 'Letter'; // Letter for US or A4 for Europe

let paperSize;

switch (options.paper) {
case 'A4':
paperSize = {
width: '210mm',
height: '297mm',
margin: {
top: '10mm',
left: '10mm',
},
};
break;
case 'Letter':
default:
paperSize = {
width: '8.5in',
height: '11in',
margin: {
top: '0.4in',
left: '0.4in',
},
};
break;
}

data.paperSize = paperSize;
options.paperSize = paperSize;

const templateFilepath = path.resolve(__dirname, `../../templates/pdf/${template}.hbs`);
const source = fs.readFileSync(templateFilepath, 'utf8');
const render = handlebars.compile(source);

const html = render(data);

if (options.format === 'html') {
return Promise.resolve(html);
}
options.format = options.paper;

options.timeout = 60000;

return new Promise((resolve, reject) => {
pdf.create(html, options).toBuffer((err, buffer) => {
if (err) {
return reject(err);
}
return resolve(buffer);
});
});
}

export const getTransactionPdf = async (transaction, user) => {
if (parseToBoolean(config.pdfService.fetchTransactionsReceipts) === false) {
return;
Expand Down
64 changes: 0 additions & 64 deletions server/lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
import crypto from 'crypto';
import fs from 'fs';
import path from 'path';
import { URL } from 'url';

import config from 'config';
import fastRedact from 'fast-redact';
import pdf from 'html-pdf';
import { filter, get, isObject, omit, padStart, sumBy } from 'lodash';
import moment from 'moment';
import pFilter from 'p-filter';

import { ZERO_DECIMAL_CURRENCIES } from '../constants/currencies';

import handlebars from './handlebars';

export function addParamsToUrl(url, obj) {
const u = new URL(url);
Object.keys(obj).forEach(key => {
Expand Down Expand Up @@ -206,65 +201,6 @@ export function exportToCSV(data, attributes, getColumnName = attr => attr, proc
return lines.join('\n');
}

/**
* export transactions to PDF
*/
export function exportToPDF(template, data, options) {
options = options || {};
options.paper = options.paper || 'Letter'; // Letter for US or A4 for Europe

let paperSize;

switch (options.paper) {
case 'A4':
paperSize = {
width: '210mm',
height: '297mm',
margin: {
top: '10mm',
left: '10mm',
},
};
break;
case 'Letter':
default:
paperSize = {
width: '8.5in',
height: '11in',
margin: {
top: '0.4in',
left: '0.4in',
},
};
break;
}

data.paperSize = paperSize;
options.paperSize = paperSize;

const templateFilepath = path.resolve(__dirname, `../../templates/pdf/${template}.hbs`);
const source = fs.readFileSync(templateFilepath, 'utf8');
const render = handlebars.compile(source);

const html = render(data);

if (options.format === 'html') {
return Promise.resolve(html);
}
options.format = options.paper;

options.timeout = 60000;

return new Promise((resolve, reject) => {
pdf.create(html, options).toBuffer((err, buffer) => {
if (err) {
return reject(err);
}
return resolve(buffer);
});
});
}

export const isValidEmail = email => {
if (typeof email !== 'string') {
return false;
Expand Down
3 changes: 2 additions & 1 deletion test/server/lib/utils.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { assert, expect } from 'chai';

import { exportToPDF, redactSensitiveFields } from '../../../server/lib/utils';
import { exportToPDF } from '../../../server/lib/pdf';
import { redactSensitiveFields } from '../../../server/lib/utils';

describe('server/lib/utils', () => {
it('redacts sensitive fields', () => {
Expand Down

0 comments on commit 38a6214

Please sign in to comment.