-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Expand file tree
/
Copy pathwix-data.js
More file actions
70 lines (58 loc) · 1.97 KB
/
wix-data.js
File metadata and controls
70 lines (58 loc) · 1.97 KB
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
const { myWixClient } = require('./wix-client');
const reduceCount = async (mobile, type) => {
const leads = await myWixClient.items
.queryDataItems({ dataCollectionId: 'Leads' })
.find(
{ fields: { "mobile": mobile }}
);
const dataItemId = leads._items[0].data._id;
const dataItem = await myWixClient.dataItems.getDataItem(dataItemId, {consistentRead: false, dataCollectionId: "Leads"});
if (type === 'blog') {
dataItem.data.remaining_blog_usage_count--;
} else {
dataItem.data.remaining_logo_usage_count--;
}
const options = { dataCollectionId: "Leads", dataItem: {
data: dataItem.data
}};
await myWixClient.dataItems.removeDataItem(dataItemId, {dataCollectionId: "Leads"});
delete dataItem.data._id
await myWixClient.dataItems.saveDataItem(options);
return type === 'blog' ? dataItem.data.remaining_blog_usage_count : dataItem.data.remaining_logo_usage_count;
}
/*
@params - user mobile number
@params - email user email id
@params - type 'blog' or 'logo'
*/
async function fetchLeads(mobile, email, type) {
let leads = false;
try {
leads = await myWixClient.items
.queryDataItems({ dataCollectionId: 'Leads', consistentRead: false })
.eq("mobile", mobile ).find();
} catch(e) {
console.log(e)
}
if (leads && leads._items.length > 0) {
console.log(leads._items[0].data.remaining_blog_usage_count)
return type === 'blog' ? leads._items[0].data.remaining_blog_usage_count : leads._items[0].data.remaining_logo_usage_count
}
return false
}
async function putLead(name, email, mobile) {
let item = {
name: name,
mobile: mobile,
email_id: email,
remaining_blog_usage_count: 3,
remaining_logo_usage_count: 5
}
try {
await myWixClient.dataItems.saveDataItem({dataCollectionId: 'Leads', dataItem: {data: item}})
return true
} catch (e) {
return false
}
}
module.exports = { fetchLeads, reduceCount, putLead }