-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
[No QA][Free trial] Implement all Free Trials utility functions #43844
Merged
chiragsalian
merged 26 commits into
Expensify:main
from
fabioh8010:feature/free-trials/onyx-keys-and-utility-functions
Jun 19, 2024
Merged
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
127d350
Add NVP_FIRST_DAY_FREE_TRIAL Onyx key
fabioh8010 3aaaac1
Add NVP_LAST_DAY_FREE_TRIAL Onyx key
fabioh8010 12fa116
Add NVP_BILLING_FUND_ID Onyx key
fabioh8010 91affbd
Add NVP_PRIVATE_AMOUNT_OWNED Onyx key
fabioh8010 a8f194f
Add NVP_PRIVATE_OWNER_BILLING_GRACE_PERIOD_END Onyx key
fabioh8010 9f41e9b
Add SHARED_NVP_PRIVATE_USER_BILLING_GRACE_PERIOD_END Onyx key
fabioh8010 3127c9d
Implement isChatUsedForOnboarding() and getChatUsedForOnboarding() fu…
fabioh8010 4eb8d41
Add descriptive comment to isSystemChat() function
fabioh8010 92ba0c8
Implement SubscriptionUtils.calculateRemainingFreeTrialDays() function
fabioh8010 ce591a3
Improve ReportUtilsTest tests
fabioh8010 ac22ed8
Implement SubscriptionUtils.isUserOnFreeTrial() function
fabioh8010 c012ed3
Implement SubscriptionUtils.hasUserFreeTrialEnded() function
fabioh8010 ccd5758
Implement SubscriptionUtils.doesUserHavePaymentCardAdded() function
fabioh8010 5fa1f17
Implement SubscriptionUtils.shouldRestrictUserBillableActions() function
fabioh8010 8e27a71
Improve shouldRestrictUserBillableActions() logic
fabioh8010 52074be
Fix TS errors
fabioh8010 dbec623
Merge remote-tracking branch 'origin/main' into feature/free-trials/o…
fabioh8010 0cf96d7
Prettier
fabioh8010 35e9bba
Changes ReportUtils.requiresAttentionFromCurrentUser() to include fre…
fabioh8010 a3e1c4a
Merge remote-tracking branch 'origin/main' into feature/free-trials/o…
fabioh8010 7eb5a07
Add another test for ReportUtils.requiresAttentionFromCurrentUser()
fabioh8010 cfd25ee
Merge remote-tracking branch 'origin/main' into feature/free-trials/o…
fabioh8010 7b5456d
Prettier fix
fabioh8010 efe02e1
Fix dates
fabioh8010 903a6c6
Merge remote-tracking branch 'origin/main' into feature/free-trials/o…
fabioh8010 9954f49
Address review comments
fabioh8010 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 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 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 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 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,135 @@ | ||
import {differenceInSeconds, fromUnixTime, isAfter, isBefore, parse as parseDate} from 'date-fns'; | ||
import type {OnyxCollection, OnyxEntry} from 'react-native-onyx'; | ||
import Onyx from 'react-native-onyx'; | ||
import CONST from '@src/CONST'; | ||
import ONYXKEYS from '@src/ONYXKEYS'; | ||
import type {BillingGraceEndPeriod, Policy} from '@src/types/onyx'; | ||
|
||
let firstDayFreeTrial: OnyxEntry<string>; | ||
Onyx.connect({ | ||
key: ONYXKEYS.NVP_FIRST_DAY_FREE_TRIAL, | ||
callback: (value) => (firstDayFreeTrial = value), | ||
}); | ||
|
||
let lastDayFreeTrial: OnyxEntry<string>; | ||
Onyx.connect({ | ||
key: ONYXKEYS.NVP_LAST_DAY_FREE_TRIAL, | ||
callback: (value) => (lastDayFreeTrial = value), | ||
}); | ||
|
||
let userBillingFundID: OnyxEntry<number>; | ||
Onyx.connect({ | ||
key: ONYXKEYS.NVP_BILLING_FUND_ID, | ||
callback: (value) => (userBillingFundID = value), | ||
}); | ||
|
||
let userBillingGraceEndPeriodCollection: OnyxCollection<BillingGraceEndPeriod>; | ||
Onyx.connect({ | ||
key: ONYXKEYS.COLLECTION.SHARED_NVP_PRIVATE_USER_BILLING_GRACE_PERIOD_END, | ||
callback: (value) => (userBillingGraceEndPeriodCollection = value), | ||
waitForCollectionCallback: true, | ||
}); | ||
|
||
let ownerBillingGraceEndPeriod: OnyxEntry<number>; | ||
Onyx.connect({ | ||
key: ONYXKEYS.NVP_PRIVATE_OWNER_BILLING_GRACE_PERIOD_END, | ||
callback: (value) => (ownerBillingGraceEndPeriod = value), | ||
}); | ||
|
||
let amountOwed: OnyxEntry<number>; | ||
Onyx.connect({ | ||
key: ONYXKEYS.NVP_PRIVATE_AMOUNT_OWNED, | ||
callback: (value) => (amountOwed = value), | ||
}); | ||
|
||
let allPolicies: OnyxCollection<Policy>; | ||
Onyx.connect({ | ||
key: ONYXKEYS.COLLECTION.POLICY, | ||
callback: (value) => (allPolicies = value), | ||
waitForCollectionCallback: true, | ||
}); | ||
|
||
/** | ||
* Calculates the remaining number of days of the workspace owner's free trial before it ends. | ||
*/ | ||
function calculateRemainingFreeTrialDays(): number { | ||
if (!lastDayFreeTrial) { | ||
return 0; | ||
} | ||
|
||
const currentDate = new Date(); | ||
const diffInSeconds = differenceInSeconds(parseDate(lastDayFreeTrial, CONST.DATE.FNS_DATE_TIME_FORMAT_STRING, currentDate), currentDate); | ||
const diffInDays = Math.ceil(diffInSeconds / 86400); | ||
|
||
return diffInDays < 0 ? 0 : diffInDays; | ||
} | ||
|
||
/** | ||
* Whether the workspace's owner is on its free trial period. | ||
*/ | ||
function isUserOnFreeTrial(): boolean { | ||
if (!firstDayFreeTrial || !lastDayFreeTrial) { | ||
return false; | ||
} | ||
|
||
const currentDate = new Date(); | ||
const firstDayFreeTrialDate = parseDate(firstDayFreeTrial, CONST.DATE.FNS_DATE_TIME_FORMAT_STRING, currentDate); | ||
const lastDayFreeTrialDate = parseDate(lastDayFreeTrial, CONST.DATE.FNS_DATE_TIME_FORMAT_STRING, currentDate); | ||
|
||
return isAfter(currentDate, firstDayFreeTrialDate) && isBefore(currentDate, lastDayFreeTrialDate); | ||
} | ||
|
||
/** | ||
* Whether the workspace owner's free trial period has ended. | ||
*/ | ||
function hasUserFreeTrialEnded(): boolean { | ||
if (!lastDayFreeTrial) { | ||
return false; | ||
} | ||
|
||
const currentDate = new Date(); | ||
const lastDayFreeTrialDate = parseDate(lastDayFreeTrial, CONST.DATE.FNS_DATE_TIME_FORMAT_STRING, currentDate); | ||
|
||
return isAfter(currentDate, lastDayFreeTrialDate); | ||
} | ||
|
||
/** | ||
* Whether the user has a payment card added to its account. | ||
*/ | ||
function doesUserHavePaymentCardAdded(): boolean { | ||
return userBillingFundID !== undefined; | ||
} | ||
|
||
/** | ||
* Whether the user's billable actions should be restricted. | ||
*/ | ||
function shouldRestrictUserBillableActions(policyID: string): boolean { | ||
const currentDate = new Date(); | ||
|
||
// This logic will be executed if the user is a workspace's non-owner (normal user or admin). | ||
// We should restrict the workspace's non-owner actions if it's member of a workspace where the owner is | ||
// past due and is past its grace period end. | ||
for (const userBillingGraceEndPeriodEntry of Object.entries(userBillingGraceEndPeriodCollection ?? {})) { | ||
const [entryKey, userBillingGracePeriodEnd] = userBillingGraceEndPeriodEntry; | ||
|
||
if (userBillingGracePeriodEnd && isAfter(currentDate, fromUnixTime(userBillingGracePeriodEnd.value))) { | ||
// Extracts the owner account ID from the collection member key. | ||
const ownerAccountID = entryKey.slice(ONYXKEYS.COLLECTION.SHARED_NVP_PRIVATE_USER_BILLING_GRACE_PERIOD_END.length); | ||
|
||
const ownerPolicy = allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${policyID}`]; | ||
if (String(ownerPolicy?.ownerAccountID ?? -1) === ownerAccountID) { | ||
return true; | ||
} | ||
} | ||
} | ||
|
||
// If it reached here it means that the user is actually the workspace's owner. | ||
// We should restrict the workspace's owner actions if it's past its grace period end date and it's owing some amount. | ||
if (ownerBillingGraceEndPeriod && amountOwed !== undefined && amountOwed > 0 && isAfter(currentDate, fromUnixTime(ownerBillingGraceEndPeriod))) { | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
export {calculateRemainingFreeTrialDays, doesUserHavePaymentCardAdded, hasUserFreeTrialEnded, isUserOnFreeTrial, shouldRestrictUserBillableActions}; |
This file contains 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NAB: we should add some context that these are used for A/B testing because it could be rather confusing for anyone reading this that we're doing different logic depending on whether accountID is odd or even.