1 | |
2 | |
3 | * Worker Direct Deposits |
4 | * Update multiple direct deposits for an "Active" or "In-Progress" worker at a time. |
5 | */ |
6 | export async function main( |
7 | auth: RT.Paychex, |
8 | workerId: string, |
9 | body: Body, |
10 | effectivitydate?: string | undefined |
11 | ) { |
12 | const accessToken = await getOAuthToken(auth, 'https://api.paychex.com/auth/oauth/v2/token') |
13 | const url = new URL(`https://api.paychex.com/workers/${workerId}/directdeposits`) |
14 | for (const [k, v] of [['effectivitydate', effectivitydate]]) { |
15 | if (v !== undefined && v !== '') { |
16 | url.searchParams.append(k, v) |
17 | } |
18 | } |
19 | const response = await fetch(url, { |
20 | method: 'PATCH', |
21 | headers: { |
22 | 'Content-Type': 'application/json', |
23 | Authorization: 'Bearer ' + accessToken |
24 | }, |
25 | body: JSON.stringify(body) |
26 | }) |
27 | if (!response.ok) { |
28 | const text = await response.text() |
29 | throw new Error(`${response.status} ${text}`) |
30 | } |
31 | return await response.json() |
32 | } |
33 |
|
34 | async function getOAuthToken(auth: RT.Paychex, tokenUrl: string): Promise<string> { |
35 | const params = new URLSearchParams({ |
36 | grant_type: 'client_credentials' |
37 | }) |
38 |
|
39 | const response = await fetch(tokenUrl, { |
40 | method: 'POST', |
41 | headers: { |
42 | Authorization: 'Basic ' + btoa(`${auth.client_id}:${auth.client_secret}`), |
43 | 'Content-Type': 'application/x-www-form-urlencoded' |
44 | }, |
45 | body: params.toString() |
46 | }) |
47 |
|
48 | if (!response.ok) { |
49 | const text = await response.text() |
50 | throw new Error(`OAuth token request failed: ${response.status} ${text}`) |
51 | } |
52 |
|
53 | const data = await response.json() |
54 | return data.access_token |
55 | } |
56 |
|
57 | |
58 | |
59 | * This file was automatically generated by json-schema-to-typescript. |
60 | * DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file, |
61 | * and run json-schema-to-typescript to regenerate this file. |
62 | */ |
63 |
|
64 | |
65 | * Worker direct deposit |
66 | */ |
67 | export interface Body { |
68 | |
69 | * The ID for the direct deposit item. |
70 | */ |
71 | directDepositId?: string |
72 | |
73 | * The date that this direct deposit will be applied to future pay periods. This data field cannot be PATCHED. |
74 | */ |
75 | startDate?: string |
76 | |
77 | * A type of payment for the direct deposit. |
78 | */ |
79 | paymentType?: 'FLAT_DOLLAR_AMOUNT' | 'PERCENTAGE' | 'REMAINDER' |
80 | |
81 | * Financial institutions account type. This data field cannot be PATCHED. |
82 | */ |
83 | accountType?: 'CHECKING' | 'SAVINGS' |
84 | |
85 | * The amount to be applied to this direct deposit. |
86 | */ |
87 | value?: number |
88 | |
89 | * The financial institutions routing number.This data field cannot be PATCHED. |
90 | */ |
91 | routingNumber?: string |
92 | |
93 | * The financial institutions account number.This data field cannot be PATCHED. |
94 | */ |
95 | accountNumber?: string |
96 | |
97 | * The priority order for which the direct deposits will be performed in. When a new direct deposit is added the priority will be assigned. The priority can be modified only by swapping with a different direct deposit using the bulk PATCH. A paymentType of REMAINDER will show a priority of 99 and can't be modified.This data field cannot be PATCHED. |
98 | */ |
99 | priority?: string |
100 | links?: { |
101 | rel?: string |
102 | href?: string |
103 | hreflang?: string |
104 | media?: string |
105 | title?: string |
106 | type?: string |
107 | deprecation?: string |
108 | profile?: string |
109 | name?: string |
110 | [k: string]: unknown |
111 | }[] |
112 | [k: string]: unknown |
113 | } |
114 |
|