-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
196 lines (168 loc) · 5.22 KB
/
index.js
File metadata and controls
196 lines (168 loc) · 5.22 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
const express = require("express");
const cors = require("cors");
const bodyParser = require("body-parser");
const axios = require("axios").create({ baseUrl: "" });
const { v4: uuidv4 } = require('uuid');
const nodemailer = require('nodemailer');
const { GoogleSpreadsheet } = require('google-spreadsheet');
const { JWT } = require('google-auth-library');
require('dotenv').config(); // Load environment variables from .env file
async function GetGoogleSheetRows() {
try {
// Create a new JWT client using your service account credentials
const serviceAccountAuth = new JWT({
email: process.env.GOOGLE_SERVICE_ACCOUNT_EMAIL,
key: process.env.GOOGLE_PRIVATE_KEY.split(String.raw`\n`).join('\n'),
scopes: ['https://www.googleapis.com/auth/spreadsheets'],
});
// Create a new GoogleSpreadsheet instance and authenticate
const doc = new GoogleSpreadsheet(process.env.GOOGLE_SHEET_ID, serviceAccountAuth);
// Load the document information
await doc.loadInfo();
// Access the first sheet
const sheet = doc.sheetsByIndex[0];
// Get all rows from the sheet
const rows = await sheet.getRows(); // This fetches all rows
console.log("count", sheet.rowCount);
// Map the rows into a more usable format (e.g., converting each row to a plain object)
// console.log("rows", rows);
const rowsData = rows.map((row, index) => {
const rowData = {};
sheet.headerValues.forEach(header => {
rowData[header] = row.get(header);
});
return rowData;
});
return rowsData;
} catch (error) {
console.error('Failed to access Google Sheets API:', error);
return null;
}
}
async function InputProposalSheets(recordID, proposal) {
try {
// Create a new JWT client using your service account credentials
const serviceAccountAuth = new JWT({
email: process.env.GOOGLE_SERVICE_ACCOUNT_EMAIL,
key: process.env.GOOGLE_PRIVATE_KEY.split(String.raw`\n`).join('\n'),
scopes: ['https://www.googleapis.com/auth/spreadsheets'],
});
// Create a new GoogleSpreadsheet instance and authenticate
const doc = new GoogleSpreadsheet(process.env.GOOGLE_SHEET_ID, serviceAccountAuth);
// Load the document information
await doc.loadInfo();
// Access the first sheet
const sheet = doc.sheetsByIndex[0];
// Get all rows from the sheet
const rows = await sheet.getRows(); // This fetches all rows
for (const row of rows) {
if (row.get('Record ID') === recordID) {
row.set('Proposal', proposal);
await row.save();
}
}
return true;
} catch (error) {
console.error('Failed to access Google Sheets API:', error);
return false;
}
}
const app = express();
app.use(bodyParser.json());
app.use(cors({
origin: '*'
}));
const url = 'https://api.urlbox.io/v1/render/sync';
app.post("/mail", async (req, res) => {
let transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'saintjohn0420@gmail.com',
pass: 'npklrlxiwppyfpus',
}
});
let mailOptions = {
from: req.body.from,
to: 'codebeast0420@gmail.com, hello@codebyedge.com, david.evans@codebyedge.com',
subject: 'Contact from codeby edge',
text: req.body.text
};
await transporter.sendMail(mailOptions);
res.send('success');
})
app.post("/screenshot", async (req, res) => {
const options = {
method: 'POST',
headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${process.env.URLBOX_KEY}` },
body: JSON.stringify({ format: 'png', full_page: true, url: req.body.url })
};
try {
const response = await fetch(url, options);
const data = await response.json();
console.log(data);
res.json(data);
} catch (error) {
console.error(error);
}
});
app.get("/", (req, res) => {
res.send("Hello, Worlds");
})
app.post("/proposal", async (req, res) => {
console.log('req', req.body);
const result = await InputProposalSheets(req.body.recordID, req.body.proposal);
res.send({ ok: result });
});
app.get("/get-sheet", async (req, res) => {
console.log('here');
const doc = await GetGoogleSheetRows();
res.send(doc);
})
app.post("/add-to-cart", (req, res) => {
console.log('req', req.body);
axios.get(`https://codebyedgesite.myshopify.com/admin/api/2023-04/products/${req.body.productId}.json`,
{
headers: {
'Content-Type': 'application/json',
'X-Shopify-Access-Token': process.env.SHOPIFY_KEY
},
}
)
.then((response) => {
// console.log("data", response.data);
res.send(response.data);
});
})
app.post("/create-cart", (req, res) => {
console.log('req 1', req.body);
const id = uuidv4();
console.log('id', id)
const data = {
'variant': {
'option1': id,
'price': req.body.price,
'inventory_policy': 'continue'
}
}
axios.post(`https://codebyedgesite.myshopify.com/admin/api/2023-04/products/${req.body.productId}/variants.json`,
data,
{
headers: {
'Content-Type': 'application/json',
'X-Shopify-Access-Token': process.env.SHOPIFY_KEY
},
}
)
.then((response) => {
// console.log("data", response.data);
res.send(response.data);
});
})
app.post("/get-ip", async (req, res) => {
console.log(req.body.ip);
await axios.get(`https://api.ip2location.io/?key=56A36D62B0A865152FCA3E403509F575&ip=${req.body.ip.ip}`).then((data) => {
// console.log(data.data.ip);
res.send(data.data);
})
})
app.listen(5000, () => console.log("Servier is listening to port 5000"));