Skip to content

Commit

Permalink
fix(api-serverless-cms): improve error handling of InstallTenant util…
Browse files Browse the repository at this point in the history
…ity (#4274)
  • Loading branch information
Pavel910 authored Sep 24, 2024
1 parent 290419b commit 9b6cf91
Showing 1 changed file with 73 additions and 24 deletions.
97 changes: 73 additions & 24 deletions packages/api-serverless-cms/src/tenancy/InstallTenant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,50 +59,99 @@ export class InstallTenant {
await this.context.tenancy.setVersion(this.context.WEBINY_VERSION);

// SECURITY: Create initial security groups.
await this.context.security.install();
await this.runOrThrow(async () => {
const isInstalled = await this.context.security.getVersion();
if (!isInstalled) {
await this.context.security.install();
}
}, "SECURITY_INSTALL");

// ADMIN USERS: Optionally, create an admin user for this tenant.
if (config.adminUsers) {
await this.context.adminUsers.install(config.adminUsers);
} else {
// We always mark `adminUsers` as installed, regardless of the config.
await this.context.adminUsers.setVersion(this.context.WEBINY_VERSION);
}
await this.runOrThrow(async () => {
const isInstalled = await this.context.adminUsers.getVersion();
if (isInstalled) {
return;
}

if (config.adminUsers) {
await this.context.adminUsers.install(config.adminUsers);
} else {
// We always mark `adminUsers` as installed, regardless of the config.
await this.context.adminUsers.setVersion(this.context.WEBINY_VERSION);
}
}, "ADMIN_USERS_INSTALL");

// I18N: Create a default locale.
await this.context.i18n.system.installSystem({
code: config.i18n.defaultLocaleCode
});
await this.runOrThrow(async () => {
const isInstalled = await this.context.i18n.system.getSystemVersion();
if (isInstalled) {
return;
}

await this.context.i18n.system.installSystem({
code: config.i18n.defaultLocaleCode
});
}, "I18N_INSTALL");

// CMS
await this.context.cms.installSystem();
await this.runOrThrow(async () => {
const isInstalled = await this.context.cms.getSystemVersion();
if (isInstalled) {
return;
}
await this.context.cms.installSystem();
}, "CMS_INSTALL");

// FILE MANAGER: Create File Manager settings.
const srcPrefix = config.fileManager
? `${new URL(config.fileManager.assetDeliveryDomain).origin}/files/`
: fmSettings?.srcPrefix;

await this.context.fileManager.install({ srcPrefix: srcPrefix || "" });
await this.runOrThrow(async () => {
const isInstalled = await this.context.fileManager.getVersion();
if (isInstalled) {
return;
}

await this.context.fileManager.install({ srcPrefix: srcPrefix || "" });
}, "FILE_MANAGER_INSTALL");

// PAGE BUILDER: Create Page Builder settings.
await this.context.pageBuilder.installSystem({
name: config.pageBuilder?.websiteName ?? tenant.name,
insertDemoData: config.pageBuilder?.insertDemoData ?? false
});
await this.runOrThrow(async () => {
const isInstalled = await this.context.pageBuilder.getSystemVersion();
if (isInstalled) {
return;
}
await this.context.pageBuilder.installSystem({
name: config.pageBuilder?.websiteName ?? tenant.name,
insertDemoData: config.pageBuilder?.insertDemoData ?? false
});
}, "PAGE_BUILDER_INSTALL");

// FORM BUILDER
await this.context.formBuilder.installSystem({});
} catch (e) {
await this.runOrThrow(async () => {
const isInstalled = await this.context.formBuilder.getSystemVersion();
if (isInstalled) {
return;
}
await this.context.formBuilder.installSystem({});
}, "FORM_BUILDER_INSTALL");
} finally {
this.context.tenancy.setCurrentTenant(currentTenant);
}
}

private async runOrThrow(cb: () => Promise<void>, errorCode: string): Promise<void> {
try {
await cb();
} catch (err) {
throw new Error({
message: e.message,
code: "INSTALL_TENANT",
message: err.message,
code: `INSTALL_TENANT:${errorCode}`,
data: {
config,
error: e
error: err
}
});
} finally {
this.context.tenancy.setCurrentTenant(currentTenant);
}
}
}

0 comments on commit 9b6cf91

Please sign in to comment.