81 lines
1.9 KiB
TypeScript
81 lines
1.9 KiB
TypeScript
import { InternalError } from "@api/errors/base/InternalError.js";
|
|
import type { FeatureOptions } from "@models/cusProductModels/cusProductModels.js";
|
|
import type {
|
|
Entitlement,
|
|
EntitlementWithFeature,
|
|
} from "../../models/productModels/entModels/entModels.js";
|
|
import type { Price } from "../../models/productModels/priceModels/priceModels.js";
|
|
import type { FullProduct } from "../../models/productModels/productModels.js";
|
|
|
|
export const entToPrice = ({
|
|
ent,
|
|
prices,
|
|
}: {
|
|
ent: Entitlement;
|
|
prices: Price[];
|
|
}) => {
|
|
return prices.find(
|
|
(price) =>
|
|
price.entitlement_id === ent.id &&
|
|
price.internal_product_id === ent.internal_product_id,
|
|
);
|
|
};
|
|
|
|
export function priceToEnt(params: {
|
|
price: Price;
|
|
entitlements: EntitlementWithFeature[];
|
|
errorOnNotFound: true;
|
|
}): EntitlementWithFeature;
|
|
export function priceToEnt(params: {
|
|
price: Price;
|
|
entitlements: EntitlementWithFeature[];
|
|
errorOnNotFound?: false;
|
|
}): EntitlementWithFeature | undefined;
|
|
export function priceToEnt({
|
|
price,
|
|
entitlements,
|
|
errorOnNotFound,
|
|
}: {
|
|
price: Price;
|
|
entitlements: EntitlementWithFeature[];
|
|
errorOnNotFound?: boolean;
|
|
}): EntitlementWithFeature | undefined {
|
|
const entitlement = entitlements.find(
|
|
(ent) =>
|
|
ent.id === price.entitlement_id &&
|
|
ent.internal_product_id === price.internal_product_id,
|
|
);
|
|
|
|
if (!entitlement && errorOnNotFound) {
|
|
throw new InternalError({
|
|
message: `Entitlement not found for price ${price.id}`,
|
|
});
|
|
}
|
|
|
|
return entitlement;
|
|
}
|
|
|
|
export const entToOptions = ({
|
|
ent,
|
|
options,
|
|
}: {
|
|
ent: Entitlement;
|
|
options: FeatureOptions[];
|
|
}) => {
|
|
return options.find(
|
|
(option) =>
|
|
option.internal_feature_id === ent.internal_feature_id ||
|
|
(ent.feature_id && option.feature_id === ent.feature_id),
|
|
);
|
|
};
|
|
|
|
export const productToEnt = ({
|
|
product,
|
|
featureId,
|
|
}: {
|
|
product: FullProduct;
|
|
featureId: string;
|
|
}) => {
|
|
return product.entitlements.find((ent) => ent.feature.id === featureId);
|
|
};
|