diff --git a/frontend/src/component/feature/StrategyTypes/FlexibleStrategy/StickinessSelect/StickinessSelect.tsx b/frontend/src/component/feature/StrategyTypes/FlexibleStrategy/StickinessSelect/StickinessSelect.tsx index 8565e13fe7..7eea39686e 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 0000000000..16344dc09f --- /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, + ); +};