Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/eleven-cameras-pretend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@flatfile/plugin-space-configure-from-template': minor
---

Bug fixes for space-configure-from-template-plugin
9 changes: 4 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions plugins/space-configure-from-template/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
)
Comment thread
ncronquist marked this conversation as resolved.

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' }
})
)
}
Expand Down