Skip to content

Commit

Permalink
fix: redirect the reporter when trying to access immutable application
Browse files Browse the repository at this point in the history
  • Loading branch information
matthieu-foucault committed Dec 7, 2020
1 parent 6d0665d commit b0f7208
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 6 deletions.
48 changes: 46 additions & 2 deletions app/containers/Applications/ApplicationWizard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React from 'react';
import {useRouter} from 'next/router';
import {graphql, createFragmentContainer} from 'react-relay';
import ApplicationFormNavbar from 'components/Forms/ApplicationFormNavbar';
import {ApplicationWizard_query} from 'ApplicationWizard_query.graphql';
import ApplicationWizardStep from './ApplicationWizardStep';

const setRouterQueryParam = (router, key, value, replace = false) => {
Expand All @@ -16,17 +17,26 @@ const setRouterQueryParam = (router, key, value, replace = false) => {
else router.push(newUrl, newUrl, {shallow: true});
};

interface Props {
query: ApplicationWizard_query;
}

/*
* The ApplicationWizard container retrieves the ordered list of forms to render in the
* application process. Each ApplicationWizardStep is rendered, and at the end the
* ApplicationWizardConfirmation is rendered.
*/
const ApplicationWizard = ({query}) => {
const ApplicationWizard: React.FunctionComponent<Props> = ({query}) => {
const {application} = query || {};
const router = useRouter();
const {formResultId} = router.query;
const confirmationPage = Boolean(router.query.confirmationPage);
const orderedFormResults = application.orderedFormResults.edges;
const {
orderedFormResults: {edges: orderedFormResults},
latestDraftRevision,
latestSubmittedRevision,
applicationRevisionByStringVersionNumber: applicationRevision
} = application;

// Redirect a certifier given a bad link to the certify page for the application
if (!application.currentUserCanEdit) {
Expand All @@ -40,6 +50,30 @@ const ApplicationWizard = ({query}) => {
return null;
}

// Redirect a reporter trying to edit an application revision that was already submitted
if (applicationRevision.isImmutable) {
console.log('redirecting');
if (
latestDraftRevision.versionNumber > latestSubmittedRevision.versionNumber
)
router.replace({
pathname: router.pathname,
query: {
...router.query,
version: latestDraftRevision.versionNumber
}
});
else
router.replace({
pathname: '/reporter/view-application',
query: {
...router.query,
version: latestDraftRevision.versionNumber
}
});
return null;
}

if (!confirmationPage && !formResultId) {
setRouterQueryParam(
router,
Expand Down Expand Up @@ -103,6 +137,16 @@ export default createFragmentContainer(ApplicationWizard, {
}
}
}
latestDraftRevision {
versionNumber
}
latestSubmittedRevision {
versionNumber
}
applicationRevisionByStringVersionNumber(versionNumberInput: $version) {
isImmutable
}
...ApplicationFormNavbar_application
}
...ApplicationWizardStep_query
Expand Down
8 changes: 6 additions & 2 deletions app/server/schema.graphql
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""The application data"""
type Application implements Node {
applicationRevisionByStringVersionNumber(versionNumberInput: String): ApplicationRevision

"""Reads and enables pagination through a set of `ApplicationRevision`."""
applicationRevisionsByApplicationId(
"""Read all values in the set after (below) this cursor."""
Expand Down Expand Up @@ -30,7 +32,7 @@ type Application implements Node {
): ApplicationRevisionsConnection!

"""
Computed column for graphql to traverse to an application_revision_status from application
Computed column to determine whether a revision is immutable (submitted)
"""
applicationRevisionStatus(versionNumberInput: String): ApplicationRevisionStatus

Expand Down Expand Up @@ -138,7 +140,8 @@ type Application implements Node {
id: ID!

"""
Computed column for graphql to traverse to the latest application_revision with status "draft" from application
Computed column for graphql to retrieve a given application_revision when
given a version number as String (e.g. from a router query)
"""
latestDraftRevision: ApplicationRevision

Expand Down Expand Up @@ -449,6 +452,7 @@ type ApplicationRevision implements Node {
Computed column indicates if a revision is the current version of the application
"""
isCurrentVersion: Boolean
isImmutable: Boolean

"""Whether or not reporter agreed the legal disclaimer"""
legalDisclaimerAccepted: Boolean!
Expand Down
39 changes: 37 additions & 2 deletions app/server/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -5503,6 +5503,18 @@
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "isImmutable",
"description": null,
"args": [],
"type": {
"kind": "SCALAR",
"name": "Boolean",
"ofType": null
},
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "legalDisclaimerAccepted",
"description": "Whether or not reporter agreed the legal disclaimer",
Expand Down Expand Up @@ -5592,6 +5604,29 @@
"name": "Application",
"description": "The application data",
"fields": [
{
"name": "applicationRevisionByStringVersionNumber",
"description": null,
"args": [
{
"name": "versionNumberInput",
"description": null,
"type": {
"kind": "SCALAR",
"name": "String",
"ofType": null
},
"defaultValue": null
}
],
"type": {
"kind": "OBJECT",
"name": "ApplicationRevision",
"ofType": null
},
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "applicationRevisionsByApplicationId",
"description": "Reads and enables pagination through a set of `ApplicationRevision`.",
Expand Down Expand Up @@ -5689,7 +5724,7 @@
},
{
"name": "applicationRevisionStatus",
"description": "Computed column for graphql to traverse to an application_revision_status from application",
"description": "Computed column to determine whether a revision is immutable (submitted)",
"args": [
{
"name": "versionNumberInput",
Expand Down Expand Up @@ -6053,7 +6088,7 @@
},
{
"name": "latestDraftRevision",
"description": "Computed column for graphql to traverse to the latest application_revision with status \"draft\" from application",
"description": "Computed column for graphql to retrieve a given application_revision when given a version number as String (e.g. from a router query)",
"args": [],
"type": {
"kind": "OBJECT",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
-- Deploy ggircs-portal:computed_columns/application_application_revision_by_string_version_number.sql to pg

begin;

create or replace function ggircs_portal.application_application_revision_by_string_version_number (
application ggircs_portal.application, version_number_input text
) returns ggircs_portal.application_revision
as $function$
begin
return (select r.* from ggircs_portal.application_revision r
where r.application_id = application_id
and r.version_number = version_number_input::int);
end;
$function$ language 'plpgsql' stable;

grant execute on function ggircs_portal.application_application_revision_by_string_version_number to ciip_administrator, ciip_analyst, ciip_industry_user;

comment on function ggircs_portal.application_latest_draft_revision is 'Computed column for graphql to retrieve a given application_revision when given a version number as String (e.g. from a router query)';

commit;
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- Revert ggircs-portal:computed_columns/application_application_revision_by_string_version_number.sql from pg

begin;

drop function ggircs_portal.application_application_revision_by_string_version_number(ggircs_portal.application, text);

commit;
1 change: 1 addition & 0 deletions schema/sqitch.plan
Original file line number Diff line number Diff line change
Expand Up @@ -204,3 +204,4 @@ tables/reporting_year_001 [tables/reporting_year trigger_functions/protect_date_
computed_columns/application_revision_is_immutable [computed_columns/application_application_revision_status] 2020-12-04T22:47:50Z Dylan Leard <dylan@button.is> # Computer column determines if a revision is immutable (has been submitted)
trigger_functions/immutable_form_result [computed_columns/application_revision_is_immutable] 2020-12-04T20:34:19Z Dylan Leard <dylan@button.is> # Trigger makes form_result immutable if submitted
tables/form_result_001 [tables/form_result trigger_functions/immutable_form_result] 2020-12-04T20:46:48Z Dylan Leard <dylan@button.is> # Migration: add immutable_form_result trigger to table
computed_columns/application_application_revision_by_string_version_number.sql 2020-12-04T23:47:35Z Matthieu Foucault <matthieu@button.is> # Add application_application_revision_by_string_version_number computed column
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- Verify ggircs-portal:computed_columns/application_application_revision_by_string_version_number.sql on pg

begin;

select pg_get_functiondef('ggircs_portal.application_application_revision_by_string_version_number(ggircs_portal.application, text)'::regprocedure);

rollback;

0 comments on commit b0f7208

Please sign in to comment.