feat(app-config): update canConnect upon dependent changes
This commit is contained in:
@@ -7,6 +7,10 @@ exports[`AppConfig model > jsonSchema should have correct validations 1`] = `
|
|||||||
"default": false,
|
"default": false,
|
||||||
"type": "boolean",
|
"type": "boolean",
|
||||||
},
|
},
|
||||||
|
"canConnect": {
|
||||||
|
"default": false,
|
||||||
|
"type": "boolean",
|
||||||
|
},
|
||||||
"createdAt": {
|
"createdAt": {
|
||||||
"type": "string",
|
"type": "string",
|
||||||
},
|
},
|
||||||
@@ -38,7 +42,6 @@ exports[`AppConfig model > jsonSchema should have correct validations 1`] = `
|
|||||||
|
|
||||||
exports[`AppConfig model > virtualAttributes should return correct properties 1`] = `
|
exports[`AppConfig model > virtualAttributes should return correct properties 1`] = `
|
||||||
[
|
[
|
||||||
"canConnect",
|
|
||||||
"canCustomConnect",
|
"canCustomConnect",
|
||||||
]
|
]
|
||||||
`;
|
`;
|
||||||
|
@@ -63,7 +63,7 @@ class AppAuthClient extends Base {
|
|||||||
async triggerAppConfigUpdate() {
|
async triggerAppConfigUpdate() {
|
||||||
const appConfig = await this.$relatedQuery('appConfig').select('*');
|
const appConfig = await this.$relatedQuery('appConfig').select('*');
|
||||||
|
|
||||||
await appConfig.$query().patch({});
|
await appConfig?.updateCanConnectProperty();
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Make another abstraction like beforeSave instead of using
|
// TODO: Make another abstraction like beforeSave instead of using
|
||||||
@@ -84,8 +84,8 @@ class AppAuthClient extends Base {
|
|||||||
this.encryptData();
|
this.encryptData();
|
||||||
}
|
}
|
||||||
|
|
||||||
async $afterUpdate(queryContext) {
|
async $afterUpdate(opt, queryContext) {
|
||||||
await super.$afterUpdate(queryContext);
|
await super.$afterUpdate(opt, queryContext);
|
||||||
|
|
||||||
await this.triggerAppConfigUpdate();
|
await this.triggerAppConfigUpdate();
|
||||||
}
|
}
|
||||||
|
@@ -46,25 +46,46 @@ class AppConfig extends Base {
|
|||||||
return await App.findOneByKey(this.key);
|
return await App.findOneByKey(this.key);
|
||||||
}
|
}
|
||||||
|
|
||||||
async updateCanConnectValue() {
|
async computeCanConnectProperty(oldAppConfig) {
|
||||||
const appAuthClients = await this.$relatedQuery('appAuthClients');
|
const appAuthClients = await oldAppConfig.$relatedQuery('appAuthClients');
|
||||||
const hasSomeActiveAppAuthClients = !!appAuthClients?.some(
|
const hasSomeActiveAppAuthClients = !!appAuthClients?.some(
|
||||||
(appAuthClient) => appAuthClient.active
|
(appAuthClient) => appAuthClient.active
|
||||||
);
|
);
|
||||||
const shared = this.shared;
|
const shared = this.shared ?? oldAppConfig.shared;
|
||||||
const active = this.disabled === false;
|
const disabled = this.disabled ?? oldAppConfig.disabled;
|
||||||
|
const active = disabled === false;
|
||||||
|
|
||||||
const conditions = [hasSomeActiveAppAuthClients, shared, active];
|
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) {
|
async $beforeUpdate(opt, queryContext) {
|
||||||
await super.$beforeUpdate(opt, queryContext);
|
await super.$beforeUpdate(opt, queryContext);
|
||||||
|
|
||||||
await opt.old.updateCanConnectValue();
|
const oldAppConfig = opt.old;
|
||||||
|
|
||||||
|
await this.computeAndAssignCanConnectProperty(oldAppConfig);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -66,24 +66,52 @@ describe('AppConfig model', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe('canConnect', () => {
|
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({
|
await createAppAuthClient({
|
||||||
appKey: 'deepl',
|
appKey: 'deepl',
|
||||||
active: true,
|
active: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
let appConfig = await createAppConfig({
|
await createAppAuthClient({
|
||||||
|
appKey: 'deepl',
|
||||||
|
active: false,
|
||||||
|
});
|
||||||
|
|
||||||
|
const appConfig = await createAppConfig({
|
||||||
disabled: false,
|
disabled: false,
|
||||||
allowCustomConnection: true,
|
allowCustomConnection: true,
|
||||||
shared: true,
|
shared: true,
|
||||||
key: 'deepl',
|
key: 'deepl',
|
||||||
});
|
});
|
||||||
|
|
||||||
appConfig = await appConfig.$query().withGraphFetched({
|
expect(appConfig.canConnect).toBe(true);
|
||||||
appAuthClients: 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,
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(appConfig.canConnect).toBe(true);
|
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 () => {
|
it('should return false when app is disabled', async () => {
|
||||||
|
Reference in New Issue
Block a user