Files
threshold-ui/addons/api/mirage/factories/session.js
T

39 lines
1.2 KiB
JavaScript
Raw Normal View History

import factory from '../generated/factories/session';
2020-09-18 12:54:31 -04:00
import { trait } from 'ember-cli-mirage';
2020-09-18 15:59:42 -04:00
import { random } from 'faker';
2020-09-18 12:54:31 -04:00
2021-03-04 15:47:38 -05:00
const statusStrings = ['pending', 'active', 'canceling', 'terminated'];
export const pickRandomStatusString = () => random.arrayElement(statusStrings);
2020-09-18 12:54:31 -04:00
export default factory.extend({
id: (i) => `session-id-${i}`,
status: pickRandomStatusString,
2020-09-18 12:54:31 -04:00
/**
* Sessions generated with this trait will automatically form
* associations to user, target, host set, and host (if available) within
* the current scope.
*/
withAssociations: trait({
afterCreate(record, server) {
const scopeId = record.scopeId;
if (record.scope.type === 'project') {
const orgScopeId = record.scope?.scope.id;
2021-03-04 15:47:38 -05:00
const user = server.schema.users.where({ scopeId: orgScopeId })
.models[0];
2020-09-18 12:54:31 -04:00
if (user) record.update({ user });
}
const target = server.schema.targets.where({ scopeId }).models[0];
if (target) {
record.update({ target });
const hostSet = target.hostSets.models[0];
if (hostSet) {
record.update({ hostSet });
const host = hostSet.hosts.models[0];
if (host) record.update({ host });
}
}
2021-03-04 15:47:38 -05:00
},
}),
2020-09-18 12:54:31 -04:00
});