feat(app-config): persist relational virtual attrs

This commit is contained in:
Ali BARIN
2024-10-07 09:11:55 +00:00
committed by Faruk AYDIN
parent ecb04b4ba9
commit f5d796ea77
4 changed files with 94 additions and 21 deletions

View File

@@ -12,6 +12,7 @@ class AppConfig extends Base {
properties: {
id: { type: 'string', format: 'uuid' },
key: { type: 'string' },
canConnect: { type: 'boolean', default: false },
allowCustomConnection: { type: 'boolean', default: false },
shared: { type: 'boolean', default: false },
disabled: { type: 'boolean', default: false },
@@ -32,30 +33,39 @@ class AppConfig extends Base {
});
static get virtualAttributes() {
return ['canConnect', 'canCustomConnect'];
return ['canCustomConnect'];
}
get canCustomConnect() {
return !this.disabled && this.allowCustomConnection;
}
get canConnect() {
const hasSomeActiveAppAuthClients = !!this.appAuthClients?.some(
(appAuthClient) => appAuthClient.active
);
const shared = this.shared;
const active = this.disabled === false;
const conditions = [hasSomeActiveAppAuthClients, shared, active];
return conditions.every(Boolean);
}
async getApp() {
if (!this.key) return null;
return await App.findOneByKey(this.key);
}
async updateCanConnectValue() {
const appAuthClients = await this.$relatedQuery('appAuthClients');
const hasSomeActiveAppAuthClients = !!appAuthClients?.some(
(appAuthClient) => appAuthClient.active
);
const shared = this.shared;
const active = this.disabled === false;
const conditions = [hasSomeActiveAppAuthClients, shared, active];
this.canConnect = conditions.every(Boolean);
return this;
}
async $beforeUpdate(opt, queryContext) {
await super.$beforeUpdate(opt, queryContext);
await opt.old.updateCanConnectValue();
}
}
export default AppConfig;