forked from segmentio/action-destinations
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
117 lines (114 loc) · 3.6 KB
/
index.ts
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import type { ActionDefinition } from '@segment/actions-core'
import type { Settings } from '../generated-types'
import type { Payload } from './generated-types'
const person = (payload: Payload) => {
return {
custom_fields: (() => {
const result = Object.fromEntries(
Object.entries(payload.custom_fields ?? {})
.filter(([_, value]) => value !== null)
.map(([key, value]) => [key, typeof value === 'object' ? JSON.stringify(value) : String(value)])
)
return Object.keys(result).length > 0 ? result : undefined
})(),
email: payload.email,
ip_address: payload.ip,
phone: payload.phone,
initial_status: payload.initial_status,
status: payload.status,
status_updated_at: payload.status_updated_at,
tags: payload.tags?.split(',').map((tag) => tag.trim()),
time_zone: payload.timezone
}
}
const action: ActionDefinition<Settings, Payload> = {
title: 'Identify',
description: 'Identify person in Drip',
defaultSubscription: 'type = "identify"',
fields: {
custom_fields: {
description: "Custom fields to add to a person's profile. Non string values will be stringified.",
label: 'Custom fields',
required: false,
type: 'object', // dictionary of strings to strings
default: { '@path': '$.traits.custom_fields' }
},
email: {
description: "The person's email address.",
label: 'Email Address',
required: true,
type: 'string',
format: 'email',
default: { '@path': '$.traits.email' }
},
ip: {
description: "The person's ip address.",
label: 'IP Address',
required: false,
type: 'string',
format: 'ipv4',
default: { '@path': '$.context.ip' }
},
phone: {
description: "The person's sms number.",
label: 'SMS Number',
required: false,
type: 'string',
default: { '@path': '$.traits.phone' }
},
initial_status: {
description: "The person's subscription status if newly identified.",
label: 'Initial Status',
required: false,
type: 'string',
default: {
'@if': {
exists: { '@path': '$.traits.initial_status' },
then: { '@path': '$.traits.initial_status' },
else: 'unsubscribed'
}
}
},
status: {
description: "The person's subscription status. Overrides initial_status.",
label: 'Status',
required: false,
type: 'string',
default: { '@path': '$.traits.status' }
},
status_updated_at: {
description: "The timestamp associated with the update to a person's status.",
label: 'Status Updated At',
required: false,
type: 'datetime',
default: { '@path': '$.traits.status_updated_at' }
},
tags: {
description: 'Comma delimited list of tags to add to a person\'s profile. e.g. "tag1,tag2".',
label: 'Tags',
required: false,
type: 'string',
default: { '@path': '$.traits.tags' }
},
timezone: {
description: "The person's timezone.",
label: 'Timezone',
required: false,
type: 'string',
default: { '@path': '$.context.timezone' }
}
},
perform: (request, { settings, payload }) => {
return request(`https://api.getdrip.com/v2/${settings.accountId}/subscribers`, {
method: 'POST',
json: { subscribers: [person(payload)] }
})
},
performBatch: (request, { settings, payload }) => {
return request(`https://api.getdrip.com/v2/${settings.accountId}/subscribers/batches`, {
method: 'POST',
json: { subscribers: payload.map(person) }
})
}
}
export default action