Skip to content

Commit

Permalink
feat(product): allow benchmark editing for read-only products, split …
Browse files Browse the repository at this point in the history
…state=archived from read-only
  • Loading branch information
dleard committed May 19, 2020
1 parent b56bbaa commit 18bbdc6
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 10 deletions.
5 changes: 3 additions & 2 deletions app/containers/Products/ProductRowItemContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ export const ProductRowItemComponent: React.FunctionComponent<Props> = ({
</>
);

if (product.productState === 'PUBLISHED')
if (product.productState === 'PUBLISHED' && !product.isReadOnly)
return (
<Button
variant="warning"
Expand All @@ -264,6 +264,7 @@ export const ProductRowItemComponent: React.FunctionComponent<Props> = ({
Archive Product
</Button>
);
return <Button className="hidden-button" />;
}

if (
Expand All @@ -290,7 +291,7 @@ export const ProductRowItemComponent: React.FunctionComponent<Props> = ({
disabled={
isProduct
? product.productState !== 'DRAFT' || product.isReadOnly
: product.isReadOnly
: product.productState === 'ARCHIVED'
}
widgets={{header: HeaderWidget}}
schema={
Expand Down
13 changes: 11 additions & 2 deletions app/cypress/integration/product-benchmark.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe('The products and benchmark page', () => {
});

it('Displays the list of all products', () => {
cy.get('tr').its('length').should('be.gte', 3);
cy.get('tr').its('length').should('be.gte', 4);
cy.get('#page-content').contains('Product A');
cy.get('#page-content').contains('Product B');
cy.get('#page-content').contains('Product C');
Expand Down Expand Up @@ -76,6 +76,15 @@ describe('The benchmark modal', () => {
cy.get('tbody > :nth-child(2) > :nth-child(3)').contains('10');
});

it('Allows editing a benchmark for a published (read-only) product', () => {
cy.get(':nth-child(4) > :nth-child(8) > .fa-tachometer-alt > path').click();
cy.get('#root_benchmark').should('have.prop', 'disabled', false);
cy.get('#root_benchmark').clear().type('10');
cy.get('.rjsf > .btn').contains('Save');
cy.get('.rjsf > .btn').click();
cy.get('tbody > :nth-child(4) > :nth-child(3)').contains('10');
});

it('Does not allow editing a benchmark for an archived product', () => {
cy.get(':nth-child(3) > :nth-child(8) > .fa-tachometer-alt > path').click();
cy.get('#root_benchmark').should('have.prop', 'disabled', true);
Expand Down Expand Up @@ -226,7 +235,7 @@ describe('The Create Product modal', () => {
'#root_subtractGeneratedHeatEmissions > :nth-child(1) > label > :nth-child(1) > input'
).click();
cy.contains('Add Product').click();
cy.get('tr').its('length').should('be.gte', 4);
cy.get('tr').its('length').should('be.gte', 5);
cy.get('tbody > :nth-child(1) > :nth-child(6)').contains('DRAFT');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ exports[`ProductList should match the previous snaphshot 1`] = `
placement="right"
>
View
Product
Product
</Tooltip>
}
placement="bottom"
Expand Down
6 changes: 4 additions & 2 deletions schema/data/fixtures/products-benchmarks-setup.sql
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ overriding system value
values
(1, 'Product A', 'tonnes','draft', true, true, true, true, true, true, true, true, true, true, false, '2018-01-01'),
(2, 'Product B', 'cubic meters', 'published', true, true, true, true, true, true, true, true, true, true, false, '2018-01-01'),
(3, 'Product C', 'unicorns per million', 'archived', true, true, true, true, true, true, true, true, true, true, true, '2018-01-01');
(3, 'Product C', 'unicorns per million', 'archived', true, true, true, true, true, true, true, true, true, true, true, '2018-01-01'),
(4, 'Product D', 'kilolitres', 'published', true, true, true, true, true, true, true, true, true, true, true, '2018-01-01');

insert into ggircs_portal.benchmark(
id,
Expand All @@ -45,6 +46,7 @@ values
(1, 1, 0.12, 0.15, 1, 2018, 2022, '2018-01-01'),
(2, 2, 888, 999, 1, 2018, 2022, '2018-01-01'),
(3, 3, 123, 456, 1, 2018, 2019, '2018-01-01'),
(4, 3, 789, 987, 1, 2019, 2022, '2019-01-01');
(4, 3, 789, 987, 1, 2019, 2022, '2019-01-01'),
(5, 4, 789, 987, 1, 2019, 2022, '2019-01-01');

commit;
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ begin;
create or replace function ggircs_portal_private.protect_read_only_products()
returns trigger as $$
begin
if (old.is_read_only = true) then
if (old.is_read_only = true or old.product_state = 'archived') then
raise exception 'Product row is read-only. Read-only products include products in ARCHIVED state and protected Energy-related products';
elsif (new.product_state != 'draft' and
(old.id != new.id
Expand All @@ -26,8 +26,6 @@ create or replace function ggircs_portal_private.protect_read_only_products()
raise exception 'Product row is read only. A published product can only change its state to archived';
elsif (old.product_state = 'published' and new.product_state != 'archived') then
raise exception 'A published product cannot change back to draft state, can only change to archived state';
elsif(new.product_state = 'archived') then
new.is_read_only = true;
end if;
return new;
end;
Expand Down

0 comments on commit 18bbdc6

Please sign in to comment.