-
Notifications
You must be signed in to change notification settings - Fork 121
Expand file tree
/
Copy pathinventory.ts
More file actions
55 lines (51 loc) · 1.81 KB
/
inventory.ts
File metadata and controls
55 lines (51 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { getTransactionsDb } from "./db.js";
/**
* Retrieves the available inventory quantity for a given product.
*
* @param productId The ID of the product to check.
* @returns The quantity available, or undefined if the product is not found in inventory.
*/
export function getInventory(productId: string): number | undefined {
const db = getTransactionsDb();
const stmt = db.prepare(
"SELECT quantity FROM inventory WHERE product_id = ?",
);
const result = stmt.get(productId) as { quantity: number } | undefined;
return result?.quantity;
}
/**
* Attempts to reserve a specified quantity of stock for a product.
* Decrements the inventory only if sufficient stock is available.
*
* @param productId The ID of the product.
* @param quantity The amount to reserve (decrement).
* @returns True if the stock was successfully reserved, false if there was insufficient stock.
*/
export function reserveStock(productId: string, quantity: number): boolean {
const db = getTransactionsDb();
// Use a transaction to ensure atomicity if needed, but a single update statement is atomic in SQLite.
// We want to decrement quantity only if we have enough.
const stmt = db.prepare(`
UPDATE inventory
SET quantity = quantity - ?
WHERE product_id = ? AND quantity >= ?
`);
const info = stmt.run(quantity, productId, quantity);
return info.changes > 0;
}
/**
* Releases reserved stock back to the inventory.
* Used for rollbacks or cancellations.
*
* @param productId The ID of the product.
* @param quantity The amount to release (increment).
*/
export function releaseStock(productId: string, quantity: number): void {
const db = getTransactionsDb();
const stmt = db.prepare(`
UPDATE inventory
SET quantity = quantity + ?
WHERE product_id = ?
`);
stmt.run(quantity, productId);
}