-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathapi.ts
114 lines (105 loc) · 2.6 KB
/
api.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
import request from 'superagent';
import {DEFAULT_BASE_URL} from './config';
import {now} from './utils';
import {CustomerMetadata, WidgetSettings} from './types';
const EMPTY_METADATA = {} as CustomerMetadata;
export const createNewCustomer = async (
accountId: string,
metadata: CustomerMetadata = EMPTY_METADATA,
baseUrl = DEFAULT_BASE_URL
) => {
return request
.post(`${baseUrl}/api/customers`)
.send({
customer: {
...metadata,
account_id: accountId,
// TODO: handle on the server instead?
first_seen: now(),
last_seen: now(),
},
})
.then((res) => res.body.data);
};
export const isValidCustomer = async (
customerId: string,
accountId: string,
baseUrl = DEFAULT_BASE_URL
) => {
return request
.get(`${baseUrl}/api/customers/${customerId}/exists`)
.query({
account_id: accountId,
})
.then((res) => res.body.data);
};
export const updateCustomerMetadata = async (
customerId: string,
metadata: CustomerMetadata = EMPTY_METADATA,
baseUrl = DEFAULT_BASE_URL
) => {
return request
.put(`${baseUrl}/api/customers/${customerId}/metadata`)
.send({
metadata,
})
.then((res) => res.body.data);
};
export const createNewConversation = async (
params: {
account_id: string;
customer_id: string;
inbox_id?: string;
},
baseUrl = DEFAULT_BASE_URL
) => {
return request
.post(`${baseUrl}/api/conversations`)
.send({conversation: params})
.then((res) => res.body.data);
};
export const findCustomerByExternalId = async (
externalId: string,
accountId: string,
filters: Record<string, any>,
baseUrl = DEFAULT_BASE_URL
) => {
return request
.get(`${baseUrl}/api/customers/identify`)
.query({...filters, external_id: externalId, account_id: accountId})
.then((res) => res.body.data);
};
export const fetchCustomerConversations = async (
query: {
customer_id: string;
account_id: string;
},
baseUrl = DEFAULT_BASE_URL
) => {
return request
.get(`${baseUrl}/api/conversations/customer`)
.query(query)
.then((res) => res.body.data);
};
export const fetchWidgetSettings = async (
query: {account_id: string; inbox_id?: string},
baseUrl = DEFAULT_BASE_URL
): Promise<WidgetSettings> => {
return request
.get(`${baseUrl}/api/widget_settings`)
.query(query)
.then((res) => res.body.data);
};
export const upload = async (
accountId: string,
file: any,
baseUrl = DEFAULT_BASE_URL
) => {
return request
.post(`${baseUrl}/api/upload`)
.send({
file,
account_id: accountId,
})
.then((res) => res.body.data);
};