From 4c6270eef4ff93b5ca8b486a4cddf7dac23d8f8b Mon Sep 17 00:00:00 2001 From: Harald Schilly Date: Wed, 10 Jul 2024 15:18:29 +0200 Subject: [PATCH] fix fallout of b546a4d268e5560 --- src/packages/server/shopping/cart/add.ts | 18 +++++++++--------- src/packages/server/shopping/cart/checked.ts | 4 ++-- src/packages/server/shopping/cart/delete.ts | 6 +++--- src/packages/server/shopping/cart/edit.ts | 2 +- .../server/shopping/cart/recent-purchases.ts | 2 +- src/packages/server/shopping/cart/remove.ts | 7 +++---- 6 files changed, 19 insertions(+), 20 deletions(-) diff --git a/src/packages/server/shopping/cart/add.ts b/src/packages/server/shopping/cart/add.ts index ffb223f87f2..a58b7614ea1 100644 --- a/src/packages/server/shopping/cart/add.ts +++ b/src/packages/server/shopping/cart/add.ts @@ -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 { if (!isValidUUID(account_id)) { throw Error("account_id is invalid"); @@ -37,16 +37,16 @@ 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 { if (!isValidUUID(account_id)) { throw Error("account_id is invalid"); @@ -54,15 +54,15 @@ export async function putBackInCart( 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 { if (!isValidUUID(account_id)) { throw Error("account_id is invalid"); diff --git a/src/packages/server/shopping/cart/checked.ts b/src/packages/server/shopping/cart/checked.ts index d76c42d0fc7..c5140eab9d7 100644 --- a/src/packages/server/shopping/cart/checked.ts +++ b/src/packages/server/shopping/cart/checked.ts @@ -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 { if (!isValidUUID(account_id)) { throw Error("account_id is invalid"); @@ -28,5 +28,5 @@ export default async function setCheck( } const { rowCount } = await pool.query(query, params); - return rowCount; + return rowCount ?? 0; } diff --git a/src/packages/server/shopping/cart/delete.ts b/src/packages/server/shopping/cart/delete.ts index 16cb4b9529d..502b33c1085 100644 --- a/src/packages/server/shopping/cart/delete.ts +++ b/src/packages/server/shopping/cart/delete.ts @@ -16,7 +16,7 @@ import getPool from "@cocalc/database/pool"; // Returns number of items "deleted". export default async function deleteItem( account_id: string, - id: number + id: number, ): Promise { if (!isValidUUID(account_id)) { throw Error("account_id is invalid"); @@ -24,7 +24,7 @@ export default async function deleteItem( 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; } diff --git a/src/packages/server/shopping/cart/edit.ts b/src/packages/server/shopping/cart/edit.ts index 46e79b903a0..bb4c22dfb7f 100644 --- a/src/packages/server/shopping/cart/edit.ts +++ b/src/packages/server/shopping/cart/edit.ts @@ -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; } diff --git a/src/packages/server/shopping/cart/recent-purchases.ts b/src/packages/server/shopping/cart/recent-purchases.ts index f89994d65d2..d0b3a7705f7 100644 --- a/src/packages/server/shopping/cart/recent-purchases.ts +++ b/src/packages/server/shopping/cart/recent-purchases.ts @@ -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; diff --git a/src/packages/server/shopping/cart/remove.ts b/src/packages/server/shopping/cart/remove.ts index f441b48a531..d36c89f5f28 100644 --- a/src/packages/server/shopping/cart/remove.ts +++ b/src/packages/server/shopping/cart/remove.ts @@ -15,7 +15,7 @@ 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 { if (!isValidUUID(account_id)) { throw Error("account_id is invalid"); @@ -23,8 +23,7 @@ export default async function removeFromCart( 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; } -