Path: blob/master/src/packages/util/licenses/store/compute-cost.ts
1450 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}7778const input: PurchaseInfo = {79version: CURRENT_VERSION,80type: "quota",81user,82upgrade: "custom" as "custom",83quantity: run_limit,84subscription: (period == "range" ? "no" : period) as85| "no"86| "monthly"87| "yearly",88custom_ram: ram,89custom_dedicated_ram: 0,90custom_cpu: cpu,91custom_dedicated_cpu: 0,92custom_disk: disk,93custom_always_running: always_running,94custom_member: member,95custom_uptime: uptime,96boost,97// For computing the *shopping cart checkout price* of a subscription,98// we remove the endpoints data. Otherwise, compute_cost(input).cost99// returns the price for that exact interval, not the generic monthly100// cost, since compute_cost is also used for refunds/value computations101// (though we never do prorated refunds of subscriptions anymore!).102// In particular, we only include start/end dates for explicit ranges.103...(period == "range"104? fixRange(range, period, noRangeShift)105: { start: null, end: null }),106};107return {108...compute_cost(input),109input,110period,111};112}113}114115116