-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathusers.ts
More file actions
73 lines (61 loc) · 2.12 KB
/
users.ts
File metadata and controls
73 lines (61 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import { createSync } from 'nango';
import type { ZendeskUser } from '../types.js';
import { getSubdomain } from '../helpers/get-subdomain.js';
import type { ProxyConfiguration } from 'nango';
import { User } from '../models.js';
import { z } from 'zod';
const sync = createSync({
description: 'Fetches a list of admin or agent users from Zendesk',
version: '2.0.1',
frequency: 'every 6 hours',
autoStart: true,
syncType: 'full',
endpoints: [
{
method: 'GET',
path: '/users'
}
],
scopes: ['users:read'],
models: {
User: User
},
metadata: z.object({}),
exec: async (nango) => {
const subdomain = await getSubdomain(nango);
const roles = ['agent', 'admin'];
const config: ProxyConfiguration = {
baseUrlOverride: `https://${subdomain}.zendesk.com`,
// https://developer.zendesk.com/api-reference/ticketing/users/users/#list-users
endpoint: `/api/v2/users`,
retries: 10,
params: {
roles: roles.join(',')
},
paginate: {
type: 'cursor',
cursor_path_in_response: 'meta.after_cursor',
limit_name_in_request: 'page[size]',
cursor_name_in_request: 'page[after]',
limit: 100,
response_path: 'users'
}
};
for await (const zUsers of nango.paginate<ZendeskUser>(config)) {
const users: User[] = zUsers.map((zUser: ZendeskUser) => {
const [firstName, lastName] = zUser.name.split(' ');
return {
id: zUser.id.toString(),
firstName: firstName || '',
lastName: lastName || '',
email: zUser.email,
user_fields: zUser.user_fields
};
});
await nango.batchSave(users, 'User');
}
await nango.deleteRecordsFromPreviousExecutions('User');
}
});
export type NangoSyncLocal = Parameters<(typeof sync)['exec']>[0];
export default sync;