2
0

Add OpenAI block

Also migrate credentials to tRPC

Closes #253
This commit is contained in:
Baptiste Arnaud
2023-03-09 08:46:36 +01:00
parent 97cfdfe79f
commit ff04edf139
86 changed files with 2583 additions and 1055 deletions

View File

@@ -1,6 +1,6 @@
import { Credentials as CredentialsFromDb } from 'db'
import { OAuth2Client, Credentials } from 'google-auth-library'
import { GoogleSheetsCredentialsData } from 'models'
import { GoogleSheetsCredentials } from 'models'
import { isDefined } from 'utils'
import { decrypt, encrypt } from 'utils/api'
import prisma from './prisma'
@@ -24,7 +24,7 @@ export const getAuthenticatedGoogleClient = async (
const data = decrypt(
credentials.data,
credentials.iv
) as GoogleSheetsCredentialsData
) as GoogleSheetsCredentials['data']
oauth2Client.setCredentials(data)
oauth2Client.on('tokens', updateTokens(credentials.id, data))
@@ -32,14 +32,17 @@ export const getAuthenticatedGoogleClient = async (
}
const updateTokens =
(credentialsId: string, existingCredentials: GoogleSheetsCredentialsData) =>
(
credentialsId: string,
existingCredentials: GoogleSheetsCredentials['data']
) =>
async (credentials: Credentials) => {
if (
isDefined(existingCredentials.id_token) &&
credentials.id_token !== existingCredentials.id_token
)
return
const newCredentials: GoogleSheetsCredentialsData = {
const newCredentials: GoogleSheetsCredentials['data'] = {
...existingCredentials,
expiry_date: credentials.expiry_date,
access_token: credentials.access_token,