I want to assign a MOD10V05 valid Bpay CRN(generated using https://github.com/JedWatson/node-bpay) to the contact in xero account using xero-api. I'm able to get the contacts and update them with the generated Bpay CRN. But I want to perform a check before updating the account number so that a duplicate account number doesn't exist already.
I've tried to get the contacts and apply a map to them to store the ContactID of those with no account numbers to an array(noAccountNumContacts). Then map that array to update the contact with Bpay CRN. But I'm unable to apply a check for duplication.
Here is the code:
const XeroClient = require("xero-node").AccountingAPIClient;
const bpay = require("bpay");
(async function() {
let xero = new XeroClient(config);
let noAccountNumContacts = [];
const result = await xero.contacts.get();
const contacts = result.Contacts;
// push contact with no account no. to array
contacts.map(contact => {
const { ContactID, Name, AccountNumber } = contact;
console.log("Contact ID: ", ContactID);
console.log("Name: ", Name);
if (AccountNumber === undefined) {
noAccountNumContacts.push(ContactID);
} else {
console.log("Account no: ", AccountNumber);
}
});
console.log("Contacts to be updated ", noAccountNumContacts);
// update account number
if (noAccountNumContacts.length !== 0) {
noAccountNumContacts.map(async contact => {
const crn = bpay.generateBpayCRN(7);
await xero.contacts.update({
ContactID: contact,
AccountNumber: crn
});
});
}
noAccountNumContacts = [];
})();
I want to generate a new Bpay CRN and check if that already exists or not in the contacts. If it exists then generate again otherwise update it to the contact with a null value in the account number field.