@stacksjs/commerce 0.75.114 → 0.75.116

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- import{db,matchedRows}from"@stacksjs/database/runtime";import{asModelRow}from"../../utils/model-row";import{parseNumberInput,parsePositiveId}from"@stacksjs/validation/input";import{restockedQuantity}from"../../inventory";import{formatDate,withLockedRow}from"@stacksjs/orm";import{adjustInventoryOnConnection}from"../../utils/inventory-adjustment";import{fetchById}from"./fetch";export async function update(id,data){try{if(!id)throw Error("Product item ID is required for update");const result=await db.updateTable("products").set({...data,updated_at:formatDate(new Date)}).where("id","=",id).returningAll().executeTakeFirst();if(!result)return;return asModelRow(result)}catch(error){if(error instanceof Error)throw TypeError(`Failed to update product item: ${error.message}`);throw error}}export async function bulkUpdate(data){if(!data.length)return 0;let updatedCount=0;try{await db.transaction(async(trx)=>{for(const item of data){if(!item.id)continue;if((await matchedRows(trx.updateTable("products").set({...item,updated_at:formatDate(new Date)}).where("id","=",item.id))).length>0)updatedCount++}});return updatedCount}catch(error){if(error instanceof Error)throw TypeError(`Failed to update product items in bulk: ${error.message}`);throw error}}export async function updateAvailability(id,isAvailable){if(!await fetchById(id))throw Error(`Product item with ID ${id} not found`);try{await db.updateTable("products").set({is_available:isAvailable,updated_at:formatDate(new Date)}).where("id","=",id).execute();return await fetchById(id)}catch(error){if(error instanceof Error)throw TypeError(`Failed to update product item availability: ${error.message}`);throw error}}export async function adjustInventory(id,delta){if(!Number.isFinite(delta)||delta===0)throw Error("[commerce/inventory] adjustInventory delta must be a non-zero finite number");if(!await adjustInventoryOnConnection(db,id,delta,formatDate(new Date)))return null;return await fetchById(id)??null}export async function adjustInventoryMany(updates){if(updates.length===0)return{ok:!0,products:[]};try{return{ok:!0,products:await db.transaction(async(trx)=>{const out=[];for(let i=0;i<updates.length;i++){const{id,delta}=updates[i];if(!Number.isFinite(delta)||delta===0)throw Error("[commerce/inventory] adjustInventoryMany delta must be a non-zero finite number");if(!await adjustInventoryOnConnection(trx,id,delta,formatDate(new Date))){const reason=await trx.selectFrom("products").where("id","=",id).select("id").executeTakeFirst()?"out-of-stock":"not-found";throw Object.assign(Error(`adjustInventoryMany failed at index ${i}: ${reason}`),{__batchFail:!0,failedAt:i,productId:id,reason})}const refreshed=await trx.selectFrom("products").where("id","=",id).selectAll().executeTakeFirst();if(refreshed)out.push(refreshed)}return out})}}catch(err){if(err?.__batchFail)return{ok:!1,failedAt:err.failedAt,productId:err.productId,reason:err.reason};throw err}}export async function updateInventory(id,inventoryCount){const productId=parsePositiveId(id),quantity=inventoryCount===void 0?void 0:parseNumberInput(inventoryCount,"inventoryCount",0,2147483647,!0),product=await withLockedRow("products",{id:productId},async(row,tx)=>{if(quantity===void 0)return asModelRow(row);await tx.updateTable("products").set({inventory_count:quantity,updated_at:formatDate(new Date)}).where("id","=",productId).execute();const updated=await tx.selectFrom("products").selectAll().where("id","=",productId).executeTakeFirst();if(!updated)throw Error("Updated product disappeared");return asModelRow(updated)});if(!product)throw Error(`Product item with ID ${id} not found`);return product}export async function restock(id,units){const productId=parsePositiveId(id);return await withLockedRow("products",{id:productId},async(row,tx)=>{const quantity=restockedQuantity(row.inventory_count,units);await tx.updateTable("products").set({inventory_count:quantity,updated_at:formatDate(new Date)}).where("id","=",productId).execute();const updated=await tx.selectFrom("products").selectAll().where("id","=",productId).executeTakeFirst();if(!updated)throw Error("Restocked product disappeared");return asModelRow(updated)})??void 0}
1
+ import{db,lockRows,matchedRows}from"@stacksjs/database/runtime";import{asModelRow}from"../../utils/model-row";import{parseNumberInput,parsePositiveId}from"@stacksjs/validation/input";import{restockedQuantity}from"../../inventory";import{formatDate,withLockedRow}from"@stacksjs/orm";import{adjustInventoryOnConnection}from"../../utils/inventory-adjustment";import{fetchById}from"./fetch";export async function update(id,data){try{if(!id)throw Error("Product item ID is required for update");const result=await db.updateTable("products").set({...data,updated_at:formatDate(new Date)}).where("id","=",id).returningAll().executeTakeFirst();if(!result)return;return asModelRow(result)}catch(error){if(error instanceof Error)throw TypeError(`Failed to update product item: ${error.message}`);throw error}}export async function bulkUpdate(data){if(!data.length)return 0;let updatedCount=0;try{await db.transaction(async(trx)=>{for(const item of data){if(!item.id)continue;if((await matchedRows(trx.updateTable("products").set({...item,updated_at:formatDate(new Date)}).where("id","=",item.id))).length>0)updatedCount++}});return updatedCount}catch(error){if(error instanceof Error)throw TypeError(`Failed to update product items in bulk: ${error.message}`);throw error}}export async function updateAvailability(id,isAvailable){if(!await fetchById(id))throw Error(`Product item with ID ${id} not found`);try{await db.updateTable("products").set({is_available:isAvailable,updated_at:formatDate(new Date)}).where("id","=",id).execute();return await fetchById(id)}catch(error){if(error instanceof Error)throw TypeError(`Failed to update product item availability: ${error.message}`);throw error}}export async function adjustInventory(id,delta){if(!Number.isFinite(delta)||delta===0)throw Error("[commerce/inventory] adjustInventory delta must be a non-zero finite number");if(!await adjustInventoryOnConnection(db,id,delta,formatDate(new Date)))return null;return await fetchById(id)??null}export async function adjustInventoryMany(updates){if(updates.length===0)return{ok:!0,products:[]};try{return{ok:!0,products:await db.transaction(async(trx)=>{await lockRows(trx,"products",updates.map((update)=>update.id));const out=[];for(let i=0;i<updates.length;i++){const{id,delta}=updates[i];if(!Number.isFinite(delta)||delta===0)throw Error("[commerce/inventory] adjustInventoryMany delta must be a non-zero finite number");if(!await adjustInventoryOnConnection(trx,id,delta,formatDate(new Date))){const reason=await trx.selectFrom("products").where("id","=",id).select("id").executeTakeFirst()?"out-of-stock":"not-found";throw Object.assign(Error(`adjustInventoryMany failed at index ${i}: ${reason}`),{__batchFail:!0,failedAt:i,productId:id,reason})}const refreshed=await trx.selectFrom("products").where("id","=",id).selectAll().executeTakeFirst();if(refreshed)out.push(refreshed)}return out})}}catch(err){if(err?.__batchFail)return{ok:!1,failedAt:err.failedAt,productId:err.productId,reason:err.reason};throw err}}export async function updateInventory(id,inventoryCount){const productId=parsePositiveId(id),quantity=inventoryCount===void 0?void 0:parseNumberInput(inventoryCount,"inventoryCount",0,2147483647,!0),product=await withLockedRow("products",{id:productId},async(row,tx)=>{if(quantity===void 0)return asModelRow(row);await tx.updateTable("products").set({inventory_count:quantity,updated_at:formatDate(new Date)}).where("id","=",productId).execute();const updated=await tx.selectFrom("products").selectAll().where("id","=",productId).executeTakeFirst();if(!updated)throw Error("Updated product disappeared");return asModelRow(updated)});if(!product)throw Error(`Product item with ID ${id} not found`);return product}export async function restock(id,units){const productId=parsePositiveId(id);return await withLockedRow("products",{id:productId},async(row,tx)=>{const quantity=restockedQuantity(row.inventory_count,units);await tx.updateTable("products").set({inventory_count:quantity,updated_at:formatDate(new Date)}).where("id","=",productId).execute();const updated=await tx.selectFrom("products").selectAll().where("id","=",productId).executeTakeFirst();if(!updated)throw Error("Restocked product disappeared");return asModelRow(updated)})??void 0}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@stacksjs/commerce",
3
3
  "type": "module",
