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

@@ -7,6 +7,10 @@ exports[`AppConfig model > jsonSchema should have correct validations 1`] = `
"default": false,
"type": "boolean",
},
"canConnect": {
"default": false,
"type": "boolean",
},
"createdAt": {
"type": "string",
},
@@ -38,7 +42,6 @@ exports[`AppConfig model > jsonSchema should have correct validations 1`] = `
exports[`AppConfig model > virtualAttributes should return correct properties 1`] = `
[
"canConnect",
"canCustomConnect",
]
`;

View File

@@ -63,7 +63,7 @@ class AppAuthClient extends Base {
async triggerAppConfigUpdate() {
const appConfig = await this.$relatedQuery('appConfig').select('*');
await appConfig.$query().patch({});
await appConfig?.updateCanConnectProperty();
}
// TODO: Make another abstraction like beforeSave instead of using
@@ -84,8 +84,8 @@ class AppAuthClient extends Base {
this.encryptData();
}
async $afterUpdate(queryContext) {
await super.$afterUpdate(queryContext);
async $afterUpdate(opt, queryContext) {
await super.$afterUpdate(opt, queryContext);
await this.triggerAppConfigUpdate();
}

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);
}
}

View File

@@ -66,24 +66,52 @@ describe('AppConfig model', () => {
});
describe('canConnect', () => {
it('should return true when app is enabled, shared and allows custom connection', async () => {
it('should return true when app is enabled, shared and allows custom connection with an active app auth client at least', async () => {
await createAppAuthClient({
appKey: 'deepl',
active: true,
});
let appConfig = await createAppConfig({
await createAppAuthClient({
appKey: 'deepl',
active: false,
});
const appConfig = await createAppConfig({
disabled: false,
allowCustomConnection: true,
shared: true,
key: 'deepl',
});
appConfig = await appConfig.$query().withGraphFetched({
appAuthClients: true,
expect(appConfig.canConnect).toBe(true);
});
expect(appConfig.canConnect).toBe(true);
it('should return true when app is enabled, shared and allows custom connection with no active app auth client', async () => {
await createAppAuthClient({
appKey: 'deepl',
active: false,
});
const appConfig = await createAppConfig({
disabled: false,
allowCustomConnection: true,
shared: true,
key: 'deepl',
});
expect(appConfig.canConnect).toBe(false);
});
it('should return false when app is enabled, shared and allows custom connection without any app auth clients', async () => {
const appConfig = await createAppConfig({
disabled: false,
allowCustomConnection: true,
shared: true,
key: 'deepl',
});
expect(appConfig.canConnect).toBe(false);
});
it('should return false when app is disabled', async () => {