mp-js-api 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +120 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +341 -0
- package/dist/tables/addresses.d.ts +55 -0
- package/dist/tables/addresses.d.ts.map +1 -0
- package/dist/tables/addresses.js +60 -0
- package/dist/tables/contact-attributes.d.ts +19 -0
- package/dist/tables/contact-attributes.d.ts.map +1 -0
- package/dist/tables/contact-attributes.js +24 -0
- package/dist/tables/contacts.d.ts +105 -0
- package/dist/tables/contacts.d.ts.map +1 -0
- package/dist/tables/contacts.js +114 -0
- package/dist/tables/event-participants.d.ts +47 -0
- package/dist/tables/event-participants.d.ts.map +1 -0
- package/dist/tables/event-participants.js +52 -0
- package/dist/tables/events.d.ts +117 -0
- package/dist/tables/events.d.ts.map +1 -0
- package/dist/tables/events.js +122 -0
- package/dist/tables/group-participants.d.ts +61 -0
- package/dist/tables/group-participants.d.ts.map +1 -0
- package/dist/tables/group-participants.js +66 -0
- package/dist/tables/groups.d.ts +103 -0
- package/dist/tables/groups.d.ts.map +1 -0
- package/dist/tables/groups.js +108 -0
- package/dist/tables/households.d.ts +55 -0
- package/dist/tables/households.d.ts.map +1 -0
- package/dist/tables/households.js +60 -0
- package/dist/tables/participants.d.ts +63 -0
- package/dist/tables/participants.d.ts.map +1 -0
- package/dist/tables/participants.js +68 -0
- package/dist/utils/strings.d.ts +2 -0
- package/dist/utils/strings.d.ts.map +1 -0
- package/dist/utils/strings.js +30 -0
- package/package.json +26 -0
- package/src/index.ts +571 -0
- package/src/tables/addresses.ts +111 -0
- package/src/tables/contact-attributes.ts +40 -0
- package/src/tables/contacts.ts +223 -0
- package/src/tables/event-participants.ts +95 -0
- package/src/tables/events.ts +235 -0
- package/src/tables/group-participants.ts +123 -0
- package/src/tables/groups.ts +207 -0
- package/src/tables/households.ts +111 -0
- package/src/tables/participants.ts +127 -0
- package/src/utils/strings.ts +29 -0
- package/src/utils/types.d.ts +7 -0
- package/test/index.spec.ts +103 -0
- package/tsconfig.json +25 -0
package/src/index.ts
ADDED
|
@@ -0,0 +1,571 @@
|
|
|
1
|
+
import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios';
|
|
2
|
+
import { URLSearchParams } from 'url';
|
|
3
|
+
import { Contact, mapContactToContactRecord } from './tables/contacts';
|
|
4
|
+
import { Participant, mapParticipantToParticipantRecord, } from './tables/participants';
|
|
5
|
+
import { EventParticipant, mapEventParticipantToEventParticipantRecord, } from './tables/event-participants';
|
|
6
|
+
import { GroupParticipant, mapGroupParticipantToGroupParticipantRecord, } from './tables/group-participants';
|
|
7
|
+
import { Event } from './tables/events';
|
|
8
|
+
import { Group } from './tables/groups';
|
|
9
|
+
import { ContactRecord } from './tables/contacts';
|
|
10
|
+
import { mapContactRecord } from './tables/contacts';
|
|
11
|
+
import { mapParticipantRecord } from './tables/participants';
|
|
12
|
+
import { ParticipantRecord } from './tables/participants';
|
|
13
|
+
import { EventRecord } from './tables/events';
|
|
14
|
+
import { mapEventRecord } from './tables/events';
|
|
15
|
+
import { GroupRecord } from './tables/groups';
|
|
16
|
+
import { mapGroupRecord } from './tables/groups';
|
|
17
|
+
import { mapEventParticipantRecord } from './tables/event-participants';
|
|
18
|
+
import { EventParticipantRecord } from './tables/event-participants';
|
|
19
|
+
import { GroupParticipantRecord } from './tables/group-participants';
|
|
20
|
+
import { mapGroupParticipantRecord } from './tables/group-participants';
|
|
21
|
+
import { Household, HouseholdRecord, mapHouseholdRecord, mapHouseholdToHouseholdRecord, } from './tables/households';
|
|
22
|
+
import { Address, AddressRecord, mapAddressRecord, mapAddressToAddressRecord } from './tables/addresses';
|
|
23
|
+
import { ContactAttribute, ContactAttributeRecord, mapContactAttributeRecord, mapContactAttributeToContactAttributeRecord } from './tables/contact-attributes';
|
|
24
|
+
|
|
25
|
+
interface TokenData {
|
|
26
|
+
access_token: string;
|
|
27
|
+
expires_in: number;
|
|
28
|
+
token_type: 'Bearer';
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
interface AccessToken {
|
|
32
|
+
digest: string;
|
|
33
|
+
expiration: number;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const createTokenGetter = (auth: { username: string; password: string; }) => {
|
|
37
|
+
let token: AccessToken | undefined;
|
|
38
|
+
|
|
39
|
+
return async () => {
|
|
40
|
+
// If the token is near expiration, get a new one.
|
|
41
|
+
if (!token || token.expiration - 60000 < Date.now()) {
|
|
42
|
+
const tokenRes = await axios.post<TokenData>(
|
|
43
|
+
'https://mp.revival.com/ministryplatformapi/oauth/connect/token',
|
|
44
|
+
new URLSearchParams({
|
|
45
|
+
grant_type: 'client_credentials',
|
|
46
|
+
scope: 'http://www.thinkministry.com/dataplatform/scopes/all',
|
|
47
|
+
}).toString(),
|
|
48
|
+
{ auth }
|
|
49
|
+
);
|
|
50
|
+
const [, payload] = tokenRes.data.access_token.split('.');
|
|
51
|
+
try {
|
|
52
|
+
const jsonPayload: { exp: number; } = JSON.parse(
|
|
53
|
+
Buffer.from(payload, 'base64url').toString()
|
|
54
|
+
);
|
|
55
|
+
token = {
|
|
56
|
+
digest: tokenRes.data.access_token,
|
|
57
|
+
expiration: jsonPayload.exp * 1000,
|
|
58
|
+
};
|
|
59
|
+
return token.digest;
|
|
60
|
+
} catch (err) {
|
|
61
|
+
console.error(err);
|
|
62
|
+
}
|
|
63
|
+
} else {
|
|
64
|
+
return token.digest;
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
type WithRequired<T, K extends keyof T> = T & Required<Pick<T, K>>;
|
|
70
|
+
|
|
71
|
+
export type CreateContactParams = WithRequired<
|
|
72
|
+
Partial<Contact>,
|
|
73
|
+
'company' | 'displayName'
|
|
74
|
+
>;
|
|
75
|
+
export type CreateHouseholdParams = WithRequired<
|
|
76
|
+
Partial<Household>,
|
|
77
|
+
'householdName'
|
|
78
|
+
>;
|
|
79
|
+
export type CreateAddressParams = WithRequired<
|
|
80
|
+
Partial<Address>,
|
|
81
|
+
'addressLine1'
|
|
82
|
+
>;
|
|
83
|
+
export type CreateParticipantParams = WithRequired<
|
|
84
|
+
Partial<Participant>,
|
|
85
|
+
'contactID' | 'participantTypeID' | 'participantStartDate'
|
|
86
|
+
>;
|
|
87
|
+
export type CreateEventParticipantParams = WithRequired<
|
|
88
|
+
Partial<EventParticipant>,
|
|
89
|
+
'eventID' | 'participantID' | 'participationStatusID'
|
|
90
|
+
>;
|
|
91
|
+
export type CreateGroupParticipantParams = WithRequired<
|
|
92
|
+
Partial<GroupParticipant>,
|
|
93
|
+
'groupID' | 'participantID' | 'groupRoleID' | 'startDate'
|
|
94
|
+
>;
|
|
95
|
+
export type CreateContactAttributeParams = WithRequired<
|
|
96
|
+
Partial<ContactAttribute>,
|
|
97
|
+
'attributeID' | 'contactID' | 'startDate'
|
|
98
|
+
>;
|
|
99
|
+
|
|
100
|
+
export type MPGetOptions = {
|
|
101
|
+
select?: string;
|
|
102
|
+
filter?: string;
|
|
103
|
+
orderBy?: string;
|
|
104
|
+
groupBy?: string;
|
|
105
|
+
top?: number;
|
|
106
|
+
skip?: number;
|
|
107
|
+
distinct?: boolean;
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
export type MPCreateOptions = {
|
|
111
|
+
user?: number;
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
export type MPUpdateOptions = MPCreateOptions;
|
|
115
|
+
export type MPGetInstance = <T = any, R = AxiosResponse<T, any>>(url: string, mpOptions?: MPGetOptions, config?: AxiosRequestConfig) => Promise<R>;
|
|
116
|
+
export type MPInstance = {
|
|
117
|
+
get: MPGetInstance; // - AxiosInstance['get']
|
|
118
|
+
post: AxiosInstance['post'];
|
|
119
|
+
put: AxiosInstance['put'];
|
|
120
|
+
getContact(
|
|
121
|
+
id: number,
|
|
122
|
+
options?: MPGetOptions
|
|
123
|
+
): Promise<Contact | undefined | { error: any; }>;
|
|
124
|
+
getHousehold(
|
|
125
|
+
id: number,
|
|
126
|
+
options?: MPGetOptions
|
|
127
|
+
): Promise<Household | undefined | { error: any; }>;
|
|
128
|
+
getAddress(
|
|
129
|
+
id: number,
|
|
130
|
+
options?: MPGetOptions
|
|
131
|
+
): Promise<Address | undefined | { error: any; }>;
|
|
132
|
+
getParticipant(
|
|
133
|
+
id: number,
|
|
134
|
+
options?: MPGetOptions
|
|
135
|
+
): Promise<Participant | undefined | { error: any; }>;
|
|
136
|
+
getEvent(
|
|
137
|
+
id: number,
|
|
138
|
+
options?: MPGetOptions
|
|
139
|
+
): Promise<Event | undefined | { error: any; }>;
|
|
140
|
+
getGroup(
|
|
141
|
+
id: number,
|
|
142
|
+
options?: MPGetOptions
|
|
143
|
+
): Promise<Group | undefined | { error: any; }>;
|
|
144
|
+
getEventParticipant(
|
|
145
|
+
id: number,
|
|
146
|
+
options?: MPGetOptions
|
|
147
|
+
): Promise<EventParticipant | undefined | { error: any; }>;
|
|
148
|
+
getGroupParticipant(
|
|
149
|
+
id: number,
|
|
150
|
+
options?: MPGetOptions
|
|
151
|
+
): Promise<GroupParticipant | undefined | { error: any; }>;
|
|
152
|
+
|
|
153
|
+
getContacts(
|
|
154
|
+
options?: MPGetOptions
|
|
155
|
+
): Promise<Contact[] | { error: any; }>;
|
|
156
|
+
getHouseholds(
|
|
157
|
+
options?: MPGetOptions
|
|
158
|
+
): Promise<Household[] | { error: any; }>;
|
|
159
|
+
getAddresses(
|
|
160
|
+
options?: MPGetOptions
|
|
161
|
+
): Promise<Address[] | { error: any; }>;
|
|
162
|
+
getParticipants(
|
|
163
|
+
options?: MPGetOptions
|
|
164
|
+
): Promise<Participant[] | { error: any; }>;
|
|
165
|
+
getEvents(options?: MPGetOptions): Promise<Event[] | { error: any; }>;
|
|
166
|
+
getGroups(options?: MPGetOptions): Promise<Group[] | { error: any; }>;
|
|
167
|
+
getEventParticipants(
|
|
168
|
+
options?: MPGetOptions
|
|
169
|
+
): Promise<EventParticipant[] | { error: any; }>;
|
|
170
|
+
getGroupParticipants(
|
|
171
|
+
options?: MPGetOptions
|
|
172
|
+
): Promise<GroupParticipant[] | { error: any; }>;
|
|
173
|
+
|
|
174
|
+
createContact(
|
|
175
|
+
params: CreateContactParams,
|
|
176
|
+
options?: MPCreateOptions
|
|
177
|
+
): Promise<Contact | { error: any; }>;
|
|
178
|
+
createHousehold(
|
|
179
|
+
params: CreateHouseholdParams,
|
|
180
|
+
options?: MPCreateOptions
|
|
181
|
+
): Promise<Household | { error: any; }>;
|
|
182
|
+
createAddress(
|
|
183
|
+
params: CreateAddressParams,
|
|
184
|
+
options?: MPCreateOptions
|
|
185
|
+
): Promise<Address | { error: any; }>;
|
|
186
|
+
createParticipant(
|
|
187
|
+
params: CreateParticipantParams,
|
|
188
|
+
options?: MPCreateOptions
|
|
189
|
+
): Promise<Participant | { error: any; }>;
|
|
190
|
+
createEventParticipant(
|
|
191
|
+
params: CreateEventParticipantParams,
|
|
192
|
+
options?: MPCreateOptions
|
|
193
|
+
): Promise<EventParticipant | { error: any; }>;
|
|
194
|
+
createGroupParticipant(
|
|
195
|
+
params: CreateGroupParticipantParams,
|
|
196
|
+
options?: MPCreateOptions
|
|
197
|
+
): Promise<GroupParticipant | { error: any; }>;
|
|
198
|
+
createContactAttribute(
|
|
199
|
+
params: CreateContactAttributeParams,
|
|
200
|
+
options?: MPCreateOptions
|
|
201
|
+
): Promise<ContactAttribute | { error: any; }>;
|
|
202
|
+
|
|
203
|
+
updateContacts(
|
|
204
|
+
contacts: WithRequired<Partial<Contact>, 'contactID'>[],
|
|
205
|
+
options?: MPUpdateOptions
|
|
206
|
+
): Promise<Contact[] | { error: any; }>;
|
|
207
|
+
|
|
208
|
+
updateEventParticipants(
|
|
209
|
+
participants: WithRequired<Partial<EventParticipant>, 'eventParticipantID'>[],
|
|
210
|
+
options?: MPUpdateOptions
|
|
211
|
+
): Promise<EventParticipant[] | { error: any; }>;
|
|
212
|
+
};
|
|
213
|
+
|
|
214
|
+
const stringifyMPOptions = (mpOptions: MPGetOptions | MPCreateOptions | MPUpdateOptions = {}) =>
|
|
215
|
+
Object.entries(mpOptions).reduce((acc, [key, value]) => {
|
|
216
|
+
if (!acc) {
|
|
217
|
+
acc += `?$${key}=${value}`;
|
|
218
|
+
} else {
|
|
219
|
+
acc += `&$${key}=${value}`;
|
|
220
|
+
}
|
|
221
|
+
return acc;
|
|
222
|
+
}, '').escapeSql();
|
|
223
|
+
|
|
224
|
+
export const createMPInstance = ({ auth }: { auth: { username: string; password: string; }; }): MPInstance => {
|
|
225
|
+
/**
|
|
226
|
+
* Gets MP oauth token.
|
|
227
|
+
* @returns token
|
|
228
|
+
*/
|
|
229
|
+
const getToken = createTokenGetter(auth);
|
|
230
|
+
const api = axios.create({
|
|
231
|
+
baseURL: 'https://mp.revival.com/ministryplatformapi',
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
const get = async <T = any, R = AxiosResponse<T, any>>(
|
|
235
|
+
url: string,
|
|
236
|
+
mpOptions: MPGetOptions,
|
|
237
|
+
config?: AxiosRequestConfig
|
|
238
|
+
) =>
|
|
239
|
+
api.get<T, R>(url + stringifyMPOptions(mpOptions), {
|
|
240
|
+
...config,
|
|
241
|
+
headers: {
|
|
242
|
+
...config?.headers,
|
|
243
|
+
Authorization: `Bearer ${await getToken()}`,
|
|
244
|
+
},
|
|
245
|
+
});
|
|
246
|
+
const post = async <T, R = AxiosResponse<T, any>>(
|
|
247
|
+
url: string,
|
|
248
|
+
data?: any,
|
|
249
|
+
config?: AxiosRequestConfig
|
|
250
|
+
) =>
|
|
251
|
+
api.post<T, R>(url, data, {
|
|
252
|
+
...config,
|
|
253
|
+
headers: {
|
|
254
|
+
...config?.headers,
|
|
255
|
+
Authorization: `Bearer ${await getToken()}`,
|
|
256
|
+
},
|
|
257
|
+
});
|
|
258
|
+
const put = async <T, R = AxiosResponse<T, any>>(
|
|
259
|
+
url: string,
|
|
260
|
+
data?: any,
|
|
261
|
+
config?: AxiosRequestConfig
|
|
262
|
+
) =>
|
|
263
|
+
api.put<T, R>(url, data, {
|
|
264
|
+
...config,
|
|
265
|
+
headers: {
|
|
266
|
+
...config?.headers,
|
|
267
|
+
Authorization: `Bearer ${await getToken()}`,
|
|
268
|
+
},
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
return {
|
|
272
|
+
get,
|
|
273
|
+
post,
|
|
274
|
+
put,
|
|
275
|
+
async getContact(id, options = {}) {
|
|
276
|
+
try {
|
|
277
|
+
const res = await get<[ContactRecord?]>(
|
|
278
|
+
`/tables/contacts/${id}`, options
|
|
279
|
+
);
|
|
280
|
+
return res.data[0] ? mapContactRecord(res.data[0]) : undefined;
|
|
281
|
+
} catch (err) {
|
|
282
|
+
return { error: err };
|
|
283
|
+
}
|
|
284
|
+
},
|
|
285
|
+
async getHousehold(id, options = {}) {
|
|
286
|
+
try {
|
|
287
|
+
const res = await get<[HouseholdRecord?]>(
|
|
288
|
+
`/tables/households/${id}`, options
|
|
289
|
+
);
|
|
290
|
+
return res.data[0]
|
|
291
|
+
? mapHouseholdRecord(res.data[0])
|
|
292
|
+
: undefined;
|
|
293
|
+
} catch (err) {
|
|
294
|
+
return { error: err };
|
|
295
|
+
}
|
|
296
|
+
},
|
|
297
|
+
async getAddress(id, options = {}) {
|
|
298
|
+
try {
|
|
299
|
+
const res = await get<[AddressRecord?]>(
|
|
300
|
+
`/tables/addresses/${id}`, options
|
|
301
|
+
);
|
|
302
|
+
return res.data[0] ? mapAddressRecord(res.data[0]) : undefined;
|
|
303
|
+
} catch (err) {
|
|
304
|
+
return { error: err };
|
|
305
|
+
}
|
|
306
|
+
},
|
|
307
|
+
async getParticipant(id, options = {}) {
|
|
308
|
+
try {
|
|
309
|
+
const res = await get<[ParticipantRecord?]>(
|
|
310
|
+
`/tables/participants/${id}`, options
|
|
311
|
+
);
|
|
312
|
+
return res.data[0]
|
|
313
|
+
? mapParticipantRecord(res.data[0])
|
|
314
|
+
: undefined;
|
|
315
|
+
} catch (err) {
|
|
316
|
+
return { error: err };
|
|
317
|
+
}
|
|
318
|
+
},
|
|
319
|
+
async getEvent(id, options = {}) {
|
|
320
|
+
try {
|
|
321
|
+
const res = await get<[EventRecord?]>(
|
|
322
|
+
`/tables/events/${id}`, options
|
|
323
|
+
);
|
|
324
|
+
return res.data[0] ? mapEventRecord(res.data[0]) : undefined;
|
|
325
|
+
} catch (err) {
|
|
326
|
+
return { error: err };
|
|
327
|
+
}
|
|
328
|
+
},
|
|
329
|
+
async getGroup(id, options = {}) {
|
|
330
|
+
try {
|
|
331
|
+
const res = await get<[GroupRecord?]>(
|
|
332
|
+
`/tables/groups/${id}`, options
|
|
333
|
+
);
|
|
334
|
+
return res.data[0] ? mapGroupRecord(res.data[0]) : undefined;
|
|
335
|
+
} catch (err) {
|
|
336
|
+
return { error: err };
|
|
337
|
+
}
|
|
338
|
+
},
|
|
339
|
+
async getEventParticipant(id, options = {}) {
|
|
340
|
+
try {
|
|
341
|
+
const res = await get<[EventParticipantRecord?]>(
|
|
342
|
+
`/tables/event_participants/${id}`, options
|
|
343
|
+
);
|
|
344
|
+
return res.data[0]
|
|
345
|
+
? mapEventParticipantRecord(res.data[0])
|
|
346
|
+
: undefined;
|
|
347
|
+
} catch (err) {
|
|
348
|
+
return { error: err };
|
|
349
|
+
}
|
|
350
|
+
},
|
|
351
|
+
async getGroupParticipant(id, options = {}) {
|
|
352
|
+
try {
|
|
353
|
+
const res = await get<[GroupParticipantRecord?]>(
|
|
354
|
+
`/tables/group_participants/${id}`, options
|
|
355
|
+
);
|
|
356
|
+
return res.data[0]
|
|
357
|
+
? mapGroupParticipantRecord(res.data[0])
|
|
358
|
+
: undefined;
|
|
359
|
+
} catch (err) {
|
|
360
|
+
return { error: err };
|
|
361
|
+
}
|
|
362
|
+
},
|
|
363
|
+
async getContacts(options = {}) {
|
|
364
|
+
try {
|
|
365
|
+
const res = await get<ContactRecord[]>(
|
|
366
|
+
`/tables/contacts`, options
|
|
367
|
+
);
|
|
368
|
+
return res.data.map(mapContactRecord);
|
|
369
|
+
} catch (err) {
|
|
370
|
+
return { error: err };
|
|
371
|
+
}
|
|
372
|
+
},
|
|
373
|
+
async getHouseholds(options = {}) {
|
|
374
|
+
try {
|
|
375
|
+
const res = await get<HouseholdRecord[]>(
|
|
376
|
+
`/tables/households`, options
|
|
377
|
+
);
|
|
378
|
+
return res.data.map(mapHouseholdRecord);
|
|
379
|
+
} catch (err) {
|
|
380
|
+
return { error: err };
|
|
381
|
+
}
|
|
382
|
+
},
|
|
383
|
+
async getAddresses(options = {}) {
|
|
384
|
+
try {
|
|
385
|
+
const res = await get<AddressRecord[]>(
|
|
386
|
+
`/tables/addresses`, options
|
|
387
|
+
);
|
|
388
|
+
return res.data.map(mapAddressRecord);
|
|
389
|
+
} catch (err) {
|
|
390
|
+
return { error: err };
|
|
391
|
+
}
|
|
392
|
+
},
|
|
393
|
+
async getParticipants(options = {}) {
|
|
394
|
+
try {
|
|
395
|
+
const res = await get<ParticipantRecord[]>(
|
|
396
|
+
`/tables/participants`, options
|
|
397
|
+
);
|
|
398
|
+
return res.data.map(mapParticipantRecord);
|
|
399
|
+
} catch (err) {
|
|
400
|
+
return { error: err };
|
|
401
|
+
}
|
|
402
|
+
},
|
|
403
|
+
async getEvents(options = {}) {
|
|
404
|
+
try {
|
|
405
|
+
const res = await get<EventRecord[]>(`/tables/events`, options);
|
|
406
|
+
return res.data.map(mapEventRecord);
|
|
407
|
+
} catch (err) {
|
|
408
|
+
return { error: err };
|
|
409
|
+
}
|
|
410
|
+
},
|
|
411
|
+
async getGroups(options = {}) {
|
|
412
|
+
try {
|
|
413
|
+
const res = await get<GroupRecord[]>(`/tables/groups`, options);
|
|
414
|
+
return res.data.map(mapGroupRecord);
|
|
415
|
+
} catch (err) {
|
|
416
|
+
return { error: err };
|
|
417
|
+
}
|
|
418
|
+
},
|
|
419
|
+
async getEventParticipants(options = {}) {
|
|
420
|
+
try {
|
|
421
|
+
const res = await get<EventParticipantRecord[]>(
|
|
422
|
+
`/tables/event_participants`, options
|
|
423
|
+
);
|
|
424
|
+
return res.data.map(mapEventParticipantRecord);
|
|
425
|
+
} catch (err) {
|
|
426
|
+
return { error: err };
|
|
427
|
+
}
|
|
428
|
+
},
|
|
429
|
+
async getGroupParticipants(options = {}) {
|
|
430
|
+
try {
|
|
431
|
+
const res = await get<GroupParticipantRecord[]>(
|
|
432
|
+
`/tables/group_participants`, options
|
|
433
|
+
);
|
|
434
|
+
return res.data.map(mapGroupParticipantRecord);
|
|
435
|
+
} catch (err) {
|
|
436
|
+
return { error: err };
|
|
437
|
+
}
|
|
438
|
+
},
|
|
439
|
+
async createContact(params, options = {}) {
|
|
440
|
+
const query = stringifyMPOptions(options);
|
|
441
|
+
try {
|
|
442
|
+
const res = await post<[ContactRecord]>(
|
|
443
|
+
`/tables/contacts${query}`,
|
|
444
|
+
[mapContactToContactRecord(params as Contact)]
|
|
445
|
+
);
|
|
446
|
+
return mapContactRecord(res.data[0]);
|
|
447
|
+
} catch (err) {
|
|
448
|
+
return { error: err };
|
|
449
|
+
}
|
|
450
|
+
},
|
|
451
|
+
async createHousehold(params, options = {}) {
|
|
452
|
+
const query = stringifyMPOptions(options);
|
|
453
|
+
try {
|
|
454
|
+
const res = await post<[HouseholdRecord]>(
|
|
455
|
+
`/tables/households${query}`,
|
|
456
|
+
[mapHouseholdToHouseholdRecord(params as Household)]
|
|
457
|
+
);
|
|
458
|
+
return mapHouseholdRecord(res.data[0]);
|
|
459
|
+
} catch (err) {
|
|
460
|
+
return { error: err };
|
|
461
|
+
}
|
|
462
|
+
},
|
|
463
|
+
async createAddress(params, options = {}) {
|
|
464
|
+
const query = stringifyMPOptions(options);
|
|
465
|
+
try {
|
|
466
|
+
const res = await post<[AddressRecord]>(
|
|
467
|
+
`/tables/addresses${query}`,
|
|
468
|
+
[mapAddressToAddressRecord(params as Address)]
|
|
469
|
+
);
|
|
470
|
+
return mapAddressRecord(res.data[0]);
|
|
471
|
+
} catch (err) {
|
|
472
|
+
return { error: err };
|
|
473
|
+
}
|
|
474
|
+
},
|
|
475
|
+
async createParticipant(params, options = {}) {
|
|
476
|
+
const query = stringifyMPOptions(options);
|
|
477
|
+
try {
|
|
478
|
+
const res = await post<[ParticipantRecord]>(
|
|
479
|
+
`/tables/participants${query}`,
|
|
480
|
+
[mapParticipantToParticipantRecord(params as Participant)]
|
|
481
|
+
);
|
|
482
|
+
return mapParticipantRecord(res.data[0]);
|
|
483
|
+
} catch (err) {
|
|
484
|
+
return { error: err };
|
|
485
|
+
}
|
|
486
|
+
},
|
|
487
|
+
async createEventParticipant(params, options = {}) {
|
|
488
|
+
const query = stringifyMPOptions(options);
|
|
489
|
+
try {
|
|
490
|
+
const res = await post<[EventParticipantRecord]>(
|
|
491
|
+
`/tables/event_participants${query}`,
|
|
492
|
+
[
|
|
493
|
+
mapEventParticipantToEventParticipantRecord(
|
|
494
|
+
params as EventParticipant
|
|
495
|
+
),
|
|
496
|
+
]
|
|
497
|
+
);
|
|
498
|
+
return mapEventParticipantRecord(res.data[0]);
|
|
499
|
+
} catch (err) {
|
|
500
|
+
return { error: err };
|
|
501
|
+
}
|
|
502
|
+
},
|
|
503
|
+
async createGroupParticipant(params, options = {}) {
|
|
504
|
+
const query = stringifyMPOptions(options);
|
|
505
|
+
try {
|
|
506
|
+
const res = await post<[GroupParticipantRecord]>(
|
|
507
|
+
`/tables/group_participants${query}`,
|
|
508
|
+
[
|
|
509
|
+
mapGroupParticipantToGroupParticipantRecord(
|
|
510
|
+
params as GroupParticipant
|
|
511
|
+
),
|
|
512
|
+
]
|
|
513
|
+
);
|
|
514
|
+
return mapGroupParticipantRecord(res.data[0]);
|
|
515
|
+
} catch (err) {
|
|
516
|
+
return { error: err };
|
|
517
|
+
}
|
|
518
|
+
},
|
|
519
|
+
async createContactAttribute(params, options = {}) {
|
|
520
|
+
const query = stringifyMPOptions(options);
|
|
521
|
+
try {
|
|
522
|
+
const res = await post<[ContactAttributeRecord]>(
|
|
523
|
+
`/tables/contact_attributes${query}`,
|
|
524
|
+
[
|
|
525
|
+
mapContactAttributeToContactAttributeRecord(
|
|
526
|
+
params as ContactAttribute
|
|
527
|
+
),
|
|
528
|
+
]
|
|
529
|
+
);
|
|
530
|
+
return mapContactAttributeRecord(res.data[0]);
|
|
531
|
+
} catch (err) {
|
|
532
|
+
return { error: err };
|
|
533
|
+
}
|
|
534
|
+
},
|
|
535
|
+
async updateContacts(contacts, options = {}) {
|
|
536
|
+
const query = stringifyMPOptions(options);
|
|
537
|
+
try {
|
|
538
|
+
const res = await put<ContactRecord[]>(
|
|
539
|
+
`/tables/contacts${query}`,
|
|
540
|
+
contacts.map(mapContactToContactRecord)
|
|
541
|
+
);
|
|
542
|
+
return res.data.map(mapContactRecord);
|
|
543
|
+
} catch (err) {
|
|
544
|
+
return { error: err };
|
|
545
|
+
}
|
|
546
|
+
},
|
|
547
|
+
async updateEventParticipants(eventParticipants, options = {}) {
|
|
548
|
+
try {
|
|
549
|
+
const res = await put<EventParticipantRecord[]>(
|
|
550
|
+
`/tables/event_participants`,
|
|
551
|
+
eventParticipants.map(mapEventParticipantToEventParticipantRecord)
|
|
552
|
+
);
|
|
553
|
+
return res.data.map(mapEventParticipantRecord);
|
|
554
|
+
} catch (err) {
|
|
555
|
+
return { error: err };
|
|
556
|
+
}
|
|
557
|
+
},
|
|
558
|
+
};
|
|
559
|
+
};
|
|
560
|
+
|
|
561
|
+
export {
|
|
562
|
+
Contact,
|
|
563
|
+
Participant,
|
|
564
|
+
Event,
|
|
565
|
+
Group,
|
|
566
|
+
EventParticipant,
|
|
567
|
+
GroupParticipant,
|
|
568
|
+
Household,
|
|
569
|
+
Address,
|
|
570
|
+
ContactAttribute
|
|
571
|
+
};
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
export interface AddressRecord {
|
|
2
|
+
Address_ID: number;
|
|
3
|
+
Address_Line_1: string;
|
|
4
|
+
Address_Line_2: string;
|
|
5
|
+
City: string;
|
|
6
|
+
'State/Region': string;
|
|
7
|
+
Postal_Code: string;
|
|
8
|
+
Foreign_Country: null | string;
|
|
9
|
+
Country_Code: string;
|
|
10
|
+
Carrier_Route: null | string;
|
|
11
|
+
Lot_Number: null | string;
|
|
12
|
+
Delivery_Point_Code: null | string;
|
|
13
|
+
Delivery_Point_Check_Digit: null | string;
|
|
14
|
+
Latitude: null | number;
|
|
15
|
+
Longitude: null | number;
|
|
16
|
+
Altitude: null | number;
|
|
17
|
+
Time_Zone: null | string;
|
|
18
|
+
Bar_Code: null | string;
|
|
19
|
+
Area_Code: null | string;
|
|
20
|
+
Last_Validation_Attempt: null | string;
|
|
21
|
+
County: string;
|
|
22
|
+
Validated: null | string;
|
|
23
|
+
Do_Not_Validate: boolean;
|
|
24
|
+
Last_GeoCode_Attempt: null | string;
|
|
25
|
+
Country: null | string;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface Address {
|
|
29
|
+
addressID: number;
|
|
30
|
+
addressLine1: string;
|
|
31
|
+
addressLine2: string;
|
|
32
|
+
city: string;
|
|
33
|
+
stateRegion: string;
|
|
34
|
+
postalCode: string;
|
|
35
|
+
foreignCountry: null | string;
|
|
36
|
+
countryCode: string;
|
|
37
|
+
carrierRoute: null | string;
|
|
38
|
+
lotNumber: null | string;
|
|
39
|
+
deliveryPointCode: null | string;
|
|
40
|
+
deliveryPointCheckDigit: null | string;
|
|
41
|
+
latitude: null | number;
|
|
42
|
+
longitude: null | number;
|
|
43
|
+
altitude: null | number;
|
|
44
|
+
timeZone: null | string;
|
|
45
|
+
barCode: null | string;
|
|
46
|
+
areaCode: null | string;
|
|
47
|
+
lastValidationAttempt: null | string;
|
|
48
|
+
county: string;
|
|
49
|
+
validated: null | string;
|
|
50
|
+
doNotValidate: boolean;
|
|
51
|
+
lastGeoCodeAttempt: null | string;
|
|
52
|
+
country: null | string;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function mapAddressRecord(addressRecord: AddressRecord): Address {
|
|
56
|
+
return {
|
|
57
|
+
addressID: addressRecord.Address_ID,
|
|
58
|
+
addressLine1: addressRecord.Address_Line_1,
|
|
59
|
+
addressLine2: addressRecord.Address_Line_2,
|
|
60
|
+
city: addressRecord.City,
|
|
61
|
+
stateRegion: addressRecord['State/Region'],
|
|
62
|
+
postalCode: addressRecord.Postal_Code,
|
|
63
|
+
foreignCountry: addressRecord.Foreign_Country,
|
|
64
|
+
countryCode: addressRecord.Country_Code,
|
|
65
|
+
carrierRoute: addressRecord.Carrier_Route,
|
|
66
|
+
lotNumber: addressRecord.Lot_Number,
|
|
67
|
+
deliveryPointCode: addressRecord.Delivery_Point_Code,
|
|
68
|
+
deliveryPointCheckDigit: addressRecord.Delivery_Point_Check_Digit,
|
|
69
|
+
latitude: addressRecord.Latitude,
|
|
70
|
+
longitude: addressRecord.Longitude,
|
|
71
|
+
altitude: addressRecord.Altitude,
|
|
72
|
+
timeZone: addressRecord.Time_Zone,
|
|
73
|
+
barCode: addressRecord.Bar_Code,
|
|
74
|
+
areaCode: addressRecord.Area_Code,
|
|
75
|
+
lastValidationAttempt: addressRecord.Last_Validation_Attempt,
|
|
76
|
+
county: addressRecord.County,
|
|
77
|
+
validated: addressRecord.Validated,
|
|
78
|
+
doNotValidate: addressRecord.Do_Not_Validate,
|
|
79
|
+
lastGeoCodeAttempt: addressRecord.Last_GeoCode_Attempt,
|
|
80
|
+
country: addressRecord.Country,
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export function mapAddressToAddressRecord(address: Address): AddressRecord {
|
|
85
|
+
return {
|
|
86
|
+
Address_ID: address.addressID,
|
|
87
|
+
Address_Line_1: address.addressLine1,
|
|
88
|
+
Address_Line_2: address.addressLine2,
|
|
89
|
+
City: address.city,
|
|
90
|
+
['State/Region']: address.stateRegion,
|
|
91
|
+
Postal_Code: address.postalCode,
|
|
92
|
+
Foreign_Country: address.foreignCountry,
|
|
93
|
+
Country_Code: address.countryCode,
|
|
94
|
+
Carrier_Route: address.carrierRoute,
|
|
95
|
+
Lot_Number: address.lotNumber,
|
|
96
|
+
Delivery_Point_Code: address.deliveryPointCode,
|
|
97
|
+
Delivery_Point_Check_Digit: address.deliveryPointCheckDigit,
|
|
98
|
+
Latitude: address.latitude,
|
|
99
|
+
Longitude: address.longitude,
|
|
100
|
+
Altitude: address.altitude,
|
|
101
|
+
Time_Zone: address.timeZone,
|
|
102
|
+
Bar_Code: address.barCode,
|
|
103
|
+
Area_Code: address.areaCode,
|
|
104
|
+
Last_Validation_Attempt: address.lastValidationAttempt,
|
|
105
|
+
County: address.county,
|
|
106
|
+
Validated: address.validated,
|
|
107
|
+
Do_Not_Validate: address.doNotValidate,
|
|
108
|
+
Last_GeoCode_Attempt: address.lastGeoCodeAttempt,
|
|
109
|
+
Country: address.country,
|
|
110
|
+
};
|
|
111
|
+
}
|