import { IClassOf } from "@entity-access/entity-access/dist/decorators/IClassOf.js"; import { RegisterSingleton, ServiceProvider } from "@entity-access/entity-access/dist/di/di.js"; import SocialMailContext from "../model/SocialMailContext.js"; import EntityAccessError from "@entity-access/entity-access/dist/common/EntityAccessError.js"; import Configuration from "../model/entities/Configuration.js"; import semver from "semver"; import { cacheFor, ICacheContainer, ObjectCache } from "../../common/cacheFor.js"; import { hash } from "node:crypto"; export class UIPackageConfig { public package: string; public version: string; public view: string; } export class FAFreePackageConfig { public package: string; public version: string; public view: string; } export class WebComponentsPackageConfig { public package: string; public version: string; public view: string; } export class CredentialsConfig { public host: string; public url: string; public username: string; public password: string; } export class ConfigItem { public get value() { return this.item.value as Partial; } public set value(v: Partial) { this.item.value = v; } constructor( public readonly item: Configuration, private config: DBConfig, private type: IClassOf ) { } public async update() { await this.config.save(this.type, this.value); } } class LastPackage { public value: string; } @RegisterSingleton export default class DBConfig implements ICacheContainer { cache: ObjectCache; private configPackagesChanged = false; // @Inject // private encryptionService: EncryptionService; async packagesChanged() { if(this.configPackagesChanged) { return true; } const last = await this.getOrCreate(LastPackage); const value = await this.getPackagesHash(); if(last.value.value === value) { this.configPackagesChanged = false; return false; } this.configPackagesChanged = true; last.value.value = value; await last.update(); return true; } @cacheFor() public async getOrCreate(type: IClassOf, defaults: Partial = {}, name = type.name) { using scope = ServiceProvider.createScope(this); const db = scope.resolve(SocialMailContext); const c = await db.configurations.statements.selectOrInsert({ name, configValue: JSON.stringify(defaults, void 0, 4) } as any); // c.decryptedPassword = this.encryptionService.decrypt(c.password); Object.setPrototypeOf(c, Configuration.prototype); return new ConfigItem(c, this, type); } @cacheFor() public async get(type: IClassOf, throwIfNotFound = true, name = type.name) { using scope = ServiceProvider.createScope(this); const db = scope.resolve(SocialMailContext); const c = await db.configurations.where({ name }, (x, p) => x.name === p.name).first(); if (!c) { if (throwIfNotFound) { throw new EntityAccessError(`${type.name} configuration does not exist`); } return null; } // c.decryptedPassword = this.encryptionService.decrypt(c.password); return new ConfigItem(c, this, type); } @cacheFor() async getPackagesHash() { const config = this; const packages = [ await config.get(UIPackageConfig), await config.get(WebComponentsPackageConfig), await config.get(FAFreePackageConfig) ]; const p = packages.map((x) => x.value.package + "@" + x.value.version).join(","); return hash("sha256", p, "base64url").replaceAll("=", "--"); } @cacheFor() async getPackages() { const config = this; const packages = [ await config.get(UIPackageConfig), await config.get(WebComponentsPackageConfig), await config.get(FAFreePackageConfig) ]; return packages.map((x) => ({ latest: x.value.package + "@latest", version: x.value.package + "@" + x.value.version })); } public async getValue(type: IClassOf, throwIfNotFound = true, name = type.name) { return (await this.get(type, throwIfNotFound, name))?.value; } public async set(values: T) { const c = Object.getPrototypeOf(values)?.constructor; if (c === Object) { throw new EntityAccessError(`Config must not be an object but an instance of a class`); } return this.save(c, values); } public async seed(c: IClassOf, values: Partial, name = c.name) { return this.save(c, values, true, name); } public async save(type: IClassOf, values: Partial, seed = false, name = type.name) { using scope = ServiceProvider.createScope(this); const db = scope.resolve(SocialMailContext); let config; if (seed) { config = await db.configurations.statements.selectOrInsert({ name, configValue: JSON.stringify(values, void 0, 4)} as any, { name }); } else { config = await db.configurations.statements.upsert({ name, configValue: JSON.stringify(values, void 0, 4) } as any, void 0, { name }); } const ci = new ConfigItem(config, this, type); this.cache.clear(); return ci; } public async saveVersion(type: IClassOf, values: Partial) { const version = values.version; const existing = await this.getValue(type, false); if (existing) { if (semver.gte(existing.version, version)) { return; } } await this.save(type, values); } }