· 7 years ago · Oct 14, 2018, 08:22 PM
1const express = require('express');
2const router = express.Router();
3const bodyParser = require('body-parser')
4const urlencodedParser = bodyParser.urlencoded({ extended: false })
5const jsforce = require('jsforce');
6const fs = require('fs');
7const Json2csvParser = require('json2csv').Parser;
8const flash = require('connect-flash');
9const jsonexport = require('jsonexport');
10var vcard = require('vcard-generator');
11
12
13
14 router.post('/auth', urlencodedParser, function (req, res) {
15
16 const ACCOUNT = {
17 username: req.body.username,
18 password: req.body.password,
19 securityToken: req.body.securityToken
20 }
21
22 const salesForceUrl = 'https://www.test.salesforce.com';
23
24 const conn = new jsforce.Connection({
25 instanceUrl : salesForceUrl,
26 });
27
28 conn.login(ACCOUNT.username, ACCOUNT.password + ACCOUNT.securityToken, function (err, res) {
29 if (err) {
30 return console.error(' I am not working correctly ' + err);
31 }
32 conn.query('SELECT Id, Name FROM Account', function (err, res) {
33 if (err) {
34 return console.error(err);
35 } else {
36
37 const contact = [];
38 for (let i = 0; i < res.records.length; i++) {
39 const vcardContent = vcard.generate({
40 name: {
41 familyName: res.records[i].Name,
42 givenName: 'T' + i,
43 middleName: 'Philip',
44
45 },
46 formattedNames: [{
47 text: res.records[i].Name,
48 }],
49 nicknames: [{
50 text: 'Phil',
51 }],
52 works: [{
53 organization: 'Google',
54 title: 'CEO',
55 role: 'Executive',
56 }],
57 emails: [{
58 type: 'work',
59 text: 'test' + i +'@gmail.com',
60 }, {
61 type: 'home',
62 text: 'test' + i +'@gmail.com',
63 }],
64 phones: [{
65 type: 'work',
66 text: '1 (234) 567-890' + i,
67 }, {
68 text: '(123) 123-1234',
69 }, {
70 uri: 'tel:1234567890' + i,
71 }],
72 addresses: [{
73 type: 'work',
74 street: '123 Forbes Ave, Apt 1' + i,
75 locality: 'San Francisco' + i,
76 region: 'CA',
77 code: '12345',
78 country: 'USA',
79 }, {
80 type: 'home',
81 street: '456 Home St',
82 locality: 'Homeland',
83 region: 'CA',
84 code: '23456',
85 country: 'USA',
86 }]
87 });
88
89 contact.push(vcardContent);
90
91 }
92 const manipulatedVcard = [];
93 for (let i = 0; i < contact.length; i++) {
94 const cleanedContacts = contact[i].replace(/BEGIN:VCARD/g, "\nBEGIN:VCARD");
95 manipulatedVcard.push(cleanedContacts);
96
97 }
98 const finalVcardFormat = manipulatedVcard.join("\n");
99 fs.appendFile(__dirname + '/contact-details.vcf', finalVcardFormat , 'utf8', function (err) {
100 if (err) {
101 console.log('Some error occured - file either not saved or corrupted file saved.');
102 } else {
103 console.log('It\'s saved!');
104 }
105 });
106 }
107 });
108 });
109 });
110
111
112
113
114
115
116module.exports = router;