feat(app-config): update canConnect upon dependent changes

This commit is contained in:
Ali BARIN
2024-10-08 12:17:44 +00:00
committed by Faruk AYDIN
parent f5d796ea77
commit aed61209fa
4 changed files with 68 additions and 16 deletions

View File

@@ -46,25 +46,46 @@ class AppConfig extends Base {
return await App.findOneByKey(this.key);
}
async updateCanConnectValue() {
const appAuthClients = await this.$relatedQuery('appAuthClients');
async computeCanConnectProperty(oldAppConfig) {
const appAuthClients = await oldAppConfig.$relatedQuery('appAuthClients');
const hasSomeActiveAppAuthClients = !!appAuthClients?.some(
(appAuthClient) => appAuthClient.active
);
const shared = this.shared;
const active = this.disabled === false;
const shared = this.shared ?? oldAppConfig.shared;
const disabled = this.disabled ?? oldAppConfig.disabled;
const active = disabled === false;
const conditions = [hasSomeActiveAppAuthClients, shared, active];
this.canConnect = conditions.every(Boolean);
const canConnect = conditions.every(Boolean);
return this;
return canConnect;
}
async updateCanConnectProperty() {
const canConnect = await this.computeCanConnectProperty(this);
return await this.$query().patch({
canConnect,
});
}
async computeAndAssignCanConnectProperty(oldAppConfig) {
this.canConnect = await this.computeCanConnectProperty(oldAppConfig);
}
async $beforeInsert(queryContext) {
await super.$beforeInsert(queryContext);
await this.computeAndAssignCanConnectProperty(this);
}
async $beforeUpdate(opt, queryContext) {
await super.$beforeUpdate(opt, queryContext);
await opt.old.updateCanConnectValue();
const oldAppConfig = opt.old;
await this.computeAndAssignCanConnectProperty(oldAppConfig);
}
}