Created
September 21, 2023 16:01
-
-
Save leifermendez/d60a549d065664cae94b16af63b45474 to your computer and use it in GitHub Desktop.
sheet.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { JWT } = require("google-auth-library"); | |
const { GoogleSpreadsheet } = require("google-spreadsheet"); | |
const SCOPES = [ | |
"https://www.googleapis.com/auth/spreadsheets", | |
"https://www.googleapis.com/auth/drive.file", | |
]; | |
class GoogleSheetService { | |
jwtFromEnv = undefined; | |
doc = undefined; | |
constructor(id = undefined) { | |
if (!id) { | |
throw new Error("ID_UNDEFINED"); | |
} | |
this.jwtFromEnv = new JWT({ | |
email: process.env.GOOGLE_SERVICE_ACCOUNT_EMAIL, | |
key: process.env.GOOGLE_PRIVATE_KEY.replace(/\\n/g, "\n"), | |
scopes: SCOPES, | |
}); | |
this.doc = new GoogleSpreadsheet(id, this.jwtFromEnv); | |
} | |
/** | |
* Guardar pedido | |
* @param {*} data | |
*/ | |
saveOrder = async (data = {}) => { | |
await this.doc.loadInfo(); | |
const sheet = this.doc.sheetsByIndex[0]; | |
const order = await sheet.addRow({ | |
date: data.date, | |
phone: `${data.phone.slice(0,4)}********`, | |
order: data.order, | |
total: data.total, | |
}); | |
return order; | |
}; | |
} | |
module.exports = GoogleSheetService |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment