AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents
TypeScript ai-agentai-agent-toolsai-agentsai-agents-frameworkmcpmcp-servermcp-toolsmcpsn8n-alternativeno-code-automationworkflowworkflow-automationworkflows
fix(tables): importing the wrong JSON file no longer empties your table (#15711)
Bartosz Majewski52476340b271
diff --git a/brain/knowledge/data-storage-observability/tables.md b/brain/knowledge/data-storage-observability/tables.mdindex 48c24ff1bc5..5dfac4a3e9e 100644--- a/brain/knowledge/data-storage-observability/tables.md+++ b/brain/knowledge/data-storage-observability/tables.md@@ -30,6 +30,10 @@ A built-in relational database inside Activepieces: users store structured data- The web table store keys each cell by the column's position (`fieldIndex`), not by field id, so every field mutator in `ap-tables-client-state.tsx` has to renumber surviving cells (`deleteField` didn't, see ENG-537). A cell edit sends only the edited cell, and `record.update` upserts just those cells, so stale indexes can't blank other columns on the server.- Concurrent field reorders are last-write-wins, same as rename — no distributed lock. Reordering *existing* fields through project-release apply is not supported: `FieldState` carries no position, so array order only applies to newly created fields.- The web client stores positional `cell.fieldIndex` references, so it must remap every record's cells when fields move.+- JSON import into an **existing** table is client-orchestrated and destructive-first: `tableHooks.importTableIntoExisting` clears records, deletes every field, renames, then recreates, as separate HTTP calls with no transaction. A failure after the clear leaves the table empty, so the import dialog validates the uploaded file with `tablesUtils.getTemplateError` in its form resolver, before the user can click Import. The import functions trust their typed input and do not re-validate; any new caller that feeds them unknown JSON must run the same check first. It checks the table name, the columns, the row structure, the field types and the dropdown options; `TableTemplate.fields[].type` is a bare `z.string()`, so the type check can't come from that schema. `externalId` is optional because already-exported files and `POST /v1/fields` allow it missing. Still unguarded: `MAX_FIELDS_PER_TABLE`, rows whose `fieldId` matches no column (they import as empty records), and a network failure mid-sequence. The client trims rows to the record limit, so the limit only bites if another writer adds rows during the import.+- Field `externalId` is optional on `POST /v1/fields` (`CreateFieldRequest`) but required on `POST /v1/tables`, whose `fields` are `FieldState[]`. So a template without column `externalId`s imports into an existing table but gets a 400 when used to create a new one.+- Re-importing into an existing table fires record webhooks for every row: the clear sends `deleted` and the insert sends `created`, so a flow on the deleted trigger runs once per old row and a flow on the created trigger runs once per imported row.+- CSV import only appends rows to an open table. It maps CSV columns onto the table's existing fields through the table store, so the import dialog offers CSV only when it has both a `tableId` and that store, which means only inside the table editor. Elsewhere the dialog accepts JSON only. Columns map by position: an empty value imports as a blank cell, but a row missing a middle column shifts every later value into the wrong field.### Key filesEntry point: `tablesModule`, registered in `packages/server/api/src/app/app.ts` and mounting the three controllers under `/v1/tables`, `/v1/fields`, `/v1/records`.diff --git a/brain/knowledge/platform-editions-ee/embed.md b/brain/knowledge/platform-editions-ee/embed.mdindex d01dbe7528d..157b105f684 100644--- a/brain/knowledge/platform-editions-ee/embed.md+++ b/brain/knowledge/platform-editions-ee/embed.md@@ -75,6 +75,7 @@ The Embed Onboarding stepper at `/platform/embedding`. Four steps exist as files- **Third-party tag failures surface as `Script error.` with no stack.** A cross-origin script (GTM tag, Clarity) that throws reaches `window.onerror` with `event.error == null` and the message `Script error.`; nothing in it is actionable. We deliberately left frontend error reporting untouched (2026-09-10: a global or embed-scoped filter was tried on #15442 and reverted to keep the change contained), so a vendor's broken tag shows up in Sentry as that opaque message. A tag script that fails to *load* removes its own `<script>` element and warns, so `isScriptLoaded` does not mistake a dead element for a loaded one.- **Embed analytics see one page per session.** The embedded app runs on a memory router, so `location` stays at `/embed` while the user moves between the flows list and the builder. GTM's History Change trigger and GA4's page_view therefore fire once at load, and Clarity records one long page. We push nothing but GTM's required `gtm.js` start event; DOM-level tracking (clicks, scroll, visibility) is the vendor's GTM configuration and needs no code from us. Route-level or app-semantic events (`route_change`, `flow_published`, …) would be small `dataLayer.push` calls mirroring what `handleClientNavigation` already posts to the vendor over `CLIENT_ROUTE_CHANGED` — not built until a customer asks.- **`VENDOR_INIT` trusts `event.source`, not `event.origin`.** The client accepts the message from `window.opener ?? window.parent`; the real trust gate is the external-token exchange succeeding. Gate anything sensitive (script injection, storage) on that `onSuccess`, not on receipt of `VENDOR_INIT`.+- **Links to activepieces.com docs must be hidden in the embed.** The embed is white-labeled, so hide any in-app "Learn more" or docs link when `useEmbedding().embedState.isEmbedded` is true. Existing examples: the formula search popover's "See All", the pieces selector's request-piece button, the table import dialog's template-format link.## Key Filesdiff --git a/docs/tables/import-export.mdx b/docs/tables/import-export.mdxindex 697b9b97158..375c67eac2a 100644--- a/docs/tables/import-export.mdx+++ b/docs/tables/import-export.mdx@@ -15,6 +15,53 @@ If a CSV can't be parsed, the import fails rather than importing part of it. CheA table holds [10,000 records](/install/reference/environment-variables) by default. A file with more rows than the table has room left for still imports, but only up to that limit and the remaining rows are dropped without an error, so check the record count afterwards.</Warning>+## Importing a JSON template++A JSON template holds a table's fields and, optionally, its records. To get one, use **Export Template** on a table. Import it from the tables list to create a new table, or from inside a table to replace that table.++<Warning>+Importing a template into an existing table deletes all of its fields and records, then rebuilds it from the file. There's no undo.+</Warning>++Activepieces checks the file before changing anything. If the file doesn't match the format below, the dialog shows which column or part of the file is wrong and nothing is imported.++```json+{+ "name": "Customers",+ "externalId": "customers",+ "fields": [+ { "name": "Email", "type": "TEXT", "externalId": "email" },+ {+ "name": "Plan",+ "type": "STATIC_DROPDOWN",+ "externalId": "plan",+ "data": { "options": [{ "value": "Free" }, { "value": "Pro" }] }+ }+ ],+ "data": {+ "type": "CSV",+ "rows": [+ [+ { "fieldId": "email", "value": "[email protected]" },+ { "fieldId": "plan", "value": "Pro" }+ ]+ ]+ }+}+```++Each column's `type` must be one of these:++| `type` | Field type |+|---|---|+| `TEXT` | Text |+| `NUMBER` | Number |+| `DATE` | Date |+| `DATETIME` | Date & Time |+| `STATIC_DROPDOWN` | Dropdown. Also needs `data.options` with its values. |++The table and every column need an `externalId`. A cell's value is only imported when its `fieldId` matches a column's `externalId`. A row whose cells match no column is imported as an empty record. Rows beyond the table's record limit are dropped.+## Exporting- **Download data** from the table, for a spreadsheet or a backupdiff --git a/packages/core/utils/package.json b/packages/core/utils/package.jsonindex f0d13a09580..03daeca95b6 100644--- a/packages/core/utils/package.json+++ b/packages/core/utils/package.json@@ -1,6 +1,6 @@{"name": "@activepieces/core-utils",- "version": "0.10.1",+ "version": "0.10.2","type": "commonjs","main": "./dist/src/index.js","scripts": {diff --git a/packages/core/utils/src/lib/form-errors.ts b/packages/core/utils/src/lib/form-errors.tsindex 4c2ed31e164..b2728aff806 100644--- a/packages/core/utils/src/lib/form-errors.ts+++ b/packages/core/utils/src/lib/form-errors.ts@@ -1,27 +1,34 @@+// formErrors values are translation keys, but they reach the UI as Zod messages, not as t('...') calls.+// i18n:extract (packages/web/i18next-parser.config.js) treats translationKey('...') like t('...'),+// so it adds every key here to translation.json instead of deleting it. Keep each value wrapped.+function translationKey<K extends string>(key: K): K {+ return key+}+export const formErrors = {- required: 'required',- invalidGitRepoSlug: 'invalidGitRepoSlug',- invalidGitRepoBranch: 'invalidGitRepoBranch',- invalidGitRepoRemoteUrl: 'invalidGitRepoRemoteUrl',- invalidExternalId: 'invalidExternalId',- invalidFileName: 'invalidFileName',- messageRequiresContentOrFiles: 'messageRequiresContentOrFiles',- agentConfigTooLarge: 'agentConfigTooLarge',- invalidGcpResourceId: 'invalidGcpResourceId',- duplicateRouteName: 'duplicateRouteName',- atLeastOne: 'atLeastOne',- wholeNumber: 'wholeNumber',- atMostTenThousand: 'atMostTenThousand',- tierNameTooLong: 'tierNameTooLong',- tierDescriptionTooLong: 'tierDescriptionTooLong',- invalidEmoji: 'invalidEmoji',- tierTooManyModels: 'tierTooManyModels',- tierDuplicateModel: 'tierDuplicateModel',- tierThinkingBudgetInvalid: 'tierThinkingBudgetInvalid',- invalidHeaderName: 'invalidHeaderName',- reservedHeaderName: 'reservedHeaderName',- duplicateHeaderName: 'duplicateHeaderName',- invalidHeaderValue: 'invalidHeaderValue',+ required: translationKey('required'),+ invalidGitRepoSlug: translationKey('invalidGitRepoSlug'),+ invalidGitRepoBranch: translationKey('invalidGitRepoBranch'),+ invalidGitRepoRemoteUrl: translationKey('invalidGitRepoRemoteUrl'),+ invalidExternalId: translationKey('invalidExternalId'),+ invalidFileName: translationKey('invalidFileName'),+ messageRequiresContentOrFiles: translationKey('messageRequiresContentOrFiles'),+ agentConfigTooLarge: translationKey('agentConfigTooLarge'),+ invalidGcpResourceId: translationKey('invalidGcpResourceId'),+ duplicateRouteName: translationKey('duplicateRouteName'),+ atLeastOne: translationKey('atLeastOne'),+ wholeNumber: translationKey('wholeNumber'),+ atMostTenThousand: translationKey('atMostTenThousand'),+ tierNameTooLong: translationKey('tierNameTooLong'),+ tierDescriptionTooLong: translationKey('tierDescriptionTooLong'),+ invalidEmoji: translationKey('invalidEmoji'),+ tierTooManyModels: translationKey('tierTooManyModels'),+ tierDuplicateModel: translationKey('tierDuplicateModel'),+ tierThinkingBudgetInvalid: translationKey('tierThinkingBudgetInvalid'),+ invalidHeaderName: translationKey('invalidHeaderName'),+ reservedHeaderName: translationKey('reservedHeaderName'),+ duplicateHeaderName: translationKey('duplicateHeaderName'),+ invalidHeaderValue: translationKey('invalidHeaderValue'),} as constexport const SAFE_EXTERNAL_ID_PATTERN = /^(?!\.{1,2}$)[A-Za-z0-9._-]{1,128}$/diff --git a/packages/web/i18next-parser.config.js b/packages/web/i18next-parser.config.jsindex 204d87fefcf..c9c3a30e8a9 100644--- a/packages/web/i18next-parser.config.js+++ b/packages/web/i18next-parser.config.js@@ -1,15 +1,15 @@module.exports = {locales: ['en', 'fr', 'de', 'nl', 'ja', 'es', 'zh', 'pt' ,'zh-TW'], // Your supported languagesoutput: 'packages/web/public/locales/$LOCALE/$NAMESPACE.json', // Where to output the JSON files- input: ['src/**/*.{js,jsx,ts,tsx}'], // Where to find your React files+ input: ['src/**/*.{js,jsx,ts,tsx}', '../core/utils/src/lib/form-errors.ts'], // Where to find your React files, plus the formErrors keysdefaultNamespace: 'translation', // Default namespace if not specifiedcreateOldCatalogs: false, // Don’t maintain the existing structure with old keysdefaultValue: (locale, namespace, key) => (locale === 'en' ? key : ''), // en is the Crowdin source: value must equal the key, other locales stay empty until translatedlexers: {- js: ['JavascriptLexer'],- jsx: ['JavascriptLexer'],- ts: ['JavascriptLexer'],- tsx: ['JavascriptLexer'],+ js: [{ lexer: 'JavascriptLexer', functions: ['t', 'translationKey'] }],+ jsx: [{ lexer: 'JavascriptLexer', functions: ['t', 'translationKey'] }],+ ts: [{ lexer: 'JavascriptLexer', functions: ['t', 'translationKey'] }],+ tsx: [{ lexer: 'JavascriptLexer', functions: ['t', 'translationKey'] }],},keepRemoved: false,keySeparator: false, // Disable key separatordiff --git a/packages/web/public/locales/en/translation.json b/packages/web/public/locales/en/translation.jsonindex e8e5baf6e34..46260ed97a1 100644--- a/packages/web/public/locales/en/translation.json+++ b/packages/web/public/locales/en/translation.json@@ -2949,6 +2949,28 @@"Highlights": "Highlights","Read": "Read","Write": "Write",+ "invalidGitRepoSlug": "Slug may only contain letters, numbers, dot, dash and underscore (up to 128 characters)",+ "invalidGitRepoBranch": "Branch name must not start with a dash and may only contain letters, numbers, dot, dash, slash and underscore",+ "invalidGitRepoRemoteUrl": "Remote URL must be in the form git@host:path",+ "invalidExternalId": "External ID may only contain letters, numbers, dot, dash and underscore (up to 128 characters)",+ "invalidFileName": "File name contains invalid characters",+ "messageRequiresContentOrFiles": "Message must have content or files",+ "agentConfigTooLarge": "Agent configuration is too large",+ "invalidGcpResourceId": "Use lowercase letters, numbers and hyphens only",+ "duplicateRouteName": "Route names must be unique",+ "atLeastOne": "Must be at least 1",+ "wholeNumber": "Must be a whole number",+ "atMostTenThousand": "Must be at most 10,000",+ "tierNameTooLong": "Tier name must be 40 characters or fewer",+ "tierDescriptionTooLong": "Description must be 120 characters or fewer",+ "invalidEmoji": "Pick an emoji",+ "tierTooManyModels": "A tier can have at most 5 models",+ "tierDuplicateModel": "This model is already in the tier",+ "tierThinkingBudgetInvalid": "Thinking budget must be 0 (off) or between 1,024 and 64,000 tokens",+ "invalidHeaderName": "A header name may only contain letters, numbers and the characters ! # $ % & ' * + - . ^ _ ` | ~",+ "reservedHeaderName": "Content-Type, Content-Length, Content-Encoding, Transfer-Encoding, Host and Connection are set by the delivery and cannot be added as custom headers",+ "duplicateHeaderName": "A header name may appear only once, whatever its letter case",+ "invalidHeaderValue": "A header value can contain only letters, numbers, spaces, tabs and common symbols. Remove any line break or hidden character, such as a zero-width space","Destructive": "Destructive","External ID can only contain letters, numbers and underscores": "External ID can only contain letters, numbers and underscores","required": "Required",@@ -3056,6 +3078,24 @@"Please select a JSON or CSV file": "Please select a JSON or CSV file","CSV import is only available for existing tables": "CSV import is only available for existing tables","No tables found in template": "No tables found in template",+ "This file is not a valid table template": "This file is not a valid table template",+ "See the template format": "See the template format",+ "This file is not a valid table template: {reason}": "This file is not a valid table template: {reason}",+ "it has no tables": "it has no tables",+ "it has more than one table, but you can only import one into an existing table": "it has more than one table, but you can only import one into an existing table",+ "one of its tables is not in the expected format": "one of its tables is not in the expected format",+ "the table has no name": "the table has no name",+ "it has no list of columns": "it has no list of columns",+ "the table status is not in the expected format": "the table status is not in the expected format",+ "the table trigger is not in the expected format": "the table trigger is not in the expected format",+ "its rows are not in the expected format": "its rows are not in the expected format",+ "column {position} has no name": "column {position} has no name",+ "column \"{name}\" has no type": "column \"{name}\" has no type",+ "column \"{name}\" has no externalId": "column \"{name}\" has no externalId",+ "the table has no externalId": "the table has no externalId",+ "column \"{name}\" has an unsupported type \"{type}\"": "column \"{name}\" has an unsupported type \"{type}\"",+ "column \"{name}\" is a dropdown with no options": "column \"{name}\" is a dropdown with no options",+ "column \"{name}\" is not in the expected format": "column \"{name}\" is not in the expected format","Import records from a CSV file": "Import records from a CSV file","Records will be added to the bottom of the table": "Records will be added to the bottom of the table","Any records after the limit ({maxRecords} records) will be ignored": "Any records after the limit ({maxRecords} records) will be ignored",@@ -3071,6 +3111,7 @@"Only {types} files are allowed": "Only {types} files are allowed","CSV import is only available from the table editor.": "CSV import is only available from the table editor.","Failed to parse CSV file": "Failed to parse CSV file",+ "recordsImported": "{count, plural, =1 {1 record imported} other {# records imported}}","An unexpected error occurred while importing the file, please hit the copy error and send it to support": "An unexpected error occurred while importing the file, please hit the copy error and send it to support","Name must be unique": "Name must be unique","Type is required": "Type is required",diff --git a/packages/web/src/features/tables/components/ap-table-actions-menu.tsx b/packages/web/src/features/tables/components/ap-table-actions-menu.tsxindex fd0d614f282..96b982152fc 100644--- a/packages/web/src/features/tables/components/ap-table-actions-menu.tsx+++ b/packages/web/src/features/tables/components/ap-table-actions-menu.tsx@@ -178,7 +178,6 @@ const ApTableActionsMenu = ({setIsOpen={setIsImportTableDialogOpen}showTrigger={false}tableId={table.id}- allowedFileTypes={['json', 'csv']}onImportSuccess={() => {refetch?.();}}diff --git a/packages/web/src/features/tables/components/ap-table-header.tsx b/packages/web/src/features/tables/components/ap-table-header.tsxindex 72dbef729d6..574cadf589f 100644--- a/packages/web/src/features/tables/components/ap-table-header.tsx+++ b/packages/web/src/features/tables/components/ap-table-header.tsx@@ -12,6 +12,7 @@ import {Lock,} from 'lucide-react';import { useState } from 'react';+import { toast } from 'sonner';import { ActiveUsersWidget } from '@/components/custom/active-users-widget';import { ConfirmationDeleteDialog } from '@/components/custom/delete-dialog';@@ -294,8 +295,14 @@ export function ApTableHeader({open={isImportTableDialogOpen}setIsOpen={setIsImportTableDialogOpen}tableId={table.id}- allowedFileTypes={['json', 'csv']}- onImportSuccess={refreshTableState}+ onImportSuccess={({ importedRecordsCount }) => {+ refreshTableState();+ if (importedRecordsCount > 0) {+ toast.success(+ t('recordsImported', { count: importedRecordsCount }),+ );+ }+ }}/></div></>diff --git a/packages/web/src/features/tables/components/import-table-dialog.tsx b/packages/web/src/features/tables/components/import-table-dialog.tsxindex 35930f7a172..5b86f22e815 100644--- a/packages/web/src/features/tables/components/import-table-dialog.tsx+++ b/packages/web/src/features/tables/components/import-table-dialog.tsx@@ -1,14 +1,20 @@-import { ApFlagId, SharedTemplate, TableTemplate } from '@activepieces/shared';+import {+ ApFlagId,+ isNil,+ SharedTemplate,+ tryCatch,+ tryCatchSync,+} from '@activepieces/shared';import { useMutation } from '@tanstack/react-query';import { t } from 'i18next';import { Import } from 'lucide-react';-import { parse } from 'papaparse';import { useState } from 'react';-import { FieldErrors, useForm } from 'react-hook-form';+import { ResolverResult, useForm } from 'react-hook-form';import { useNavigate } from 'react-router-dom';import { CopyButton } from '@/components/custom/clipboard/copy-button';import { ApMarkdown } from '@/components/custom/markdown';+import { useEmbedding } from '@/components/providers/embed-provider';import { Button } from '@/components/ui/button';import {Dialog,@@ -35,437 +41,413 @@ import { authenticationSession } from '@/lib/authentication-session';import { recordsApi } from '../api/records-api';import { tableHooks } from '../hooks/table-hooks';-import { FieldsMapping, fileUtils, SupportedFileType } from '../utils/utils';+import {+ FieldsMapping,+ fileUtils,+ SupportedFileType,+ tablesUtils,+} from '../utils/utils';import { useOptionalTableStore } from './ap-table-state-provider';import { FieldsMappingControl } from './fields-mapping';-type ImportTableDialogProps = {- open?: boolean;- setIsOpen?: (open: boolean) => void;- showTrigger?: boolean;- tableId?: string;- folderId?: string;- onImportSuccess?: () => void;- allowedFileTypes?: SupportedFileType[];-};-const ImportTableDialog = ({open,setIsOpen,showTrigger = true,+ ...formProps+}: ImportTableDialogProps) => {+ return (+ <Dialog open={open} onOpenChange={(value) => setIsOpen?.(value)}>+ {showTrigger && (+ <DialogTrigger asChild>+ <Button+ variant="outline"+ size="sm"+ className="flex gap-2 items-center"+ >+ <Import className="w-4 h-4 shrink-0" />+ {t('Import')}+ </Button>+ </DialogTrigger>+ )}+ <DialogContent>+ <DialogHeader>+ <DialogTitle>{t('Import Table')}</DialogTitle>+ </DialogHeader>+ <ImportTableForm {...formProps} onClose={() => setIsOpen?.(false)} />+ </DialogContent>+ </Dialog>+ );+};++const ImportTableForm = ({tableId,folderId,onImportSuccess,- allowedFileTypes = ['json'],-}: ImportTableDialogProps) => {+ onClose,+}: ImportTableFormProps & { onClose: () => void }) => {const navigate = useNavigate();const projectId = authenticationSession.getProjectId() ?? '';- const [serverError, setServerError] = useState<string | null>(null);- const [csvColumns, setCsvColumns] = useState<string[]>([]);- const [csvRecords, setCsvRecords] = useState<string[][]>([]);- const [fileType, setFileType] = useState<SupportedFileType | null>(null);+ const [columnNamesInCsv, setColumnNamesInCsv] = useState<string[]>([]);const { data: maxFileSize } = flagsHooks.useFlag<number>(ApFlagId.MAX_FILE_SIZE_MB,);- const { data: maxRecords } = flagsHooks.useFlag<number>(- ApFlagId.MAX_RECORDS_PER_TABLE,- );-+ const maxRecords =+ flagsHooks.useFlag<number>(ApFlagId.MAX_RECORDS_PER_TABLE).data ?? 1000;const tableStore = useOptionalTableStore();+ const isEmbedded = useEmbedding().embedState.isEmbedded;+ const allowedFileTypes: SupportedFileType[] =+ tableId && tableStore ? ['json', 'csv'] : ['json'];- const getTableState = () => {- if (!tableStore) return null;- const state = tableStore.getState();- return {- serverFields: state.serverFields,- serverRecords: state.serverRecords,- recordsCount: state.records.length,- setRecords: state.setRecords,- };- };-- const resetState = () => {- form.reset();- setCsvColumns([]);- setCsvRecords([]);- setFileType(null);- setServerError(null);- };-- const form = useForm<{- file: File;- fieldsMapping: FieldsMapping;- }>({+ const form = useForm<ImportTableFormValues>({+ mode: 'onChange',defaultValues: {fieldsMapping: [],},- resolver: (values) => {- const errors: FieldErrors<{- file: File | null;- fieldsMapping: FieldsMapping;- }> = {};-- if (!values.file) {- errors.file = {- message: t('Please select a JSON or CSV file'),- type: 'required',- };- return { values: {}, errors };- }-- const validation = fileUtils.validateFile(- values.file,- maxFileSize ?? undefined,- );- if (!validation.valid) {- errors.file = {- message: t(validation.error!),- type: 'invalid',- };- return { values: {}, errors };+ resolver: async (+ values,+ ): Promise<ResolverResult<ImportTableFormValues>> => {+ const error = await getFileError(values.file);+ if (isNil(error)) {+ return { values, errors: {} };}-return {- values: Object.keys(errors).length === 0 ? values : {},- errors,+ values: {},+ errors: { file: { type: 'invalid', message: error } },};},});- const handleCsvImport = async (data: { fieldsMapping: FieldsMapping }) => {- const tableState = getTableState();- if (!tableId || !tableState) {- throw new Error(t('CSV import is only available for existing tables'));+ const file = form.watch('file');+ const fileType = file ? fileUtils.getExtension(file.name) : null;+ const serverError = form.formState.errors.root?.serverError?.message;+ const showTemplateDocsLink =+ fileType === 'json' && !isNil(form.formState.errors.file) && !isEmbedded;++ const getFileError = async (file: File | undefined) => {+ if (isNil(file)) {+ return t('Please select a JSON or CSV file');+ }+ const extension = fileUtils.getExtension(file.name);+ if (+ !fileUtils.isValidType(extension) ||+ !allowedFileTypes.includes(extension)+ ) {+ return t('Only {types} files are allowed', {+ types: allowedFileTypes.map((type) => type.toUpperCase()).join(', '),+ });+ }+ const validation = fileUtils.validateFile(file, maxFileSize ?? undefined);+ if (!validation.valid) {+ return t(validation.error ?? '');+ }+ if (extension === 'csv') {+ const { error } = await tryCatch(() => fileUtils.parseCsv(file));+ return error ? t('Failed to parse CSV file') : null;+ }+ const template = await readJsonTemplate(file);+ if (isNil(template)) {+ return t('This file is not a valid table template');}+ return tablesUtils.getTemplateError({ template, existingTableId: tableId });+ };+ const handleCsvImport = async ({+ file,+ fieldsMapping,+ }: ImportTableFormValues): Promise<ImportResult> => {+ if (!tableId || !tableStore) {+ throw new Error(t('CSV import is only available for existing tables'));+ }+ const [, ...csvRecords] = await fileUtils.parseCsv(file);+ const state = tableStore.getState();const records = await recordsApi.importCsv({csvRecords,tableId,- fieldsMapping: data.fieldsMapping,- maxRecordsLimit: (maxRecords ?? 1000) - tableState.recordsCount,+ fieldsMapping,+ maxRecordsLimit: maxRecords - state.records.length,});-- tableState.setRecords([...tableState.serverRecords, ...records]);- return null;+ state.setRecords([...state.serverRecords, ...records]);+ return { tableId, importedRecordsCount: records.length };};- const handleJsonImport = async (data: { file: File }) => {- const fileContent = await data.file.text();- const parsedContent = JSON.parse(fileContent);-- let template: SharedTemplate;- if ('tables' in parsedContent && Array.isArray(parsedContent.tables)) {- template = parsedContent as SharedTemplate;- } else {- const singleTableTemplate = parsedContent as TableTemplate;- template = {- name: singleTableTemplate.name,- type: parsedContent.type,- summary: '',- description: '',- tags: [],- blogUrl: null,- metadata: null,- author: '',- categories: [],- pieces: [],- tables: [singleTableTemplate],- status: parsedContent.status,- };- }-- if (!template.tables || template.tables.length === 0) {- throw new Error(t('No tables found in template'));+ const handleJsonImport = async (file: File): Promise<ImportResult> => {+ const template = await readJsonTemplate(file);+ if (isNil(template)) {+ throw new Error(t('This file is not a valid table template'));}-if (tableId) {- return await tableHooks.importTableIntoExisting({- template,- existingTableId: tableId,- maxRecords: maxRecords ?? 1000,- });- } else {- const tables = await tableHooks.importTablesFromTemplates({+ const { importedRecordsCount } = await tableHooks.importTableIntoExisting(+ {+ template,+ existingTableId: tableId,+ maxRecords,+ },+ );+ return { tableId, importedRecordsCount };+ }+ const [{ table, importedRecordsCount }] =+ await tableHooks.importTablesFromTemplates({templates: [template],projectId,- maxRecords: maxRecords ?? 1000,+ maxRecords,folderId,});- return tables[0];- }+ return { tableId: table.id, importedRecordsCount };};const { mutate: importFile, isPending: isLoading } = useMutation({- mutationFn: async (data: { file: File; fieldsMapping: FieldsMapping }) => {- setServerError(null);-- if (fileType === 'csv') {- return await handleCsvImport(data);- } else {- return await handleJsonImport(data);+ mutationFn: async ({ file, fieldsMapping }: ImportTableFormValues) => {+ form.clearErrors('root.serverError');+ if (fileUtils.getExtension(file.name) === 'csv') {+ return await handleCsvImport({ file, fieldsMapping });}+ return await handleJsonImport(file);},- onSuccess: async (table) => {- setIsOpen?.(false);- onImportSuccess?.();- if (!tableId && table) {- navigate(`/projects/${projectId}/tables/${table.id}`);+ onSuccess: (result) => {+ onClose();+ onImportSuccess?.({ importedRecordsCount: result.importedRecordsCount });+ if (!tableId) {+ navigate(`/projects/${projectId}/tables/${result.tableId}`);}},onError: (error) => {- const errorMessage =- api.isError(error) && error.response?.data- ? JSON.stringify(error.response.data)- : error.message;- setServerError(errorMessage);+ form.setError('root.serverError', {+ type: 'manual',+ message:+ api.isError(error) && error.response?.data+ ? JSON.stringify(error.response.data)+ : error.message,+ });},});return (- <Dialog- open={open}- onOpenChange={(value) => {- setIsOpen?.(value);- if (!value) resetState();- }}- >- {showTrigger && (- <DialogTrigger asChild>- <Button- variant="outline"- size="sm"- className="flex gap-2 items-center"- >- <Import className="w-4 h-4 shrink-0" />- {t('Import')}- </Button>- </DialogTrigger>- )}- <DialogContent>- <DialogHeader>- <DialogTitle>{t('Import Table')}</DialogTitle>- </DialogHeader>-- <Form {...form}>- <form- onSubmit={form.handleSubmit((data) => importFile(data))}- className="space-y-4"- >- <ApMarkdown- className="text-left"- markdown={(() => {- if (fileType === 'csv') {- return [- t('Import records from a CSV file'),- t('Records will be added to the bottom of the table'),- t(- 'Any records after the limit ({maxRecords} records) will be ignored',- {- maxRecords: maxRecords ?? 0,- },- ),- ].join('\n\n');- }-- if (fileType === 'json' && tableId) {- return [- t(- '⚠️ **Warning:** This will completely replace the current table',- ),- t('All existing fields and records will be deleted'),- t(- 'Any records after the limit ({maxRecords} records) will be ignored',- {- maxRecords: maxRecords ?? 0,- },- ),- ].join('\n\n');- }-- if (!allowedFileTypes.includes('csv')) {- return [- t('Import a table from JSON template'),- tableId- ? t(- '⚠️ This will completely replace the current table with the template structure and data',- )- : t(- 'The table will be created with all its fields and data',- ),- t(- 'Any records after the limit ({maxRecords} records) will be ignored',- {- maxRecords: maxRecords ?? 0,- },- ),- ].join('\n\n');- }+ <Form {...form}>+ <form+ onSubmit={form.handleSubmit((data) => importFile(data))}+ className="space-y-4"+ >+ <ApMarkdown+ className="text-left"+ markdown={(() => {+ if (fileType === 'csv') {+ return [+ t('Import records from a CSV file'),+ t('Records will be added to the bottom of the table'),+ t(+ 'Any records after the limit ({maxRecords} records) will be ignored',+ {+ maxRecords,+ },+ ),+ ].join('\n\n');+ }++ if (fileType === 'json' && tableId) {+ return [+ t(+ '⚠️ **Warning:** This will completely replace the current table',+ ),+ t('All existing fields and records will be deleted'),+ t(+ 'Any records after the limit ({maxRecords} records) will be ignored',+ {+ maxRecords,+ },+ ),+ ].join('\n\n');+ }++ if (!allowedFileTypes.includes('csv')) {+ return [+ t('Import a table from JSON template'),+ tableId+ ? t(+ '⚠️ This will completely replace the current table with the template structure and data',+ )+ : t('The table will be created with all its fields and data'),+ t(+ 'Any records after the limit ({maxRecords} records) will be ignored',+ {+ maxRecords,+ },+ ),+ ].join('\n\n');+ }++ return [+ t('Import a table from JSON or add records from CSV'),+ t('**JSON:** Creates a new table with fields and data'),+ t('**CSV:** Adds records to an existing table'),+ t(+ 'Any records after the limit ({maxRecords} records) will be ignored',+ {+ maxRecords,+ },+ ),+ ].join('\n\n');+ })()}+ />+ <FormField+ control={form.control}+ name="file"+ render={({ field }) => (+ <FormItem>+ <FormLabel>+ {t(+ allowedFileTypes.map((t) => t.toUpperCase()).join(' or ') ++ ' file',+ )}+ </FormLabel>+ <FormControl>+ <Input+ type="file"+ accept={allowedFileTypes.map((t) => `.${t}`).join(',')}+ onChange={async (e) => {+ const file = e.target.files?.[0];+ if (!file) return;++ field.onChange(file);+ setColumnNamesInCsv([]);+ if (+ fileUtils.getExtension(file.name) !== 'csv' ||+ !tableId ||+ !tableStore+ ) {+ return;+ }++ const { data: rows, error } = await tryCatch(() =>+ fileUtils.parseCsv(file),+ );+ if (error) {+ return;+ }+ setColumnNamesInCsv(rows[0] ?? []);+ }}+ />+ </FormControl>+ <FormMessage />+ {showTemplateDocsLink && (+ <a+ href={TABLE_TEMPLATE_DOCS_URL}+ target="_blank"+ rel="noreferrer"+ className="text-sm text-accent-11 underline"+ >+ {t('See the template format')}+ </a>+ )}+ </FormItem>+ )}+ />- return [- t('Import a table from JSON or add records from CSV'),- t('**JSON:** Creates a new table with fields and data'),- t('**CSV:** Adds records to an existing table'),- t(- 'Any records after the limit ({maxRecords} records) will be ignored',- {- maxRecords: maxRecords ?? 0,- },- ),- ].join('\n\n');- })()}- />+ {fileType === 'csv' && columnNamesInCsv.length > 0 && tableStore && (+ <ScrollArea className="max-h-[calc(100vh-500px)] overflow-y-auto flex-1"><FormFieldcontrol={form.control}- name="file"+ name="fieldsMapping"+ key={file?.name}render={({ field }) => (<FormItem>- <FormLabel>- {t(- allowedFileTypes- .map((t) => t.toUpperCase())- .join(' or ') + ' file',- )}- </FormLabel>- <FormControl>- <Input- type="file"- accept={allowedFileTypes.map((t) => `.${t}`).join(',')}- onChange={async (e) => {- const file = e.target.files?.[0];- if (!file) return;-- field.onChange(file);- setServerError(null);-- const validation = fileUtils.validateFile(- file,- maxFileSize ?? undefined,- );- if (!validation.valid) {- setServerError(t(validation.error!));- return;- }-- const extension = fileUtils.getExtension(file.name);- if (!fileUtils.isValidType(extension)) {- setServerError(t('Invalid file type'));- return;- }-- if (!allowedFileTypes.includes(extension)) {- setServerError(- t('Only {types} files are allowed', {- types: allowedFileTypes- .map((t) => t.toUpperCase())- .join(', '),- }),- );- return;- }-- setFileType(extension);-- if (extension === 'csv') {- if (!tableId) {- setServerError(- t(- 'CSV import is only available for existing tables',- ),- );- return;- }-- if (!tableStore) {- setServerError(- t(- 'CSV import is only available from the table editor.',- ),- );- return;- }-- try {- const parsedCsvRecords = await new Promise<- string[][]- >((resolve, reject) => {- parse(file, {- header: false,- skipEmptyLines: 'greedy',- worker: true,- complete: (results) =>- resolve(results.data as string[][]),- error: (error) => reject(error),- });- });- setCsvColumns(parsedCsvRecords[0] ?? []);- setCsvRecords(parsedCsvRecords.slice(1));- } catch (error) {- setServerError(t('Failed to parse CSV file'));- }- } else {- setCsvColumns([]);- setCsvRecords([]);- }- }}- />- </FormControl>- <FormMessage />+ <FieldsMappingControl+ fields={tableStore.getState().serverFields}+ csvColumns={columnNamesInCsv}+ onChange={field.onChange}+ /></FormItem>)}/>-- {fileType === 'csv' && csvColumns.length > 0 && tableStore && (- <ScrollArea className="max-h-[calc(100vh-500px)] overflow-y-auto flex-1">- <FormField- control={form.control}- name="fieldsMapping"- key={form.watch('file')?.name}- render={({ field }) => (- <FormItem>- <FieldsMappingControl- fields={tableStore.getState().serverFields}- csvColumns={csvColumns}- onChange={field.onChange}- />- </FormItem>- )}- />- </ScrollArea>- )}-- {serverError && (- <div className=" flex items-center justify-between">- <div className="text-danger-11">- {t(- 'An unexpected error occurred while importing the file, please hit the copy error and send it to support',- )}- </div>- <div className="min-w-4">- <CopyButton- variant="ghost"- withoutTooltip={true}- textToCopy={serverError}- />- </div>- </div>- )}- <DialogFooter>- <DialogClose asChild>- <Button variant="outline" size="sm" disabled={isLoading}>- {t('Cancel')}- </Button>- </DialogClose>- <Button type="submit" size="sm" loading={isLoading}>- {t('Import')}- </Button>- </DialogFooter>- </form>- </Form>- </DialogContent>- </Dialog>+ </ScrollArea>+ )}++ {serverError && (+ <div className=" flex items-center justify-between">+ <div className="text-danger-11">+ {t(+ 'An unexpected error occurred while importing the file, please hit the copy error and send it to support',+ )}+ </div>+ <div className="min-w-4">+ <CopyButton+ variant="ghost"+ withoutTooltip={true}+ textToCopy={serverError}+ />+ </div>+ </div>+ )}+ <DialogFooter>+ <DialogClose asChild>+ <Button+ type="button"+ variant="outline"+ size="sm"+ disabled={isLoading}+ >+ {t('Cancel')}+ </Button>+ </DialogClose>+ <Button type="submit" size="sm" loading={isLoading}>+ {t('Import')}+ </Button>+ </DialogFooter>+ </form>+ </Form>);};+async function readJsonTemplate(file: File): Promise<SharedTemplate | null> {+ const text = await file.text();+ const { data: content, error } = tryCatchSync(() => JSON.parse(text));+ if (error || typeof content !== 'object' || content === null) {+ return null;+ }+ if (Array.isArray(content.tables)) {+ return content;+ }+ return {+ name: content.name,+ type: content.type,+ summary: '',+ description: '',+ tags: [],+ blogUrl: null,+ metadata: null,+ author: '',+ categories: [],+ pieces: [],+ tables: [content],+ status: content.status,+ };+}++const TABLE_TEMPLATE_DOCS_URL =+ 'https://www.activepieces.com/docs/tables/import-export#importing-a-json-template';+ImportTableDialog.displayName = 'ImportTableDialog';export { ImportTableDialog };++type ImportTableFormProps = {+ tableId?: string;+ folderId?: string;+ onImportSuccess?: (result: { importedRecordsCount: number }) => void;+};++type ImportTableDialogProps = ImportTableFormProps & {+ open?: boolean;+ setIsOpen?: (open: boolean) => void;+ showTrigger?: boolean;+};++type ImportResult = {+ tableId: string;+ importedRecordsCount: number;+};++type ImportTableFormValues = {+ file: File;+ fieldsMapping: FieldsMapping;+};diff --git a/packages/web/src/features/tables/hooks/table-hooks.ts b/packages/web/src/features/tables/hooks/table-hooks.tsindex f705aa4db00..08447aa7cb7 100644--- a/packages/web/src/features/tables/hooks/table-hooks.ts+++ b/packages/web/src/features/tables/hooks/table-hooks.ts@@ -1,4 +1,5 @@import {+ CreateFieldRequest,FieldType,SharedTemplate,TableTemplate,@@ -91,18 +92,18 @@ export const tableHooks = {template: SharedTemplate;existingTableId: string;maxRecords?: number;- }): Promise<Table> => {- const tables = template.tables || [];- if (tables.length === 0) {- throw new Error('Template has no tables');- }- if (tables.length > 1) {+ }): Promise<ImportedTable> => {+ const tables = template.tables ?? [];+ if (tables.length !== 1) {throw new Error('Template must contain exactly one table when importing into existing table',);}-- const tableTemplate = tables[0];+ const [tableTemplate] = tables;+ const fieldsToCreate = parseFieldsToCreate({+ tableTemplate,+ tableId: existingTableId,+ });const [, existingFields] = await Promise.all([tablesApi.clear(existingTableId),@@ -117,19 +118,9 @@ export const tableHooks = {name: tableTemplate.name,});- await Promise.all(- tableTemplate.fields.map((fieldState, position) =>- fieldsApi.create({- name: fieldState.name,- type: fieldState.type as any,- tableId: existingTableId,- data: fieldState.data as any,- externalId: fieldState.externalId,- position,- }),- ),- );+ await Promise.all(fieldsToCreate.map((field) => fieldsApi.create(field)));+ let importedRecordsCount = 0;if (tableTemplate.data && tableTemplate.data.rows.length > 0) {const createdFields = await fieldsApi.list({ tableId: existingTableId });@@ -142,7 +133,7 @@ export const tableHooks = {maxRecords ?? 1000,);- await recordsApi.create({+ const importedRecords = await recordsApi.create({tableId: existingTableId,records: recordsToImport.map((record) =>record@@ -159,9 +150,12 @@ export const tableHooks = {.filter((cell) => cell !== null),),});+ importedRecordsCount = importedRecords.length;}- return tablesApi.getById(existingTableId);+ return tablesApi+ .getById(existingTableId)+ .then((table) => ({ table, importedRecordsCount }));},importTablesFromTemplates: async ({templates,@@ -173,7 +167,7 @@ export const tableHooks = {projectId: string;maxRecords?: number;folderId?: string;- }): Promise<Table[]> => {+ }): Promise<ImportedTable[]> => {if (templates.length === 0) {return [];}@@ -186,26 +180,21 @@ export const tableHooks = {tableTemplate: TableTemplate;}> = [];- for (const template of templates) {- const tables = template.tables || [];- if (tables.length === 0) {- continue;- }-- for (const tableTemplate of tables) {- const table = await tablesApi.create({- projectId,- name: tableTemplate.name,- externalId: tableTemplate.externalId,- fields: tableTemplate.fields,- folderId: targetFolderId,- });+ for (const tableTemplate of templates.flatMap(+ (template) => template.tables ?? [],+ )) {+ const table = await tablesApi.create({+ projectId,+ name: tableTemplate.name,+ externalId: tableTemplate.externalId,+ fields: tableTemplate.fields,+ folderId: targetFolderId,+ });- allTablesToImport.push({- table,- tableTemplate,- });- }+ allTablesToImport.push({+ table,+ tableTemplate,+ });}const importPromises = allTablesToImport.map(@@ -222,7 +211,7 @@ export const tableHooks = {maxRecords ?? 1000,);- await recordsApi.create({+ const importedRecords = await recordsApi.create({tableId: table.id,records: recordsToImport.map((record) =>record@@ -239,12 +228,34 @@ export const tableHooks = {.filter((cell) => cell !== null),),});+ return { table, importedRecordsCount: importedRecords.length };}- return table;+ return { table, importedRecordsCount: 0 };},);return await Promise.all(importPromises);},};++function parseFieldsToCreate({+ tableTemplate,+ tableId,+}: {+ tableTemplate: TableTemplate;+ tableId: string;+}): CreateFieldRequest[] {+ return CreateFieldRequest.array().parse(+ tableTemplate.fields.map((field, position) => ({+ ...field,+ tableId,+ position,+ })),+ );+}++type ImportedTable = {+ table: Table;+ importedRecordsCount: number;+};diff --git a/packages/web/src/features/tables/utils/utils.tsx b/packages/web/src/features/tables/utils/utils.tsxindex 9baaac51119..66016bee1de 100644--- a/packages/web/src/features/tables/utils/utils.tsx+++ b/packages/web/src/features/tables/utils/utils.tsx@@ -1,4 +1,11 @@-import { ExportTableResponse, FieldType } from '@activepieces/shared';+import {+ ExportTableResponse,+ FieldType,+ isNil,+ SharedTemplate,+ TableTemplate,+} from '@activepieces/shared';+import { t } from 'i18next';import JSZip from 'jszip';import {Type,@@ -7,7 +14,9 @@ import {Hash,ChevronDownCircle,} from 'lucide-react';+import { parse } from 'papaparse';import { createContext, ReactNode } from 'react';+import { z } from 'zod';import { downloadFile } from '@/lib/dom-utils';@@ -98,7 +107,133 @@ function exportRecords({extension: 'csv',});}+function getTemplateError({+ template,+ existingTableId,+}: {+ template: SharedTemplate;+ existingTableId?: string;+}): string | null {+ const reason = findTemplateIssue({ template, existingTableId });+ return isNil(reason)+ ? null+ : t('This file is not a valid table template: {reason}', { reason });+}++function findTemplateIssue({+ template,+ existingTableId,+}: {+ template: SharedTemplate;+ existingTableId?: string;+}): string | null {+ const tables = template.tables ?? [];+ if (tables.length === 0) {+ return t('it has no tables');+ }+ if (!isNil(existingTableId) && tables.length > 1) {+ return t(+ 'it has more than one table, but you can only import one into an existing table',+ );+ }+ const parsed = TableTemplate.array().safeParse(tables);+ if (!parsed.success) {+ const { issues } = parsed.error;+ const tableNameOrFieldsIssue = issues.find(+ ({ path }) =>+ path.length === 2 && (path[1] === 'name' || path[1] === 'fields'),+ );+ return describeTemplateIssue({+ path: (tableNameOrFieldsIssue ?? issues[0]).path,+ tables,+ });+ }+ return (+ tables+ .flatMap((table) => table.fields)+ .map(findFieldIssue)+ .find((issue) => !isNil(issue)) ?? null+ );+}++function describeTemplateIssue({+ path,+ tables,+}: {+ path: PropertyKey[];+ tables: TableTemplate[];+}): string {+ const [tableIndex, section, fieldIndex, key] = path;+ const tableKey = TableTemplate.keyof().safeParse(section);+ if (!tableKey.success || typeof tableIndex !== 'number') {+ return t('one of its tables is not in the expected format');+ }+ switch (tableKey.data) {+ case 'externalId':+ return t('the table has no externalId');+ case 'data':+ return t('its rows are not in the expected format');+ case 'fields':+ return describeFieldIssue({+ fields: tables[tableIndex].fields,+ fieldIndex,+ key,+ });+ case 'name':+ return t('the table has no name');+ case 'status':+ return t('the table status is not in the expected format');+ case 'trigger':+ return t('the table trigger is not in the expected format');+ }+}++function describeFieldIssue({+ fields,+ fieldIndex,+ key,+}: {+ fields: TableTemplate['fields'];+ fieldIndex: PropertyKey | undefined;+ key: PropertyKey | undefined;+}): string {+ if (typeof fieldIndex !== 'number') {+ return t('it has no list of columns');+ }+ const name = fields[fieldIndex]?.name;+ const fieldKey = TableTemplate.shape.fields.element.keyof().safeParse(key);+ if (!fieldKey.success) {+ return t('column "{name}" is not in the expected format', { name });+ }+ switch (fieldKey.data) {+ case 'name':+ return t('column {position} has no name', { position: fieldIndex + 1 });+ case 'type':+ return t('column "{name}" has no type', { name });+ case 'externalId':+ return t('column "{name}" has no externalId', { name });+ case 'data':+ return t('column "{name}" is not in the expected format', { name });+ }+}++function findFieldIssue(field: TableTemplate['fields'][number]): string | null {+ if (!z.enum(FieldType).safeParse(field.type).success) {+ return t('column "{name}" has an unsupported type "{type}"', {+ name: field.name,+ type: field.type,+ });+ }+ if (field.type === FieldType.STATIC_DROPDOWN && isNil(field.data)) {+ return t('column "{name}" is a dropdown with no options', {+ name: field.name,+ });+ }+ return null;+}+export const tablesUtils = {+ getTemplateError,exportTables,getColumnIcon,exportRecords,@@ -159,4 +294,16 @@ export const fileUtils = {return { valid: true };},++ parseCsv: (file: File): Promise<string[][]> => {+ return new Promise((resolve, reject) => {+ parse<string[]>(file, {+ header: false,+ skipEmptyLines: 'greedy',+ worker: true,+ complete: (results) => resolve(results.data),+ error: (error) => reject(error),+ });+ });+ },};diff --git a/packages/web/test/features/tables/import-table-into-existing.test.ts b/packages/web/test/features/tables/import-table-into-existing.test.tsnew file mode 100644index 00000000000..a6786f3749b--- /dev/null+++ b/packages/web/test/features/tables/import-table-into-existing.test.ts@@ -0,0 +1,173 @@+import {+ SharedTemplate,+ TemplateStatus,+ TemplateType,+} from '@activepieces/shared';+import { beforeEach, describe, expect, it, vi } from 'vitest';++const { tablesApi, fieldsApi, recordsApi } = vi.hoisted(() => ({+ tablesApi: {+ clear: vi.fn(),+ update: vi.fn(),+ getById: vi.fn(),+ create: vi.fn(),+ },+ fieldsApi: { list: vi.fn(), delete: vi.fn(), create: vi.fn() },+ recordsApi: { create: vi.fn() },+}));++vi.mock('@/features/tables/api/tables-api', () => ({ tablesApi }));+vi.mock('@/features/tables/api/fields-api', () => ({ fieldsApi }));+vi.mock('@/features/tables/api/records-api', () => ({ recordsApi }));+vi.mock('@/lib/authentication-session', () => ({+ authenticationSession: { getProjectId: () => 'project-1' },+}));++import { tableHooks } from '@/features/tables/hooks/table-hooks';++const wrapAsDialogDoes = (fileContent: unknown): SharedTemplate => {+ const parsedContent = JSON.parse(JSON.stringify(fileContent));+ return {+ name: 'anything',+ type: TemplateType.SHARED,+ summary: '',+ description: '',+ tags: [],+ blogUrl: null,+ metadata: null,+ author: '',+ categories: [],+ pieces: [],+ tables: [parsedContent],+ status: TemplateStatus.PUBLISHED,+ };+};++const exportedTableTemplate = {+ name: 'Customers',+ externalId: 'customers',+ fields: [{ name: 'Email', type: 'TEXT', externalId: 'email' }],+ data: {+ type: 'CSV',+ rows: [[{ fieldId: 'email', value: '[email protected]' }]],+ },+};++const templateWithFieldMissingExternalId = {+ name: 'Customers',+ externalId: 'customers',+ fields: [{ name: 'Email', type: 'TEXT' }],+};++const handWrittenTemplate = {+ name: 'Customers',+ fields: [{ name: 'Email', type: 'TEXT', externalId: 'email' }],+ data: {+ rows: [[{ fieldId: 'email', value: 42 }]],+ },+};++const gitSyncTableState = {+ id: 'table-2',+ name: 'Leads',+ externalId: 'leads',+ fields: [{ name: 'Name', type: 'TEXT', externalId: 'name' }],+ status: null,+ trigger: null,+};++describe('tableHooks.importTableIntoExisting', () => {+ beforeEach(() => {+ vi.resetAllMocks();+ tablesApi.clear.mockResolvedValue(undefined);+ tablesApi.update.mockResolvedValue(undefined);+ tablesApi.getById.mockResolvedValue({ id: 'existing-1' });+ fieldsApi.delete.mockResolvedValue(undefined);+ fieldsApi.create.mockResolvedValue(undefined);+ recordsApi.create.mockResolvedValue([]);+ fieldsApi.list+ .mockResolvedValueOnce([{ id: 'old-field-1', externalId: 'email' }])+ .mockResolvedValue([{ id: 'new-field-1', externalId: 'email' }]);+ });++ it('imports a template whose columns have no externalId', async () => {+ await tableHooks.importTableIntoExisting({+ template: wrapAsDialogDoes(templateWithFieldMissingExternalId),+ existingTableId: 'existing-1',+ });++ expect(fieldsApi.create).toHaveBeenCalledWith(+ expect.objectContaining({ name: 'Email', tableId: 'existing-1' }),+ );+ });++ it('imports an exported table template', async () => {+ await tableHooks.importTableIntoExisting({+ template: wrapAsDialogDoes(exportedTableTemplate),+ existingTableId: 'existing-1',+ });++ expect(tablesApi.clear).toHaveBeenCalledWith('existing-1');+ expect(fieldsApi.delete).toHaveBeenCalledWith('old-field-1');+ expect(tablesApi.update).toHaveBeenCalledWith('existing-1', {+ name: 'Customers',+ });+ expect(fieldsApi.create).toHaveBeenCalledWith(+ expect.objectContaining({+ name: 'Email',+ tableId: 'existing-1',+ position: 0,+ }),+ );+ expect(recordsApi.create).toHaveBeenCalledWith({+ tableId: 'existing-1',+ records: [[{ fieldId: 'new-field-1', value: '[email protected]' }]],+ });+ });++ it('imports a hand-written template that omits externalId and uses non-string cell values', async () => {+ await tableHooks.importTableIntoExisting({+ template: wrapAsDialogDoes(handWrittenTemplate),+ existingTableId: 'existing-1',+ });++ expect(tablesApi.clear).toHaveBeenCalledWith('existing-1');+ expect(recordsApi.create).toHaveBeenCalledWith({+ tableId: 'existing-1',+ records: [[{ fieldId: 'new-field-1', value: 42 }]],+ });+ });++ it('imports a git-sync table state that carries no data rows', async () => {+ await tableHooks.importTableIntoExisting({+ template: wrapAsDialogDoes(gitSyncTableState),+ existingTableId: 'existing-1',+ });++ expect(tablesApi.clear).toHaveBeenCalledWith('existing-1');+ expect(recordsApi.create).not.toHaveBeenCalled();+ });+});++describe('tableHooks.importTablesFromTemplates', () => {+ beforeEach(() => {+ vi.resetAllMocks();+ tablesApi.create.mockResolvedValue({ id: 'new-1' });+ recordsApi.create.mockResolvedValue([]);+ fieldsApi.list.mockResolvedValue([{ id: 'field-1', externalId: 'email' }]);+ });++ it('creates the table for an exported table template', async () => {+ const tables = await tableHooks.importTablesFromTemplates({+ templates: [wrapAsDialogDoes(exportedTableTemplate)],+ projectId: 'project-1',+ });++ expect(tables).toEqual([+ { table: { id: 'new-1' }, importedRecordsCount: 0 },+ ]);+ expect(tablesApi.create).toHaveBeenCalledWith(+ expect.objectContaining({ name: 'Customers', externalId: 'customers' }),+ );+ });+});diff --git a/packages/web/test/features/tables/utils/utils.test.ts b/packages/web/test/features/tables/utils/utils.test.tsnew file mode 100644index 00000000000..91b90d73010--- /dev/null+++ b/packages/web/test/features/tables/utils/utils.test.ts@@ -0,0 +1,162 @@+import {+ SharedTemplate,+ TemplateStatus,+ TemplateType,+} from '@activepieces/shared';+import { describe, expect, it, vi } from 'vitest';++vi.mock('i18next', () => ({+ t: (key: string, params?: Record<string, unknown>) =>+ key.replace(/\{(\w+)\}/g, (_, name) => String(params?.[name])),+}));++vi.mock('@/lib/dom-utils', () => ({ downloadFile: vi.fn() }));++import { tablesUtils } from '@/features/tables/utils/utils';++const asTemplate = (tables: unknown[]): SharedTemplate =>+ JSON.parse(+ JSON.stringify({+ name: 'anything',+ type: TemplateType.SHARED,+ summary: '',+ description: '',+ tags: [],+ blogUrl: null,+ metadata: null,+ author: '',+ categories: [],+ pieces: [],+ tables,+ status: TemplateStatus.PUBLISHED,+ }),+ );++const customers = {+ name: 'Customers',+ externalId: 'customers',+ fields: [{ name: 'Email', type: 'TEXT', externalId: 'email' }],+ data: { type: 'CSV', rows: [[{ fieldId: 'email', value: '[email protected]' }]] },+};++const invalidPrefix = 'This file is not a valid table template: ';++describe('tablesUtils.getTemplateError', () => {+ it.each([+ ['an exported table template', customers],+ [+ 'a git-sync table state with no data',+ {+ name: 'Leads',+ externalId: 'leads',+ fields: [{ name: 'Name', type: 'TEXT', externalId: 'name' }],+ status: null,+ trigger: null,+ },+ ],+ ])('accepts %s', (_, table) => {+ expect(+ tablesUtils.getTemplateError({ template: asTemplate([table]) }),+ ).toBeNull();+ });++ it.each([+ [+ 'a file that is not a table',+ { name: 'Send a Slack message', trigger: { type: 'PIECE_TRIGGER' } },+ 'it has no list of columns',+ ],+ [+ 'a table that is not an object',+ 'Customers',+ 'one of its tables is not in the expected format',+ ],+ [+ 'a table with no name',+ { ...customers, name: undefined },+ 'the table has no name',+ ],+ [+ 'a status that is not text',+ { ...customers, status: true },+ 'the table status is not in the expected format',+ ],+ [+ 'a trigger that is not text',+ { ...customers, trigger: { type: 'ON_NEW_RECORD' } },+ 'the table trigger is not in the expected format',+ ],+ [+ 'an unsupported column type',+ { ...customers, fields: [+ { name: 'Email', type: 'NOT_A_REAL_TYPE', externalId: 'email' },+ ], },+ 'column "Email" has an unsupported type "NOT_A_REAL_TYPE"',+ ],+ [+ 'a dropdown with no options',+ {+ ...customers,+ fields: [+ {+ name: 'Plan',+ type: 'STATIC_DROPDOWN',+ data: null,+ externalId: 'plan',+ },+ ],+ },+ 'column "Plan" is a dropdown with no options',+ ],+ [+ 'a column with no name',+ { ...customers, fields: [{ type: 'TEXT' }] },+ 'column 1 has no name',+ ],+ [+ 'a table with no externalId',+ { ...customers, externalId: undefined },+ 'the table has no externalId',+ ],+ [+ 'a column with no externalId',+ { ...customers, fields: [{ name: 'Email', type: 'TEXT' }] },+ 'column "Email" has no externalId',+ ],+ [+ 'a cell whose value is not text',+ {+ ...customers,+ data: { type: 'CSV', rows: [[{ fieldId: 'email', value: 42 }]] },+ },+ 'its rows are not in the expected format',+ ],+ [+ 'rows that are not arrays of cells',+ { ...customers, data: { rows: [{ fieldId: 'email', value: 'x' }] } },+ 'its rows are not in the expected format',+ ],+ ])('explains %s', (_, table, reason) => {+ expect(+ tablesUtils.getTemplateError({ template: asTemplate([table]) }),+ ).toBe(invalidPrefix + reason);+ });++ it('explains a template with no tables', () => {+ expect(tablesUtils.getTemplateError({ template: asTemplate([]) })).toBe(+ invalidPrefix + 'it has no tables',+ );+ });++ it('rejects more than one table only when importing into an existing table', () => {+ const template = asTemplate([customers, customers]);++ expect(+ tablesUtils.getTemplateError({ template, existingTableId: 'table-1' }),+ ).toBe(+ invalidPrefix ++ 'it has more than one table, but you can only import one into an existing table',+ );+ expect(tablesUtils.getTemplateError({ template })).toBeNull();+ });+});diff --git a/packages/web/test/i18n/form-error-keys.test.ts b/packages/web/test/i18n/form-error-keys.test.tsnew file mode 100644index 00000000000..9c411292f3f--- /dev/null+++ b/packages/web/test/i18n/form-error-keys.test.ts@@ -0,0 +1,19 @@+import { readFileSync } from 'fs';+import path from 'path';++import { formErrors } from '@activepieces/shared';+import { describe, expect, it } from 'vitest';++const translations: Record<string, string> = JSON.parse(+ readFileSync(+ path.resolve(__dirname, '../../public/locales/en/translation.json'),+ 'utf-8',+ ),+);++describe('formErrors translations', () => {+ it.each(Object.values(formErrors))('has English text for %s', (key) => {+ expect(translations[key]).toBeDefined();+ expect(translations[key]).not.toBe(key);+ });+});