fix: imports
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
import {
|
||||
type ApiBalanceInput,
|
||||
apiBalanceToAllowed,
|
||||
type FeatureInput,
|
||||
} from "../convert/apiBalanceToAllowed";
|
||||
|
||||
type CheckFeatureInput = FeatureInput & {
|
||||
id: string;
|
||||
};
|
||||
|
||||
type CheckEntityInput = {
|
||||
balances?: Record<string, ApiBalanceInput | undefined>;
|
||||
};
|
||||
|
||||
export const getFeatureToUseForCheck = ({
|
||||
creditSystems,
|
||||
feature,
|
||||
apiEntity,
|
||||
requiredBalance,
|
||||
}: {
|
||||
creditSystems: CheckFeatureInput[];
|
||||
feature: CheckFeatureInput;
|
||||
apiEntity: CheckEntityInput;
|
||||
requiredBalance: number;
|
||||
}) => {
|
||||
// 1. If there's a credit system & cusEnts for that credit system -> return credit system
|
||||
// 2. If there's cusEnts for the feature -> return feature
|
||||
// 3. Otherwise, feature to use is credit system if exists, otherwise return feature
|
||||
if (creditSystems.length === 0) return feature;
|
||||
|
||||
const mainBalance = apiEntity?.balances?.[feature.id];
|
||||
|
||||
if (
|
||||
mainBalance &&
|
||||
apiBalanceToAllowed({
|
||||
apiBalance: mainBalance,
|
||||
feature,
|
||||
requiredBalance,
|
||||
})
|
||||
) {
|
||||
return feature;
|
||||
}
|
||||
|
||||
for (const creditSystem of creditSystems) {
|
||||
const apiBalance = apiEntity?.balances?.[creditSystem.id];
|
||||
if (!apiBalance) continue;
|
||||
|
||||
if (
|
||||
apiBalanceToAllowed({
|
||||
apiBalance,
|
||||
feature: creditSystem,
|
||||
requiredBalance,
|
||||
})
|
||||
) {
|
||||
return creditSystem;
|
||||
}
|
||||
}
|
||||
|
||||
return creditSystems[0];
|
||||
};
|
||||
1
shared/api/customers/cusFeatures/utils/check/index.ts
Normal file
1
shared/api/customers/cusFeatures/utils/check/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from "./getFeatureToUseForCheck";
|
||||
@@ -0,0 +1,75 @@
|
||||
import { Decimal } from "decimal.js";
|
||||
import { apiBalanceV1ToAvailableOverage } from "./apiBalanceV1ToAvailableOverage";
|
||||
|
||||
export type ApiBalanceBreakdownInput = {
|
||||
usage: number;
|
||||
included_grant: number;
|
||||
prepaid_grant: number;
|
||||
price?: {
|
||||
billing_method?: string;
|
||||
max_purchase: number | null;
|
||||
} | null;
|
||||
overage?: number;
|
||||
};
|
||||
|
||||
export type ApiBalanceInput = {
|
||||
unlimited: boolean;
|
||||
overage_allowed: boolean;
|
||||
remaining: number;
|
||||
max_purchase: number | null;
|
||||
breakdown?: ApiBalanceBreakdownInput[];
|
||||
};
|
||||
|
||||
export type FeatureInput = {
|
||||
type?: string;
|
||||
};
|
||||
|
||||
export const apiBalanceToAllowed = ({
|
||||
apiBalance,
|
||||
feature,
|
||||
requiredBalance,
|
||||
}: {
|
||||
apiBalance: ApiBalanceInput;
|
||||
feature: FeatureInput;
|
||||
requiredBalance: number;
|
||||
}) => {
|
||||
if (!apiBalance) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 1. Boolean
|
||||
if (feature.type === "boolean") {
|
||||
return true;
|
||||
}
|
||||
|
||||
// 2. Unlimited
|
||||
if (apiBalance.unlimited) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// 2. Required balance is negative
|
||||
if (requiredBalance < 0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// 3. Overage allowed
|
||||
if (apiBalance.overage_allowed) {
|
||||
// 1. Available overage
|
||||
const availableOverage = apiBalanceV1ToAvailableOverage({ apiBalance });
|
||||
|
||||
if (availableOverage !== null && availableOverage !== undefined) {
|
||||
return new Decimal(availableOverage)
|
||||
.add(apiBalance.remaining)
|
||||
.gte(requiredBalance);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// 4. Balance >= required balance (V1 uses 'remaining' instead of 'current_balance')
|
||||
if (new Decimal(apiBalance.remaining).gte(requiredBalance)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
@@ -1,18 +1,19 @@
|
||||
import { apiBalanceV1ToOverage } from "@api/customers/cusFeatures/utils/convert/apiBalanceV1ToOverage.js";
|
||||
import { BillingMethod } from "@api/products/components/billingMethod.js";
|
||||
import { sumValues } from "@utils/utils.js";
|
||||
import { Decimal } from "decimal.js";
|
||||
import type {
|
||||
ApiBalanceBreakdownV1,
|
||||
ApiBalanceV1,
|
||||
} from "../../apiBalanceV1.js";
|
||||
ApiBalanceBreakdownInput,
|
||||
ApiBalanceInput,
|
||||
} from "./apiBalanceToAllowed";
|
||||
import { apiBalanceV1ToOverage } from "./apiBalanceV1ToOverage";
|
||||
|
||||
const sumValues = (values: number[]) =>
|
||||
values.reduce((total, value) => total + value, 0);
|
||||
|
||||
export const apiBalanceBreakdownV1ToMaxOverage = ({
|
||||
apiBalanceBreakdown,
|
||||
}: {
|
||||
apiBalanceBreakdown: ApiBalanceBreakdownV1;
|
||||
apiBalanceBreakdown: ApiBalanceBreakdownInput;
|
||||
}): number | undefined => {
|
||||
if (apiBalanceBreakdown.price?.billing_method === BillingMethod.UsageBased) {
|
||||
if (apiBalanceBreakdown.price?.billing_method === "usage_based") {
|
||||
return apiBalanceBreakdown.price?.max_purchase ?? undefined;
|
||||
}
|
||||
|
||||
@@ -22,7 +23,7 @@ export const apiBalanceBreakdownV1ToMaxOverage = ({
|
||||
export const apiBalanceV1ToMaxOverage = ({
|
||||
apiBalance,
|
||||
}: {
|
||||
apiBalance: ApiBalanceV1;
|
||||
apiBalance: ApiBalanceInput;
|
||||
}): number | undefined => {
|
||||
const breakdownItems = apiBalance.breakdown ?? [];
|
||||
|
||||
@@ -42,7 +43,7 @@ export const apiBalanceV1ToMaxOverage = ({
|
||||
export const apiBalanceV1ToAvailableOverage = ({
|
||||
apiBalance,
|
||||
}: {
|
||||
apiBalance: ApiBalanceV1;
|
||||
apiBalance: ApiBalanceInput;
|
||||
}): number | undefined => {
|
||||
const maxOverage = apiBalanceV1ToMaxOverage({ apiBalance });
|
||||
const overage = apiBalanceV1ToOverage({ apiBalance });
|
||||
|
||||
@@ -1,13 +1,15 @@
|
||||
import { sumValues } from "@utils/utils.js";
|
||||
import type {
|
||||
ApiBalanceBreakdownV1,
|
||||
ApiBalanceV1,
|
||||
} from "../../apiBalanceV1.js";
|
||||
ApiBalanceBreakdownInput,
|
||||
ApiBalanceInput,
|
||||
} from "./apiBalanceToAllowed";
|
||||
|
||||
const sumValues = (values: number[]) =>
|
||||
values.reduce((total, value) => total + value, 0);
|
||||
|
||||
export const apiBalanceBreakdownV1ToOverage = ({
|
||||
apiBalanceBreakdown,
|
||||
}: {
|
||||
apiBalanceBreakdown: ApiBalanceBreakdownV1;
|
||||
apiBalanceBreakdown: ApiBalanceBreakdownInput;
|
||||
}) => {
|
||||
// const overage = Math.max(
|
||||
// 0,
|
||||
@@ -23,7 +25,7 @@ export const apiBalanceBreakdownV1ToOverage = ({
|
||||
export const apiBalanceV1ToOverage = ({
|
||||
apiBalance,
|
||||
}: {
|
||||
apiBalance: ApiBalanceV1;
|
||||
apiBalance: ApiBalanceInput;
|
||||
}) => {
|
||||
const breakdownItems = apiBalance.breakdown ?? [];
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { sumValues } from "@utils/utils.js";
|
||||
import type { ApiBalanceV1 } from "../../apiBalanceV1.js";
|
||||
import { sumValues } from "@utils/utils";
|
||||
import type { ApiBalanceV1 } from "../../apiBalanceV1";
|
||||
|
||||
export const apiBalanceV1ToPrepaidQuantity = ({
|
||||
apiBalance,
|
||||
|
||||
@@ -2,12 +2,12 @@ import { Decimal } from "decimal.js";
|
||||
import type {
|
||||
ApiBalanceBreakdownV1,
|
||||
ApiBalanceV1,
|
||||
} from "../../apiBalanceV1.js";
|
||||
} from "../../apiBalanceV1";
|
||||
import {
|
||||
apiBalanceBreakdownV1ToOverage,
|
||||
apiBalanceV1ToOverage,
|
||||
} from "./apiBalanceV1ToOverage.js";
|
||||
import { apiBalanceV1ToPrepaidQuantity } from "./apiBalanceV1ToPrepaidQuantity.js";
|
||||
} from "./apiBalanceV1ToOverage";
|
||||
import { apiBalanceV1ToPrepaidQuantity } from "./apiBalanceV1ToPrepaidQuantity";
|
||||
|
||||
export const apiBalanceBreakdownV1ToPurchasedBalance = ({
|
||||
apiBalanceBreakdown,
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
import type { ApiBalanceInput } from "@api/customers/cusFeatures/utils/convert/apiBalanceToAllowed";
|
||||
|
||||
export const balancesToCheckFeature = ({
|
||||
balances,
|
||||
}: {
|
||||
balances: Record<string, ApiBalanceInput>;
|
||||
}) => {
|
||||
return balances.map((balance) => {
|
||||
return {
|
||||
featureId: balance.featureId,
|
||||
};
|
||||
});
|
||||
};
|
||||
Reference in New Issue
Block a user