Skip to content

Commit

Permalink
fix: sync polaris loan 's schedule (#5064)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mygmarsuren committed Mar 18, 2024
1 parent b5c497c commit caf5036
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 13 deletions.
20 changes: 20 additions & 0 deletions packages/plugin-loans-api/src/messageBroker.ts
Expand Up @@ -15,6 +15,15 @@ export const setupMessageConsumers = async () => {
};
});

consumeRPCQueue('loans:firstLoanSchedules.findOne', async ({ subdomain, data }) => {
const models = await generateModels(subdomain);

return {
status: 'success',
data: await models.FirstSchedules.findOne(data).lean(),
};
});

consumeRPCQueue('loans:contracts.update', async ({ subdomain, data }) => {
const models = await generateModels(subdomain);
const { selector, modifier } = data;
Expand Down Expand Up @@ -92,6 +101,17 @@ export const setupMessageConsumers = async () => {
status: 'success',
};
});
consumeRPCQueue(
'loans:firstLoanSchedules.insertMany',
async ({ subdomain,data }) => {
const models = await generateModels(subdomain);

return {
data: await models.FirstSchedules.insertMany(data),
status: 'success',
};
},
)
};

export const sendMessageBroker = async (
Expand Down
@@ -1,11 +1,10 @@
import { fetchPolaris } from '../utils';

export const getLoanDetail = async (subdomain, params) => {
const loanDetail = await fetchPolaris({
export const getLoanSchedule = async (subdomain, params) => {

return await fetchPolaris({
subdomain,
op: '13610200',
data: [params.number, 0],
op: '13610203',
data: [params.number],
}).then((response) => JSON.parse(response));

return loanDetail;
};
89 changes: 82 additions & 7 deletions packages/plugin-syncpolaris-api/src/utils/toSyncUtils/utils.ts
@@ -1,12 +1,14 @@

import { sendCommonMessage } from '../../messageBroker';
import { getCustomerDetail } from '../customer/getCustomerDetail';
import { getLoanDetail } from '../loan/getLoanDetail';
import { getLoanSchedule } from '../loan/getLoanSchedule';
import { getSavingDetail } from '../saving/getSavingDetail';
import {
getContract,
getCustomer,
updateContract,
updateCustomer,
updateCustomer
} from '../utils';

export const getCustomFields = async (subdomain, customFieldType, item?) => {
Expand Down Expand Up @@ -42,6 +44,7 @@ export const getCustomFields = async (subdomain, customFieldType, item?) => {
item: item || [],
};
};

export const dateNames = ['startDate', 'endDate'];
export const findDiffrentData = async (mainData, polarisData) => {
if (polarisData) {
Expand Down Expand Up @@ -137,12 +140,7 @@ export const syncDataToErxes = async (type, subdomain, item, updateData) => {
case 'contacts:customer':
return await updateCustomer(subdomain, { code: item.code }, updateData);
case 'loans:contract':
return updateContract(
subdomain,
{ number: item.number },
{ $set: updateData },
'loans',
);
return setLoanWithSchedule(subdomain, item, updateData);
case 'savings:contract':
return await updateContract(
subdomain,
Expand Down Expand Up @@ -171,3 +169,80 @@ export const getMainDatas = async (subdomain, type) => {
}
}
};

export const setLoanWithSchedule = async (subdomain, item, updateData) => {

await updateContract(
subdomain,
{ number: item.number },
{ $set: updateData },
'loans',
)
await preLoanSchedule(subdomain,item)
};


export const preLoanSchedule = async (subdomain, item) => {
try {

const mainLoanSchedule = await getMainLoanSchedule(subdomain, {contractId: item._id})
const loanSchedules = await getLoanSchedule(subdomain, { number: item.number })

if(!mainLoanSchedule && loanSchedules) {
await createLoanSchedule(subdomain,loanSchedules,item._id)
}
} catch (error) {
console.log('update schedule:',error)
}
};

export const getMainLoanSchedule = async (subdomain, data) => {
return await sendCommonMessage({
subdomain,
action: 'firstLoanSchedules.findOne',
serviceName: 'loans',
data: data,
isRPC: true,
});
}

export const insertLoanSchedule = async (subdomain, data) => {
return await sendCommonMessage({
subdomain,
action: 'firstLoanSchedules.insertMany',
serviceName: 'loans',
data: data,
isRPC: true,
});
}
export const createLoanSchedule = async (subdomain,loanSchedules, contractId) => {
try {
const result: any[] = []

for(const schedule of loanSchedules) {
const loanSchedule = {
"status": "pending",
"payDate": new Date(schedule.schdDate),
"scheduleDidStatus": "pending",
"transactionIds": [],
"isDefault": true,
"scopeBrandIds": [],
"createdAt": new Date(new Date().getTime()),
"contractId": contractId,
"version": "0",
"balance": schedule.theorBal,
"payment": schedule.totalAmount,
"interestEve": schedule.intAmount,
"interestNonce": schedule.amount,
"total": schedule.totalAmount,
"insurance": 0,
}
result.push(loanSchedule)
}

await insertLoanSchedule(subdomain,result)

} catch (error) {
console.log('insert loan schedule', error)
}
}

0 comments on commit caf5036

Please sign in to comment.