2022-06-21 11:22:27 +02:00
|
|
|
import { parseISO, isPast } from 'date-fns';
|
2024-03-18 13:58:05 +01:00
|
|
|
import { type IInstanceStatus, InstanceState } from 'interfaces/instance';
|
2022-06-14 11:51:11 +02:00
|
|
|
import differenceInDays from 'date-fns/differenceInDays';
|
|
|
|
|
2022-06-21 11:22:27 +02:00
|
|
|
const TRIAL_EXPIRES_SOON_DAYS_THRESHOLD = 10;
|
2022-06-14 11:51:11 +02:00
|
|
|
|
2022-06-21 11:22:27 +02:00
|
|
|
export const isTrialInstance = (
|
2023-10-02 14:25:46 +02:00
|
|
|
instanceStatus: IInstanceStatus | undefined,
|
2022-06-14 11:51:11 +02:00
|
|
|
): boolean => {
|
2022-06-21 11:22:27 +02:00
|
|
|
return (
|
|
|
|
instanceStatus?.state === InstanceState.TRIAL ||
|
|
|
|
instanceStatus?.state === InstanceState.EXPIRED ||
|
|
|
|
instanceStatus?.state === InstanceState.CHURNED
|
|
|
|
);
|
2022-06-14 11:51:11 +02:00
|
|
|
};
|
|
|
|
|
2022-06-21 11:22:27 +02:00
|
|
|
export const trialHasExpired = (
|
2023-10-02 14:25:46 +02:00
|
|
|
instanceStatus: IInstanceStatus | undefined,
|
2022-06-21 11:22:27 +02:00
|
|
|
): boolean => {
|
|
|
|
if (
|
|
|
|
instanceStatus?.state === InstanceState.EXPIRED ||
|
|
|
|
instanceStatus?.state === InstanceState.CHURNED
|
|
|
|
) {
|
|
|
|
return true;
|
2022-06-14 11:51:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
2022-06-21 11:22:27 +02:00
|
|
|
instanceStatus?.state === InstanceState.TRIAL &&
|
|
|
|
instanceStatus?.trialExpiry
|
2022-06-14 11:51:11 +02:00
|
|
|
) {
|
2022-06-21 11:22:27 +02:00
|
|
|
return isPast(parseISO(instanceStatus.trialExpiry));
|
2022-06-14 11:51:11 +02:00
|
|
|
}
|
2022-06-21 11:22:27 +02:00
|
|
|
|
|
|
|
return false;
|
2022-06-14 11:51:11 +02:00
|
|
|
};
|
|
|
|
|
2022-06-21 11:22:27 +02:00
|
|
|
export const trialExpiresSoon = (
|
2023-10-02 14:25:46 +02:00
|
|
|
instanceStatus: IInstanceStatus | undefined,
|
2022-06-21 11:22:27 +02:00
|
|
|
) => {
|
2022-06-14 11:51:11 +02:00
|
|
|
if (
|
2022-06-21 11:22:27 +02:00
|
|
|
!instanceStatus ||
|
|
|
|
instanceStatus.state !== InstanceState.TRIAL ||
|
|
|
|
!instanceStatus.trialExpiry
|
2022-06-14 11:51:11 +02:00
|
|
|
) {
|
2022-06-21 11:22:27 +02:00
|
|
|
return false;
|
2022-06-14 11:51:11 +02:00
|
|
|
}
|
2022-06-21 11:22:27 +02:00
|
|
|
|
|
|
|
return (
|
|
|
|
differenceInDays(parseISO(instanceStatus.trialExpiry), new Date()) <=
|
|
|
|
TRIAL_EXPIRES_SOON_DAYS_THRESHOLD
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export const canExtendTrial = (
|
2023-10-02 14:25:46 +02:00
|
|
|
instanceStatus: IInstanceStatus | undefined,
|
2022-06-21 11:22:27 +02:00
|
|
|
): boolean => {
|
|
|
|
return Boolean(
|
|
|
|
instanceStatus &&
|
|
|
|
instanceStatus.state === InstanceState.EXPIRED &&
|
2023-10-02 14:25:46 +02:00
|
|
|
!instanceStatus.trialExtended,
|
2022-06-21 11:22:27 +02:00
|
|
|
);
|
2022-06-14 11:51:11 +02:00
|
|
|
};
|