-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Lead and sales rewards metadata condition #4032
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
Merged
Merged
Changes from 14 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
8bc444a
lead and sales rewards metadata condition
pepeladeira f2bc6ff
Merge branch 'main' into lead-sales-rewards-metadata
steven-tey cc03da1
Merge branch 'main' into lead-sales-rewards-metadata
devkiran 65a7963
Merge branch 'main' into lead-sales-rewards-metadata
devkiran c17fafe
Pass lead context consistently for reward condition evaluation
devkiran d1e3464
Merge branch 'lead-sales-rewards-metadata' of https://github.com/dubi…
devkiran 20e060a
Merge branch 'main' into lead-sales-rewards-metadata
steven-tey e6f9af3
Merge branch 'main' into lead-sales-rewards-metadata
devkiran d637a2a
Extract reward metadata field coercion into dedicated module
devkiran 8b30a7c
Update evaluate-reward-conditions.ts
devkiran 4f40041
Update determine-partner-reward.ts
devkiran bb87b8f
Merge branch 'main' into lead-sales-rewards-metadata
steven-tey 5aa8749
Merge branch 'main' into lead-sales-rewards-metadata
devkiran 754fa2b
Merge branch 'main' into lead-sales-rewards-metadata
devkiran 043224a
update toNumber func
pepeladeira b2fcb7f
Merge branch 'main' into lead-sales-rewards-metadata
pepeladeira 51fd292
Merge branch 'main' into lead-sales-rewards-metadata
steven-tey 3469071
add e2e test for sale metadata reward condition
steven-tey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| import { RewardCondition } from "@/lib/types"; | ||
| import { | ||
| METADATA_CONDITION_OPERATORS, | ||
| METADATA_NUMBER_CONDITION_OPERATORS, | ||
| } from "@/lib/zod/schemas/rewards"; | ||
|
|
||
| function toNumber(fieldKey: unknown): number | undefined { | ||
| if (fieldKey == null) { | ||
| return undefined; | ||
| } | ||
|
|
||
| if (typeof fieldKey === "number" && !Number.isNaN(fieldKey)) { | ||
| return fieldKey; | ||
| } | ||
|
|
||
| if (typeof fieldKey === "boolean") { | ||
| return undefined; | ||
| } | ||
|
|
||
| if (typeof fieldKey === "string") { | ||
| if (fieldKey.trim() === "" || Number.isNaN(Number(fieldKey))) { | ||
| return undefined; | ||
| } | ||
|
|
||
| return Number(fieldKey); | ||
| } | ||
|
|
||
| const n = Number(fieldKey); | ||
|
devkiran marked this conversation as resolved.
|
||
| return Number.isNaN(n) ? undefined : n; | ||
| } | ||
|
|
||
| function toString(fieldKey: unknown) { | ||
| return typeof fieldKey === "string" ? fieldKey : String(fieldKey); | ||
| } | ||
|
|
||
| /** | ||
| * Normalizes a raw metadata value (from lead/sale JSON) into the type that | ||
| * `evaluateCondition` expects for the given operator. | ||
| * | ||
| * Metadata is stored as loosely typed JSON, so the same field may arrive as a | ||
| * string, number, or other primitive. Text operators (starts_with, contains, | ||
| * etc.) always receive a string; numeric operators receive a parsed number or | ||
| * undefined when parsing fails. For equals_to / not_equals, the return type is | ||
| * aligned with `condition.value` so strict equality checks compare like types. | ||
| */ | ||
| export function prepareMetadataFieldValue( | ||
| fieldKey: unknown, | ||
| condition: RewardCondition, | ||
| ): string | number | string[] | number[] | undefined { | ||
| if (fieldKey == null) { | ||
| return undefined; | ||
| } | ||
|
|
||
| const { operator, value: conditionValue } = condition; | ||
|
|
||
| // Metadata conditions do not support in / not_in and other non-metadata operators. | ||
| if (!METADATA_CONDITION_OPERATORS.includes(operator)) { | ||
| return undefined; | ||
| } | ||
|
|
||
| // Pattern text operators always compare against a stringified metadata value. | ||
| if ( | ||
| operator === "starts_with" || | ||
| operator === "ends_with" || | ||
| operator === "contains" || | ||
| operator === "not_contains" | ||
| ) { | ||
| return toString(fieldKey); | ||
| } | ||
|
|
||
| // Numeric operators require a parsed number, or undefined when coercion fails. | ||
| if (METADATA_NUMBER_CONDITION_OPERATORS.includes(operator)) { | ||
| return toNumber(fieldKey); | ||
| } | ||
|
|
||
| // For equals_to / not_equals with a numeric condition value, prefer number coercion. | ||
| if (typeof conditionValue === "number") { | ||
| const numeric = toNumber(fieldKey); | ||
| return numeric !== undefined ? numeric : toString(fieldKey); | ||
| } | ||
|
|
||
| // All other operators use string comparison. | ||
| return toString(fieldKey); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.