2
0

feat(integration): ️ Improve feedback on GSheets errors

This commit is contained in:
Baptiste Arnaud
2022-03-04 14:40:13 +01:00
parent 9b8f153579
commit d13ca0fa9a
6 changed files with 45 additions and 16 deletions

View File

@@ -12,14 +12,14 @@ export const oauth2Client = new OAuth2Client(
export const getAuthenticatedGoogleClient = async (
credentialsId: string
): Promise<OAuth2Client> => {
): Promise<OAuth2Client | undefined> => {
const credentials = (await prisma.credentials.findFirst({
where: { id: credentialsId },
})) as CredentialsFromDb
const data = decrypt(
credentials.data,
credentials.iv
) as GoogleSheetsCredentialsData
const data = decrypt(credentials.data, credentials.iv) as
| GoogleSheetsCredentialsData
| undefined
if (!data) return
oauth2Client.setCredentials(data)
oauth2Client.on('tokens', updateTokens(credentialsId))
return oauth2Client

View File

@@ -23,7 +23,10 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
)
if (!extractingColumns) return badRequest(res)
const doc = new GoogleSpreadsheet(spreadsheetId)
doc.useOAuth2Client(await getAuthenticatedGoogleClient(credentialsId))
const client = await getAuthenticatedGoogleClient(credentialsId)
if (!client)
return res.status(404).send("Couldn't find credentials in database")
doc.useOAuth2Client(client)
await doc.loadInfo()
const sheet = doc.sheetsById[sheetId]
const rows = await sheet.getRows()
@@ -48,7 +51,10 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
values: { [key: string]: string }
}
const doc = new GoogleSpreadsheet(spreadsheetId)
doc.useOAuth2Client(await getAuthenticatedGoogleClient(credentialsId))
const auth = await getAuthenticatedGoogleClient(credentialsId)
if (!auth)
return res.status(404).send("Couldn't find credentials in database")
doc.useOAuth2Client(auth)
await doc.loadInfo()
const sheet = doc.sheetsById[sheetId]
await sheet.addRow(values)
@@ -65,7 +71,10 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
values: { [key: string]: string }
}
const doc = new GoogleSpreadsheet(spreadsheetId)
doc.useOAuth2Client(await getAuthenticatedGoogleClient(credentialsId))
const auth = await getAuthenticatedGoogleClient(credentialsId)
if (!auth)
return res.status(404).send("Couldn't find credentials in database")
doc.useOAuth2Client(auth)
await doc.loadInfo()
const sheet = doc.sheetsById[sheetId]
const rows = await sheet.getRows()