From c863a50e61d78294695b791a50b1f8aefb145028 Mon Sep 17 00:00:00 2001 From: Miguel Lansdorf Date: Tue, 31 Oct 2023 18:36:45 +0800 Subject: [PATCH] Automatically update dashboard.content_id Update dashboard.content_id when null upon creation of dashboard content --- api/src/services/dashboard_content.service.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/api/src/services/dashboard_content.service.ts b/api/src/services/dashboard_content.service.ts index 05bd4aedf..dd76140a7 100644 --- a/api/src/services/dashboard_content.service.ts +++ b/api/src/services/dashboard_content.service.ts @@ -122,6 +122,7 @@ export class DashboardContentService { async create(dashboard_id: string, name: string, content: Content, locale: string): Promise { const dashboardContentRepo = dashboardDataSource.getRepository(DashboardContent); + const dashboardRepo = dashboardDataSource.getRepository(Dashboard); if (await dashboardContentRepo.exist({ where: { dashboard_id, name } })) { throw new ApiError(BAD_REQUEST, { message: translate('DASHBOARD_CONTENT_NAME_ALREADY_EXISTS', locale) }); } @@ -133,7 +134,13 @@ export class DashboardContentService { if (!(await migrateOneDashboardContent(dashboardContent))) { throw new ApiError(BAD_REQUEST, { message: translate('DASHBOARD_CONTENT_MIGRATION_FAILED', locale) }); } - return dashboardContentRepo.save(dashboardContent); + const result = await dashboardContentRepo.save(dashboardContent); + const dashboard = await dashboardRepo.findOneByOrFail({ id: dashboard_id }); + if (!dashboard.content_id) { + dashboard.content_id = result.id; + await dashboardRepo.save(dashboard); + } + return result; } async get(id: string): Promise {