Skip to content

Commit

Permalink
Fix/issue-1000 Updated mark call related changes (#1011)
Browse files Browse the repository at this point in the history
* Solve mark and assignment issue

* add v4 validator changes as well

* remove unused import

* remove payload from mark validator v5 also

---------

Co-authored-by: RidhamShah <[email protected]>
Co-authored-by: danoswaltCL <[email protected]>
  • Loading branch information
3 people authored Sep 14, 2023
1 parent ce82483 commit ab68afe
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import {
IUserAliases,
IWorkingGroup,
PAYLOAD_TYPE,
EXPERIMENT_TYPE,
IPayload,
} from 'upgrade_types';
import { FeatureFlag } from '../models/FeatureFlag';
Expand Down Expand Up @@ -528,7 +527,6 @@ export class ExperimentClientController {
}
return {
...rest,
experimentType: assignedFactor ? EXPERIMENT_TYPE.FACTORIAL : EXPERIMENT_TYPE.SIMPLE,
assignedCondition: {
id: assignedCondition[0].id,
conditionCode: assignedCondition[0].conditionCode,
Expand Down Expand Up @@ -663,7 +661,7 @@ export class ExperimentClientController {
@Req()
request: AppRequest
): Promise<Log[]> {
let result = envelope.data.map(async log => {
const result = envelope.data.map(async (log) => {
// getOriginalUserDoc call for alias
const experimentUserDoc = await this.getUserDoc(log.object.assignee.id, request.logger);
if (experimentUserDoc) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import {
IGroupMembership,
IUserAliases,
IWorkingGroup,
EXPERIMENT_TYPE,
PAYLOAD_TYPE,
IPayload,
} from 'upgrade_types';
Expand Down Expand Up @@ -540,7 +539,6 @@ export class ExperimentClientController {
});
return {
...rest,
experimentType: assignedFactor ? EXPERIMENT_TYPE.FACTORIAL : EXPERIMENT_TYPE.SIMPLE,
assignedCondition: finalConditionData,
assignedFactor: assignedFactor ? finalFactorData : undefined,
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
import { Type } from 'class-transformer';
import { IsNotEmpty, IsDefined, IsString, IsOptional, IsEnum, IsObject, ValidateIf, ValidateNested, ValidationOptions, registerDecorator } from 'class-validator';
import { EXPERIMENT_TYPE, MARKED_DECISION_POINT_STATUS, PAYLOAD_TYPE } from 'upgrade_types';
import {
IsNotEmpty,
IsDefined,
IsString,
IsOptional,
IsEnum,
IsObject,
ValidateIf,
ValidateNested,
ValidationOptions,
registerDecorator,
} from 'class-validator';
import { MARKED_DECISION_POINT_STATUS, PAYLOAD_TYPE } from 'upgrade_types';

const IsAssignedFactorRecord = (validationOptions?: ValidationOptions) => {
return function (object: unknown, propertyName: string) {
Expand All @@ -15,16 +26,14 @@ 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];
Expand All @@ -45,9 +54,7 @@ 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'
);
}
class Payload {
Expand All @@ -71,20 +78,13 @@ class AssignedFactor {
}

class AssignedCondition {
@IsNotEmpty()
@IsOptional()
@IsString()
id: string;
id?: string;

@IsDefined()
@IsNotEmpty()
@IsOptional()
@IsString()
conditionCode: string;

@IsObject()
@ValidateNested()
@ValidateIf((object, value) => value !== null)
@Type(() => Payload)
payload: Payload | null;
conditionCode?: string;

@IsOptional()
@IsString()
Expand All @@ -100,11 +100,6 @@ class Data {
@IsString()
target: string;

@IsEnum(EXPERIMENT_TYPE)
@IsDefined()
@IsNotEmpty()
experimentType: EXPERIMENT_TYPE;

@IsOptional()
@ValidateNested()
@Type(() => AssignedCondition)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
import { Type } from 'class-transformer';
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';
import {
IsNotEmpty,
IsDefined,
IsString,
IsOptional,
IsEnum,
IsObject,
ValidateNested,
ValidateIf,
ValidationOptions,
registerDecorator,
} from 'class-validator';
import { MARKED_DECISION_POINT_STATUS, PAYLOAD_TYPE } from 'upgrade_types';

const IsAssignedFactorRecord = (validationOptions?: ValidationOptions) => {
return function (object: unknown, propertyName: string) {
Expand All @@ -15,16 +26,14 @@ 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];
Expand All @@ -45,9 +54,7 @@ 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'
);
}

Expand All @@ -72,20 +79,13 @@ class AssignedFactor {
}

class AssignedCondition {
@IsNotEmpty()
@IsOptional()
@IsString()
id: string;
id?: string;

@IsDefined()
@IsNotEmpty()
@IsOptional()
@IsString()
conditionCode: string;

@IsObject()
@ValidateNested()
@ValidateIf((object, value) => value !== null)
@Type(() => Payload)
payload: Payload | null;
conditionCode?: string;

@IsOptional()
@IsString()
Expand All @@ -101,11 +101,6 @@ class Data {
@IsString()
target: string;

@IsEnum(EXPERIMENT_TYPE)
@IsDefined()
@IsNotEmpty()
experimentType: EXPERIMENT_TYPE;

@IsOptional()
@ValidateNested()
@Type(() => AssignedCondition)
Expand Down
28 changes: 23 additions & 5 deletions clientlibs/js/src/common/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IExperimentAssignmentv5 } from "../../../../types/src";
import { IExperimentAssignmentv5, PAYLOAD_TYPE } from 'upgrade_types';

export function rotateAssignmentList(assignment: IExperimentAssignmentv5) {
if (assignment.assignedCondition.length > 1) {
Expand All @@ -10,7 +10,25 @@ 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;
};
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,
},
],
}
);
}
18 changes: 7 additions & 11 deletions clientlibs/js/src/functions/getDecisionPointAssignment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,14 @@ 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
);

if (experimentAssignment) {
const assignment = new Assignment(
experimentAssignment,
clientState,
);

return assignment;
} else {
return null;
}
const assignment = new Assignment(experimentAssignment, clientState);
return assignment;
} else {
return null;
}
Expand Down
16 changes: 9 additions & 7 deletions clientlibs/js/src/functions/markDecisionPoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,11 @@ export default async function markDecisionPoint(
uniquifier?: string,
clientError?: string
): Promise<UpGradeClientInterfaces.IMarkExperimentPoint> {
const assignment = findExperimentAssignmentBySiteAndTarget(site, target, experimentAssignmentData)

if (!assignment) {
throw new Error('No assignment found');
}
const assignment = findExperimentAssignmentBySiteAndTarget(site, target, experimentAssignmentData);

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,
Expand All @@ -44,7 +40,13 @@ 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 {
Expand Down

0 comments on commit ab68afe

Please sign in to comment.