2023-09-20 15:26:52 +02:00
|
|
|
import { TRPCError } from '@trpc/server'
|
|
|
|
|
import { GoogleSpreadsheet } from 'google-spreadsheet'
|
2023-12-18 15:43:58 +01:00
|
|
|
import { getAuthenticatedGoogleClient } from '@typebot.io/lib/google'
|
2023-09-20 15:26:52 +02:00
|
|
|
|
|
|
|
|
export const getAuthenticatedGoogleDoc = async ({
|
|
|
|
|
credentialsId,
|
|
|
|
|
spreadsheetId,
|
|
|
|
|
}: {
|
|
|
|
|
credentialsId?: string
|
|
|
|
|
spreadsheetId?: string
|
|
|
|
|
}) => {
|
|
|
|
|
if (!credentialsId || !spreadsheetId)
|
|
|
|
|
throw new TRPCError({
|
|
|
|
|
code: 'BAD_REQUEST',
|
|
|
|
|
message: 'Missing credentialsId or spreadsheetId',
|
|
|
|
|
})
|
|
|
|
|
const auth = await getAuthenticatedGoogleClient(credentialsId)
|
|
|
|
|
if (!auth)
|
|
|
|
|
throw new TRPCError({
|
|
|
|
|
code: 'NOT_FOUND',
|
|
|
|
|
message: "Couldn't find credentials in database",
|
|
|
|
|
})
|
|
|
|
|
return new GoogleSpreadsheet(spreadsheetId, auth)
|
|
|
|
|
}
|