Skip to content

Commit

Permalink
fix fallout of b546a4d
Browse files Browse the repository at this point in the history
  • Loading branch information
haraldschilly committed Jul 10, 2024
1 parent 3bcabeb commit 5d57624
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/packages/next/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
"@cocalc/util": "workspace:*",
"@openapitools/openapi-generator-cli": "^2.13.4",
"@types/express": "^4.17.13",
"@types/pg": "8.11.6",
"@types/pg": "^8.11.6",
"@types/react": "^18.0.26",
"@types/react-dom": "^18.0.10",
"@vscode/vscode-languagedetection": "^1.0.22",
Expand Down
18 changes: 9 additions & 9 deletions src/packages/server/shopping/cart/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@ checking periodically, then blacklisting...? This isn't something of
any value to a spammer so it's very unlikely to be exploited maliciously.
*/

import { isValidUUID } from "@cocalc/util/misc";
import getPool from "@cocalc/database/pool";
import {
ProductType,
ProductDescription,
ProductType,
} from "@cocalc/util/db-schema/shopping-cart-items";
import { isValidUUID } from "@cocalc/util/misc";
import { getItem } from "./get";

export default async function addToCart(
account_id: string,
product: ProductType,
description?: ProductDescription,
project_id?: string
project_id?: string,
): Promise<number> {
if (!isValidUUID(account_id)) {
throw Error("account_id is invalid");
Expand All @@ -37,32 +37,32 @@ export default async function addToCart(
const { rowCount } = await pool.query(
`INSERT INTO shopping_cart_items (account_id, added, product, description, checked, project_id)
VALUES($1, NOW(), $2, $3, true, $4)`,
[account_id, product, description, project_id]
[account_id, product, description, project_id],
);
return rowCount;
return rowCount ?? 0;
}

// Puts an item back in the cart that was removed.
// - Mutates item that was actually removed and not purchased.
export async function putBackInCart(
account_id: string,
id: number
id: number,
): Promise<number> {
if (!isValidUUID(account_id)) {
throw Error("account_id is invalid");
}
const pool = getPool();
const { rowCount } = await pool.query(
"UPDATE shopping_cart_items SET removed=NULL, checked=TRUE WHERE account_id=$1 AND id=$2 AND removed IS NOT NULL AND purchased IS NULL",
[account_id, id]
[account_id, id],
);
return rowCount;
return rowCount ?? 0;
}

// Makes copy of item that was purchased and puts it in the cart.
export async function buyItAgain(
account_id: string,
id: number
id: number,
): Promise<number> {
if (!isValidUUID(account_id)) {
throw Error("account_id is invalid");
Expand Down
4 changes: 2 additions & 2 deletions src/packages/server/shopping/cart/checked.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import getPool from "@cocalc/database/pool";
export default async function setCheck(
account_id: string,
checked: boolean,
id?: number
id?: number,
): Promise<number> {
if (!isValidUUID(account_id)) {
throw Error("account_id is invalid");
Expand All @@ -28,5 +28,5 @@ export default async function setCheck(
}

const { rowCount } = await pool.query(query, params);
return rowCount;
return rowCount ?? 0;
}
6 changes: 3 additions & 3 deletions src/packages/server/shopping/cart/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ import getPool from "@cocalc/database/pool";
// Returns number of items "deleted".
export default async function deleteItem(
account_id: string,
id: number
id: number,
): Promise<number> {
if (!isValidUUID(account_id)) {
throw Error("account_id is invalid");
}
const pool = getPool();
const { rowCount } = await pool.query(
"DELETE FROM shopping_cart_items WHERE account_id=$1 AND id=$2 AND purchased IS NULL",
[account_id, id]
[account_id, id],
);
return rowCount;
return rowCount ?? 0;
}
2 changes: 1 addition & 1 deletion src/packages/server/shopping/cart/edit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,5 @@ export default async function editCart({
query += " AND purchased IS NULL";

const { rowCount } = await pool.query(query, params);
return rowCount;
return rowCount ?? 0;
}
2 changes: 1 addition & 1 deletion src/packages/server/shopping/cart/recent-purchases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export default async function getRecentPurchases({
const pool = getPool();
const { rows } = await pool.query(
`SELECT * FROM shopping_cart_items WHERE account_id=$1 AND purchased IS NOT NULL AND (purchased#>>'{time}')::timestamptz >= NOW() - $2::interval AND purchased#>>'{voucher_id}' IS NULL`,
[account_id, recent ?? "1 week"]
[account_id, recent ?? "1 week"],
);
rows.sort((a, b) => -cmp(a.purchased?.time, b.purchased?.time));
return rows;
Expand Down
7 changes: 3 additions & 4 deletions src/packages/server/shopping/cart/remove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,15 @@ import getPool from "@cocalc/database/pool";
// You can't remove an item more than once from a cart.
export default async function removeFromCart(
account_id: string,
id: number
id: number,
): Promise<number> {
if (!isValidUUID(account_id)) {
throw Error("account_id is invalid");
}
const pool = getPool();
const { rowCount } = await pool.query(
"UPDATE shopping_cart_items SET removed=NOW() WHERE account_id=$1 AND id=$2 AND removed IS NULL AND purchased IS NULL",
[account_id, id]
[account_id, id],
);
return rowCount;
return rowCount ?? 0;
}

0 comments on commit 5d57624

Please sign in to comment.