From 9b5a0e117d64c4fe60cfaf8fff89dbf07fc32fa8 Mon Sep 17 00:00:00 2001 From: John Yeo Date: Wed, 20 Aug 2025 15:16:56 -0700 Subject: [PATCH] custom product free -> paid --- .../add-product/createFullCusProduct.ts | 20 ++++++++++++++++++- .../addProductFlow/handleAddProduct.ts | 2 ++ .../attachParams/getAttachParams.ts | 5 +++++ .../attach/attachUtils/getAttachBranch.ts | 8 ++++++-- shared/models/attachModels/attachBody.ts | 2 +- 5 files changed, 33 insertions(+), 4 deletions(-) diff --git a/server/src/internal/customers/add-product/createFullCusProduct.ts b/server/src/internal/customers/add-product/createFullCusProduct.ts index 40f2cd434..743008f00 100644 --- a/server/src/internal/customers/add-product/createFullCusProduct.ts +++ b/server/src/internal/customers/add-product/createFullCusProduct.ts @@ -450,7 +450,25 @@ export const createFullCusProduct = async ({ apiVersion: attachParams.apiVersion, }); - // Expire previous product if not one off + // Expire previous product if not one off and add on...? + if (!isOneOff(prices) && product.is_add_on) { + const { curSameProduct } = getExistingCusProducts({ + product, + cusProducts: attachParams.cusProducts!, + internalEntityId: attachParams.internalEntityId, + }); + + if (curSameProduct) { + await CusProductService.update({ + db, + cusProductId: curSameProduct.id, + updates: { + status: CusProductStatus.Expired, + }, + }); + } + } + if (!isOneOff(prices) && !product.is_add_on) { await expireOrDeleteCusProduct({ db, diff --git a/server/src/internal/customers/attach/attachFunctions/addProductFlow/handleAddProduct.ts b/server/src/internal/customers/attach/attachFunctions/addProductFlow/handleAddProduct.ts index 5d6b808a6..e0c454302 100644 --- a/server/src/internal/customers/attach/attachFunctions/addProductFlow/handleAddProduct.ts +++ b/server/src/internal/customers/attach/attachFunctions/addProductFlow/handleAddProduct.ts @@ -69,6 +69,8 @@ export const handleAddProduct = async ({ anchorToUnix = end * 1000; } + // Expire previous product + batchInsert.push( createFullCusProduct({ db: req.db, diff --git a/server/src/internal/customers/attach/attachUtils/attachParams/getAttachParams.ts b/server/src/internal/customers/attach/attachUtils/attachParams/getAttachParams.ts index 9870f7c63..587d6300f 100644 --- a/server/src/internal/customers/attach/attachUtils/attachParams/getAttachParams.ts +++ b/server/src/internal/customers/attach/attachUtils/attachParams/getAttachParams.ts @@ -4,6 +4,7 @@ import { processAttachBody } from "./processAttachBody.js"; import { orgToVersion } from "@/utils/versionUtils.js"; import { APIVersion } from "@autumn/shared"; import { AttachParams } from "../../../cusProducts/AttachParams.js"; +import { nullish } from "@/utils/genUtils.js"; export const getAttachParams = async ({ req, @@ -40,6 +41,10 @@ export const getAttachParams = async ({ const internalEntityId = entityId ? customer.entity?.internal_id : undefined; const { stripeCli, stripeCus, paymentMethod, now } = stripeVars; + if (nullish(attachBody.finalize_invoice)) { + attachBody.finalize_invoice = true; + } + const attachParams: AttachParams = { stripeCli, stripeCus, diff --git a/server/src/internal/customers/attach/attachUtils/getAttachBranch.ts b/server/src/internal/customers/attach/attachUtils/getAttachBranch.ts index c6e3002a2..5f63dcde4 100644 --- a/server/src/internal/customers/attach/attachUtils/getAttachBranch.ts +++ b/server/src/internal/customers/attach/attachUtils/getAttachBranch.ts @@ -177,8 +177,12 @@ const getSameProductBranch = async ({ curSameProduct = curSameProduct!; // 1. If new version? - // console.log("Current same product version:", curSameProduct.product.version); - // console.log("New Product version:", product.version); + + const curPrices = cusProductToPrices({ cusProduct: curSameProduct }); + if (isFreeProduct(curPrices)) { + return AttachBranch.MainIsFree; + } + if (curSameProduct.product.version !== product.version) { return AttachBranch.NewVersion; } diff --git a/shared/models/attachModels/attachBody.ts b/shared/models/attachModels/attachBody.ts index 2fbf3f549..ce16f22a7 100644 --- a/shared/models/attachModels/attachBody.ts +++ b/shared/models/attachModels/attachBody.ts @@ -45,7 +45,7 @@ export const AttachBodySchema = z reward: z.string().optional(), invoice: z.boolean().optional(), enable_product_immediately: z.boolean().optional(), - finalize_invoice: z.boolean().optional().default(true), + finalize_invoice: z.boolean().optional(), }) .refine( (data) => {