From f9b8abfe344ee5ec8d85607aed9381dab77dfda5 Mon Sep 17 00:00:00 2001 From: andreas-unleash Date: Fri, 21 Apr 2023 17:13:42 +0300 Subject: [PATCH] fix: override built in stickiness options (#3563) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Created migration for updating context fields Context fields with stickiness are now the source of truth for stickiness options. ## About the changes `userId` context field is updated to have stickiness on by default `sessionId` context field is added with stickiness on by default If `default` or `random` are not part of the context field - we add them This will allow for the option to disable them, by creating a eg. `random` context field with stickiness off https://user-images.githubusercontent.com/104830839/233581147-9a514af4-1fdd-4c2e-b954-3cfa13b1a203.mov Closes to # [1-794](https://linear.app/unleash/issue/1-794/reach-out-to-yousician-for-early-beta-test) ### Important files ## Discussion points --------- Signed-off-by: andreas-unleash --- .../StickinessSelect/StickinessSelect.tsx | 46 +++++++++++-------- ...308-update-context-fields-add-sessionId.js | 28 +++++++++++ 2 files changed, 54 insertions(+), 20 deletions(-) create mode 100644 src/migrations/20230420211308-update-context-fields-add-sessionId.js diff --git a/frontend/src/component/feature/StrategyTypes/FlexibleStrategy/StickinessSelect/StickinessSelect.tsx b/frontend/src/component/feature/StrategyTypes/FlexibleStrategy/StickinessSelect/StickinessSelect.tsx index 8565e13fe71..7eea39686e4 100644 --- a/frontend/src/component/feature/StrategyTypes/FlexibleStrategy/StickinessSelect/StickinessSelect.tsx +++ b/frontend/src/component/feature/StrategyTypes/FlexibleStrategy/StickinessSelect/StickinessSelect.tsx @@ -1,12 +1,11 @@ import Select from 'component/common/select'; import { SelectChangeEvent, useTheme } from '@mui/material'; import useUnleashContext from 'hooks/api/getters/useUnleashContext/useUnleashContext'; -const builtInStickinessOptions = [ - { key: 'default', label: 'default' }, - { key: 'userId', label: 'userId' }, - { key: 'sessionId', label: 'sessionId' }, - { key: 'random', label: 'random' }, -]; + +type OptionType = { key: string; label: string }; + +const DEFAULT_RANDOM_OPTION = 'random'; +const DEFAULT_STICKINESS_OPTION = 'default'; interface IStickinessSelectProps { label: string; @@ -25,20 +24,27 @@ export const StickinessSelect = ({ const { context } = useUnleashContext(); const theme = useTheme(); - const resolveStickinessOptions = () => - builtInStickinessOptions.concat( - context - .filter(contextDefinition => contextDefinition.stickiness) - .filter( - contextDefinition => - !builtInStickinessOptions.find( - builtInStickinessOption => - builtInStickinessOption.key === - contextDefinition.name - ) - ) - .map(c => ({ key: c.name, label: c.name })) - ); + const resolveStickinessOptions = () => { + const options = context + .filter(field => field.stickiness) + .map(c => ({ key: c.name, label: c.name })) as OptionType[]; + + if ( + !options.find(option => option.key === 'default') && + !context.find(field => field.name === DEFAULT_STICKINESS_OPTION) + ) { + options.push({ key: 'default', label: 'default' }); + } + + if ( + !options.find(option => option.key === 'random') && + !context.find(field => field.name === DEFAULT_RANDOM_OPTION) + ) { + options.push({ key: 'random', label: 'random' }); + } + + return options; + }; const stickinessOptions = resolveStickinessOptions(); return ( diff --git a/src/migrations/20230420211308-update-context-fields-add-sessionId.js b/src/migrations/20230420211308-update-context-fields-add-sessionId.js new file mode 100644 index 00000000000..16344dc09f8 --- /dev/null +++ b/src/migrations/20230420211308-update-context-fields-add-sessionId.js @@ -0,0 +1,28 @@ +'use strict'; + +exports.up = function (db, callback) { + db.runSql( + ` + INSERT INTO context_fields(name, description, sort_order, stickiness) VALUES('sessionId', 'Allows you to constrain on sessionId', 4, true); + + UPDATE context_fields + SET stickiness = true + WHERE name LIKE 'userId' AND stickiness is null; + `, + callback, + ); +}; + +exports.down = function (db, callback) { + db.runSql( + ` + DELETE FROM context_fields + WHERE name LIKE 'sessionId'; + + UPDATE context_fields + SET stickiness = null + WHERE name LIKE 'userId'; + `, + callback, + ); +};