From b347dd5c33cb9e66438ba4e0f4607c5ad9d65a7a Mon Sep 17 00:00:00 2001 From: Timothee Groleau Date: Thu, 26 May 2022 11:24:30 +0800 Subject: [PATCH] feat: add a priority UI selector cookie for react rollout QA (#3894) * feat: Add a priority UI selector cookie for QA * fix: update ReactMigrationConfig * chore: use an enum to document cookie values * fix: Use CamelCase for enum keys * feat: log qaCoookie in the reactMigration block --- src/app/config/schema.ts | 6 ++++ src/app/loaders/express/logging.ts | 5 ++- .../react-migration.controller.ts | 31 ++++++++++++++----- src/types/config.ts | 2 ++ 4 files changed, 36 insertions(+), 8 deletions(-) diff --git a/src/app/config/schema.ts b/src/app/config/schema.ts index d74415c88f..e644814165 100644 --- a/src/app/config/schema.ts +++ b/src/app/config/schema.ts @@ -315,6 +315,12 @@ export const optionalVarsSchema: Schema = { default: 'v2-admin-ui', env: 'REACT_MIGRATION_ADMIN_COOKIE_NAME', }, + qaCookieName: { + doc: 'Priority cookie to select react/angular during QA.', + format: String, + default: 'v2-qa-ui', + env: 'REACT_MIGRATION_QA_COOKIE_NAME', + }, }, } diff --git a/src/app/loaders/express/logging.ts b/src/app/loaders/express/logging.ts index 88f7adc46e..5dac747f82 100644 --- a/src/app/loaders/express/logging.ts +++ b/src/app/loaders/express/logging.ts @@ -17,6 +17,7 @@ type LogMeta = { reactMigration?: { respRolloutAuth: number respRolloutNoAuth: number + qaCookie: string | undefined adminCookie: string | undefined respCookie: string | undefined } @@ -71,11 +72,13 @@ const loggingMiddleware = () => { // Temporary: cookies are blacklisted, but we to track the state of the rollout for this particular request if ( req.cookies?.[config.reactMigration.adminCookieName] || - req.cookies?.[config.reactMigration.respondentCookieName] + req.cookies?.[config.reactMigration.respondentCookieName] || + req.cookies?.[config.reactMigration.qaCookieName] ) { meta.reactMigration = { respRolloutAuth: config.reactMigration.respondentRolloutAuth, respRolloutNoAuth: config.reactMigration.respondentRolloutNoAuth, + qaCookie: req.cookies?.[config.reactMigration.qaCookieName], adminCookie: req.cookies?.[config.reactMigration.adminCookieName], respCookie: req.cookies?.[config.reactMigration.respondentCookieName], } diff --git a/src/app/modules/react-migration/react-migration.controller.ts b/src/app/modules/react-migration/react-migration.controller.ts index 4a91568f64..c9075c3129 100644 --- a/src/app/modules/react-migration/react-migration.controller.ts +++ b/src/app/modules/react-migration/react-migration.controller.ts @@ -9,8 +9,13 @@ import * as PublicFormController from '../form/public-form/public-form.controlle import { RedirectParams } from '../form/public-form/public-form.types' import * as HomeController from '../home/home.controller' +export enum UiCookieValues { + React = 'react', + Angular = 'angular', +} + export type SetEnvironmentParams = { - ui: 'react' | 'angular' + ui: UiCookieValues } export const RESPONDENT_COOKIE_OPTIONS = { @@ -74,7 +79,10 @@ export const serveForm: ControllerHandler< ? config.reactMigration.respondentRolloutAuth : config.reactMigration.respondentRolloutNoAuth - if (threshold <= 0) { + if (config.reactMigration.qaCookieName in req.cookies) { + showReact = + req.cookies[config.reactMigration.qaCookieName] === UiCookieValues.React + } else if (threshold <= 0) { // Check the rollout value first, if it's 0, react is DISABLED // And we ignore cookies entirely! showReact = false @@ -82,13 +90,16 @@ export const serveForm: ControllerHandler< if (config.reactMigration.adminCookieName in req.cookies) { // Admins are dogfooders, the choice they made for the admin environment // also applies to the forms they need to fill themselves - showReact = req.cookies[config.reactMigration.adminCookieName] === 'react' + showReact = + req.cookies[config.reactMigration.adminCookieName] === + UiCookieValues.React } else if (config.reactMigration.respondentCookieName in req.cookies) { // Note: the respondent cookie is for the whole session, not for a specific form. // That means that within a session, a respondent will see the same environment // for all the forms he/she fills. showReact = - req.cookies[config.reactMigration.respondentCookieName] === 'react' + req.cookies[config.reactMigration.respondentCookieName] === + UiCookieValues.React } } @@ -109,7 +120,7 @@ export const serveForm: ControllerHandler< res.cookie( config.reactMigration.respondentCookieName, - showReact ? 'react' : 'angular', + showReact ? UiCookieValues.React : UiCookieValues.Angular, RESPONDENT_COOKIE_OPTIONS, ) } @@ -134,7 +145,10 @@ export const serveForm: ControllerHandler< export const serveDefault: ControllerHandler = (req, res, next) => { // only admin who chose react should see react, everybody else is plain angular - if (req.cookies?.[config.reactMigration.adminCookieName] === 'react') { + if ( + req.cookies?.[config.reactMigration.adminCookieName] === + UiCookieValues.React + ) { // react return serveFormReact(req, res, next) } else { @@ -150,7 +164,10 @@ export const adminChooseEnvironment: ControllerHandler< unknown, Record > = (req, res) => { - const ui = req.params.ui === 'react' ? 'react' : 'angular' + const ui = + req.params.ui === UiCookieValues.React + ? UiCookieValues.React + : UiCookieValues.Angular res.cookie(config.reactMigration.adminCookieName, ui, ADMIN_COOKIE_OPTIONS) return res.json({ ui }) } diff --git a/src/types/config.ts b/src/types/config.ts index f778cfb6c8..07b22b0b27 100644 --- a/src/types/config.ts +++ b/src/types/config.ts @@ -57,6 +57,7 @@ export type ReactMigrationConfig = { respondentRolloutAuth: number respondentCookieName: string adminCookieName: string + qaCookieName: string } export type Config = { @@ -161,6 +162,7 @@ export interface IOptionalVarsSchema { respondentRolloutAuth: number respondentCookieName: string adminCookieName: string + qaCookieName: string } }