refactor: 💡 use recrusive listing for desktop queries
and add mock rescursive support for scopes, targets, and sessions
This commit is contained in:
11 files changed
+132
-110
No files matched your search
@@ -32,6 +32,14 @@ export default class SessionModel extends GeneratedSessionModel {
|
||||
: null;
|
||||
}
|
||||
|
||||
/**
|
||||
* The target associated with this session (if loaded).
|
||||
* @type {TargetModel}
|
||||
*/
|
||||
get target() {
|
||||
return this.store.peekRecord('target', this.target_id);
|
||||
}
|
||||
|
||||
// =methods
|
||||
|
||||
/**
|
||||
|
||||
@@ -45,6 +45,14 @@ export default class TargetModel extends GeneratedTargetModel {
|
||||
.filter(s => s && (s.target_id === this.id));
|
||||
}
|
||||
|
||||
/**
|
||||
* The project associated with this target (if already loaded).
|
||||
* @type {ScopeModel}
|
||||
*/
|
||||
get project() {
|
||||
return this.store.peekRecord('scope', this.scopeID);
|
||||
}
|
||||
|
||||
/**
|
||||
* True if any sessions associated with this target are active or pending.
|
||||
* @type {boolean}
|
||||
|
||||
@@ -23,13 +23,16 @@ export default function() {
|
||||
|
||||
// Scope resources
|
||||
|
||||
this.get('/scopes', ({ scopes }, { queryParams: { scope_id } }) => {
|
||||
this.get('/scopes', ({ scopes }, { queryParams: { scope_id, recursive } }) => {
|
||||
// Default parent scope is global
|
||||
if (!scope_id) scope_id = 'global';
|
||||
const results = scopes.where(scope => {
|
||||
return scope.scope ? scope.scope.id === scope_id : false
|
||||
});
|
||||
return results;
|
||||
if (recursive && scope_id === 'global') {
|
||||
return scopes.all();
|
||||
} else {
|
||||
return scopes.where(scope => {
|
||||
return scope.scope ? scope.scope.id === scope_id : false
|
||||
});
|
||||
}
|
||||
});
|
||||
this.post('/scopes', function ({ scopes }) {
|
||||
// Parent scope comes through the payload via `scope_id`, but this needs
|
||||
@@ -287,7 +290,16 @@ export default function() {
|
||||
|
||||
// target
|
||||
|
||||
this.get('/targets', function ({ targets }, { queryParams: { scope_id } }) {
|
||||
this.get('/targets', function ({ targets }, { queryParams: { scope_id, recursive } }) {
|
||||
if (recursive && scope_id === 'global') {
|
||||
return targets.all();
|
||||
} else if (recursive) {
|
||||
return targets.where(target => {
|
||||
const targetModel = targets.find(target.id);
|
||||
return (target.scopeId === scope_id)
|
||||
|| (targetModel?.scope?.scope?.id === scope_id)
|
||||
});
|
||||
}
|
||||
return targets.where(target => target.scopeId === scope_id);
|
||||
});
|
||||
this.post('/targets', function ({ targets }) {
|
||||
@@ -325,7 +337,7 @@ export default function() {
|
||||
|
||||
// session
|
||||
|
||||
this.get('/sessions', function ({ sessions }, { queryParams: { scope_id } }) {
|
||||
this.get('/sessions', function ({ sessions }, { queryParams: { scope_id, recursive } }) {
|
||||
// To simulate changes to `session.status` that may occur in the backend,
|
||||
// we quietly randomize the value of the field on GET.
|
||||
// To populate sessions for logged in user,
|
||||
@@ -333,7 +345,7 @@ export default function() {
|
||||
// But only if not in testing mode.
|
||||
// In tests, we need deterministic statuses.
|
||||
if (!Ember.testing) {
|
||||
sessions.where(session => session.scopeId === scope_id)
|
||||
sessions.all()
|
||||
.models
|
||||
.forEach(session => {
|
||||
session.update({
|
||||
@@ -344,7 +356,16 @@ export default function() {
|
||||
});
|
||||
});
|
||||
}
|
||||
return sessions.where(session => session.scopeId === scope_id)
|
||||
if (recursive && scope_id === 'global') {
|
||||
return sessions.all();
|
||||
} else if (recursive) {
|
||||
return sessions.where(session => {
|
||||
const sessionModel = sessions.find(session.id);
|
||||
return (session.scopeId === scope_id)
|
||||
|| (sessionModel?.scope?.scope?.id === scope_id)
|
||||
});
|
||||
}
|
||||
return sessions.where(session => session.scopeId === scope_id);
|
||||
});
|
||||
this.get('/sessions/:id', function ({ sessions }, { params: { id } }) {
|
||||
const session = sessions.find(id);
|
||||
|
||||
Reference in new issue
Block a user