Files
threshold-ui/ui/admin/app/routes/scopes/scope/credential-stores/index.js
T
Zhihe Li 5d32d987bc feat: 🎸 Update how we store tokens in indexed-db (#2223)
Added logic to make the list tokens stored in indexed DB more granular.
I get a hash of the query parameters that will get sent to the controller
and use that as the id for the token. This also means extra logic for when
a token is invalid as we will need to delete all tokens of the same
resource type.
2024-04-12 14:55:34 -04:00

106 lines
2.5 KiB
JavaScript

/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: MPL-2.0
*/
import Route from '@ember/routing/route';
import { inject as service } from '@ember/service';
export default class ScopesScopeCredentialStoresIndexRoute extends Route {
// =services
@service store;
@service can;
// =attributes
queryParams = {
search: {
refreshModel: true,
replace: true,
},
types: {
refreshModel: true,
replace: true,
},
page: {
refreshModel: true,
},
pageSize: {
refreshModel: true,
},
};
// =methods
/**
* Loads queried credential-stores and the number of credential-stores under current scope.
* @returns {Promise<{totalItems: number, credentialStores: [CredentialStoreModel], credentialStoresExist: boolean }> }
*/
async model({ search, types, page, pageSize }) {
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 });
});
let credentialStores;
let totalItems = 0;
let credentialStoresExist = false;
if (
this.can.can('list model', scope, {
collection: 'credential-stores',
})
) {
credentialStores = await this.store.query('credential-store', {
scope_id,
query: { search, filters },
page,
pageSize,
});
totalItems = credentialStores.meta?.totalItems;
credentialStoresExist = await this.getCredentialStoresExist(
scope_id,
totalItems,
);
}
return { credentialStores, credentialStoresExist, totalItems };
}
/**
* Sets credentialStoresExist to true if there exists any credential-stores.
* @param {string} scope_id
* @param {number} totalItems
* @returns {Promise<boolean>}
*/
async getCredentialStoresExist(scope_id, totalItems) {
if (totalItems > 0) {
return true;
}
const options = { pushToStore: false, peekIndexedDB: 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 });
}
}