From ce8248304566eaee424ed9084294e6c5cb0275e3 Mon Sep 17 00:00:00 2001 From: RidhamShah Date: Thu, 14 Sep 2023 10:21:40 +0530 Subject: [PATCH] Revert "Solve mark and assignment issue" This reverts commit 59ca301899dc619ccac68003474f0af06b98db9c. --- .../ExperimentClientController.v5.ts | 2 + .../validators/MarkExperimentValidator.v5.ts | 39 +++++++++---------- clientlibs/js/src/common/index.ts | 28 +++---------- .../functions/getDecisionPointAssignment.ts | 18 +++++---- .../js/src/functions/markDecisionPoint.ts | 16 ++++---- 5 files changed, 44 insertions(+), 59 deletions(-) diff --git a/backend/packages/Upgrade/src/api/controllers/ExperimentClientController.v5.ts b/backend/packages/Upgrade/src/api/controllers/ExperimentClientController.v5.ts index 3427578d50..b05476d33f 100644 --- a/backend/packages/Upgrade/src/api/controllers/ExperimentClientController.v5.ts +++ b/backend/packages/Upgrade/src/api/controllers/ExperimentClientController.v5.ts @@ -22,6 +22,7 @@ import { IGroupMembership, IUserAliases, IWorkingGroup, + EXPERIMENT_TYPE, PAYLOAD_TYPE, IPayload, } from 'upgrade_types'; @@ -539,6 +540,7 @@ export class ExperimentClientController { }); return { ...rest, + experimentType: assignedFactor ? EXPERIMENT_TYPE.FACTORIAL : EXPERIMENT_TYPE.SIMPLE, assignedCondition: finalConditionData, assignedFactor: assignedFactor ? finalFactorData : undefined, }; diff --git a/backend/packages/Upgrade/src/api/controllers/validators/MarkExperimentValidator.v5.ts b/backend/packages/Upgrade/src/api/controllers/validators/MarkExperimentValidator.v5.ts index 845057f5c1..8b7dc5c013 100644 --- a/backend/packages/Upgrade/src/api/controllers/validators/MarkExperimentValidator.v5.ts +++ b/backend/packages/Upgrade/src/api/controllers/validators/MarkExperimentValidator.v5.ts @@ -1,17 +1,6 @@ import { Type } from 'class-transformer'; -import { - IsNotEmpty, - IsDefined, - IsString, - IsOptional, - IsEnum, - IsObject, - ValidateNested, - ValidateIf, - ValidationOptions, - registerDecorator, -} from 'class-validator'; -import { MARKED_DECISION_POINT_STATUS, PAYLOAD_TYPE } from 'upgrade_types'; +import { IsNotEmpty, IsDefined, IsString, IsOptional, IsEnum, IsObject, ValidateNested, ValidateIf, ValidationOptions, registerDecorator } from 'class-validator'; +import { EXPERIMENT_TYPE, MARKED_DECISION_POINT_STATUS, PAYLOAD_TYPE } from 'upgrade_types'; const IsAssignedFactorRecord = (validationOptions?: ValidationOptions) => { return function (object: unknown, propertyName: string) { @@ -26,14 +15,16 @@ const IsAssignedFactorRecord = (validationOptions?: ValidationOptions) => { }, validator: { validate(value: any) { - return validateAssignedFactorData(value); + return validateAssignedFactorData(value) }, }, }); }; }; -function validateAssignedFactorData(data: any): boolean { +function validateAssignedFactorData( + data: any +): boolean { const keys = Object.keys(data); for (const key of keys) { const factor = data[key]; @@ -54,7 +45,9 @@ function isValidAssignedFactor(value: any): value is AssignedFactor { function isValidPayload(value: any): value is Payload { return ( - typeof value === 'object' && Object.values(PAYLOAD_TYPE).includes(value.type) && typeof value.value === 'string' + typeof value === 'object' && + Object.values(PAYLOAD_TYPE).includes(value.type) && + typeof value.value === 'string' ); } @@ -79,13 +72,14 @@ class AssignedFactor { } class AssignedCondition { - @IsOptional() + @IsNotEmpty() @IsString() - id?: string; + id: string; - @IsOptional() + @IsDefined() + @IsNotEmpty() @IsString() - conditionCode?: string; + conditionCode: string; @IsObject() @ValidateNested() @@ -107,6 +101,11 @@ class Data { @IsString() target: string; + @IsEnum(EXPERIMENT_TYPE) + @IsDefined() + @IsNotEmpty() + experimentType: EXPERIMENT_TYPE; + @IsOptional() @ValidateNested() @Type(() => AssignedCondition) diff --git a/clientlibs/js/src/common/index.ts b/clientlibs/js/src/common/index.ts index 55d12bcfc8..d97600ca3d 100644 --- a/clientlibs/js/src/common/index.ts +++ b/clientlibs/js/src/common/index.ts @@ -1,4 +1,4 @@ -import { IExperimentAssignmentv5, PAYLOAD_TYPE } from 'upgrade_types'; +import { IExperimentAssignmentv5 } from "../../../../types/src"; export function rotateAssignmentList(assignment: IExperimentAssignmentv5) { if (assignment.assignedCondition.length > 1) { @@ -10,25 +10,7 @@ export function rotateAssignmentList(assignment: IExperimentAssignmentv5) { return assignment; } -export function findExperimentAssignmentBySiteAndTarget( - site: string, - target: string, - experimentAssignmentData: IExperimentAssignmentv5[] -): IExperimentAssignmentv5 { - const assignment = experimentAssignmentData.find( - (assignment) => assignment.site === site && assignment.target === target - ); - return ( - assignment || { - site: site, - target: target, - assignedCondition: [ - { - conditionCode: null, - payload: null, - id: null, - }, - ], - } - ); -} +export function findExperimentAssignmentBySiteAndTarget(site: string, target: string, experimentAssignmentData: IExperimentAssignmentv5[]): IExperimentAssignmentv5 { + const assignment = experimentAssignmentData.find((assignment) => assignment.site === site && assignment.target === target); + return assignment; +}; \ No newline at end of file diff --git a/clientlibs/js/src/functions/getDecisionPointAssignment.ts b/clientlibs/js/src/functions/getDecisionPointAssignment.ts index 454fa18495..b590348ae1 100644 --- a/clientlibs/js/src/functions/getDecisionPointAssignment.ts +++ b/clientlibs/js/src/functions/getDecisionPointAssignment.ts @@ -8,14 +8,18 @@ export default function getDecisionPointAssignment( clientState: UpGradeClientInterfaces.IClientState ): Assignment { if (clientState?.allExperimentAssignmentData) { - const experimentAssignment = findExperimentAssignmentBySiteAndTarget( - site, - target, - clientState.allExperimentAssignmentData - ); + const experimentAssignment = findExperimentAssignmentBySiteAndTarget(site, target, clientState.allExperimentAssignmentData) - const assignment = new Assignment(experimentAssignment, clientState); - return assignment; + if (experimentAssignment) { + const assignment = new Assignment( + experimentAssignment, + clientState, + ); + + return assignment; + } else { + return null; + } } else { return null; } diff --git a/clientlibs/js/src/functions/markDecisionPoint.ts b/clientlibs/js/src/functions/markDecisionPoint.ts index a26c06c146..0b8f1097a0 100644 --- a/clientlibs/js/src/functions/markDecisionPoint.ts +++ b/clientlibs/js/src/functions/markDecisionPoint.ts @@ -16,11 +16,15 @@ export default async function markDecisionPoint( uniquifier?: string, clientError?: string ): Promise { - const assignment = findExperimentAssignmentBySiteAndTarget(site, target, experimentAssignmentData); + const assignment = findExperimentAssignmentBySiteAndTarget(site, target, experimentAssignmentData) + + if (!assignment) { + throw new Error('No assignment found'); + } rotateAssignmentList(assignment); - const data = { ...assignment, assignedCondition: { ...assignment.assignedCondition[0], conditionCode: condition } }; + const data = { ...assignment, assignedCondition: { ...assignment.assignedCondition[0], conditionCode : condition} }; let requestBody: UpGradeClientInterfaces.IMarkDecisionPointRequestBody = { userId, @@ -40,13 +44,7 @@ export default async function markDecisionPoint( clientError, }; } - const response = await fetchDataService( - url, - token, - clientSessionId, - requestBody, - UpGradeClientEnums.REQUEST_TYPES.POST - ); + const response = await fetchDataService(url, token, clientSessionId, requestBody, UpGradeClientEnums.REQUEST_TYPES.POST); if (response.status) { return response.data; } else {