fix: ui rewards bug & version default product
This commit is contained in:
@@ -62,6 +62,16 @@ export const handleVersionProductV2 = async ({
|
||||
env,
|
||||
});
|
||||
|
||||
if (latestProduct.is_default) {
|
||||
await ProductService.updateByInternalId({
|
||||
db,
|
||||
internalId: latestProduct.internal_id,
|
||||
update: {
|
||||
is_default: false,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
await ProductService.insert({ db, product: newProduct });
|
||||
|
||||
await handleNewProductItems({
|
||||
|
||||
@@ -5,6 +5,7 @@ import {
|
||||
EntitlementWithFeature,
|
||||
PriceType,
|
||||
ProductItem,
|
||||
ProductItemInterval,
|
||||
} from "@autumn/shared";
|
||||
|
||||
import { FixedPriceConfig, Price, UsagePriceConfig } from "@autumn/shared";
|
||||
@@ -13,7 +14,7 @@ import { isFeatureItem } from "./getItemType";
|
||||
|
||||
export const validBillingInterval = (
|
||||
prices: Price[],
|
||||
config: FixedPriceConfig | UsagePriceConfig
|
||||
config: FixedPriceConfig | UsagePriceConfig,
|
||||
) => {
|
||||
const interval1 = config.interval;
|
||||
if (!interval1 || interval1 == BillingInterval.OneOff) {
|
||||
@@ -37,7 +38,7 @@ export const validBillingInterval = (
|
||||
|
||||
export const getBillingUnits = (
|
||||
config: UsagePriceConfig,
|
||||
entitlements: EntitlementWithFeature[]
|
||||
entitlements: EntitlementWithFeature[],
|
||||
) => {
|
||||
if (!entitlements) return "(error)";
|
||||
|
||||
@@ -46,11 +47,11 @@ export const getBillingUnits = (
|
||||
config.bill_when == BillWhen.StartOfPeriod ||
|
||||
config.bill_when == BillWhen.InAdvance
|
||||
) {
|
||||
return `${config.billing_units} ` || "n";
|
||||
return `${config.billing_units} `;
|
||||
}
|
||||
|
||||
const entitlement = entitlements?.find(
|
||||
(e) => e.internal_feature_id == config?.internal_feature_id
|
||||
(e) => e.internal_feature_id == config?.internal_feature_id,
|
||||
);
|
||||
if (!entitlement) return "n";
|
||||
|
||||
@@ -89,9 +90,9 @@ export const getDefaultPriceConfig = (type: PriceType) => {
|
||||
|
||||
export const pricesOnlyOneOff = (
|
||||
items: ProductItem[],
|
||||
isAddOn: boolean = false
|
||||
isAddOn: boolean = false,
|
||||
) => {
|
||||
let prices = items.filter((item) => !isFeatureItem(item));
|
||||
const prices = items.filter((item) => !isFeatureItem(item));
|
||||
|
||||
if (prices.length == 0 && isAddOn) return true;
|
||||
if (prices.length == 0) return false;
|
||||
@@ -99,14 +100,8 @@ export const pricesOnlyOneOff = (
|
||||
return prices.every((price) => {
|
||||
return intervalIsNone(price.interval);
|
||||
});
|
||||
// if (items.length == 0) return false;
|
||||
// return items.every((item) => {
|
||||
// return item.interval == ProductItemInterval.None;
|
||||
// });
|
||||
};
|
||||
|
||||
export const isFreeProduct = (prices: Price[]) => {
|
||||
return prices.every((price) => {
|
||||
return price.config?.interval == BillingInterval.OneOff;
|
||||
});
|
||||
export const isFreeProduct = (items: ProductItem[]) => {
|
||||
return items.every((item) => isFeatureItem(item));
|
||||
};
|
||||
|
||||
@@ -5,7 +5,13 @@ import { useEffect, useState } from "react";
|
||||
import { Select, SelectContent, SelectItem } from "@/components/ui/select";
|
||||
import { SelectTrigger, SelectValue } from "@/components/ui/select";
|
||||
import { keyToTitle, slugify } from "@/utils/formatUtils/formatTextUtils";
|
||||
import { Reward, RewardType, Product, FullProduct } from "@autumn/shared";
|
||||
import {
|
||||
Reward,
|
||||
RewardType,
|
||||
Product,
|
||||
FullProduct,
|
||||
ProductV2,
|
||||
} from "@autumn/shared";
|
||||
import { useProductsContext } from "../ProductsContext";
|
||||
import { DiscountConfig } from "./DiscountConfig";
|
||||
import { notNullish } from "@/utils/genUtils";
|
||||
@@ -20,6 +26,7 @@ export const RewardConfig = ({
|
||||
setReward: (reward: Reward) => void;
|
||||
}) => {
|
||||
const { products } = useProductsContext();
|
||||
|
||||
const [idChanged, setIdChanged] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
@@ -110,10 +117,8 @@ export const RewardConfig = ({
|
||||
>
|
||||
{(() => {
|
||||
const freeAddOns = products
|
||||
.filter((product: FullProduct) => product.is_add_on)
|
||||
.filter((product: FullProduct) =>
|
||||
isFreeProduct(product.prices),
|
||||
);
|
||||
.filter((product: ProductV2) => product.is_add_on)
|
||||
.filter((product: ProductV2) => isFreeProduct(product.items));
|
||||
|
||||
const empty = freeAddOns.length === 0;
|
||||
return (
|
||||
|
||||
@@ -13,8 +13,6 @@ export const RewardsTable = () => {
|
||||
const [selectedReward, setSelectedReward] = useState<Reward | null>(null);
|
||||
const [open, setOpen] = useState(false);
|
||||
|
||||
console.log("products: ", products);
|
||||
|
||||
return (
|
||||
<>
|
||||
<UpdateReward
|
||||
|
||||
Reference in New Issue
Block a user