Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for datetime conditions match #93

Closed
wants to merge 4 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 56 additions & 9 deletions packages/api/src/services/sdkService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,41 @@ export type EvaluateResponse = {
responseId: string;
};

// New condition matcher for datetime conditions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please move this function at the bottom of the file.

const datetimeConditionMatcher = (
condition: ConditionModel,
contextValue: string, // Assuming contextValue is a string representing a datetime
): boolean => {
// Convert context and condition values to Date objects
const contextDate = new Date(contextValue);
const conditionDate = new Date(condition.value);

// Evaluate the datetime condition
switch (condition.operator) {
case "before": {
return contextDate < conditionDate;
}
case "after": {
return contextDate > conditionDate;
}
case "beforeOrAt": {
return contextDate <= conditionDate;
}
case "afterOrAt": {
return contextDate >= conditionDate;
}
case "equals": {
return contextDate.getTime() === conditionDate.getTime();
}
case "notEquals": {
return contextDate.getTime() !== conditionDate.getTime();
}
default: {
return false; // Unsupported datetime condition
}
}
};

export const evaluateFlag = async (
flag: FlagModel,
context: Record<string, string>,
Expand Down Expand Up @@ -52,15 +87,27 @@ export const evaluateFlag = async (
(y) => y.context === firstContextKey,
)[0];
if (matchCondition) {
const hasMatch = getMatchByCondition(
matchCondition,
contextValue,
);
response.match = hasMatch;
response.meta.segment = x.segment.key;
response.meta.condition = matchCondition.key;
foundMatchCondition = true;
response.reason = ApiResponseCodes.FlagMatch;
if (matchCondition.conditionType === "string") {
const hasMatch = getMatchByCondition(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getMatchByCondition is where the check on the conditionType should happen. please move the call of datetimeConditionMatcher into that function.

matchCondition,
contextValue,
);
response.match = hasMatch;
response.meta.segment = x.segment.key;
response.meta.condition = matchCondition.key;
foundMatchCondition = true;
response.reason = ApiResponseCodes.FlagMatch;
} else if (matchCondition.conditionType === "datetime") {
const hasMatch = datetimeConditionMatcher(
matchCondition,
contextValue,
);
response.match = hasMatch;
response.meta.segment = x.segment.key;
response.meta.condition = matchCondition.key;
foundMatchCondition = true;
response.reason = ApiResponseCodes.FlagMatch;
}
}
}
});
Expand Down