Creates a timesheet

Script xero Verified

by hugo697 ยท 12/20/2024

The script

Submitted by hugo697 Bun
Verified 515 days ago
1
//native
2
type Xero = {
3
  token: string;
4
};
5
/**
6
 * Creates a timesheet
7
 *
8
 */
9
export async function main(
10
  auth: Xero,
11
  Xero_Tenant_Id: string,
12
  Idempotency_Key: string,
13
  body: {
14
    EmployeeID: string;
15
    StartDate: string;
16
    EndDate: string;
17
    Status?: "DRAFT" | "PROCESSED" | "APPROVED" | "REJECTED" | "REQUESTED";
18
    Hours?: number;
19
    TimesheetID?: string;
20
    TimesheetLines?: {
21
      EarningsRateID?: string;
22
      TrackingItemID?: string;
23
      NumberOfUnits?: number[];
24
      UpdatedDateUTC?: string;
25
    }[];
26
    UpdatedDateUTC?: string;
27
    ValidationErrors?: { Message?: string }[];
28
  }[],
29
) {
30
  const url = new URL(`https://api.xero.com/payroll.xro/1.0/Timesheets`);
31

32
  const response = await fetch(url, {
33
    method: "POST",
34
    headers: {
35
      Accept: 'application/json',
36
      "Xero-Tenant-Id": Xero_Tenant_Id,
37
      "Idempotency-Key": Idempotency_Key,
38
      "Content-Type": "application/json",
39
      Authorization: "Bearer " + auth.token,
40
    },
41
    body: JSON.stringify(body),
42
  });
43
  if (!response.ok) {
44
    const text = await response.text();
45
    throw new Error(`${response.status} ${text}`);
46
  }
47
  return await response.json();
48
}
49