gopalswami's picture
Remove name from updateonduplicate condition from gl account service
fbb83a2
import { logger } from '../../utils/logger';
import { fetchGLAccountsForSync } from './propertyware.service';
import PWGlaccounts from '../../models/pwGlaccounts';
const ExpenseAccountTypes = ['Expense', 'Income', 'Current Liability', 'Current Asset', 'Equity'];
const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms));
export const syncGlAccountsFromJsonService = async (): Promise<string> => {
const allGlAccounts = [];
let offset = 0;
const limit = 500;
const includeDeactivated = false;
try {
let totalRecords = 0;
let response;
do {
response = await fetchGLAccountsForSync({ limit, offset, includeDeactivated });
allGlAccounts.push(...response.data);
if (response.headers && response.headers['x-total-count']) {
totalRecords = parseInt(response.headers['x-total-count'], 10);
}
offset += limit;
await sleep(1000);
} while (allGlAccounts.length < totalRecords);
const glAccountsResponseData = allGlAccounts
// .filter((account) => ExpenseAccountTypes.includes(account.accountType))
.map((account) => ({
pw_id: account.id,
name: account.name,
account_number: account.accountNumber
}));
await PWGlaccounts.bulkCreate(glAccountsResponseData, {
updateOnDuplicate: ['account_number'],
});
logger.info("Total " + glAccountsResponseData.length + " GL Accounts synced successfully");
return "Total " + glAccountsResponseData.length + " GL Accounts synced successfully";
} catch (error) {
logger.error("Error syncing Gl Accounts");
logger.error(error);
throw new Error("Error syncing Gl Accounts");
}
};