Skip to content

Commit

Permalink
Address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
fabioh8010 committed Jun 18, 2024
1 parent 903a6c6 commit 9954f49
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/ONYXKEYS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ const ONYXKEYS = {
SNAPSHOT: 'snapshot_',

// Shared NVPs
/** Collection of objects where each objects represents a workspace’s owner which is past due billing AND the user is a member of. */
/** Collection of objects where each object represents the owner of the workspace that is past due billing AND the user is a member of. */
SHARED_NVP_PRIVATE_USER_BILLING_GRACE_PERIOD_END: 'sharedNVP_private_billingGracePeriodEnd_',
},

Expand Down
12 changes: 7 additions & 5 deletions src/libs/SubscriptionUtils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {differenceInCalendarDays, fromUnixTime, isAfter, isBefore, parse as parseDate} from 'date-fns';
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';
Expand Down Expand Up @@ -58,16 +58,18 @@ function calculateRemainingFreeTrialDays(): number {
}

const currentDate = new Date();
const difference = differenceInCalendarDays(parseDate(lastDayFreeTrial, CONST.DATE.FNS_DATE_TIME_FORMAT_STRING, currentDate), currentDate);
return difference < 0 ? 0 : difference;
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 true;
return false;
}

const currentDate = new Date();
Expand Down Expand Up @@ -123,7 +125,7 @@ function shouldRestrictUserBillableActions(policyID: string): boolean {

// 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 && isAfter(currentDate, fromUnixTime(ownerBillingGraceEndPeriod))) {
if (ownerBillingGraceEndPeriod && amountOwed !== undefined && amountOwed > 0 && isAfter(currentDate, fromUnixTime(ownerBillingGraceEndPeriod))) {
return true;
}

Expand Down
4 changes: 4 additions & 0 deletions tests/unit/ReportUtilsTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -912,6 +912,10 @@ describe('ReportUtils', () => {
await Onyx.set(ONYXKEYS.SESSION, {email: currentUserEmail, accountID: currentUserAccountID});
});

it('should return false if the report is neither the system or concierge chat', () => {
expect(ReportUtils.isChatUsedForOnboarding(LHNTestUtils.getFakeReport())).toBeFalsy();
});

it('should return true if the user account ID is odd and report is the system chat', async () => {
const accountID = 1;

Expand Down
65 changes: 54 additions & 11 deletions tests/unit/SubscriptionUtilsTest.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {addDays, format as formatDate, getUnixTime, subDays} from 'date-fns';
import {addDays, addMinutes, format as formatDate, getUnixTime, subDays} from 'date-fns';
import Onyx from 'react-native-onyx';
import * as SubscriptionUtils from '@libs/SubscriptionUtils';
import CONST from '@src/CONST';
Expand Down Expand Up @@ -32,6 +32,11 @@ describe('SubscriptionUtils', () => {
expect(SubscriptionUtils.calculateRemainingFreeTrialDays()).toBe(0);
});

it('should return 1 if the current date is on the same day of the free trial end date, but some minutes earlier', async () => {
await Onyx.set(ONYXKEYS.NVP_LAST_DAY_FREE_TRIAL, formatDate(addMinutes(new Date(), 30), CONST.DATE.FNS_DATE_TIME_FORMAT_STRING));
expect(SubscriptionUtils.calculateRemainingFreeTrialDays()).toBe(1);
});

it('should return the remaining days if the current date is before the free trial end date', async () => {
await Onyx.set(ONYXKEYS.NVP_LAST_DAY_FREE_TRIAL, formatDate(addDays(new Date(), 5), CONST.DATE.FNS_DATE_TIME_FORMAT_STRING));
expect(SubscriptionUtils.calculateRemainingFreeTrialDays()).toBe(5);
Expand All @@ -47,27 +52,54 @@ describe('SubscriptionUtils', () => {
});
});

it('should return true if the Onyx keys are not set', () => {
expect(SubscriptionUtils.isUserOnFreeTrial()).toBeTruthy();
it('should return false if the Onyx keys are not set', () => {
expect(SubscriptionUtils.isUserOnFreeTrial()).toBeFalsy();
});

it('should return true if the current date is between the free trial start and end dates', async () => {
it('should return false if the current date is before the free trial start date', async () => {
await Onyx.multiSet({
[ONYXKEYS.NVP_FIRST_DAY_FREE_TRIAL]: formatDate(subDays(new Date(), 1), CONST.DATE.FNS_DATE_TIME_FORMAT_STRING),
[ONYXKEYS.NVP_LAST_DAY_FREE_TRIAL]: formatDate(addDays(new Date(), 3), CONST.DATE.FNS_DATE_TIME_FORMAT_STRING),
[ONYXKEYS.NVP_FIRST_DAY_FREE_TRIAL]: formatDate(addDays(new Date(), 2), CONST.DATE.FNS_DATE_TIME_FORMAT_STRING),
[ONYXKEYS.NVP_LAST_DAY_FREE_TRIAL]: formatDate(addDays(new Date(), 4), CONST.DATE.FNS_DATE_TIME_FORMAT_STRING),
});

expect(SubscriptionUtils.isUserOnFreeTrial()).toBeTruthy();
expect(SubscriptionUtils.isUserOnFreeTrial()).toBeFalsy();
});

it('should return false if the current date is after the free trial end date', async () => {
await Onyx.multiSet({
[ONYXKEYS.NVP_FIRST_DAY_FREE_TRIAL]: formatDate(subDays(new Date(), 10), CONST.DATE.FNS_DATE_TIME_FORMAT_STRING),
[ONYXKEYS.NVP_LAST_DAY_FREE_TRIAL]: formatDate(subDays(new Date(), 3), CONST.DATE.FNS_DATE_TIME_FORMAT_STRING),
[ONYXKEYS.NVP_FIRST_DAY_FREE_TRIAL]: formatDate(subDays(new Date(), 4), CONST.DATE.FNS_DATE_TIME_FORMAT_STRING),
[ONYXKEYS.NVP_LAST_DAY_FREE_TRIAL]: formatDate(subDays(new Date(), 2), CONST.DATE.FNS_DATE_TIME_FORMAT_STRING),
});

expect(SubscriptionUtils.isUserOnFreeTrial()).toBeFalsy();
});

it('should return true if the current date is on the same date of free trial start date', async () => {
await Onyx.multiSet({
[ONYXKEYS.NVP_FIRST_DAY_FREE_TRIAL]: formatDate(new Date(), CONST.DATE.FNS_DATE_TIME_FORMAT_STRING),
[ONYXKEYS.NVP_LAST_DAY_FREE_TRIAL]: formatDate(addDays(new Date(), 3), CONST.DATE.FNS_DATE_TIME_FORMAT_STRING),
});

expect(SubscriptionUtils.isUserOnFreeTrial()).toBeTruthy();
});

it('should return true if the current date is on the same date of free trial end date, but some minutes earlier', async () => {
await Onyx.multiSet({
[ONYXKEYS.NVP_FIRST_DAY_FREE_TRIAL]: formatDate(subDays(new Date(), 2), CONST.DATE.FNS_DATE_TIME_FORMAT_STRING),
[ONYXKEYS.NVP_LAST_DAY_FREE_TRIAL]: formatDate(addMinutes(new Date(), 30), CONST.DATE.FNS_DATE_TIME_FORMAT_STRING),
});

expect(SubscriptionUtils.isUserOnFreeTrial()).toBeTruthy();
});

it('should return true if the current date is between the free trial start and end dates', async () => {
await Onyx.multiSet({
[ONYXKEYS.NVP_FIRST_DAY_FREE_TRIAL]: formatDate(subDays(new Date(), 1), CONST.DATE.FNS_DATE_TIME_FORMAT_STRING),
[ONYXKEYS.NVP_LAST_DAY_FREE_TRIAL]: formatDate(addDays(new Date(), 3), CONST.DATE.FNS_DATE_TIME_FORMAT_STRING),
});

expect(SubscriptionUtils.isUserOnFreeTrial()).toBeTruthy();
});
});

describe('hasUserFreeTrialEnded', () => {
Expand Down Expand Up @@ -191,12 +223,23 @@ describe('SubscriptionUtils', () => {
expect(SubscriptionUtils.shouldRestrictUserBillableActions(policyID)).toBeFalsy();
});

it('should return true if the user is a workspace owner but is past due billing', async () => {
it("should return false if the user is a workspace owner but is past due billing but isn't owning any amount", async () => {
const policyID = '1001';

await Onyx.multiSet({
[ONYXKEYS.NVP_PRIVATE_OWNER_BILLING_GRACE_PERIOD_END]: getUnixTime(subDays(new Date(), 3)), // past due
[ONYXKEYS.NVP_PRIVATE_AMOUNT_OWNED]: 0,
});

expect(SubscriptionUtils.shouldRestrictUserBillableActions(policyID)).toBeFalsy();
});

it('should return true if the user is a workspace owner but is past due billing and is owning some amount', async () => {
const policyID = '1001';

await Onyx.multiSet({
[ONYXKEYS.NVP_PRIVATE_OWNER_BILLING_GRACE_PERIOD_END]: getUnixTime(subDays(new Date(), 3)), // past due
[ONYXKEYS.NVP_PRIVATE_AMOUNT_OWNED]: 8010, // owing some amount
[ONYXKEYS.NVP_PRIVATE_AMOUNT_OWNED]: 8010,
});

expect(SubscriptionUtils.shouldRestrictUserBillableActions(policyID)).toBeTruthy();
Expand Down

0 comments on commit 9954f49

Please sign in to comment.