From bd2de24da29b1ebffb857941b14f207d956210f2 Mon Sep 17 00:00:00 2001 From: Nicholas Cronquist Date: Wed, 22 Jan 2025 21:06:48 -0500 Subject: [PATCH 1/3] feat: Use SpaceTemplate info for Namespace - Get space template by appid and use the namespace from the space template instead of from the app - Getting the app requires account level access, but agents only have environment level access - Also fixed a bug with space update request body --- package-lock.json | 9 +- .../package.json | 3 +- .../src/space.configure.from.template.ts | 132 +++++++++--------- 3 files changed, 73 insertions(+), 71 deletions(-) diff --git a/package-lock.json b/package-lock.json index 326644b76..4e30ff193 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3842,9 +3842,9 @@ } }, "node_modules/@flatfile/api": { - "version": "1.13.0", - "resolved": "https://registry.npmjs.org/@flatfile/api/-/api-1.13.0.tgz", - "integrity": "sha512-fdE6d8L0jUeIxVHOCEZaZA8mcqZoxvEC0Bywn3RTf5v3w2KZWSJLLfWD7NxltiS9OxH3lvqOrqPofznv3OxD4w==", + "version": "1.15.0", + "resolved": "https://registry.npmjs.org/@flatfile/api/-/api-1.15.0.tgz", + "integrity": "sha512-qOIqZYidzXulyTOdtglWVhboC++3uGa8+Ys6+o0XR+l7Jatmd36uJr3cVJ/7xSbq3rHYKNNGBNUA8fSd2Q0eDw==", "dependencies": { "@flatfile/cross-env-config": "0.0.4", "@types/pako": "2.0.1", @@ -17882,7 +17882,6 @@ "@flatfile/plugin-job-handler": "^0.8.1" }, "devDependencies": { - "@flatfile/api": "^1.9.19", "@flatfile/bundler-config-tsup": "^0.2.0", "@flatfile/config-vitest": "^0.0.0", "@flatfile/utils-testing": "^0.5.0" @@ -17891,7 +17890,7 @@ "node": ">= 18" }, "peerDependencies": { - "@flatfile/api": "^1.9.19", + "@flatfile/api": "^1.15.0", "@flatfile/listener": "^1.1.0" } }, diff --git a/plugins/space-configure-from-template/package.json b/plugins/space-configure-from-template/package.json index d698df9bc..37366fc15 100644 --- a/plugins/space-configure-from-template/package.json +++ b/plugins/space-configure-from-template/package.json @@ -64,11 +64,10 @@ "@flatfile/plugin-job-handler": "^0.8.1" }, "peerDependencies": { - "@flatfile/api": "^1.9.19", + "@flatfile/api": "^1.15.0", "@flatfile/listener": "^1.1.0" }, "devDependencies": { - "@flatfile/api": "^1.9.19", "@flatfile/bundler-config-tsup": "^0.2.0", "@flatfile/config-vitest": "^0.0.0", "@flatfile/utils-testing": "^0.5.0" diff --git a/plugins/space-configure-from-template/src/space.configure.from.template.ts b/plugins/space-configure-from-template/src/space.configure.from.template.ts index cc3e0bc5c..8de23890f 100644 --- a/plugins/space-configure-from-template/src/space.configure.from.template.ts +++ b/plugins/space-configure-from-template/src/space.configure.from.template.ts @@ -30,82 +30,86 @@ export function configureSpaceFromTemplate( return function (listener: FlatfileListener) { listener.use( jobHandler('space:configure', async (event, tick) => { - const { spaceId, environmentId, appId } = event.context - const app = await api.apps.get(appId) + try { + const { spaceId, environmentId, appId } = event.context - // Get all the space templates for the app sorted by creation date, oldest first - const spaceTemplates = await api.spaces.list({ - namespace: app.data.namespace, - isAppTemplate: true, - sortField: 'createdAt', - sortDirection: 'asc', - }) + // Get all the space templates for the app sorted by creation date, oldest first + const spaceTemplates = await api.spaces.list({ + appId, + isAppTemplate: true, + sortField: 'createdAt', + sortDirection: 'asc', + }) - if (spaceTemplates.data.length === 0) { - throw new Error('No space template found') - } + if (spaceTemplates.data.length === 0) { + throw new Error('No space template found') + } - // Get the oldest space template - const spaceTemplate = spaceTemplates.data[0] - const spaceTemplateId = spaceTemplate.id + // Get the oldest space template + const spaceTemplate = spaceTemplates.data[0] + const spaceTemplateId = spaceTemplate.id - // Get all the workbooks for the space template - const workbooks = await api.workbooks.list({ - spaceId: spaceTemplateId, - }) + // Get all the workbooks for the space template + const workbooks = await api.workbooks.list({ + spaceId: spaceTemplateId, + }) - // Convert workbooks from the template to workbook configs - const workbookConfigs: Flatfile.CreateWorkbookConfig[] = - workbooks.data.map((workbook) => ({ - name: workbook.name, - labels: workbook.labels, - spaceId: spaceId, - environmentId: environmentId, - namespace: app.data.namespace, - sheets: workbook.sheets.map((sheet) => ({ - ...sheet.config, - })), - actions: workbook.actions, - settings: workbook.settings, - metadata: workbook.metadata, - treatments: workbook.treatments, - })) + // Convert workbooks from the template to workbook configs + const workbookConfigs: Flatfile.CreateWorkbookConfig[] = + workbooks.data.map((workbook) => ({ + name: workbook.name, + labels: workbook.labels, + spaceId: spaceId, + environmentId: environmentId, + namespace: spaceTemplate.namespace, + sheets: workbook.sheets.map((sheet) => ({ + ...sheet.config, + })), + actions: workbook.actions, + settings: workbook.settings, + metadata: workbook.metadata, + treatments: workbook.treatments, + })) - // Create the workbooks - const workbookIds = await Promise.all( - workbookConfigs.map(async (workbookConfig) => { - const workbook = await api.workbooks.create(workbookConfig) - return workbook.data.id - }) - ) + // Create the workbooks + const workbookIds = await Promise.all( + workbookConfigs.map(async (workbookConfig) => { + const workbook = await api.workbooks.create(workbookConfig) + return workbook.data.id + }) + ) - await tick(50, 'Workbook created') + await tick(50, 'Workbook created') - // Set some metadata on the space - await api.spaces.update(spaceId, { - primaryWorkbookId: - workbookIds && workbookIds.length > 0 ? workbookIds[0] : '', - settings: spaceTemplate.settings, - metadata: spaceTemplate.metadata, - actions: spaceTemplate.actions, - access: spaceTemplate.access, - labels: spaceTemplate.labels, - translationsPath: spaceTemplate.translationsPath, - languageOverride: spaceTemplate.languageOverride, - }) + // Set some metadata on the space + await api.spaces.update(spaceId, { + primaryWorkbookId: + workbookIds && workbookIds.length > 0 ? workbookIds[0] : '', + settings: spaceTemplate.settings || {}, + metadata: spaceTemplate.metadata, + actions: spaceTemplate.actions || [], + access: spaceTemplate.access || [], + labels: spaceTemplate.labels, + translationsPath: spaceTemplate.translationsPath || '', + languageOverride: spaceTemplate.languageOverride || '', + }) - // Get all the documents for the space template... - const documents = await api.documents.list(spaceTemplateId) + // Get all the documents for the space template... + const documents = await api.documents.list(spaceTemplateId) - // ...and create them in the new space - for (const document of documents.data) { - await api.documents.create(spaceId, document) - } + // ...and create them in the new space + for (const document of documents.data) { + await api.documents.create(spaceId, document) + } - if (callback) { - await callback(event, workbookIds, tick) + if (callback) { + await callback(event, workbookIds, tick) + } + return { info: 'Space configured' } + } catch (error: any) { + console.log('Space configuration failed with error:', error) + throw new Error('Space configuration failed') } - return { info: 'Space configured' } }) ) } From 5ebe0160b38ab8760ff0e12e909942b127ee16b1 Mon Sep 17 00:00:00 2001 From: Nicholas Cronquist Date: Wed, 22 Jan 2025 21:14:46 -0500 Subject: [PATCH 2/3] Changeset and version bump --- .changeset/eleven-cameras-pretend.md | 5 +++++ plugins/space-configure-from-template/package.json | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) create mode 100644 .changeset/eleven-cameras-pretend.md diff --git a/.changeset/eleven-cameras-pretend.md b/.changeset/eleven-cameras-pretend.md new file mode 100644 index 000000000..c69e06d85 --- /dev/null +++ b/.changeset/eleven-cameras-pretend.md @@ -0,0 +1,5 @@ +--- +'@flatfile/plugin-space-configure-from-template': minor +--- + +Bug fixes for space-configure-from-template-plugin diff --git a/plugins/space-configure-from-template/package.json b/plugins/space-configure-from-template/package.json index 37366fc15..13da25345 100644 --- a/plugins/space-configure-from-template/package.json +++ b/plugins/space-configure-from-template/package.json @@ -1,6 +1,6 @@ { "name": "@flatfile/plugin-space-configure-from-template", - "version": "0.0.1", + "version": "0.1.0", "url": "https://github.com/FlatFilers/flatfile-plugins/tree/main/plugins/space-configure-from-template", "description": "A plugin for configuring a Flatfile Space from a Space Template.", "registryMetadata": { From 8e873dcbd77f8d5b8fd784be0469c31dac18626c Mon Sep 17 00:00:00 2001 From: Nicholas Cronquist Date: Wed, 22 Jan 2025 21:20:32 -0500 Subject: [PATCH 3/3] Fix accidental version bump --- plugins/space-configure-from-template/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/space-configure-from-template/package.json b/plugins/space-configure-from-template/package.json index 13da25345..37366fc15 100644 --- a/plugins/space-configure-from-template/package.json +++ b/plugins/space-configure-from-template/package.json @@ -1,6 +1,6 @@ { "name": "@flatfile/plugin-space-configure-from-template", - "version": "0.1.0", + "version": "0.0.1", "url": "https://github.com/FlatFilers/flatfile-plugins/tree/main/plugins/space-configure-from-template", "description": "A plugin for configuring a Flatfile Space from a Space Template.", "registryMetadata": {