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