intacct / intacct-sdk-js

Official repository of the Sage Intacct SDK for JavaScript in Node.js
https://developer.intacct.com/tools/sdk-node-js/
Apache License 2.0
22 stars 31 forks source link

FEATURE: pass object representation to all *Create and *Update classes' constructor methods #132

Open craibuc opened 2 months ago

craibuc commented 2 months ago

Rather than having to do this:

const invoiceCreate = new IA.Functions.AccountsReceivable.InvoiceCreate();
invoiceCreate.customerId = 'Acme, Inc.';
invoiceCreate. glPostingDate = '2024-05-31';
...

const invoiceLineCreate = new IA.Functions.AccountsReceivable.InvoiceLineCreate();
invoiceLineCreate.departmentId = 'Department A';
...

invoiceCreate.lines.add(invoiceLineCreate);
...

It would be helpful to be able to do this instead:

class InvoiceCreate {

  constructor (keyValues) {
    this.lines = []

    for (let key in keyValues) {

      if (key === 'lines') {
        keyValues['lines'].forEach(line => {
          const invoiceLineCreate = new InvoiceLineCreate(line);
          this.lines.push(invoiceLineCreate);            
        });
      }
      else {
        Reflect.set(this, key, keyValues[key]);
      }

    };

  }

}

class InvoiceLineCreate {

  constructor (keyValues) {
    this.customFields = [];

    for (let key in keyValues) {

      if (key === 'customFields') {
        keyValues['customFields'].forEach(field => {
          this.customFields.push(field);       
        });
      }
      else {
        Reflect.set(this, key, keyValues[key]);
      }

    };

  }

}

// usage of Update and Creates are greatly simplified
const invoiceCreate = new InvoiceCreate({
  customerId: 'Acme, Inc.',
  invoiceNumber: '12345',
  description: 'lorem ipsum',
  paymentTerm: 'Pre-pay',
  transactionDate: '2024-05-31',
  glPostingDate: '2024-05-31',
  dueDate: '2024-06-30',
  lines: [
    {
      glAccountNumber: '100100',
      transactionAmount: 123.45,
      memo: 'lorem ipsum',
      locationId: 'Location A',
      departmentId: 'Department A',
      projectId: 'Project A',
      customerId: 'Customer A',
      classId: 'Class A',
      customFields: [
        {
          customFieldName: 'some name',
          customFieldValue: 'some value'
        }
      ],
    }
  ]
});

console.log('invoiceCreate',invoiceCreate);
..
// save, etc.