dealing with custom product adding

This commit is contained in:
Ayush
2025-01-24 20:46:25 +00:00
parent 48f122499b
commit 35e7d0f98e
6 changed files with 75 additions and 1609 deletions

View File

@@ -85,23 +85,53 @@ export default function CustomerProductView({
const foundProduct = data.products.find((p) => p.id === product_id);
if (!foundProduct) return;
const customerOptions =
data.customer.products.find((p) => p.product_id === product_id)
?.options ?? [];
setOptions(customerOptions);
const enrichedProduct = {
...foundProduct,
isActive: data.customer.products.some(
(p) => p.product_id === product_id && p.status === "active"
),
options: customerOptions,
};
const customerProduct = data.customer.products.find(
(p) => p.product_id === product_id
);
// console.log("foundProduct", foundProduct);
console.log("customerProduc1t", customerProduct);
const enrichedProduct = enrichProduct(foundProduct, customerProduct);
setOptions(enrichedProduct.options);
setProduct(enrichedProduct);
initialProductRef.current = enrichedProduct;
}, [data, product_id]);
// Pure function to handle product enrichment
const enrichProduct = (
baseProduct: FrontendProduct,
customerProduct?: {
status?: string;
options?: OptionValue[];
entitlements?: typeof baseProduct.entitlements;
prices?: typeof baseProduct.prices;
}
) => {
if (!customerProduct) {
console.log("baseProduct", baseProduct);
return {
...baseProduct,
isActive: false,
options: [],
};
}
console.log("baseProduct", baseProduct);
console.log("customerProduct", customerProduct);
return {
...baseProduct,
isActive: customerProduct.status === "active",
options: customerProduct.options ?? [],
...(customerProduct.entitlements && {
entitlements: customerProduct.entitlements,
}),
...(customerProduct.prices && {
prices: customerProduct.prices,
}),
};
};
//check if the user has made changes to the product state
useEffect(() => {
if (!initialProductRef.current || !product) {

View File

@@ -6,11 +6,14 @@ import React from "react";
function LoadingScreen() {
const texts = [
"Counting pennies",
"Forging products",
"Forging products",
"Increasing ARR",
"Optimizing pricing",
"Blasting competitors",
"Shipping faster",
"Stopping churn"
];
const [loadingText, setLoadingText] = React.useState(texts[0]);
const [loadingText, setLoadingText] = React.useState(texts[Math.floor(Math.random() * texts.length)]);
React.useEffect(() => {
let currentIndex = 0;

1590
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -21,6 +21,7 @@ import { handleAddProduct } from "@/internal/customers/add-product/handleAddProd
import { ErrorMessages } from "@/errors/errMessages.js";
import { CusProductService } from "@/internal/customers/products/CusProductService.js";
import {
getBillingType,
getEntOptions,
getPriceEntitlement,
haveDifferentRecurringIntervals,
@@ -59,9 +60,10 @@ const checkAddProductErrors = async ({
}
// 2. Check if options are valid
for (const price of prices) {
if (price.billing_type === BillingType.UsageInAdvance) {
for (const price of prices) {
const billingType = getBillingType(price.config!);
if (billingType === BillingType.UsageInAdvance) {
// Get options for price
let priceEnt = getPriceEntitlement(price, entitlements);
let options = getEntOptions(optionsList, priceEnt);
@@ -72,7 +74,7 @@ const checkAddProductErrors = async ({
statusCode: 400,
});
}
} else if (price.billing_type === BillingType.UsageBelowThreshold) {
} else if (billingType === BillingType.UsageBelowThreshold) {
let priceEnt = getPriceEntitlement(price, entitlements);
let options = getEntOptions(optionsList, priceEnt);

View File

@@ -255,6 +255,7 @@ export const getDefaultAndCustomEnts = ({
const customEnts: any = [];
for (const ent of entsInput) {
// 1. Validate entitlement
if (!validateEntitlement(ent, features)) {
@@ -301,13 +302,16 @@ export const getDefaultAndCustomEnts = ({
allowance: ent.allowance,
interval: ent.interval,
});
defaultEnts = defaultEnts.filter(
(e) => e.feature_id != existingEnt.feature_id
);
}
defaultEnts = defaultEnts.filter(
(e) => e.feature_id != existingEnt.feature_id
);
}
return { defaultEnts, customEnts };
};

View File

@@ -9,6 +9,8 @@ import {
Entitlement,
EntitlementWithFeature,
FeatureOptions,
FullCustomerEntitlement,
FullCustomerPrice,
} from "@autumn/shared";
export const cusRouter = Router();
@@ -21,6 +23,8 @@ cusRouter.get("", async (req: any, res: any) => {
req.env,
page
);
res.status(200).send({ customers, totalCount: count });
});
@@ -56,6 +60,17 @@ cusRouter.get("/:customer_id/data", async (req: any, res: any) => {
customerId: customer_id,
});
for (const product of customer.products) {
product.entitlements = product.customer_entitlements.map((cusEnt: FullCustomerEntitlement) => {
return cusEnt.entitlement;
});
product.prices = product.customer_prices.map((cusPrice: FullCustomerPrice) => {
return cusPrice.price;
});
}
console.log("customer", customer);
// Get customer invoices
const invoices = await InvoiceService.getInvoices({
sb,