src/module/settings/settings.service.ts
Properties |
|
Methods |
|
constructor(orm: MikroORM, repository: EntityRepository<Setting>, em: EntityManager)
|
||||||||||||
Defined in src/module/settings/settings.service.ts:12
|
||||||||||||
Parameters :
|
Private generateDefaultSettings |
generateDefaultSettings()
|
Defined in src/module/settings/settings.service.ts:117
|
Generates a The defaults can be overwritten by the config file. If they are present in said file those values will be used.
Returns :
Setting
|
Async getClientSettings |
getClientSettings()
|
Defined in src/module/settings/settings.service.ts:28
|
See getSettingsEntity
Returns :
Promise<IClientSettings>
The current settings saved in the DB. |
Async getMailingOptions |
getMailingOptions()
|
Defined in src/module/settings/settings.service.ts:51
|
Returns :
Promise<IMailingSettings | undefined>
MailingConfiguration saved in the DB or |
Private Async getSettingsEntity |
getSettingsEntity()
|
Defined in src/module/settings/settings.service.ts:98
|
Find the settings document in the database. Finds and returns the
Returns :
Promise<Setting>
|
Async onApplicationBootstrap |
onApplicationBootstrap()
|
Decorators :
@CreateRequestContext()
|
Defined in src/module/settings/settings.service.ts:64
|
Checks if there is a If there is NO settings document after module initialization a new default If there is ONE nothing is done.
Returns :
Promise<void>
|
Async setClientSettings | ||||||||
setClientSettings(dto: ClientSettingsDTO)
|
||||||||
Defined in src/module/settings/settings.service.ts:40
|
||||||||
Changes the settings saved in the DB to use the new settings. If
Parameters :
Returns :
Promise<void>
|
Private assertConfigNoErrors | ||||||||
assertConfigNoErrors(errors: ValidationError[])
|
||||||||
Inherited from
StaticSettings
|
||||||||
Defined in
StaticSettings:232
|
||||||||
Checks if the If it is not empty an exception is thrown.
Parameters :
Returns :
void
|
Private Static assertEnvNoErrors | ||||||||
assertEnvNoErrors(errors: ValidationError[])
|
||||||||
Inherited from
StaticSettings
|
||||||||
Defined in
StaticSettings:207
|
||||||||
Checks if the
Parameters :
Returns :
void
|
getAPIPrefix |
getAPIPrefix()
|
Inherited from
StaticSettings
|
Defined in
StaticSettings:132
|
Returns :
string
Prefix for the API path. |
getConfigPath |
getConfigPath()
|
Inherited from
StaticSettings
|
Defined in
StaticSettings:125
|
Returns :
string
Path to the configuration folder. |
getDatabaseConfiguration |
getDatabaseConfiguration()
|
Inherited from
StaticSettings
|
Defined in
StaticSettings:59
|
Returns :
DatabaseConfiguration
Configuration for the database. |
getDatabaseConnectionInformation |
getDatabaseConnectionInformation()
|
Inherited from
StaticSettings
|
Defined in
StaticSettings:66
|
Returns :
DatabaseConnectionOptions
Object containing the necessary information to connect to the database. |
getDatabaseSecret |
getDatabaseSecret()
|
Inherited from
StaticSettings
|
Defined in
StaticSettings:52
|
Returns the encryption secret for the database.
Returns :
string
Encryption secret. |
getGotenbergConfiguration |
getGotenbergConfiguration()
|
Inherited from
StaticSettings
|
Defined in
StaticSettings:79
|
@returns Configuration for the gotenberg instance. Can be
Returns :
GotenbergConfiguration | undefined
Configuration for the gotenberg instance. Can be |
getHandbookUrl |
getHandbookUrl()
|
Inherited from
StaticSettings
|
Defined in
StaticSettings:145
|
Returns :
string | undefined
If configured it returns the URL to the handbook, otherwise |
getPathPrefix |
getPathPrefix()
|
Inherited from
StaticSettings
|
Defined in
StaticSettings:109
|
Returns the prefix for the app if there is one. If there is one any trailing '/' will be removed before returning the prefix.
Returns :
string | null
Prefix for the app or |
Static getService |
getService()
|
Inherited from
StaticSettings
|
Defined in
StaticSettings:43
|
Returns the currently active service. If no service was previously created a new one is created and returned.
Returns :
StaticSettings
Current SettingsService. |
getSessionTimeout |
getSessionTimeout()
|
Inherited from
StaticSettings
|
Defined in
StaticSettings:90
|
Returns the value of the If no
Returns :
number
The specified session timeout in minutes. |
getStaticFolder |
getStaticFolder()
|
Inherited from
StaticSettings
|
Defined in
StaticSettings:152
|
Returns :
string
Path to the static folder. |
Private getStringForError | |||||||||||||||
getStringForError(error: ValidationError, depth: number)
|
|||||||||||||||
Inherited from
StaticSettings
|
|||||||||||||||
Defined in
StaticSettings:254
|
|||||||||||||||
Converts the given ValidationError to a formatted String.
Parameters :
Returns :
string
String for the given ValidationError. |
Private initConfig | ||||||||||||
initConfig(fileContent: string, environment: string)
|
||||||||||||
Inherited from
StaticSettings
|
||||||||||||
Defined in
StaticSettings:321
|
||||||||||||
Loads the configuration from the given file content. If the content is not a valid configuration or valid YAML an exception is thrown.
Parameters :
Returns :
ApplicationConfiguration
Parsed and validated ApplicationConfiguration. |
Private loadConfigFile |
loadConfigFile()
|
Inherited from
StaticSettings
|
Defined in
StaticSettings:289
|
Loads the configuration file and returns the parsed configuration. The configuration file for the current environment gets loaded and parsed.
Returns :
ApplicationConfiguration
Parsed and validated ApplicationConfiguration. |
Private loadDatabaseConfig |
loadDatabaseConfig()
|
Inherited from
StaticSettings
|
Defined in
StaticSettings:163
|
Loads the configuration of the database. This is achieved by combining the required options from the config-file and the required environment variables.
Returns :
DatabaseConfiguration
Configuration options for the database. |
Private Static loadEnvironmentVariables |
loadEnvironmentVariables()
|
Inherited from
StaticSettings
|
Defined in
StaticSettings:191
|
Loads the environment variables as
Returns :
EnvironmentConfig
Valid configuration extracted from the environment variables. |
Private Static Readonly API_PREFIX |
Type : string
|
Default value : 'api'
|
Inherited from
StaticSettings
|
Defined in
StaticSettings:20
|
Protected Readonly config |
Type : ApplicationConfiguration
|
Inherited from
StaticSettings
|
Defined in
StaticSettings:23
|
Private Readonly databaseConfig |
Type : DatabaseConfiguration
|
Inherited from
StaticSettings
|
Defined in
StaticSettings:24
|
Private Readonly envConfig |
Type : EnvironmentConfig
|
Inherited from
StaticSettings
|
Defined in
StaticSettings:25
|
Protected Readonly logger |
Default value : new ConsoleLogger(StaticSettings.name)
|
Inherited from
StaticSettings
|
Defined in
StaticSettings:27
|
Private Static service |
Type : StaticSettings
|
Default value : new StaticSettings()
|
Inherited from
StaticSettings
|
Defined in
StaticSettings:18
|
Private Static Readonly STATIC_FOLDER |
Type : string
|
Default value : 'app'
|
Inherited from
StaticSettings
|
Defined in
StaticSettings:21
|
import { CreateRequestContext, EntityRepository, MikroORM } from '@mikro-orm/core';
import { EntityManager } from '@mikro-orm/mysql';
import { InjectRepository } from '@mikro-orm/nestjs';
import { Inject, Injectable, OnApplicationBootstrap } from '@nestjs/common';
import { IClientSettings, IMailingSettings } from 'shared/model/Settings';
import { Setting } from '../../database/entities/settings.entity';
import { StartUpException } from '../../exceptions/StartUpException';
import { ClientSettingsDTO } from './settings.dto';
import { StaticSettings } from './settings.static';
@Injectable()
export class SettingsService extends StaticSettings implements OnApplicationBootstrap {
constructor(
private readonly orm: MikroORM,
@InjectRepository(Setting)
private readonly repository: EntityRepository<Setting>,
@Inject(EntityManager)
private readonly em: EntityManager
) {
super();
}
/**
* @returns The current settings saved in the DB.
*
* @see getSettingsEntity
*/
async getClientSettings(): Promise<IClientSettings> {
const settings = await this.getSettingsEntity();
return settings.toDTO();
}
/**
* Changes the settings saved in the DB to use the new settings.
*
* If `settings` does not contain a property this setting-property will be untouched (ie the previous value will be used).
*
* @param dto New settings to use.
*/
async setClientSettings(dto: ClientSettingsDTO): Promise<void> {
const settings = await this.getSettingsEntity();
settings.updateFromDTO(dto);
await this.em.persistAndFlush(settings);
}
/**
* @returns MailingConfiguration saved in the DB or `undefined` if none are saved.
*/
async getMailingOptions(): Promise<IMailingSettings | undefined> {
const settings = await this.getSettingsEntity();
return settings.mailSettings?.toDTO();
}
/**
* Checks if there is a `SettingsDocument` in the database.
*
* If there is NO settings document after module initialization a new default `SettingsDocument` is created.
*
* If there is ONE nothing is done.
*/
@CreateRequestContext()
async onApplicationBootstrap(): Promise<void> {
const settings = await this.repository.findOne({
id: Setting.SETTING_ID,
});
if (!settings) {
this.logger.log(
'No settings were provided. Creating new default settings...',
SettingsService.name
);
try {
const defaults = this.generateDefaultSettings();
this.logger.log(
`Default settings used: ${JSON.stringify(defaults)}`,
SettingsService.name
);
await this.em.persistAndFlush(defaults);
this.logger.log('Default settings successfully created.', SettingsService.name);
} catch (err) {
throw new StartUpException('Could not create the default settings.');
}
}
}
/**
* Find the settings document in the database.
*
* Finds and returns the `SettingsDocument` saved in the database. If there is more than one the first document is returned.
*
* @returns `SettingsDocument` if there is one, `undefined` else.
* @throws `Error` - If there is not `SettingsDocument` saved in the database.
*/
private async getSettingsEntity(): Promise<Setting> {
const settings = await this.repository.findOne({
id: Setting.SETTING_ID,
});
if (!settings) {
throw new Error(
'No settings document saved in the database. This might be due to a failed initialization.'
);
}
return settings;
}
/**
* Generates a `SettingsModel` with some default values.
*
* The defaults can be overwritten by the config file. If they are present in said file those values will be used.
*/
private generateDefaultSettings(): Setting {
const defaultsFromConfig = this.config.defaultSettings;
return Setting.fromDTO(defaultsFromConfig);
}
}