Skip to content

Commit

Permalink
: πŸ”’οΈ Investigate on why spreadsheets sometimes fail
Browse files Browse the repository at this point in the history
  • Loading branch information
baptisteArno committed Apr 20, 2022
1 parent b175974 commit bdd7a17
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 13 deletions.
10 changes: 6 additions & 4 deletions apps/builder/libs/google-sheets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ export const oauth2Client = new OAuth2Client(
export const getAuthenticatedGoogleClient = async (
userId: string,
credentialsId: string
): Promise<OAuth2Client | undefined> => {
const credentials = (await prisma.credentials.findUnique({
where: { id: credentialsId },
): Promise<
{ client: OAuth2Client; credentials: CredentialsFromDb } | undefined
> => {
const credentials = (await prisma.credentials.findFirst({
where: { id: credentialsId, ownerId: userId },
})) as CredentialsFromDb | undefined
if (!credentials || credentials.ownerId !== userId) return
const data = decrypt(
Expand All @@ -25,7 +27,7 @@ export const getAuthenticatedGoogleClient = async (

oauth2Client.setCredentials(data)
oauth2Client.on('tokens', updateTokens(credentialsId, data))
return oauth2Client
return { client: oauth2Client, credentials }
}

const updateTokens =
Expand Down
2 changes: 1 addition & 1 deletion apps/builder/mocks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const handlers = () => [
const authenticatedUser = JSON.parse(
typeof localStorage !== 'undefined'
? (localStorage.getItem('authenticatedUser') as string)
: '{"id":"proUser","name":"John Smith","email":"john@smith.com","emailVerified":null,"image":"https://github.com/avatars/u/16015833?v=4","plan":"PRO","stripeId":null}'
: '{"id":"proUser","name":"Pro user","email":"pro-user@email.com","emailVerified":null,"image":"https://github.com/avatars/u/16015833?v=4","plan":"PRO","stripeId":null}'
)
return res(
ctx.json({
Expand Down
20 changes: 17 additions & 3 deletions apps/builder/pages/api/integrations/google-sheets/spreadsheets.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { NextApiRequest, NextApiResponse } from 'next'
import { drive } from '@googleapis/drive'
import { getAuthenticatedGoogleClient } from 'libs/google-sheets'
import { badRequest, methodNotAllowed, notAuthenticated } from 'utils'
import { setUser, withSentry } from '@sentry/nextjs'
import {
badRequest,
forbidden,
methodNotAllowed,
notAuthenticated,
} from 'utils'
import { captureException, setUser, withSentry } from '@sentry/nextjs'
import { getAuthenticatedUser } from 'services/api/utils'

const handler = async (req: NextApiRequest, res: NextApiResponse) => {
Expand All @@ -16,9 +21,18 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
const auth = await getAuthenticatedGoogleClient(user.id, credentialsId)
if (!auth)
return res.status(404).send("Couldn't find credentials in database")
console.log(auth.credentials.name, user.email)
if (auth.credentials.name !== user.email) {
captureException(
new Error(
`Credentials name does not match user email ${auth?.credentials.name} !== ${user.email}`
)
)
return forbidden(res)
}
const response = await drive({
version: 'v3',
auth: auth,
auth: auth.client,
}).files.list({
q: "mimeType='application/vnd.google-apps.spreadsheet'",
fields: 'nextPageToken, files(id, name)',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {

const spreadsheetId = req.query.id.toString()
const doc = new GoogleSpreadsheet(spreadsheetId)
const client = await getAuthenticatedGoogleClient(user.id, credentialsId)
if (!client)
const auth = await getAuthenticatedGoogleClient(user.id, credentialsId)
if (!auth)
return res
.status(404)
.send({ message: "Couldn't find credentials in database" })
doc.useOAuth2Client(client)
doc.useOAuth2Client(auth.client)
await doc.loadInfo()
return res.send({
sheets: (
Expand Down
2 changes: 1 addition & 1 deletion apps/builder/playwright/services/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ const createCredentials = () => {
return prisma.credentials.createMany({
data: [
{
name: 'test2@gmail.com',
name: 'pro-user@email.com',
ownerId: 'proUser',
type: CredentialsType.GOOGLE_SHEETS,
data: encryptedData,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ test.describe.parallel('Google sheets integration', () => {
const fillInSpreadsheetInfo = async (page: Page) => {
await page.click('text=Configure...')
await page.click('text=Select an account')
await page.click('text=test2@gmail.com')
await page.click('text=pro-user@email.com')

await page.fill('input[placeholder="Search for spreadsheet"]', 'CR')
await page.click('text=CRM')
Expand Down

4 comments on commit bdd7a17

@vercel
Copy link

@vercel vercel bot commented on bdd7a17 Apr 20, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on bdd7a17 Apr 20, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

landing-page-v2 – ./apps/landing-page

get-typebot.com
typebot.io
www.typebot.io
landing-page-v2-typebot-io.vercel.app
landing-page-v2-git-main-typebot-io.vercel.app
www.get-typebot.com

@vercel
Copy link

@vercel vercel bot commented on bdd7a17 Apr 20, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

builder-v2 – ./apps/builder

builder-v2-git-main-typebot-io.vercel.app
app.typebot.io
builder-v2-typebot-io.vercel.app

Please sign in to comment.