Skip to content
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

Update PDF util #4742

Merged
merged 1 commit into from
Oct 22, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
44 changes: 44 additions & 0 deletions server/lib/fetch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { get } from 'lodash';
import fetch from 'node-fetch';

import logger from './logger';

/**
* Make a fetch call with a timeout. Returns a thenable Promise.
* @param {String} url The url to fetch.
* @param {Object} [fetchOptions] The method, headers, and other options for node-fetch. Default method is GET.
* @param {Number} [fetchOptions.timeoutInMs] The timeout in ms. Default is 5000.
*/

export const fetchWithTimeout = (url, fetchOptions) => {
const timeoutInMs = get(fetchOptions, 'timeoutInMs', 5000);

return new Promise((resolve, reject) => {
let timedOut = false;

const timer = setTimeout(() => {
timedOut = true;
logger.warn(`Fetch request to ${url} timed out`);
return reject(new Error(`Fetch request to ${url} timed out`));
}, timeoutInMs);

fetch(url, fetchOptions)
.then(
response => {
if (!timedOut) {
logger.info(`Fetch request to ${url} successful`);
return resolve(response);
}
},
err => {
if (timedOut) {
return;
}
return reject(new Error(`Fetch error: ${err.message}`));
},
)
.finally(() => {
clearTimeout(timer);
});
});
};
9 changes: 6 additions & 3 deletions server/lib/payments.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { find, get, includes, omit, pick } from 'lodash';

import activities from '../constants/activities';
import status from '../constants/order_status';
import { PAYMENT_METHOD_TYPES } from '../constants/paymentMethods';
import roles from '../constants/roles';
import tiers from '../constants/tiers';
import { FEES_ON_TOP_TRANSACTION_PROPERTIES, OC_FEE_PERCENT } from '../constants/transactions';
Expand Down Expand Up @@ -394,7 +395,7 @@ const validatePayment = payment => {
const sendOrderConfirmedEmail = async (order, transaction) => {
let pdf;
const attachments = [];
const { collective, tier, interval, fromCollective } = order;
const { collective, tier, interval, fromCollective, paymentMethod } = order;
const user = order.createdByUser;
const host = await collective.getHostCollective();

Expand Down Expand Up @@ -428,8 +429,10 @@ const sendOrderConfirmedEmail = async (order, transaction) => {
subscriptionsLink: interval && `${config.host.website}/${fromCollective.slug}/recurring-contributions`,
};

// hit PDF service and get PDF
pdf = await getTransactionPdf(transaction, user);
// hit PDF service and get PDF (unless payment method type is gift card)
if (paymentMethod?.type !== PAYMENT_METHOD_TYPES.VIRTUALCARD) {
pdf = await getTransactionPdf(transaction, user);
}

// attach pdf
if (pdf) {
Expand Down
11 changes: 6 additions & 5 deletions server/lib/pdf.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import config from 'config';
import fetch from 'node-fetch';

import { TOKEN_EXPIRATION_LOGIN } from '../lib/auth';
import logger from '../lib/logger';
import { TOKEN_EXPIRATION_LOGIN } from './auth';
import { fetchWithTimeout } from './fetch';
import logger from './logger';

export const getTransactionPdf = async (transaction, user) => {
if (['ci', 'test'].includes(config.env)) {
Expand All @@ -13,7 +13,8 @@ export const getTransactionPdf = async (transaction, user) => {
const headers = {
Authorization: `Bearer ${accessToken}`,
};
return await fetch(pdfUrl, { method: 'get', headers })

return fetchWithTimeout(pdfUrl, { method: 'get', headers, timeoutInMs: 10000 })
.then(response => {
const { status } = response;
if (status >= 200 && status < 300) {
Expand All @@ -24,6 +25,6 @@ export const getTransactionPdf = async (transaction, user) => {
}
})
.catch(error => {
logger.error(`Error fetching PDF: ${error}`);
logger.error(`Error fetching PDF: ${error.message}`);
});
};