-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.js
More file actions
202 lines (197 loc) · 6.72 KB
/
Copy pathapi.js
File metadata and controls
202 lines (197 loc) · 6.72 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
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
fs = require("fs")
var detect = require('charset-detector')
var utf8 = require('to-utf-8')
var gcloud = require('gcloud')({
projectId: 'gcdc2013-iogrow',
keyFilename: "cridentials.json"
});
var Iconv = require('iconv').Iconv;
var peek = require('peek-stream')
var splicer = require('stream-splicer')
var iconv = require('iconv-lite')
var Converter = require("csvtojson").Converter;
var gcs = gcloud.storage()
var backups = gcs.bucket('gcdc2013-iogrow.appspot.com');
var _this= this;
//var insertContactUrl = "http://localhost:8090/_ah/api/crmengine/v1/contacts/insertv2?alt=json"
//var access_token = "ya29.6gHU9LqUTZp4e_VdorIB3xRUHQ6M3SYIForWXLdOzGWwFScoiDR0p-MAtq8qGKRC5IaA"
function ioconvertFrom (encoding) {
return splicer([
iconv.decodeStream(encoding)
])
}
function getSupportedEncoding (encoding) {
if(encoding === 'ISO-8859-8-I') encoding = 'ISO-8859-8'
if(iconv.encodingExists(encoding)) return encoding
return 'utf8' // default
}
var ioEncoding = function(){
return peek(function (data, swap) {
var matches = detect(data)
var encoding = matches.length > 0 && matches[0].confidence > 0
? matches[0].charsetName
: 'utf8'
encoding = getSupportedEncoding(encoding)
if (encoding ==='ISO-8859-1'){
swap(null, splicer())
}else{
swap(null, splicer([
iconv.decodeStream('cp1252')
]));
}
})
}
exports.Import = function (params, file, map, callback , end ) {
var fileStream = backups.file(file).createReadStream();
var converter = new Converter({constructResult: false});
converter.on("end_parsed", end);
converter.on("record_parsed", callback);
fileStream.pipe(ioEncoding()).pipe(converter);
}
exports.isEmpty = function(obj) {
return Object.keys(obj).length === 0;
}
exports.getDefaultFields = function(){
return {
'phones' : {'default_field' : 'number'},
'emails' : {'default_field' : 'email'},
'addresses' : {'default_field' : 'formatted'},
'sociallinks' : {'default_field' : 'url'},
'websites' : {'default_field' : 'url'}
}
}
exports.constructInfoNode = function(obj){
var infoNode = {};
var defaultFields = _this.getDefaultFields();
for (var key in obj){
infoNode['kind']=key;
infoNode['fields']=[];
var defaultField = defaultFields[key]['default_field'];
if (typeof(obj[key])=='object'){
for (var i in obj[key]){
var field = {};
field['field']=defaultField;
field['value']=obj[key][i];
if (!(_this.isEmpty(field))){
infoNode['fields'].push(field);
}
}
}else{
var field = {};
field['field']=defaultField;
field['value']=obj[key];
if (!(_this.isEmpty(field))){
infoNode['fields'].push(field);
}
}
}
return infoNode;
}
exports.constructCustomField = function(obj){
var customField = {};
for (var key in obj){
customField['kind']='customfields';
customField['fields']=[];
if (typeof(obj[key])=='object'){
for (var i in obj[key]){
var field = {};
field['field']=key;
field['value']=obj[key][i];
if (!(_this.isEmpty(field))){
customField['fields'].push(field);
}
}
}else{
var field = {};
field['field']=key;
field['value']=obj[key];
if (!(_this.isEmpty(field))){
customField['fields'].push(field);
}
}
}
return customField;
}
exports.getInfoNodes = function(params){
var infoNodes = [];
var defaultFields = _this.getDefaultFields();
for (var attr in params){
if (defaultFields.hasOwnProperty(attr)){
var obj={};
obj[attr]=params[attr];
infoNodes.push(_this.constructInfoNode(obj));
}else if(attr==='customFields'){
for (i=0;i<params['customFields'].length;i++){
var obj={};
obj[params['customFields'][i]['default_field']]=params['customFields'][i]['value'];
var customFieldObj = _this.constructCustomField(obj);
if (!(_this.isEmpty(customFieldObj.fields))){
infoNodes.push(customFieldObj);
}
}
}
}
return infoNodes;
}
exports.prepareParams = function(rawRow,matchedColumns,customFields){
var params = {'access':'public'};
for(var key in matchedColumns){
if (rawRow[key]){
if (matchedColumns[key]==='fullname'){
params.firstname = rawRow[key].split(' ').slice(0, -1).join(' ') || " ";
params.lastname = rawRow[key].split(' ').slice(-1).join(' ') || " ";
}
if (params.hasOwnProperty(matchedColumns[key])){
if (typeof(params[matchedColumns[key]])=='object'){
params[matchedColumns[key]].push(rawRow[key]);
}else{
var newList = [];
newList.push(params[matchedColumns[key]]);
newList.push(rawRow[key]);
params[matchedColumns[key]]=newList;
}
}
else{
params[matchedColumns[key]]=rawRow[key];
}
}
}
params['customFields'] = [];
for(var key in customFields){
if (rawRow[key]){
var customObj = {};
if (params['customFields'].hasOwnProperty(customFields[key])){
if (typeof(params[customFields[key]])=='object'){
customObj['default_field']=customFields[key]
customObj['value']=rawRow[key]
params['customFields'].push(customObj);
}else{
var newList = [];
customObj['default_field']=customFields[key]
customObj['value']=rawRow[key]
newList['customFields'].push(customObj);
params['customFields']=newList;
}
}
else{
customObj['default_field']=customFields[key]
customObj['value']=rawRow[key]
params['customFields'].push(customObj);
}
}
}
console.log('---------------PARAMS BEFORE TRANSOFRMATION----------');
console.log(params);
params.infonodes = _this.getInfoNodes(params);
delete params.emails;
delete params.phones;
delete params.addresses;
console.log('---------------PARAMS After TRANSOFRMATION----------');
for (var key in params.infonodes){
for (var ind in params.infonodes[key]){
console.log(ind);
console.log(params.infonodes[key][ind]);
}
}
return params
}