/** * Copyright (c) HashiCorp, Inc. * SPDX-License-Identifier: MPL-2.0 */ import Route from '@ember/routing/route'; import { inject as service } from '@ember/service'; import { action } from '@ember/object'; import { loading } from 'ember-loading'; import { confirm } from 'core/decorators/confirm'; import { notifySuccess, notifyError } from 'core/decorators/notify'; export default class ScopesScopeWorkersRoute extends Route { // =services @service store; @service can; @service session; @service router; // =methods /** * If arriving here unauthenticated, redirect to index for further processing. */ beforeModel() { if (!this.session.isAuthenticated) this.router.transitionTo('index'); } /** * Load all workers. * @return {WorkerModel} */ model() { const scope = this.modelFor('scopes.scope'); const { id: scope_id } = scope; if (this.can.can('list worker', scope, { collection: 'workers' })) { return this.store.query('worker', { scope_id }); } } /** * Refreshes worker data. */ @action refresh() { return super.refresh(...arguments); } /** * Save a worker in current scope. * @param {WorkerModel} worker * @param {Event} e */ @action @loading @notifyError(({ message }) => message) @notifySuccess(({ isNew }) => isNew ? 'notifications.create-success' : 'notifications.save-success' ) async save(worker) { await worker.save(); if (this.can.can('read model', worker)) { await this.router.transitionTo('scopes.scope.workers.worker', worker); } else { await this.router.transitionTo('scopes.scope.workers'); } this.refresh(); } /** * Delete worker in current scope and redirect to index. * @param {UserModel} worker */ @action @loading @confirm('questions.delete-confirm') @notifyError(({ message }) => message, { catch: true }) @notifySuccess('notifications.delete-success') async delete(worker) { await worker.destroyRecord(); await this.router.replaceWith('scopes.scope.workers'); this.refresh(); } }