|
|
@@ -18,11 +18,19 @@ export class GlobalSettingsService {
|
|
|
) {}
|
|
|
|
|
|
/**
|
|
|
- * Ensure there is a global settings row in the database.
|
|
|
+ * Ensure there is a single global settings row in the database.
|
|
|
*/
|
|
|
async initGlobalSettings() {
|
|
|
try {
|
|
|
- await this.getSettings(RequestContext.empty());
|
|
|
+ const result = await this.connection.getRepository(GlobalSettings).find();
|
|
|
+ if (result.length === 0) {
|
|
|
+ throw new Error('No global settings');
|
|
|
+ }
|
|
|
+ if (1 < result.length) {
|
|
|
+ // Strange edge case, see https://github.com/vendure-ecommerce/vendure/issues/987
|
|
|
+ const toDelete = result.slice(1);
|
|
|
+ await this.connection.getRepository(GlobalSettings).remove(toDelete);
|
|
|
+ }
|
|
|
} catch (err) {
|
|
|
const settings = new GlobalSettings({
|
|
|
availableLanguages: [this.configService.defaultLanguageCode],
|
|
|
@@ -32,7 +40,11 @@ export class GlobalSettingsService {
|
|
|
}
|
|
|
|
|
|
async getSettings(ctx: RequestContext): Promise<GlobalSettings> {
|
|
|
- const settings = await this.connection.getRepository(ctx, GlobalSettings).findOne();
|
|
|
+ const settings = await this.connection.getRepository(ctx, GlobalSettings).findOne({
|
|
|
+ order: {
|
|
|
+ createdAt: 'ASC',
|
|
|
+ },
|
|
|
+ });
|
|
|
if (!settings) {
|
|
|
throw new InternalServerError(`error.global-settings-not-found`);
|
|
|
}
|