From bc56ed3f036d2e1da9715e30b70a47ba92112702 Mon Sep 17 00:00:00 2001 From: Bilal Catic Date: Fri, 4 Oct 2019 18:52:41 +0200 Subject: [PATCH] [NOT FINISHED] delete fees in chunks --- constants/constants.js | 7 ++++++- environment.env | 3 +++ services/officeRnD/fees.js | 31 ++++++++++++++++++++++++++----- 3 files changed, 35 insertions(+), 6 deletions(-) diff --git a/constants/constants.js b/constants/constants.js index e603879..90d1fdb 100644 --- a/constants/constants.js +++ b/constants/constants.js @@ -135,7 +135,10 @@ const CUSTOM_FEES_PREFIXES = process.env.CUSTOM_FEES_PREFIXES.split(',') const UNPAID_FEE_STATUS = 'not_paid'; -const ALLOW_SENDING_FEES = parseInt(process.env.ALLOW_SENDING_FEES_FOR_CURRENT_AND_FUTURE_MONTHS) ? true : false; +const ALLOW_SENDING_FEES = !!parseInt(process.env.ALLOW_SENDING_FEES_FOR_CURRENT_AND_FUTURE_MONTHS); + +const MAX_FEES_TO_DELETE = parseInt(process.env.MAX_FEES_TO_DELETE) || 10; +const FEES_DELETE_DELAY = parseInt(process.env.FEES_DELETE_DELAY) || 0; module.exports = { VALID_CSV_HEADERS, @@ -162,4 +165,6 @@ module.exports = { UNPAID_FEE_STATUS, CUSTOM_FEES_PREFIXES, ALLOW_SENDING_FEES, + MAX_FEES_TO_DELETE, + FEES_DELETE_DELAY }; diff --git a/environment.env b/environment.env index 22db46a..a09762e 100644 --- a/environment.env +++ b/environment.env @@ -41,6 +41,9 @@ CUSTOM_FEES_PREFIXES=Array of prefixes to recognize manually added fees. Comma-s ALLOW_SENDING_FEES_FOR_CURRENT_AND_FUTURE_MONTHS=0 - false => Sending fees is disabled for current and future months, 1 - true => Sending fees is never disabled +MAX_FEES_TO_DELETE=Max number of fees to delete (from ORD) in one API call +FEES_DELETE_DELAY=Number of miliseconds to wait between two API calls to delete fees (from ORD) + #More about pool option : http://docs.sequelizejs.com/class/lib/sequelize.js~Sequelize.html DB_POOL_MAX_CONNECTIONS=Maximum number of connection in pool (ex. 18) DB_POOL_ACQUIRE=The maximum time, in milliseconds, that pool will try to get connection before throwing error (ex. 120000) diff --git a/services/officeRnD/fees.js b/services/officeRnD/fees.js index 8833839..b3e3663 100644 --- a/services/officeRnD/fees.js +++ b/services/officeRnD/fees.js @@ -3,7 +3,14 @@ const moment = require('moment-timezone'); const { API } = require('../../helpers/api'); -const { officeRnDAPIErrors, DEFAULT_DATE_FORMAT, UNPAID_FEE_STATUS, CUSTOM_FEES_PREFIXES } = require('../../constants/constants'); +const { + officeRnDAPIErrors, + DEFAULT_DATE_FORMAT, + UNPAID_FEE_STATUS, + CUSTOM_FEES_PREFIXES, + MAX_FEES_TO_DELETE, + FEES_DELETE_DELAY, +} = require('../../constants/constants'); const deleteFeesFromORD = (dateRange, memberIds) => { return new Promise((resolve, reject) => { @@ -63,14 +70,28 @@ const deleteFeesFromORD = (dateRange, memberIds) => { } }); - API.delete('fees', { data: feeIdsToRemove }) + const asyncDeleteCalls = []; + + let i,j; + for (i=0, j=feeIdsToRemove; i resolve(true)).catch((error) => { + console.log('[Delete Fees From ORD] Error deleting fees from ORD : ', error); + reject(officeRnDAPIErrors.FAILED_TO_DELETE_FEES); + }); + + asyncDeleteCalls.push(deleteFeesPromise); + //sleep for FEES_DELETE_DELAY ms + } + + Promise.all(asyncDeleteCalls) .then(() => { resolve(feesToSkip); }) .catch((error) => { - console.log('[Delete Fees From ORD] Error deleting fees from ORD : ', error); - reject(officeRnDAPIErrors.FAILED_TO_DELETE_FEES); - }); + reject(error); + }) + }) .catch((error) => { console.log("[Delete Fees From ORD] Error fetching fees and plans from ORD : ", error);