Path: blob/master/src/packages/util/licenses/store/compute-cost.ts
6023 views
/*1* This file is part of CoCalc: Copyright © 2022 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45import { compute_cost } from "@cocalc/util/licenses/purchase/compute-cost";6import type {7CostInputPeriod,8PurchaseInfo,9} from "@cocalc/util/licenses/purchase/types";10import { fixRange } from "@cocalc/util/licenses/purchase/purchase-info";11import type { ComputeCostProps } from "@cocalc/util/upgrades/shopping";12import { CURRENT_VERSION } from "@cocalc/util/licenses/purchase/consts";13import { decimalMultiply } from "@cocalc/util/stripe/calc";1415function computeCashVoucherPrice(props: ComputeCostProps) {16if (props.type != "cash-voucher") {17throw Error("BUG");18}19const cost_per_unit = props.whenPay == "admin" ? 0 : props.amount;20const quantity = props.numVouchers ?? 1;21const cost = decimalMultiply(cost_per_unit, quantity);22return {23// a lot of this is mainly for typescript.24cost,25cost_per_unit,26input: {27...props,28subscription: "no",29},30period: "range",31cost_per_project_per_month: 0,32cost_sub_month: 0,33cost_sub_year: 0,34quantity,35} as const;36}3738export function computeCost(39props: ComputeCostProps,40noRangeShift?: boolean,41): CostInputPeriod | undefined {42const type = props.type ?? "quota";43switch (type) {44case "cash-voucher":45return computeCashVoucherPrice(props);4647case "disk":48case "vm":49throw Error(`computing cost of item of type ${type} is deprecated`);5051case "quota":52default:53if (54props.type == "disk" ||55props.type == "vm" ||56props.type == "cash-voucher"57) {58throw Error("must be a quota upgrade license");59}60const {61user,62run_limit,63period,64range,65ram,66cpu,67disk,68always_running,69member,70uptime,71boost = false, // if true, allow "all zero" values and start at 0 USD72} = props;7374if (period == "range" && range?.[1] == null) {75return undefined;76}7778if (run_limit == null) {79return undefined;80}8182const input: PurchaseInfo = {83version: CURRENT_VERSION,84type: "quota",85user,86upgrade: "custom" as "custom",87quantity: run_limit,88subscription: (period == "range" ? "no" : period) as89| "no"90| "monthly"91| "yearly",92custom_ram: ram,93custom_dedicated_ram: 0,94custom_cpu: cpu,95custom_dedicated_cpu: 0,96custom_disk: disk,97custom_always_running: always_running,98custom_member: member,99custom_uptime: uptime,100boost,101// For computing the *shopping cart checkout price* of a subscription,102// we remove the endpoints data. Otherwise, compute_cost(input).cost103// returns the price for that exact interval, not the generic monthly104// cost, since compute_cost is also used for refunds/value computations105// (though we never do prorated refunds of subscriptions anymore!).106// In particular, we only include start/end dates for explicit ranges.107...(period == "range"108? fixRange(range, period, noRangeShift)109: { start: null, end: null }),110};111112return {113...compute_cost(input),114input,115period,116};117}118}119120121