-
Notifications
You must be signed in to change notification settings - Fork 0
/
field_values.ts
354 lines (335 loc) · 11.2 KB
/
field_values.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
import type { AxiosInstance } from 'axios'
import { defaultTransformers } from './axios_default_transformers.ts'
import type { FieldBase } from './fields.ts'
import { FieldValueType } from './lists.ts'
import type { DateTime, Replace } from './types.ts'
import { fieldValuesUrl } from './urls.ts'
export type { DateTime } from './types.ts'
export type DropdownValue = string
export type RankedDropdownValue = {
/**
* The unique identifier of the value.
*/
id: number
/**
* Represents the field's value.
*/
text: string
/**
* The color associated with the value.
*/
color: number
/**
* The rank of the value.
*/
rank: number
}
export type NumberValue = number
export type PersonValue = number
export type OrganizationValue = number
export type LocationValue = {
city: string | null
state: string | null
country: string | null
street_address: string | null
}
export type TextValue = string
export type ValueRaw =
| DropdownValue
| RankedDropdownValue
| NumberValue
| PersonValue
| OrganizationValue
| LocationValue
| DateTime
| TextValue
export type Value = Exclude<ValueRaw, DateTime> | Date
export interface FieldValueRawValues
extends Record<keyof FieldValueType, ValueRaw> {
[FieldValueType.RANKED_DROPDOWN]: RankedDropdownValue
[FieldValueType.DROPDOWN]: DropdownValue
[FieldValueType.NUMBER]: NumberValue
[FieldValueType.PERSON]: PersonValue
[FieldValueType.ORGANIZATION]: OrganizationValue
[FieldValueType.LOCATION]: LocationValue
[FieldValueType.DATE]: DateTime
[FieldValueType.TEXT]: TextValue
}
export interface FieldValueValues extends Record<keyof FieldValueType, Value> {
[FieldValueType.RANKED_DROPDOWN]: RankedDropdownValue
[FieldValueType.DROPDOWN]: DropdownValue
[FieldValueType.NUMBER]: NumberValue
[FieldValueType.PERSON]: PersonValue
[FieldValueType.ORGANIZATION]: OrganizationValue
[FieldValueType.LOCATION]: LocationValue
[FieldValueType.DATE]: Date
[FieldValueType.TEXT]: TextValue
}
export type ValueTypeMixin<T extends (FieldValueRawValues | FieldValueValues)> =
| {
value_type: FieldValueType.DROPDOWN
value: T[FieldValueType.DROPDOWN]
}
| {
value_type: FieldValueType.RANKED_DROPDOWN
value: T[FieldValueType.RANKED_DROPDOWN]
}
| {
value_type: FieldValueType.NUMBER
value: T[FieldValueType.NUMBER]
}
| {
value_type: FieldValueType.PERSON
value: T[FieldValueType.PERSON]
}
| {
value_type: FieldValueType.ORGANIZATION
value: T[FieldValueType.ORGANIZATION]
}
| {
value_type: FieldValueType.LOCATION
value: T[FieldValueType.LOCATION]
}
| {
value_type: FieldValueType.DATE
value: T[FieldValueType.DATE]
}
| {
value_type: FieldValueType.TEXT
value: T[FieldValueType.TEXT]
}
/**
* Each field value object has a unique id.
*
* A field value also has `field_id`, `entity_id`, and `list_entry_id` attributes that give it the appropriate associations.
*
* Use the `created_at` and `updated_at` timestamps on field values to determine when the value(s) for a given field have last been added or changed.
* Please note that what might amount to an “update” in-product (e.g. for dropdown fields) might result in a newly created field value rather than an updated old one in the API.
*
* A field value can take on many different kinds of values, depending on the field value type.
*
* *Note*: When retrieving Field Values from a specific cell in your Affinity list, it may be helpful to think about it as an XY coordinate system. The X coordinate is the List Entry *or* Entity and the Y coordinate is the Field ID. You will need to have both to find the appropriate Field Value ID.
*/
export type FieldValueRaw =
& FieldBase
& {
/**
* The unique identifier of the field value object.
*/
id: number
/**
* The unique identifier of the field the value is associated with.
*/
field_id: number
/**
* The unique identifier of the person, organization, or opportunity object the field value is associated with.
*/
entity_id: number
/**
* The unique identifier of the list entry object the field value is associated with.
* This only exists if the field the value is associated with is list-specific, `null` marks a global field value.
*/
list_entry_id: number | null
/**
* The string representing the time when the field value was created.
*/
created_at: DateTime
/**
* The string representing the last time the field value was updated.
*/
updated_at: DateTime | null
}
& ValueTypeMixin<FieldValueRawValues>
export type FieldValueResponseRaw = FieldValueRaw[]
export type FieldValue = Replace<
FieldValueRaw,
& {
updated_at: Date | null
created_at: Date
}
& ValueTypeMixin<FieldValueValues>
>
export type FieldValueResponse = FieldValue[]
export type CreateFieldValueRequest = {
field_id: number
entity_id: number
/**
* The value of the field value.
* In case of a {@link FieldValueType.DROPDOWN} or {@link FieldValueType.RANKED_DROPDOWN} field, this should be the text of the value.
*/
value: Exclude<Value, RankedDropdownValue>
list_entry_id?: number
}
export type UpdateFieldValueRequest = {
field_value_id: number
value: Value
}
export type AllQueryParameters =
| {
/** The unique ID of the person object whose field values are to be retrieved. */
person_id: number
}
| {
/** The unique ID of the organization object whose field values are to be retrieved. */
organization_id: number
}
| {
/** The unique ID of the opportunity object whose field values are to be retrieved. */
opportunity_id: number
}
| {
/** The unique ID of the list entry object whose field values are to be retrieved. */
list_entry_id: number
}
/**
* @module
* Field values are displayed in Affinity as the data in the cells of an Affinity spreadsheet.
*
* As an example for how a field value is created:
* 1. Assume there exists a list of people called "Business Development Leads".
* 2. A custom field called "Deal Size" is created on this list. Fields are visually displayed as columns.
* 3. A person X is added to this list. This action creates a list entry for this person.
* 4. A value, 50,000, is entered in the cell corresponding to person X in the Deal Size column.
* 5. 50,000 is now a field value tied to the list entry corresponding to person X, and the "Deal Size" field.
*
* *Notes*:
* - By default, Affinity creates some fields for you automatically. These include Location, Industry, Job Titles, and more. See the [Default Fields section](https://api-docs.affinity.co/#default-fields) for more information.
* - Opportunities cannot have global field values
*/
export class FieldValues {
/** @hidden */
constructor(private readonly axios: AxiosInstance) {
}
private static transformFieldValue(
fieldValue: FieldValueRaw,
): FieldValue {
return {
...fieldValue,
value: fieldValue.value_type === FieldValueType.DATE
? new Date(fieldValue.value)
: fieldValue.value,
updated_at: fieldValue.updated_at === null
? null
: new Date(fieldValue.updated_at),
created_at: new Date(fieldValue.created_at),
} as // TODO(@joscha): remove this ugly cast
FieldValue
}
/**
* Fetches field values based on the provided parameters.
*
* @returns An array of field value objects.
*
* @example
* ```typescript
* const fieldValues = await affinity.fieldValues.getAll({ person_id: 1234 })
* console.log(fieldValues)
* ```
*/
async all(params: AllQueryParameters): Promise<FieldValueResponse> {
const response = await this.axios.get<FieldValueResponse>(
fieldValuesUrl(),
{
params,
transformResponse: [
...defaultTransformers(),
(json: FieldValueResponseRaw) => {
return json.map(FieldValues.transformFieldValue)
},
],
},
)
return response.data
}
/**
* Creates a new field value.
*
* @param data - Object containing the data for creating a new field value
* @returns The created field value object.
*
* @example
* ```typescript
* const newFieldValue = await affinity.fieldValues.create({
* field_id: 1284,
* entity_id: 38706,
* value: "Architecture"
* })
* console.log(newFieldValue)
* ```
*/
async create(data: CreateFieldValueRequest): Promise<FieldValue> {
const response = await this.axios.post<FieldValue>(
fieldValuesUrl(),
{
...data,
value: data.value instanceof Date
? data.value.toISOString()
: data.value,
},
{
transformResponse: [
...defaultTransformers(),
(json: FieldValueRaw) => {
return FieldValues.transformFieldValue(json)
},
],
},
)
return response.data
}
/**
* Updates an existing field value.
*
* @param data - Object containing the data for updating a field value
* @returns The updated field value object.
*
* @example
* ```typescript
* const updatedFieldValue = await affinity.fieldValues.update({
* field_value_id: 20406836,
* value: "Healthcare"
* })
* console.log(updatedFieldValue)
* ```
*/
async update(data: UpdateFieldValueRequest): Promise<FieldValue> {
const response = await this.axios.put<FieldValue>(
fieldValuesUrl(data.field_value_id),
{
value: data.value instanceof Date
? data.value.toISOString()
: data.value,
},
{
transformResponse: [
...defaultTransformers(),
(json: FieldValueRaw) => {
return FieldValues.transformFieldValue(json)
},
],
},
)
return response.data
}
/**
* Deletes a specific field value.
*
* @returns boolean indicating whether the field value was successfully deleted.
*
* @example
* ```typescript
* const success = await affinity.fieldValues.delete({
* field_value_id: 20406836
* })
* console.log(success ? 'Field value deleted': 'Field value not deleted')
* ```
*/
async delete(
{ field_value_id }: { field_value_id: number },
): Promise<boolean> {
const response = await this.axios.delete<{ success: boolean }>(
fieldValuesUrl(field_value_id),
)
return response.data.success === true
}
}