Wrap sample data creation with custom error catching and move link generation out of main controller

This commit is contained in:
Andrew Kingston 2025-03-12 08:31:45 +00:00
parent 8906a159c6
commit bbd9f607cd
No known key found for this signature in database
1 changed files with 24 additions and 15 deletions

View File

@ -198,6 +198,19 @@ async function addSampleDataScreen() {
await db.put(screen)
}
function addSampleDataNavLinks(app: App) {
if (app.navigation) {
app.navigation.links = [
{
text: "Inventory",
url: "/inventory",
type: "link",
roleId: roles.BUILTIN_ROLE_IDS.BASIC,
},
]
}
}
export const addSampleData = async (
ctx: UserCtx<void, AddAppSampleDataResponse>
) => {
@ -311,18 +324,6 @@ async function performAppCreate(
await updateUserColumns(appId, db, ctx.user._id!)
}
// Add link to sample data page if required
const newLinks = addSampleData
? [
{
text: "Inventory",
url: "/inventory",
type: "link",
roleId: roles.BUILTIN_ROLE_IDS.BASIC,
},
]
: []
const newApplication: App = {
_id: DocumentType.APP_METADATA,
_rev: undefined,
@ -344,7 +345,7 @@ async function performAppCreate(
navWidth: "Large",
navBackground: "var(--spectrum-global-color-static-blue-800)",
navTextColor: "var(--spectrum-global-color-static-white)",
links: newLinks,
links: [],
},
theme: DefaultAppTheme,
customTheme: {
@ -358,6 +359,10 @@ async function performAppCreate(
creationVersion: undefined,
}
if (addSampleData) {
addSampleDataNavLinks(newApplication)
}
if (!isImport) {
newApplication.creationVersion = envCore.VERSION
}
@ -406,8 +411,12 @@ async function performAppCreate(
// Add sample datasource and example screen for non-templates/non-imports
if (addSampleData) {
try {
await addSampleDataDocs()
await addSampleDataScreen()
} catch (err) {
ctx.throw(400, "App created, but failed to add sample data")
}
}
const latestMigrationId = appMigrations.getLatestEnabledMigrationId()