Skip to content

Commit

Permalink
fix one test and the transition problem
Browse files Browse the repository at this point in the history
  • Loading branch information
Monkeychip committed Oct 2, 2024
1 parent 864b522 commit aa313d2
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 31 deletions.
55 changes: 29 additions & 26 deletions ui/app/components/transformation-edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,12 @@ export default TransformBase.extend({
this.set('initialRoles', this.model.allowed_roles);
},

updateOrCreateRole(role, transformationId, backend) {
return this.store
async updateOrCreateRole(role, transformationId, backend) {
const roleStore = await this.store
.queryRecord('transform/role', {
backend,
id: role.id,
})
.then((roleStore) => {
let transformations = roleStore.transformations;
if (role.action === 'ADD') {
transformations = addToList(transformations, transformationId);
} else if (role.action === 'REMOVE') {
transformations = removeFromList(transformations, transformationId);
}
roleStore.setProperties({
backend,
transformations,
});
return roleStore.save().catch((e) => {
return {
errorStatus: e.httpStatus,
...role,
};
});
})
.catch((e) => {
if (e.httpStatus !== 403 && role.action === 'ADD') {
// If role doesn't yet exist, create it with this transformation attached
Expand All @@ -64,24 +46,45 @@ export default TransformBase.extend({
errorStatus: e.httpStatus,
};
});
// if an error occurs while querying the role, exit function and return the error
if (roleStore.errorStatus) return roleStore;
// otherwise update the role with the transformation and save
let transformations = roleStore.transformations;
if (role.action === 'ADD') {
transformations = addToList(transformations, transformationId);
} else if (role.action === 'REMOVE') {
transformations = removeFromList(transformations, transformationId);
}
roleStore.setProperties({
backend,
transformations,
});
return roleStore.save().catch((e) => {
return {
errorStatus: e.httpStatus,
...role,
};
});
},

handleUpdateRoles(updateRoles, transformationId) {
if (!updateRoles) return;
const { backend } = this.model;
updateRoles.forEach((record) => {
// for each role that needs to be updated or created, update the role with the transformation. If there is an error, intercept it and show a message.
this.updateOrCreateRole(record, transformationId, backend).catch((e) => {
updateRoles.forEach(async (record) => {
// For each role that needs to be updated, update the role with the transformation.
const updateOrCreateResponse = await this.updateOrCreateRole(record, transformationId, backend);
// If an error was returned, check error type and show a message.
const errorStatus = updateOrCreateResponse.errorStatus;
if (errorStatus) {
let message = `The edits to this transformation were successful, but transformations for its roles was not edited due to a lack of permissions.`;
if (e.httpStatus !== 403) {
if (errorStatus !== 403) {
message = `You've edited the allowed_roles for this transformation. However, the corresponding edits to some roles' transformations were not made.`;
}
this.flashMessages.info(message, {
sticky: true,
priority: 300,
});
return; // exit out of the forEach loop if an error occurs
});
}
});
},

Expand Down
3 changes: 0 additions & 3 deletions ui/app/models/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import Model, { attr } from '@ember-data/model';
import { computed } from '@ember/object';
import { alias } from '@ember/object/computed';
import lazyCapabilities, { apiPath } from 'vault/macros/lazy-capabilities';
import { expandAttributeMeta } from 'vault/utils/field-to-attrs';

Expand Down Expand Up @@ -138,6 +137,4 @@ export default Model.extend({
readOnly: true,
}),
updatePath: lazyCapabilities(apiPath`${'backend'}/transformation/${'id'}`, 'backend', 'id'),
canDelete: alias('updatePath.canDelete'),
canUpdate: alias('updatePath.canUpdate'),
});
4 changes: 2 additions & 2 deletions ui/app/templates/components/transformation-edit.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
{{#if (eq this.mode "show")}}
<Toolbar>
<ToolbarActions>
{{#if this.model.canDelete}}
{{#if this.model.updatePath.canDelete}}
{{#if (gt this.model.allowed_roles.length 0)}}
<ToolTip @verticalPosition="above" @horizontalPosition="center" as |T|>
<T.Trigger @tabindex="-1">
Expand Down Expand Up @@ -58,7 +58,7 @@
{{/if}}
<div class="toolbar-separator"></div>
{{/if}}
{{#if this.model.canUpdate}}
{{#if this.model.updatePath.canUpdate}}
{{#if (gt this.model.allowed_roles.length 0)}}
<Hds::Button
@text="Edit transformation"
Expand Down

0 comments on commit aa313d2

Please sign in to comment.