diff --git a/components/case/representation/index.js b/components/case/representation/index.js
index 5df1aaf2..3508ff40 100644
--- a/components/case/representation/index.js
+++ b/components/case/representation/index.js
@@ -1,13 +1,12 @@
import { JSONPath as jsonpath } from "jsonpath-plus";
-import _ from "lodash";
import useTranslation from "next-translate/useTranslation";
import { useRouter } from "next/router";
-import { useEffect, useState } from "react";
+import { useState } from "react";
import { useDropzone } from "react-dropzone";
import { connect } from "react-redux";
import { formValueSelector, reduxForm } from "redux-form";
-import { signIn, useSession } from "next-auth/react";
+import { useSession } from "next-auth/react";
import { generateRepPDF, sendEmail, uploadRepFiles } from "../../../actions";
import { formatDates, updateLinks } from "../../../components/utils";
import {
@@ -17,14 +16,14 @@ import {
setRepresentationSubmit,
setRepresentationSubmitConfirmation,
} from "../../../store/currentView/action";
+import ConsultationAgent from "./consultationAgent";
+import ConsultationAppellant from "./consultationAppellant";
+import ConsultationInterestedPartyPerson from "./consultationInterestedPartyPerson";
import RepAgent from "./representationAgent";
import RepAppellant from "./representationAppellant";
-import ConsultationAppellant from "./consultationAppellant";
-import ConsultationAgent from "./consultationAgent";
import RepCapacitySelection from "./representationCapacitySelection";
import RepCompleteSubmit from "./representationCompleteSubmit";
import RepInterestedPartyPerson from "./representationInterestedPartyPerson";
-import ConsultationInterestedPartyPerson from "./consultationInterestedPartyPerson";
import RepLPA from "./representationLPA";
import RepLPACapacitySelection from "./representationLPACapacitySelection";
@@ -76,28 +75,21 @@ let MakeRepresentation = (props) => {
const caseReferenceObj = props.props.currentView.caseReference;
const updateQuestionnaireCount = () => {
- {
- switch (appealType) {
- case 846040000:
- case 846040001:
- case 846040025:
- case 846040004:
- case 846040018:
- case 846040003:
- case 846040009:
- case 846040008:
- return 7;
- break;
- case 846040005:
- case 846040006:
- case 846040007:
- return 9;
- break;
- default:
- return 7;
- break;
- }
- }
+ const countMap = {
+ 846040000: 7,
+ 846040001: 7,
+ 846040025: 7,
+ 846040004: 7,
+ 846040018: 7,
+ 846040003: 7,
+ 846040009: 7,
+ 846040008: 7,
+ 846040005: 9,
+ 846040006: 9,
+ 846040007: 9,
+ };
+
+ return countMap[appealType] || 7;
};
let questionnaireCount = updateQuestionnaireCount();
@@ -109,147 +101,91 @@ let MakeRepresentation = (props) => {
? true
: false;
- // const isNRW = accountDetails.emailaddress1.includes(
- // "@cyfoethnaturiolcymru.gov.uk"
- // )
- // ? true
- // : false;
-
const isNRW = currentView.caseReference?.isNRW;
const setRepFileName = (values) => {
- //console.log("---------------\n", values);
if (
- values.hasOwnProperty("representationCapacity") &&
- values.hasOwnProperty("representationType")
+ !values.hasOwnProperty("representationCapacity") ||
+ !values.hasOwnProperty("representationType")
) {
- let repType = "";
- let repCap = "";
-
- const repDate = new Date();
-
- let day = ("0" + repDate.getDate()).slice(-2);
- let month = ("0" + (repDate.getMonth() + 1)).slice(-2);
- let year = repDate.getFullYear();
-
- switch (values.representationCapacity) {
- case t("myrepresentations:capacity-options-arr-appellant"):
- repCap = "APP";
- break;
- case t("myrepresentations:capacity-options-arr-agent"):
- repCap = "APP"; //set as App even tho agent as is from an appeallant by proxy
- break;
- case t("myrepresentations:capacity-options-arr-interested"):
- repCap = "IP";
- break;
- case "Land Owner":
- repCap = "LO";
- break;
- case "lpa":
- case "LPA":
- repCap = "LPA";
- case "NRW":
- repCap = "NRW";
- break;
-
- default:
- // code block
- }
- switch (values.representationType) {
- case "Statement":
- repType = "Statement";
- break;
- case "Statement of common ground":
- repType = "SCG";
- break;
- case "Written statement":
- repType = "WS";
- break;
- case "Written statement of evidence":
- repType = "WSE";
- break;
- case "Questionnaire":
- repType = "Questionnaire";
- break;
- case "Final comments":
- repType = "Comments";
- break;
- case "Consultation Response":
- repType = "Consultation_Response";
- break;
- case "Local Impact Report":
- repType = "Local_Impact_Report";
- break;
- case "Marine Impact Report":
- repType = "Marine_Impact_Report";
- break;
-
- case "Other":
- repType = "OTHER";
- break;
-
- default:
- // code block
- }
- Object.assign(values, {
- "repfile_name": values.hasOwnProperty("repfile_name")
- ? values.repfile_name
- : year +
- "-" +
- month +
- "-" +
- day +
- "-" +
- ("0" + repDate.getHours()).slice(-2) +
- ":" +
- ("0" + repDate.getMinutes()).slice(-2) +
- ":" +
- ("0" + repDate.getSeconds()).slice(-2) +
- "_-_" +
- repCap +
- "_-_" +
- repType +
- "_-_" +
- (isLPA
- ? values.onBehalfOfLPA.replace(
- /[\s~`!@#$%^&*(){}\[\];:"'<,.>?\/\\|_+=-]/g,
- ""
- )
- : values.lastname.replace(
- /[\s~`!@#$%^&*(){}\[\];:"'<,.>?\/\\|_+=-]/g,
- ""
- ) +
- "_" +
- values.firstname.replace(
- /[\s~`!@#$%^&*(){}\[\];:"'<,.>?\/\\|_+=-]/g,
- ""
- )),
- });
+ return values;
}
+ const repCapMap = {
+ [t("myrepresentations:capacity-options-arr-appellant")]: "APP",
+ [t("myrepresentations:capacity-options-arr-agent")]: "APP", // agent treated as appellant proxy
+ [t("myrepresentations:capacity-options-arr-interested")]: "IP",
+ "Land Owner": "LO",
+ lpa: "LPA",
+ LPA: "LPA",
+ NRW: "NRW",
+ };
+
+ const repTypeMap = {
+ Statement: "Statement",
+ "Statement of common ground": "SCG",
+ "Written statement": "WS",
+ "Written statement of evidence": "WSE",
+ Questionnaire: "Questionnaire",
+ "Final comments": "Comments",
+ "Consultation Response": "Consultation_Response",
+ "Local Impact Report": "Local_Impact_Report",
+ "Marine Impact Report": "Marine_Impact_Report",
+ Other: "OTHER",
+ };
+
+ const now = new Date();
+ const pad = (num) => String(num).padStart(2, "0");
+
+ const dateStr = `${now.getFullYear()}-${pad(now.getMonth() + 1)}-${pad(
+ now.getDate()
+ )}-${pad(now.getHours())}:${pad(now.getMinutes())}:${pad(
+ now.getSeconds()
+ )}`;
+
+ const repCap = repCapMap[values.representationCapacity] || "";
+ const repType = repTypeMap[values.representationType] || "";
+
+ const sanitize = (str) =>
+ str.replace(/[\s~`!@#$%^&*(){}\[\];:"'<,.>?\/\\|_+=-]/g, "");
+
+ const namePart = isLPA
+ ? sanitize(values.onBehalfOfLPA || "")
+ : `${sanitize(values.lastname || "")}_${sanitize(
+ values.firstname || ""
+ )}`;
+
+ values.repfile_name =
+ values.repfile_name ||
+ `${dateStr}_-_${repCap}_-_${repType}_-_${namePart}`;
+
return values;
};
isLPA && setRepresentationCapacity("LPA");
- currentType == "searchResultsObj"
- ? (casesObj = jsonpath({
- path: '$..[?(@ && @.title=="' + caseReference + '")]',
- json: setCaseQueryObj,
- eval: true,
- }))
- : currentType == "watchedCases"
- ? (casesObj = jsonpath({
- path: '$..[?(@ && @.ticketnumber=="' + caseReference + '")]',
- json: watchedCases,
- eval: true,
- }))
- : (casesObj = jsonpath({
- path: '$..[?(@ && @.reference=="' + caseReference + '")]',
- json: setCaseQueryObj,
- eval: true,
- }));
+ const getCasePath = () => {
+ switch (currentType) {
+ case "searchResultsObj":
+ return {
+ path: `$..[?(@ && @.title=="${caseReference}")]`,
+ json: setCaseQueryObj,
+ };
+ case "watchedCases":
+ return {
+ path: `$..[?(@ && @.ticketnumber=="${caseReference}")]`,
+ json: watchedCases,
+ };
+ default:
+ return {
+ path: `$..[?(@ && @.reference=="${caseReference}")]`,
+ json: setCaseQueryObj,
+ };
+ }
+ };
+ const { path, json } = getCasePath();
+ casesObj = jsonpath({ path, json, eval: true });
casesObj = Object.assign({}, ...casesObj);
var detailsObj = {};
@@ -259,559 +195,399 @@ let MakeRepresentation = (props) => {
t("myrepresentations:capacity-options-arr-agent"),
];
- appealType != 846040017
- ? capacityOptionsArr.push(
- t("myrepresentations:capacity-options-arr-interested")
- )
- : caseReferenceObj.specialistProcess != 846040000 &&
- capacityOptionsArr.push(
- t("myrepresentations:capacity-options-arr-interested")
- );
+ const shouldIncludeInterested =
+ appealType !== 846040017 ||
+ caseReferenceObj.specialistProcess !== 846040000;
- let setCaseDetailsObj = router.query.hasOwnProperty("state")
- ? router.query.state == "edit"
- ? props.props.myRepresentations.myRepresentations
- : props.props.searchResultsObj.searchDetailsObj
- : props.currentType == "watchedCases"
- ? props.props.watchedCases.watchedCasesDetails
- : props.props.searchResultsObj.searchDetailsObj;
+ if (shouldIncludeInterested) {
+ capacityOptionsArr.push(
+ t("myrepresentations:capacity-options-arr-interested")
+ );
+ }
- let setCaseResultsObj = router.query.hasOwnProperty("state")
- ? router.query.state == "edit"
- ? props.props.myRepresentations.myRepresentations
- : props.props.searchResultsObj.searchResultsObj
- : props.currentType == "watchedCases"
- ? props.props.watchedCases.watchedCasesDetails
- : props.props.searchResultsObj.searchResultsObj;
+ const isEditState = router.query.state === "edit";
+ const hasState = router.query.hasOwnProperty("state");
- var detailsObj = {};
+ const getCaseDetailsObj = () => {
+ if (hasState) {
+ return isEditState
+ ? props.props.myRepresentations.myRepresentations
+ : props.props.searchResultsObj.searchDetailsObj;
+ }
+ return props.currentType === "watchedCases"
+ ? props.props.watchedCases.watchedCasesDetails
+ : props.props.searchResultsObj.searchDetailsObj;
+ };
- var resultsObj = {};
+ const getCaseResultsObj = () => {
+ if (hasState) {
+ return isEditState
+ ? props.props.myRepresentations.myRepresentations
+ : props.props.searchResultsObj.searchResultsObj;
+ }
+ return props.currentType === "watchedCases"
+ ? props.props.watchedCases.watchedCasesDetails
+ : props.props.searchResultsObj.searchResultsObj;
+ };
- resultsObj = jsonpath({
- path:
- '$..[?(@ && @.ticketnumber=="' +
- currentView.caseReference.currentReference +
- '")]',
+ const setCaseDetailsObj = getCaseDetailsObj();
+ const setCaseResultsObj = getCaseResultsObj();
+
+ let resultsObj = jsonpath({
+ path: `$..[?(@ && @.ticketnumber=="${currentView.caseReference.currentReference}")]`,
json: setCaseResultsObj,
eval: true,
- });
+ })[0];
- resultsObj = resultsObj[0];
+ // let detailsObj;
- detailsObj = router.query.hasOwnProperty("state")
- ? router.query.state == "edit"
- ? jsonpath({
- path:
- '$..[?(@ && @.repfile_name =="' +
- router.query.created +
- '")]',
- json: setCaseDetailsObj,
- eval: true,
- })
- : jsonpath({
- path:
- '$..[?(@ && @.pinswg_name =="' +
- currentView.caseReference.currentReference +
- '")]',
- json: setCaseDetailsObj,
- eval: true,
- })
- : jsonpath({
- path:
- '$..[?(@ && @.pinswg_name =="' +
- currentView.caseReference.currentReference +
- '")]',
- json: setCaseDetailsObj,
- eval: true,
- });
-
- detailsObj = detailsObj[0];
+ if (hasState && isEditState) {
+ detailsObj = jsonpath({
+ path: `$..[?(@ && @.repfile_name=="${router.query.created}")]`,
+ json: setCaseDetailsObj,
+ eval: true,
+ })[0];
+ } else {
+ detailsObj = jsonpath({
+ path: `$..[?(@ && @.pinswg_name=="${currentView.caseReference.currentReference}")]`,
+ json: setCaseDetailsObj,
+ eval: true,
+ })[0];
+ }
const buildRepsArr = (detailsObj) => {
- let buildArr = [];
+ const buildArr = [];
+ const todaysDate = new Date();
- // console.log(appealType, "");
+ const isDNS = appealType === 846040011;
+ const isSIPS = appealType === 846040002;
- let todaysDate = new Date();
+ // Determine key dates based on appeal type
+ const startDate = new Date(
+ isDNS
+ ? detailsObj.pinswg_applicationacceptedasvalid
+ : detailsObj.pinswg_startdate || detailsObj.pinswg_startdates
+ );
- const isDNS = appealType == 846040011;
+ const questionnaireDate = new Date(
+ isDNS
+ ? detailsObj.pinswg_endofrepresentationperiod
+ : detailsObj.pinswg_questionnaireduedate
+ );
+ questionnaireDate.setHours(23, 59, 59, 999);
- const isSIPS = appealType == 846040002;
+ const finalCommentsDate = new Date(
+ isDNS
+ ? detailsObj.pinswg_endofrepresentationperiod
+ : detailsObj.pinswg_finalcommentsduedate
+ );
+ finalCommentsDate.setHours(23, 59, 59, 999);
- let startDate;
- let questionnaireDate;
- let finalCommentsDate;
- let statementDueDate;
+ const statementDueDate = new Date(
+ isDNS
+ ? detailsObj.pinswg_endofrepresentationperiod
+ : detailsObj.pinswg_statementduedate ||
+ detailsObj.pinswg_statementsduedate
+ );
+ statementDueDate.setHours(23, 59, 59, 999);
- isDNS
- ? ((startDate = new Date(
- detailsObj.pinswg_applicationacceptedasvalid
- )),
- (questionnaireDate = new Date(
- new Date(
- detailsObj.pinswg_endofrepresentationperiod
- ).setHours(23, 59, 59, 999)
- )),
- (finalCommentsDate = new Date(
- new Date(
- detailsObj.pinswg_endofrepresentationperiod
- ).setHours(23, 59, 59, 999)
- )),
- (statementDueDate = new Date(
- new Date(
- detailsObj.pinswg_endofrepresentationperiod
- ).setHours(23, 59, 59, 999)
- )))
- : ((startDate = new Date(
- detailsObj.pinswg_startdate || detailsObj.pinswg_startdates
- )),
- (questionnaireDate = new Date(
- new Date(detailsObj.pinswg_questionnaireduedate).setHours(
- 23,
- 59,
- 59,
- 999
- )
- )),
- (finalCommentsDate = new Date(
- new Date(detailsObj.pinswg_finalcommentsduedate).setHours(
- 23,
- 59,
- 59,
- 999
- )
- )),
- (statementDueDate = new Date(
- new Date(
- detailsObj.pinswg_statementduedate ||
- detailsObj.pinswg_statementsduedate
- ).setHours(23, 59, 59, 999)
- )));
+ // Shared condition
+ const isWithin = (start, end) =>
+ todaysDate >= start && todaysDate <= end;
+ const notAppeal = (...types) => !types.includes(appealType);
- isLPA &&
- appealType != 846040019 &&
- appealType != 846040011 &&
- appealType != 846040015 &&
- appealType != 846040016 &&
- todaysDate >= startDate &&
- todaysDate <= finalCommentsDate &&
+ // Questionnaire
+ if (
+ isLPA &&
+ notAppeal(846040019, 846040011, 846040015, 846040016) &&
+ isWithin(startDate, finalCommentsDate)
+ ) {
buildArr.push("Questionnaire");
+ }
- isLPA &&
+ // DNS LPA Documents
+ if (
+ isLPA &&
isDNS &&
- appealType != 846040019 &&
- todaysDate >= startDate &&
- todaysDate <= finalCommentsDate &&
- buildArr.push("Local Impact Report");
+ appealType !== 846040019 &&
+ isWithin(startDate, finalCommentsDate)
+ ) {
+ buildArr.push("Local Impact Report", "Statement");
+ }
- isLPA &&
- isDNS &&
- appealType != 846040019 &&
- todaysDate >= startDate &&
- todaysDate <= finalCommentsDate &&
+ // Non-DNS Statement
+ if (
+ !isDNS &&
+ appealType !== 846040004 &&
+ isWithin(startDate, statementDueDate)
+ ) {
buildArr.push("Statement");
+ }
- !isDNS &&
- appealType != 846040004 &&
- todaysDate >= startDate &&
- todaysDate <= statementDueDate &&
+ // DNS Non-LPA Statement
+ if (isDNS && !isLPA && isWithin(startDate, statementDueDate)) {
buildArr.push("Statement");
+ }
- isDNS &&
- !isLPA &&
- todaysDate >= startDate &&
- todaysDate <= statementDueDate &&
- buildArr.push("Statement");
+ // Final Comments
+ const isSpecialistNonHearing =
+ currentView.caseReference.specialistProcess !== 846040001;
- (appealType != 846040004 ||
- (appealType == 846040015 &&
- currentView.caseReference.specialistProcess != 846040001)) &&
- todaysDate >= statementDueDate &&
- todaysDate <= finalCommentsDate &&
+ if (
+ (appealType !== 846040004 ||
+ (appealType === 846040015 && isSpecialistNonHearing)) &&
+ isWithin(statementDueDate, finalCommentsDate)
+ ) {
buildArr.push("Final comments");
+ }
- appealType == 846040004 &&
- currentView.caseReference.specialistProcess != 846040001 &&
- todaysDate >= statementDueDate &&
- todaysDate <= finalCommentsDate &&
+ if (
+ appealType === 846040004 &&
+ isSpecialistNonHearing &&
+ isWithin(statementDueDate, finalCommentsDate)
+ ) {
buildArr.push("Final comments");
+ }
- //is LPA Advert part 3 no statement
- isLPA &&
- appealType == 846040018 &&
- currentView.caseReference.specialistProcess == 846040000 &&
- buildArr.pop("Statements");
+ // Remove "Statement" for LPA Advert Part 3 (no statement)
+ if (
+ isLPA &&
+ appealType === 846040018 &&
+ currentView.caseReference.specialistProcess === 846040000
+ ) {
+ const index = buildArr.indexOf("Statement");
+ if (index !== -1) buildArr.splice(index, 1);
+ }
- //console.log("is this a sip......", isSIPS, !isLPA);
+ // SIPS-related documents
+ if (isSIPS) {
+ const capacity = repCapacityType();
+ if (capacity === "appellant") {
+ buildArr.push("Consultation Response");
+ } else {
+ buildArr.push("Consultation Response", "Local Impact Report");
+ }
- isSIPS && buildArr.push("Consultation Response");
- isSIPS && buildArr.push("Local Impact Report");
- isSIPS && isNRW && buildArr.push("Marine Impact Report");
+ if (isNRW) {
+ buildArr.push("Marine Impact Report");
+ }
+ }
return buildArr;
};
const repCapacityType = () => {
- return _.isEmpty(formObj["representationForm"])
- ? false
- : _.isEmpty(formObj["representationForm"].values)
- ? false
- : _.isEmpty(
- formObj["representationForm"].values.representationCapacity
- )
- ? false
- : formObj["representationForm"].values.representationCapacity
- .replace(/(\band|[\s\-\+\(\)\,\&])/g, "")
- .toLowerCase();
+ const repForm = formObj["representationForm"];
+ if (
+ !repForm ||
+ !repForm.values ||
+ !repForm.values.representationCapacity
+ ) {
+ return false;
+ }
+
+ return repForm.values.representationCapacity
+ .replace(/(\band|[\s\-\+\(\)\,\&])/g, "")
+ .toLowerCase();
};
const whichControl = () => {
- let SipsOrRep;
+ const commonProps = {
+ formObj,
+ setRepresentationCapacity,
+ setRepresentationSubmit,
+ onHandleSubmit,
+ currentView,
+ setSubmitBack,
+ props,
+ setRepFileName,
+ updateRepresentation,
+ setSavingStatus,
+ savingStatus,
+ setCurrentReference,
+ };
+
+ const getNormalizedCapacity = () => {
+ const rawCapacity =
+ currentView.representationCapacity ||
+ currentView.caseReference?.repDetails?.representationCapacity ||
+ "false";
+ return rawCapacity.toLowerCase();
+ };
+
if (
isLPA &&
- _.isEmpty(currentView.representationCapacity)
- // ||
- // (isLPA &&
- // currentView.caseReference.hasOwnProperty("repDetails") &&
- // !currentView.caseReference.repDetails.representationCapacity)
+ (!currentView.representationCapacity ||
+ currentView.representationCapacity === "")
) {
return (
- //setRepresentationCapacity("LPA"),