XeroAPI/xero-node

Error when calling payrollUKApi.createEmployee: Invalid fields `_default` with reason "Please add a salary & wage."

aprilmintacpineda opened this issue · 1 comments

SDK you're using (please complete the following information):

  • Version 4.29.0

Describe the bug

I'm trying to create an employee and attach salary and wages to that employee (possibly other data such as pension, etc the idea is it should create a complete employee record) but I always get error when I call POST SalaryAndWages endpoint, I am able to successfully create employee though.

I am using the Xero-Node package, I logged the body being sent and I got this:

{
  method: 'POST',
  qs: {},
  headers: {
    'user-agent': 'xero-node-4.29.0',
    'Xero-Tenant-Id': '<hidden>',
  },
  uri: 'https://api.xero.com/payroll.xro/2.0/Employees/<hidden>/SalaryAndWages',
  useQuerystring: false,
  json: true,
  body: {
    salaryAndWagesID: undefined,
    earningsRateID: <hidden>,
    numberOfUnitsPerWeek: 40,
    ratePerUnit: undefined,
    numberOfUnitsPerDay: 8,
    effectiveFrom: '2022-12-13',
    annualSalary: 16693,
    status: 'Active',
    paymentType: 'Salary'
  }
}

The response I get is this:

{
    "id": <hidden>,
    "providerName": <hidden>,
    "dateTimeUTC": "2022-12-13T07:36:26.8819194",
    "httpStatusCode": "BadRequest",
    "pagination": null,
    "problem": {
        "type": "application/problem+json",
        "title": "BadRequest",
        "status": 400,
        "detail": "Validation error occurred.",
        "instance": null,
        "invalidFields": [
            {
                "name": "_default",
                "reason": "Please add a salary &amp; wage."
            }
        ],
        "invalidObjects": null
    },
    "salaryAndWages": null
}

As you can see, the error is not specific nor does it help at all when. It does not tell me where the error occurred and what action I should do.

Here's the scope of the token set that I am using:

[
  "openid",
  "profile",
  "email",
  "payroll.settings",
  "payroll.employees",
  "payroll.payruns",
  "offline_access",
]

To Reproduce

import 'dotenv/config';
import { XeroClient } from 'xero-node';
import { faker } from '@faker-js/faker';
import { format } from 'date-fns';
import { SalaryAndWage } from 'xero-node/dist/gen/model/payroll-uk/salaryAndWage';
import { Employee } from 'xero-node/dist/gen/model/payroll-uk/employee';

const tokenSet = <hidden>;

const client = new XeroClient({
  clientId: process.env.XERO_CLIENT_ID as string,
  clientSecret: process.env.XERO_CLIENT_SECRET as string,
  redirectUris: [process.env.XERO_REDIRECT_URI as string],
  scopes: [
    'openid',
    'profile',
    'email',
    'payroll.settings',
    'payroll.employees',
    'payroll.payruns',
    'offline_access'
  ]
});

async function main() {
  try {
    await client.initialize();
    client.setTokenSet(tokenSet);

    if (client.readTokenSet().expired()) {
      const newTokenSet = await client.refreshToken();
      client.setTokenSet(newTokenSet);
      console.log('newTokenSet', newTokenSet);
    }

    await client.updateTenants();
    const tenantId = client.tenants[0].tenantId;

    // add employee
    const startDate = format(faker.date.recent(), 'yyyy-MM-dd');

    const {
      body: { employee }
    } = await client.payrollUKApi.createEmployee(tenantId, {
      firstName: faker.name.firstName(),
      lastName: faker.name.lastName(),
      dateOfBirth: format(faker.date.birthdate(), 'yyyy-MM-dd'),
      startDate,
      title: 'Mr.',
      gender: faker.helpers.arrayElement([
        Employee.GenderEnum.F,
        Employee.GenderEnum.M
      ]),
      address: {
        addressLine1: '123 Test st',
        city: 'Rangiora',
        postCode: '7400',
        countryName: 'UNITED KINGDOM'
      }
    });

    // add salary and wages to this employee
    const {
      body: { salaryAndWages }
    } = await client.payrollUKApi.createEmployeeSalaryAndWage(
      tenantId,
      employee?.employeeID as string,
      {
        earningsRateID: <hidden>,
        annualSalary: faker.datatype.number({ max: 100000 }),
        effectiveFrom: startDate,
        numberOfUnitsPerDay: 8,
        numberOfUnitsPerWeek: 40,
        paymentType: SalaryAndWage.PaymentTypeEnum.Salary,
        status: SalaryAndWage.StatusEnum.Active
      }
    );

    console.log(
      JSON.stringify(
        {
          ...employee,
          salaryAndWages
        },
        null,
        4
      )
    );
  } catch (error) {
    console.log(JSON.stringify(error, null, 4));
  }
}

main();

Expected behavior

Screenshots
If applicable, add screenshots to help explain your problem.

Additional context
Add any other context about the problem here.

To answer my own issue, you need to make a call to createEmployment first, https://xeroapi.github.io/xero-node/payroll-uk/index.html#api-PayrollUk-createEmployment before calling createEmployeeSalaryAndWage