mirror of
https://github.com/formbricks/formbricks.git
synced 2026-05-02 03:15:05 -05:00
fix: Airtable fix (#5976)
Co-authored-by: Victor Santos <victor@formbricks.com>
This commit is contained in:
committed by
GitHub
parent
e358104f7c
commit
385e8a4262
@@ -13,7 +13,8 @@ import {
|
||||
ZIntegrationAirtableTokenSchema,
|
||||
} from "@formbricks/types/integration/airtable";
|
||||
import { AIRTABLE_CLIENT_ID, AIRTABLE_MESSAGE_LIMIT } from "../constants";
|
||||
import { createOrUpdateIntegration, deleteIntegration, getIntegrationByType } from "../integration/service";
|
||||
import { createOrUpdateIntegration, getIntegrationByType } from "../integration/service";
|
||||
import { delay } from "../utils/promises";
|
||||
import { truncateText } from "../utils/strings";
|
||||
|
||||
export const getBases = async (key: string) => {
|
||||
@@ -99,7 +100,11 @@ export const getAirtableToken = async (environmentId: string) => {
|
||||
});
|
||||
|
||||
if (!newToken) {
|
||||
throw new Error("Failed to create new token");
|
||||
logger.error("Failed to fetch new Airtable token", {
|
||||
environmentId,
|
||||
airtableIntegration,
|
||||
});
|
||||
throw new Error("Failed to fetch new Airtable token");
|
||||
}
|
||||
|
||||
await createOrUpdateIntegration(environmentId, {
|
||||
@@ -116,9 +121,11 @@ export const getAirtableToken = async (environmentId: string) => {
|
||||
|
||||
return access_token;
|
||||
} catch (error) {
|
||||
await deleteIntegration(environmentId);
|
||||
|
||||
throw new Error("invalid token");
|
||||
logger.error("Failed to get Airtable token", {
|
||||
environmentId,
|
||||
error,
|
||||
});
|
||||
throw new Error("Failed to get Airtable token");
|
||||
}
|
||||
};
|
||||
|
||||
@@ -178,6 +185,18 @@ const addField = async (
|
||||
return await req.json();
|
||||
};
|
||||
|
||||
const getExistingFields = async (key: TIntegrationAirtableCredential, baseId: string, tableId: string) => {
|
||||
const req = await tableFetcher(key, baseId);
|
||||
const tables = ZIntegrationAirtableTablesWithFields.parse(req).tables;
|
||||
const currentTable = tables.find((t) => t.id === tableId);
|
||||
|
||||
if (!currentTable) {
|
||||
throw new Error(`Table with ID ${tableId} not found`);
|
||||
}
|
||||
|
||||
return new Set(currentTable.fields.map((f) => f.name));
|
||||
};
|
||||
|
||||
export const writeData = async (
|
||||
key: TIntegrationAirtableCredential,
|
||||
configData: TIntegrationAirtableConfigData,
|
||||
@@ -186,6 +205,7 @@ export const writeData = async (
|
||||
const responses = values[0];
|
||||
const questions = values[1];
|
||||
|
||||
// 1) Build the record payload
|
||||
const data: Record<string, string> = {};
|
||||
for (let i = 0; i < questions.length; i++) {
|
||||
data[questions[i]] =
|
||||
@@ -194,34 +214,73 @@ export const writeData = async (
|
||||
: responses[i];
|
||||
}
|
||||
|
||||
const req = await tableFetcher(key, configData.baseId);
|
||||
const tables = ZIntegrationAirtableTablesWithFields.parse(req).tables;
|
||||
// 2) Figure out which fields need creating
|
||||
const existingFields = await getExistingFields(key, configData.baseId, configData.tableId);
|
||||
const fieldsToCreate = questions.filter((q) => !existingFields.has(q));
|
||||
|
||||
const currentTable = tables.find((table) => table.id === configData.tableId);
|
||||
if (currentTable) {
|
||||
const currentFields = new Set(currentTable.fields.map((field) => field.name));
|
||||
const fieldsToCreate = new Set<string>();
|
||||
for (const field of questions) {
|
||||
const hasField = currentFields.has(field);
|
||||
if (!hasField) {
|
||||
fieldsToCreate.add(field);
|
||||
// 3) Create any missing fields with throttling to respect Airtable's 5 req/sec per base limit
|
||||
if (fieldsToCreate.length > 0) {
|
||||
// Sequential processing with delays
|
||||
const DELAY_BETWEEN_REQUESTS = 250; // 250ms = 4 requests per second (staying under 5/sec limit)
|
||||
|
||||
for (let i = 0; i < fieldsToCreate.length; i++) {
|
||||
const fieldName = fieldsToCreate[i];
|
||||
|
||||
const createRes = await addField(key, configData.baseId, configData.tableId, {
|
||||
name: fieldName,
|
||||
type: "singleLineText",
|
||||
});
|
||||
|
||||
if (createRes?.error) {
|
||||
throw new Error(`Failed to create field "${fieldName}": ${JSON.stringify(createRes)}`);
|
||||
}
|
||||
|
||||
// Add delay between requests (except for the last one)
|
||||
if (i < fieldsToCreate.length - 1) {
|
||||
await delay(DELAY_BETWEEN_REQUESTS);
|
||||
}
|
||||
}
|
||||
|
||||
if (fieldsToCreate.size > 0) {
|
||||
const createFieldPromise: Promise<any>[] = [];
|
||||
fieldsToCreate.forEach((fieldName) => {
|
||||
createFieldPromise.push(
|
||||
addField(key, configData.baseId, configData.tableId, {
|
||||
name: fieldName,
|
||||
type: "singleLineText",
|
||||
})
|
||||
);
|
||||
});
|
||||
// 4) Wait for the new fields to show up
|
||||
await waitForFieldsToExist(key, configData, fieldsToCreate);
|
||||
}
|
||||
|
||||
await Promise.all(createFieldPromise);
|
||||
// 5) Finally, add the records
|
||||
await addRecords(key, configData.baseId, configData.tableId, data);
|
||||
};
|
||||
|
||||
async function waitForFieldsToExist(
|
||||
key: TIntegrationAirtableCredential,
|
||||
configData: TIntegrationAirtableConfigData,
|
||||
fieldNames: string[],
|
||||
maxRetries = 5,
|
||||
intervalMs = 2000
|
||||
) {
|
||||
let existingFields: Set<string> = new Set(),
|
||||
missingFields: string[] = [];
|
||||
|
||||
for (let attempt = 1; attempt <= maxRetries; attempt++) {
|
||||
existingFields = await getExistingFields(key, configData.baseId, configData.tableId);
|
||||
missingFields = fieldNames.filter((f) => !existingFields.has(f));
|
||||
|
||||
if (missingFields.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (attempt < maxRetries) {
|
||||
logger.error(
|
||||
`Attempt ${attempt}/${maxRetries}: ${missingFields.length} field(s) still missing [${missingFields.join(
|
||||
", "
|
||||
)}], retrying in ${intervalMs / 1000}s…`
|
||||
);
|
||||
|
||||
await new Promise((r) => setTimeout(r, intervalMs));
|
||||
}
|
||||
}
|
||||
|
||||
await addRecords(key, configData.baseId, configData.tableId, data);
|
||||
};
|
||||
throw new Error(
|
||||
`Timed out waiting for ${missingFields.length} field(s) [${missingFields.join(
|
||||
", "
|
||||
)}] to become available. Available fields: [${Array.from(existingFields).join(", ")}]`
|
||||
);
|
||||
}
|
||||
|
||||
@@ -79,7 +79,6 @@
|
||||
},
|
||||
"signup_without_verification_success": {
|
||||
"user_successfully_created": "Benutzer erfolgreich erstellt",
|
||||
"user_successfully_created_description": "Dein neuer Benutzer wurde erfolgreich erstellt. Bitte klicke auf den untenstehenden Button und melde Dich in deinem Konto an.",
|
||||
"user_successfully_created_info": "Wir haben nach einem Konto gesucht, das mit {email} verknüpft ist. Wenn keines existierte, haben wir eines für Dich erstellt. Wenn bereits ein Konto existierte, wurden keine Änderungen vorgenommen. Bitte melde Dich unten an, um fortzufahren."
|
||||
},
|
||||
"testimonial_1": "Als open-source Firma ist uns Datenschutz extrem wichtig! Formbricks bietet die perfekte Mischung aus modernster Technologie und solidem Datenschutz.",
|
||||
@@ -96,7 +95,6 @@
|
||||
"resend_verification_email": "Bestätigungs-E-Mail erneut senden",
|
||||
"verification_email_resent_successfully": "Bestätigungs-E-Mail gesendet! Bitte überprüfe dein Postfach.",
|
||||
"verification_email_successfully_sent_info": "Wenn ein Konto mit {email} verknüpft ist, haben wir einen Bestätigungslink an diese Adresse gesendet. Bitte überprüfe dein Postfach, um die Anmeldung abzuschließen.",
|
||||
"we_sent_an_email_to": "Wir haben eine E-Mail an {email} gesendet",
|
||||
"you_didnt_receive_an_email_or_your_link_expired": "Hast Du keine E-Mail erhalten oder ist dein Link abgelaufen?"
|
||||
},
|
||||
"verify": {
|
||||
|
||||
@@ -79,7 +79,6 @@
|
||||
},
|
||||
"signup_without_verification_success": {
|
||||
"user_successfully_created": "User successfully created",
|
||||
"user_successfully_created_description": "Your new user has been created successfully. Please click the button below and sign in to your account.",
|
||||
"user_successfully_created_info": "We’ve checked for an account associated with {email}. If none existed, we’ve created one for you. If an account already existed, no changes were made. Please log in below to continue."
|
||||
},
|
||||
"testimonial_1": "We measure the clarity of our docs and learn from churn all on one platform. Great product, very responsive team!",
|
||||
@@ -96,7 +95,6 @@
|
||||
"resend_verification_email": "Resend verification email",
|
||||
"verification_email_resent_successfully": "Verification email sent! Please check your inbox.",
|
||||
"verification_email_successfully_sent_info": "If there’s an account associated with {email}, we’ve sent a verification link to that address. Please check your inbox to complete the sign-up.",
|
||||
"we_sent_an_email_to": "We sent an email to {email}. ",
|
||||
"you_didnt_receive_an_email_or_your_link_expired": "You didn't receive an email or your link expired?"
|
||||
},
|
||||
"verify": {
|
||||
|
||||
@@ -79,7 +79,6 @@
|
||||
},
|
||||
"signup_without_verification_success": {
|
||||
"user_successfully_created": "Utilisateur créé avec succès",
|
||||
"user_successfully_created_description": "Votre nouvel utilisateur a été créé avec succès. Veuillez cliquer sur le bouton ci-dessous et vous connecter à votre compte.",
|
||||
"user_successfully_created_info": "Nous avons vérifié s'il existait un compte associé à {email}. Si aucun n'existait, nous en avons créé un pour vous. Si un compte existait déjà, aucune modification n'a été apportée. Veuillez vous connecter ci-dessous pour continuer."
|
||||
},
|
||||
"testimonial_1": "Nous mesurons la clarté de nos documents et apprenons des abandons, le tout sur une seule plateforme. Excellent produit, équipe très réactive !",
|
||||
@@ -96,7 +95,6 @@
|
||||
"resend_verification_email": "Renvoyer l'email de vérification",
|
||||
"verification_email_resent_successfully": "E-mail de vérification envoyé ! Veuillez vérifier votre boîte de réception.",
|
||||
"verification_email_successfully_sent_info": "Si un compte est associé à {email}, nous avons envoyé un lien de vérification à cette adresse. Veuillez vérifier votre boîte de réception pour terminer l'inscription.",
|
||||
"we_sent_an_email_to": "Nous avons envoyé un email à {email}",
|
||||
"you_didnt_receive_an_email_or_your_link_expired": "Vous n'avez pas reçu d'email ou votre lien a expiré ?"
|
||||
},
|
||||
"verify": {
|
||||
|
||||
@@ -79,7 +79,6 @@
|
||||
},
|
||||
"signup_without_verification_success": {
|
||||
"user_successfully_created": "Usuário criado com sucesso",
|
||||
"user_successfully_created_description": "Seu novo usuário foi criado com sucesso. Por favor, clique no botão abaixo e faça login na sua conta.",
|
||||
"user_successfully_created_info": "Verificamos se há uma conta associada a {email}. Se não existia, criamos uma para você. Se uma conta já existia, nenhuma alteração foi feita. Por favor, faça login abaixo para continuar."
|
||||
},
|
||||
"testimonial_1": "Mediamos a clareza dos nossos documentos e aprendemos com a rotatividade tudo em uma única plataforma. Ótimo produto, equipe muito atenciosa!",
|
||||
@@ -96,7 +95,6 @@
|
||||
"resend_verification_email": "Reenviar e-mail de verificação",
|
||||
"verification_email_resent_successfully": "E-mail de verificação enviado! Por favor, verifique sua caixa de entrada.",
|
||||
"verification_email_successfully_sent_info": "Se houver uma conta associada a {email}, enviamos um link de verificação para esse endereço. Por favor, verifique sua caixa de entrada para completar o cadastro.",
|
||||
"we_sent_an_email_to": "Enviamos um email para {email}",
|
||||
"you_didnt_receive_an_email_or_your_link_expired": "Você não recebeu um e-mail ou seu link expirou?"
|
||||
},
|
||||
"verify": {
|
||||
|
||||
@@ -79,7 +79,6 @@
|
||||
},
|
||||
"signup_without_verification_success": {
|
||||
"user_successfully_created": "Utilizador criado com sucesso",
|
||||
"user_successfully_created_description": "O seu novo utilizador foi criado com sucesso. Por favor, clique no botão abaixo e inicie sessão na sua conta.",
|
||||
"user_successfully_created_info": "Verificámos a existência de uma conta associada a {email}. Se não existia, criámos uma para si. Se já existia uma conta, não foram feitas alterações. Por favor, inicie sessão abaixo para continuar."
|
||||
},
|
||||
"testimonial_1": "Medimos a clareza dos nossos documentos e aprendemos com a rotatividade, tudo numa só plataforma. Ótimo produto, equipa muito responsiva!",
|
||||
@@ -96,7 +95,6 @@
|
||||
"resend_verification_email": "Reenviar email de verificação",
|
||||
"verification_email_resent_successfully": "Email de verificação enviado! Por favor, verifique a sua caixa de entrada.",
|
||||
"verification_email_successfully_sent_info": "Se houver uma conta associada a {email}, enviámos um link de verificação para esse endereço. Por favor, verifique a sua caixa de entrada para completar o registo.",
|
||||
"we_sent_an_email_to": "Enviámos um email para {email}. ",
|
||||
"you_didnt_receive_an_email_or_your_link_expired": "Não recebeu um email ou o seu link expirou?"
|
||||
},
|
||||
"verify": {
|
||||
|
||||
@@ -79,7 +79,6 @@
|
||||
},
|
||||
"signup_without_verification_success": {
|
||||
"user_successfully_created": "使用者建立成功",
|
||||
"user_successfully_created_description": "您的新使用者已成功建立。請點擊下方按鈕並登入您的帳戶。",
|
||||
"user_successfully_created_info": "我們已檢查與 {email} 相關聯的帳戶。如果不存在,我們已為您建立一個。如果帳戶已存在,則未進行任何更改。請在下方登入以繼續。"
|
||||
},
|
||||
"testimonial_1": "我們在同一個平台上測量文件的清晰度,並從客戶流失中學習。很棒的產品,團隊反應非常迅速!",
|
||||
@@ -96,7 +95,6 @@
|
||||
"resend_verification_email": "重新發送驗證電子郵件",
|
||||
"verification_email_resent_successfully": "驗證電子郵件已發送!請檢查您的收件箱。",
|
||||
"verification_email_successfully_sent_info": "如果有一個帳戶與 {email} 相關聯,我們已發送驗證連結至該地址。請檢查您的收件箱以完成註冊。",
|
||||
"we_sent_an_email_to": "我們已發送一封電子郵件至 <email>'{'email'}'</email>。",
|
||||
"you_didnt_receive_an_email_or_your_link_expired": "您沒有收到電子郵件或您的連結已過期?"
|
||||
},
|
||||
"verify": {
|
||||
|
||||
Reference in New Issue
Block a user