-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomplex.js
50 lines (40 loc) · 1.67 KB
/
complex.js
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
// const {AmoApiClient} = require('@mobilon/amotop');
const {AmoApiClient} = require('../../dist');
const {domain, accessToken, debug} = require('../_config');
const amoApiClient = new AmoApiClient(domain, accessToken, {debug});
const phone = '79135292926';
const start = async () => {
try {
// получаем список контактов
let response = await amoApiClient.getContacts({with: 'leads', query: phone});
console.log('response', JSON.stringify(response, null, 2));
if(response === "") {
// если список пустой, то добавляем контакт
const contactPayload = amoApiClient.getContactPayload('test', phone);
console.log('contact payload', contactPayload);
response = await amoApiClient.addContact(contactPayload);
console.log('response', JSON.stringify(response, null, 2));
response = await amoApiClient.getContacts({with: 'leads', query: phone});
console.log('contacts', JSON.stringify(response, null, 2));
}
const contact = response._embedded.contacts[0];
// к сделке добавляем id контакта
const lead1 = {
name: 'Продать слона',
price: 1000,
_embedded: {
contacts: [
{id: contact.id}
],
},
};
console.log('lead payload', lead1);
// добавляем сделку в amoCRM
const leadResponse1 = await amoApiClient.addLead(lead1);
console.log('leadResponse', JSON.stringify(leadResponse1, null, 2));
} catch (err) {
const errMessage = err.response?.data ? JSON.stringify(err.response.data, null, 2) : err;
console.log('err', errMessage);
}
}
(start)();