4
- "version": "0.75.114",
4
+ "version": "0.75.116",
5
5
  "description": "Stacks commerce utilities.",
6
6
  "author": "Chris Breuer",
7
7
  "contributors": [
@@ -57,18 +57,18 @@
57
57
  "prepublishOnly": "bun run build"
58
58
  },
59
59
  "dependencies": {
60
- "@stacksjs/cache": "0.75.114",
61
- "@stacksjs/config": "0.75.114",
62
- "@stacksjs/database": "0.75.114",
63
- "@stacksjs/env": "0.75.114",
64
- "@stacksjs/error-handling": "0.75.114",
65
- "@stacksjs/events": "0.75.114",
66
- "@stacksjs/logging": "0.75.114",
67
- "@stacksjs/orm": "0.75.114",
68
- "@stacksjs/payments": "0.75.114",
69
- "@stacksjs/realtime": "0.75.114",
70
- "@stacksjs/strings": "0.75.114",
71
- "@stacksjs/validation": "0.75.114",
60
+ "@stacksjs/cache": "0.75.116",
61
+ "@stacksjs/config": "0.75.116",
62
+ "@stacksjs/database": "0.75.116",
63
+ "@stacksjs/env": "0.75.116",
64
+ "@stacksjs/error-handling": "0.75.116",
65
+ "@stacksjs/events": "0.75.116",
66
+ "@stacksjs/logging": "0.75.116",
67
+ "@stacksjs/orm": "0.75.116",
68
+ "@stacksjs/payments": "0.75.116",
69
+ "@stacksjs/realtime": "0.75.116",
70
+ "@stacksjs/strings": "0.75.116",
71
+ "@stacksjs/validation": "0.75.116",
72
72
  "ts-spreadsheets": "^0.2.0"
73
73
  },
74
74
  "devDependencies": {