chore: 🤖 reduce unnecessary calls and sort by time and status

This commit is contained in:
Randall Morey committed 2020-10-03 10:48:53 -04:00
1 parent 9c3dce7110
commit 6d6ed1b70b
1 file changed
+19 -4
@@ -43,17 +43,32 @@ export default class ScopesScopeProjectsProjectSessionsRoute extends Route {
async model() {
const { id: scope_id } = this.modelFor('scopes.scope.projects.project');
const sessions = await this.store.query('session', { scope_id });
return all(
const sessionAggregates = await all(
sessions.map(session => hash({
session,
user: session.user_id
? this.store.findRecord('user', session.user_id)
? (
this.store.peekRecord('user', session.user_id) ||
this.store.findRecord('user', session.user_id)
)
: null,
target: session.target_id
? this.store.findRecord('target', session.target_id)
? (
this.store.peekRecord('target', session.target_id) ||
this.store.findRecord('target', session.target_id)
)
: null,
}))
).then((model) => A(model).sortBy('session.status'));
);
// Sort sessions by time created...
let sortedSessionAggregates =
A(sessionAggregates).sortBy('session.created_time').reverse();
// Then move active sessions to the top...
sortedSessionAggregates = [
...sortedSessionAggregates.filter((aggregate) => aggregate.session.status === 'active'),
...sortedSessionAggregates.filter((aggregate) => aggregate.session.status !== 'active'),
];
return sortedSessionAggregates;
}
/**