|
1 | 1 | import { engineParser } from './Engine'; |
2 | 2 | import { thenable } from '../utils/promise/thenable'; |
3 | | -import { EXCEPTION, SPLIT_NOT_FOUND } from '../utils/labels'; |
| 3 | +import { EXCEPTION, NO_CONDITION_MATCH, SPLIT_NOT_FOUND } from '../utils/labels'; |
4 | 4 | import { CONTROL } from '../utils/constants'; |
5 | 5 | import { ISplit, MaybeThenable } from '../dtos/types'; |
6 | 6 | import { IStorageAsync, IStorageSync } from '../storages/types'; |
@@ -207,3 +207,40 @@ function getEvaluations( |
207 | 207 |
|
208 | 208 | return thenables.length > 0 ? Promise.all(thenables).then(() => result) : result; |
209 | 209 | } |
| 210 | + |
| 211 | +export function evaluateDefaultTreatment( |
| 212 | + splitName: string, |
| 213 | + storage: IStorageSync | IStorageAsync, |
| 214 | +): MaybeThenable<IEvaluationResult> { |
| 215 | + let parsedSplit; |
| 216 | + |
| 217 | + try { |
| 218 | + parsedSplit = storage.splits.getSplit(splitName); |
| 219 | + } catch (e) { |
| 220 | + // Exception on sync `getSplit` storage. Not possible ATM with InMemory and InLocal storages. |
| 221 | + return treatmentException; |
| 222 | + } |
| 223 | + |
| 224 | + return thenable(parsedSplit) ? |
| 225 | + parsedSplit.then(getDefaultTreatment).catch(() => treatmentException) : |
| 226 | + getDefaultTreatment(parsedSplit); |
| 227 | +} |
| 228 | + |
| 229 | +function getDefaultTreatment( |
| 230 | + splitJSON: ISplit | null, |
| 231 | +): MaybeThenable<IEvaluationResult> { |
| 232 | + if (splitJSON) { |
| 233 | + return { |
| 234 | + treatment: splitJSON.defaultTreatment, |
| 235 | + label: NO_CONDITION_MATCH, // "default rule" |
| 236 | + config: splitJSON.configurations && splitJSON.configurations[splitJSON.defaultTreatment] || null, |
| 237 | + changeNumber: splitJSON.changeNumber |
| 238 | + }; |
| 239 | + } |
| 240 | + |
| 241 | + return { |
| 242 | + treatment: CONTROL, |
| 243 | + label: SPLIT_NOT_FOUND, |
| 244 | + config: null |
| 245 | + }; |
| 246 | +} |
0 commit comments