11import { NextFunction , Request , Response } from "express" ;
22import { ApplicationData } from "../model" ;
3- import { IsSecureRegisterKey , OverseasEntityKey , Transactionkey } from "../model/data.types.model" ;
4- import { fetchApplicationData , setExtraData } from "./application.data" ;
53import { logger } from "./logger" ;
6- import { getUrlWithTransactionIdAndSubmissionId , isRemoveJourney , isRegistrationJourney } from "../utils/url" ;
74import * as config from "../config" ;
85import { isActiveFeature } from "./feature.flag" ;
9- import { updateOverseasEntity } from "../service/overseas.entities.service" ;
6+
107import { Session } from "@companieshouse/node-session-handler" ;
8+ import { postTransaction } from "../service/transaction.service" ;
9+
10+ import { IsSecureRegisterKey , OverseasEntityKey , Transactionkey } from "../model/data.types.model" ;
11+ import { getApplicationData , setExtraData } from "./application.data" ;
12+ import { createOverseasEntity , updateOverseasEntity } from "../service/overseas.entities.service" ;
13+
14+ import {
15+ getUrlWithTransactionIdAndSubmissionId ,
16+ isRemoveJourney ,
17+ isUpdateJourney
18+ } from "../utils/url" ;
1119
1220export const getFilterPage = async ( req : Request , res : Response , next : NextFunction , templateName : string , backLinkUrl : string ) : Promise < void > => {
1321
1422 try {
1523
1624 logger . debugRequest ( req , `${ req . method } ${ req . route . path } ` ) ;
1725
18- const isRegistration = isRegistrationJourney ( req ) ;
1926 const isRemove = await isRemoveJourney ( req ) ;
20- const appData : ApplicationData = await fetchApplicationData ( req , isRegistration ) ;
27+ const appData : ApplicationData = await getApplicationData ( req , true ) ;
2128
2229 if ( isRemove ) {
2330 return res . render ( templateName , {
@@ -52,32 +59,24 @@ export const postFilterPage = async (
5259
5360 logger . debugRequest ( req , `${ req . method } ${ req . route . path } ` ) ;
5461
55- const isRegistration : boolean = isRegistrationJourney ( req ) ;
62+ const isRedisRemovalFlag = isActiveFeature ( config . FEATURE_FLAG_ENABLE_REDIS_REMOVAL ) ;
63+ const isUpdate : boolean = await isUpdateJourney ( req ) ;
5664 const isRemove : boolean = await isRemoveJourney ( req ) ;
57- const appData : ApplicationData = await fetchApplicationData ( req , isRegistration ) ;
58-
65+ const appData : ApplicationData = await getApplicationData ( req , true ) ;
5966 const isSecureRegister = ( req . body [ IsSecureRegisterKey ] ) . toString ( ) ;
6067 appData [ IsSecureRegisterKey ] = isSecureRegister ;
61- const session = req . session as Session ;
6268
6369 let nextPageUrl : string = "" ;
6470
6571 if ( isSecureRegister === "1" ) {
6672 nextPageUrl = isSecureRegisterYesUrl ;
67- if ( isActiveFeature ( config . FEATURE_FLAG_ENABLE_REDIS_REMOVAL ) && isRegistration ) {
68- nextPageUrl = getUrlWithTransactionIdAndSubmissionId ( isSecureRegisterYesUrl , appData [ Transactionkey ] as string , appData [ OverseasEntityKey ] as string ) ;
69- }
7073 }
7174
7275 if ( isSecureRegister === "0" ) {
7376 nextPageUrl = isSecureRegisterNoUrl ;
74- if ( isActiveFeature ( config . FEATURE_FLAG_ENABLE_REDIS_REMOVAL ) && isRegistration ) {
75- if ( appData [ Transactionkey ] && appData [ OverseasEntityKey ] ) {
76- await updateOverseasEntity ( req , session , appData ) ;
77- } else {
78- throw new Error ( "Error: is_secure_register filter cannot be updated - transaction_id or overseas_entity_id is missing" ) ;
79- }
80- nextPageUrl = getUrlWithTransactionIdAndSubmissionId ( isSecureRegisterNoUrl , appData [ Transactionkey ] as string , appData [ OverseasEntityKey ] as string ) ;
77+ if ( isRedisRemovalFlag && ! isRemove ) {
78+ await createOrUpdateEntityDetails ( req , appData , isUpdate ) ;
79+ nextPageUrl = getNextPageUrl ( appData , isSecureRegisterNoUrl , isRemove , isRedisRemovalFlag ) ;
8180 }
8281 }
8382
@@ -93,3 +92,32 @@ export const postFilterPage = async (
9392 next ( error ) ;
9493 }
9594} ;
95+
96+ const createOrUpdateEntityDetails = async ( req : Request , appData : ApplicationData , isUpdate : boolean ) : Promise < void > => {
97+
98+ const session = req . session as Session ;
99+
100+ if ( isUpdate && ! appData [ Transactionkey ] ) {
101+ const transactionID = await postTransaction ( req , session ) ;
102+ appData [ Transactionkey ] = transactionID ;
103+ appData [ OverseasEntityKey ] = await createOverseasEntity ( req , session , transactionID ) ;
104+ }
105+
106+ if ( appData [ Transactionkey ] && appData [ OverseasEntityKey ] ) {
107+ await updateOverseasEntity ( req , session , appData ) ;
108+ } else {
109+ throw new Error ( "Error: is_secure_register filter cannot be updated - transaction_id or overseas_entity_id is missing" ) ;
110+ }
111+ } ;
112+
113+ const getNextPageUrl = ( appData : ApplicationData , fallbackUrl : string , isRemove : boolean , isRedisRemovalFlag : boolean ) : string => {
114+ try {
115+ if ( isRedisRemovalFlag && ! isRemove ) {
116+ return getUrlWithTransactionIdAndSubmissionId ( fallbackUrl , appData [ Transactionkey ] as string , appData [ OverseasEntityKey ] as string ) ;
117+ }
118+ return fallbackUrl ;
119+ } catch ( error ) {
120+ logger . error ( `Error generating nextPageUrl with transactionId and submissionId: ${ error } ` ) ;
121+ return fallbackUrl ;
122+ }
123+ } ;
0 commit comments