Skip to content

Commit 01367a9

Browse files
committed
pricing control -- make version required field for PurchaseInfo
1 parent 9f62a5c commit 01367a9

File tree

5 files changed

+38
-10
lines changed

5 files changed

+38
-10
lines changed

src/packages/frontend/site-licenses/purchase/purchase.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ import {
4242
discount_yearly_pct,
4343
} from "@cocalc/util/licenses/purchase/consts";
4444
import { compute_cost } from "@cocalc/util/licenses/purchase/compute-cost";
45-
import {
45+
import type {
4646
Cost,
47-
PurchaseInfoQuota,
47+
PurchaseInfo,
4848
Subscription,
4949
Upgrade,
5050
User,
@@ -71,6 +71,7 @@ import { create_quote_support_ticket } from "./get-a-quote";
7171
import { PurchaseMethod } from "./purchase-method";
7272
import { QuotaEditor } from "./quota-editor";
7373
import { RadioGroup } from "./radio-group";
74+
import { CURRENT_VERSION } from "@cocalc/util/licenses/purchase/consts";
7475

7576
const { RangePicker } = DatePicker;
7677

@@ -173,6 +174,7 @@ export const PurchaseOneLicense: React.FC<Props> = React.memo(({ onClose }) => {
173174
return undefined;
174175
}
175176
return compute_cost({
177+
version: CURRENT_VERSION,
176178
type: "quota",
177179
quantity,
178180
user,
@@ -589,7 +591,8 @@ export const PurchaseOneLicense: React.FC<Props> = React.memo(({ onClose }) => {
589591
quote == null
590592
)
591593
return;
592-
const info: PurchaseInfoQuota = {
594+
const info: PurchaseInfo = {
595+
version: CURRENT_VERSION,
593596
type: "quota",
594597
quantity,
595598
user,

src/packages/next/pages/pricing/subscriptions.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import { Customize } from "lib/customize";
3232
import withCustomize from "lib/with-customize";
3333
import { Paragraph, Title } from "components/misc";
3434
import dayjs from "dayjs";
35+
import { CURRENT_VERSION } from "@cocalc/util/licenses/purchase/consts";
3536

3637
function addMonth(date: Date): Date {
3738
return dayjs(date).add(30, "days").add(12, "hours").toDate();
@@ -64,6 +65,7 @@ const hobby: Item = (() => {
6465
} as const;
6566

6667
const info: PurchaseInfo = {
68+
version: CURRENT_VERSION,
6769
type: "quota",
6870
user: conf.user,
6971
upgrade: "custom",
@@ -109,6 +111,7 @@ const academic: Item = (() => {
109111
} as const;
110112

111113
const info: PurchaseInfo = {
114+
version: CURRENT_VERSION,
112115
type: "quota",
113116
user: conf.user,
114117
upgrade: "custom",
@@ -155,6 +158,7 @@ const business: Item = (() => {
155158
} as const;
156159

157160
const info: PurchaseInfo = {
161+
version: CURRENT_VERSION,
158162
type: "quota",
159163
user: conf.user,
160164
upgrade: "custom",

src/packages/server/prices.test.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@
66
// test product ID and pricing
77

88
import { ONE_DAY_MS } from "@cocalc/util/consts/billing";
9-
import type {
10-
PurchaseInfo,
11-
PurchaseInfoQuota,
12-
} from "@cocalc/util/licenses/purchase/types";
9+
import type { PurchaseInfo } from "@cocalc/util/licenses/purchase/types";
1310
import { compute_cost } from "@cocalc/util/licenses/purchase/compute-cost";
1411
import { round2 } from "@cocalc/util/misc";
1512
import {
@@ -31,7 +28,8 @@ describe("product id and compute cost", () => {
3128
// (since it isn't!) we set it back to run this test.
3229
// @ts-ignore
3330
COSTS.online_discount = 0.75;
34-
const info1: Omit<PurchaseInfoQuota, "quantity"> = {
31+
const info1 = {
32+
version: "1",
3533
type: "quota",
3634
user: "academic",
3735
upgrade: "custom",
@@ -75,6 +73,7 @@ describe("product id and compute cost", () => {
7573
new Date((info1.start as Date).getTime() + days * ONE_DAY_MS),
7674
),
7775
};
76+
// @ts-ignore
7877
info2.cost = compute_cost(info2);
7978
// console.log(days, info2, Math.round(info2.cost.cost_per_unit * 10000));
8079
const unit_amount = unitAmount(info2);
@@ -89,6 +88,7 @@ describe("product id and compute cost", () => {
8988
start: new Date("2022-04-28T10:08:10.072Z"),
9089
end: new Date("2022-05-05T10:08:10.072Z"),
9190
};
91+
// @ts-ignore
9292
info2.cost = compute_cost(info2);
9393
expect(unitAmount(info2)).toEqual(111);
9494
});
@@ -152,6 +152,7 @@ describe("dedicated disk", () => {
152152
it("calculates subscription price of one disk", () => {
153153
const start = startOfDay(new Date());
154154
const purchaseInfo: PurchaseInfo = {
155+
version: "1",
155156
type: "disk",
156157
start,
157158
end: dayjs(start).add(30, "days").add(12, "hours").toDate(), // adding exact amount of time to make test well defined

src/packages/server/vouchers/charge-for-unpaid-vouchers.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { getLogger } from "@cocalc/backend/logger";
2020
import { chargeUser } from "@cocalc/server/licenses/purchase/charge";
2121
import { StripeClient } from "@cocalc/server/stripe/client";
2222
import type { PurchaseInfo } from "@cocalc/util/db-schema/vouchers";
23+
import { CURRENT_VERSION } from "@cocalc/util/licenses/purchase/consts";
2324

2425
const log = getLogger("charge-for-unpaid-vouchers");
2526

@@ -34,7 +35,7 @@ export default async function chargeForUnpaidVouchers(): Promise<Result> {
3435

3536
const pool = getPool();
3637
const { rows } = await pool.query(
37-
"SELECT id, cost, tax, created_by, title FROM vouchers WHERE purchased is NULL AND when_pay='invoice' AND expire < NOW()"
38+
"SELECT id, cost, tax, created_by, title FROM vouchers WHERE purchased is NULL AND when_pay='invoice' AND expire < NOW()",
3839
);
3940
const result: Result = {};
4041
for (const row of rows) {
@@ -67,7 +68,7 @@ async function chargeForUnpaidVoucher({
6768
const pool = getPool();
6869
const { rows } = await pool.query(
6970
"SELECT COUNT(*) AS quantity FROM voucher_codes WHERE id=$1 AND when_redeemed IS NOT NULL AND canceled IS NULL",
70-
[id]
71+
[id],
7172
);
7273
const quantity = rows[0].quantity;
7374
let purchased: PurchaseInfo;
@@ -76,6 +77,8 @@ async function chargeForUnpaidVoucher({
7677
} else {
7778
const stripe = new StripeClient({ account_id: created_by });
7879
const info = {
80+
// version doesn't really matter since the cost is already explicitly given below
81+
version: CURRENT_VERSION,
7982
type: "vouchers",
8083
quantity,
8184
cost,

src/packages/util/licenses/purchase/cost-versions.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,23 @@ const COST = {
5050
ALWAYS_RUNNING_FACTOR: 2,
5151
},
5252

53+
2: {
54+
SUB_DISCOUNT: { no: 1, monthly: 0.85, yearly: 0.8 },
55+
GCE_COSTS: {
56+
ram: 0.67,
57+
cpu: 5,
58+
disk: 0.05,
59+
non_pre_factor: 3.5,
60+
},
61+
COST_MULTIPLIER: 1.2,
62+
NONMEMBER_DENSITY: 2,
63+
ACADEMIC_DISCOUNT: 0.6,
64+
DISK_FACTOR: 20,
65+
RAM_OVERCOMMIT: 5,
66+
CPU_OVERCOMMIT: 10,
67+
ALWAYS_RUNNING_FACTOR: 2,
68+
},
69+
5370
// this version is PURELY for testing purposes
5471
test_1: {
5572
SUB_DISCOUNT: { no: 1, monthly: 0.9, yearly: 0.85 },

0 commit comments

Comments
 (0)