phanquocdat123 commited on
Commit
4c36df5
·
verified ·
1 Parent(s): cba3206

Create orderService.js

Browse files
Files changed (1) hide show
  1. services/orderService.js +26 -0
services/orderService.js ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const { google } = require('googleapis');
2
+
3
+ async function saveOrderToSheet(orderData) {
4
+ const auth = new google.auth.GoogleAuth({
5
+ keyFile: 'credentials.json', // File key của Google Cloud
6
+ scopes: ['https://www.googleapis.com/auth/spreadsheets'],
7
+ });
8
+ const sheets = google.sheets({ version: 'v4', auth });
9
+
10
+ const values = [[
11
+ new Date().toLocaleString(),
12
+ `DH${Date.now()}`,
13
+ orderData.phone,
14
+ orderData.address,
15
+ orderData.items.join(", "),
16
+ orderData.totalPrice,
17
+ orderData.weight
18
+ ]];
19
+
20
+ await sheets.spreadsheets.values.append({
21
+ spreadsheetId: process.env.SPREADSHEET_ID,
22
+ range: 'khachhang!A:G',
23
+ valueInputOption: 'USER_ENTERED',
24
+ resource: { values },
25
+ });
26
+ }