fix(core): Banner dismissal should also work for users migrating to v1 (no-changelog) (#6700)

This commit is contained in:
कारतोफ्फेलस्क्रिप्ट™ 2023-07-19 15:59:49 +02:00 committed by GitHub
parent cc29c4da74
commit 8bb7243c2d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,5 +1,6 @@
import { Service } from 'typedi';
import { DataSource, Repository } from 'typeorm';
import { ErrorReporterProxy as ErrorReporter } from 'n8n-workflow';
import { Settings } from '../entities/Settings';
import config from '@/config';
@ -10,32 +11,24 @@ export class SettingsRepository extends Repository<Settings> {
}
async dismissBanner({ bannerName }: { bannerName: string }): Promise<{ success: boolean }> {
const dismissedBannersSetting = await this.findOneBy({ key: 'ui.banners.dismissed' });
if (dismissedBannersSetting) {
try {
const key = 'ui.banners.dismissed';
const dismissedBannersSetting = await this.findOneBy({ key });
try {
let value: string;
if (dismissedBannersSetting) {
const dismissedBanners = JSON.parse(dismissedBannersSetting.value) as string[];
await this.saveSetting(
'ui.banners.dismissed',
JSON.stringify([...dismissedBanners, bannerName]),
);
return { success: true };
} catch (error) {
return { success: false };
const updatedValue = [...new Set([...dismissedBanners, bannerName].sort())];
value = JSON.stringify(updatedValue);
await this.update({ key }, { value, loadOnStartup: true });
} else {
value = JSON.stringify([bannerName]);
await this.save({ key, value, loadOnStartup: true });
}
config.set(key, value);
return { success: true };
} catch (error) {
ErrorReporter.error(error);
}
return { success: false };
}
async saveSetting(key: string, value: string, loadOnStartup = true) {
const setting = await this.findOneBy({ key });
if (setting) {
await this.update({ key }, { value, loadOnStartup });
} else {
await this.save({ key, value, loadOnStartup });
}
if (loadOnStartup) config.set('ui.banners.dismissed', value);
}
}