* chore: 🤖 bump ember-can * chore: 🤖 rename "can" service (deprecated) to abilities See https://github.com/minutebase/ember-can/pull/152 * chore: 🤖 bump to ember-can@^8 * chore: 🤖 extend resolver for ember-can v7 breaking change https://github.com/minutebase/ember-can/pull/186
143 lines
3.4 KiB
JavaScript
143 lines
3.4 KiB
JavaScript
/**
|
|
* Copyright (c) HashiCorp, Inc.
|
|
* SPDX-License-Identifier: BUSL-1.1
|
|
*/
|
|
|
|
import Route from '@ember/routing/route';
|
|
import { service } from '@ember/service';
|
|
import { restartableTask, timeout } from 'ember-concurrency';
|
|
|
|
export default class ScopesScopeCredentialStoresIndexRoute extends Route {
|
|
// =services
|
|
|
|
@service store;
|
|
@service abilities;
|
|
|
|
// =attributes
|
|
|
|
queryParams = {
|
|
search: {
|
|
refreshModel: true,
|
|
replace: true,
|
|
},
|
|
types: {
|
|
refreshModel: true,
|
|
replace: true,
|
|
},
|
|
page: {
|
|
refreshModel: true,
|
|
},
|
|
pageSize: {
|
|
refreshModel: true,
|
|
},
|
|
sortAttribute: {
|
|
refreshModel: true,
|
|
replace: true,
|
|
},
|
|
sortDirection: {
|
|
refreshModel: true,
|
|
replace: true,
|
|
},
|
|
};
|
|
|
|
// =methods
|
|
|
|
/**
|
|
* Loads queried credential-stores and the number of credential-stores under current scope.
|
|
* @returns {Promise<{totalItems: number, credentialStores: [CredentialStoreModel], doCredentialStoresExist: boolean }> }
|
|
*/
|
|
async model(params) {
|
|
const useDebounce =
|
|
this.retrieveData?.lastPerformed?.args?.[0].search !== params.search;
|
|
return this.retrieveData.perform({ ...params, useDebounce });
|
|
}
|
|
|
|
retrieveData = restartableTask(
|
|
async ({
|
|
search,
|
|
types,
|
|
page,
|
|
pageSize,
|
|
sortAttribute,
|
|
sortDirection,
|
|
useDebounce,
|
|
}) => {
|
|
if (useDebounce) {
|
|
await timeout(250);
|
|
}
|
|
|
|
const scope = this.modelFor('scopes.scope');
|
|
const { id: scope_id } = scope;
|
|
|
|
const filters = {
|
|
scope_id: [{ equals: scope_id }],
|
|
type: [],
|
|
};
|
|
types.forEach((type) => {
|
|
filters.type.push({ equals: type });
|
|
});
|
|
|
|
const sort =
|
|
sortAttribute === 'name'
|
|
? {
|
|
attributes: [sortAttribute, 'id'],
|
|
direction: sortDirection,
|
|
isCoalesced: true,
|
|
}
|
|
: { attributes: [sortAttribute], direction: sortDirection };
|
|
|
|
let credentialStores;
|
|
let totalItems = 0;
|
|
let doCredentialStoresExist = false;
|
|
if (
|
|
this.abilities.can('list model', scope, {
|
|
collection: 'credential-stores',
|
|
})
|
|
) {
|
|
credentialStores = await this.store.query('credential-store', {
|
|
scope_id,
|
|
query: { search, filters, sort },
|
|
page,
|
|
pageSize,
|
|
});
|
|
totalItems = credentialStores.meta?.totalItems;
|
|
doCredentialStoresExist = await this.getDoCredentialStoresExist(
|
|
scope_id,
|
|
totalItems,
|
|
);
|
|
}
|
|
return { credentialStores, doCredentialStoresExist, totalItems };
|
|
},
|
|
);
|
|
|
|
/**
|
|
* Sets doCredentialStoresExist to true if there exists any credential-stores.
|
|
* @param {string} scope_id
|
|
* @param {number} totalItems
|
|
* @returns {Promise<boolean>}
|
|
*/
|
|
async getDoCredentialStoresExist(scope_id, totalItems) {
|
|
if (totalItems > 0) {
|
|
return true;
|
|
}
|
|
const options = { pushToStore: false, peekDb: true };
|
|
const credentialStore = await this.store.query(
|
|
'credential-store',
|
|
{
|
|
scope_id,
|
|
query: { filters: { scope_id: [{ equals: scope_id }] } },
|
|
page: 1,
|
|
pageSize: 1,
|
|
},
|
|
options,
|
|
);
|
|
return credentialStore.length > 0;
|
|
}
|
|
|
|
setupController(controller) {
|
|
const scope = this.modelFor('scopes.scope');
|
|
super.setupController(...arguments);
|
|
controller.setProperties({ scope });
|
|
}
|
|
}
|