@stacksjs/commerce 0.70.55 → 0.70.57
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.
- package/package.json +10 -7
- package/src/carts/destroy.ts +0 -54
- package/src/carts/fetch.ts +0 -20
- package/src/carts/index.ts +0 -13
- package/src/carts/store.ts +0 -75
- package/src/carts/update.ts +0 -78
- package/src/coupons/destroy.ts +0 -49
- package/src/coupons/fetch.ts +0 -510
- package/src/coupons/index.ts +0 -30
- package/src/coupons/store.ts +0 -46
- package/src/coupons/update.ts +0 -147
- package/src/customers/destroy.ts +0 -62
- package/src/customers/fetch.ts +0 -20
- package/src/customers/index.ts +0 -17
- package/src/customers/store.ts +0 -45
- package/src/customers/update.ts +0 -44
- package/src/devices/destroy.ts +0 -54
- package/src/devices/export.ts +0 -98
- package/src/devices/fetch.ts +0 -125
- package/src/devices/index.ts +0 -31
- package/src/devices/store.ts +0 -82
- package/src/devices/update.ts +0 -125
- package/src/errors/destroy.ts +0 -54
- package/src/errors/fetch.ts +0 -197
- package/src/errors/index.ts +0 -28
- package/src/errors/update.ts +0 -116
- package/src/gift-cards/destroy.ts +0 -87
- package/src/gift-cards/fetch.ts +0 -431
- package/src/gift-cards/index.ts +0 -26
- package/src/gift-cards/store.ts +0 -56
- package/src/gift-cards/update.ts +0 -102
- package/src/index.ts +0 -78
- package/src/orders/destroy.ts +0 -111
- package/src/orders/events.ts +0 -121
- package/src/orders/export.ts +0 -158
- package/src/orders/fetch.ts +0 -379
- package/src/orders/guards.ts +0 -261
- package/src/orders/index.ts +0 -91
- package/src/orders/place-order.ts +0 -274
- package/src/orders/store.ts +0 -51
- package/src/orders/totals.ts +0 -190
- package/src/orders/update.ts +0 -164
- package/src/orders/webhook.ts +0 -312
- package/src/payments/destroy.ts +0 -56
- package/src/payments/fetch.ts +0 -288
- package/src/payments/index.ts +0 -16
- package/src/payments/store.ts +0 -63
- package/src/payments/update.ts +0 -52
- package/src/product.ts +0 -0
- package/src/products/categories/destroy.ts +0 -103
- package/src/products/categories/fetch.ts +0 -285
- package/src/products/categories/index.ts +0 -36
- package/src/products/categories/store.ts +0 -87
- package/src/products/categories/update.ts +0 -251
- package/src/products/index.ts +0 -35
- package/src/products/items/destroy.ts +0 -54
- package/src/products/items/fetch.ts +0 -282
- package/src/products/items/index.ts +0 -26
- package/src/products/items/store.ts +0 -84
- package/src/products/items/update.ts +0 -324
- package/src/products/manufacturers/destroy.ts +0 -56
- package/src/products/manufacturers/fetch.ts +0 -117
- package/src/products/manufacturers/index.ts +0 -24
- package/src/products/manufacturers/store.ts +0 -82
- package/src/products/manufacturers/update.ts +0 -113
- package/src/products/reviews/destroy.ts +0 -56
- package/src/products/reviews/fetch.ts +0 -63
- package/src/products/reviews/index.ts +0 -25
- package/src/products/reviews/store.ts +0 -46
- package/src/products/reviews/update.ts +0 -82
- package/src/products/units/destroy.ts +0 -56
- package/src/products/units/fetch.ts +0 -20
- package/src/products/units/index.ts +0 -13
- package/src/products/units/store.ts +0 -162
- package/src/products/units/update.ts +0 -159
- package/src/products/variants/destroy.ts +0 -56
- package/src/products/variants/fetch.ts +0 -20
- package/src/products/variants/index.ts +0 -21
- package/src/products/variants/store.ts +0 -136
- package/src/products/variants/update.ts +0 -104
- package/src/receipts/destroy.ts +0 -54
- package/src/receipts/export.ts +0 -0
- package/src/receipts/fetch.ts +0 -222
- package/src/receipts/index.ts +0 -24
- package/src/receipts/printer.ts +0 -69
- package/src/receipts/store.ts +0 -69
- package/src/receipts/update.ts +0 -124
- package/src/shippings/delivery-routes/destroy.ts +0 -54
- package/src/shippings/delivery-routes/fetch.ts +0 -42
- package/src/shippings/delivery-routes/index.ts +0 -26
- package/src/shippings/delivery-routes/store.ts +0 -69
- package/src/shippings/delivery-routes/update.ts +0 -120
- package/src/shippings/digital-deliveries/destroy.ts +0 -111
- package/src/shippings/digital-deliveries/fetch.ts +0 -20
- package/src/shippings/digital-deliveries/index.ts +0 -26
- package/src/shippings/digital-deliveries/store.ts +0 -76
- package/src/shippings/digital-deliveries/update.ts +0 -129
- package/src/shippings/drivers/destroy.ts +0 -78
- package/src/shippings/drivers/fetch.ts +0 -20
- package/src/shippings/drivers/index.ts +0 -24
- package/src/shippings/drivers/store.ts +0 -69
- package/src/shippings/drivers/update.ts +0 -117
- package/src/shippings/index.ts +0 -39
- package/src/shippings/license-keys/destroy.ts +0 -111
- package/src/shippings/license-keys/fetch.ts +0 -20
- package/src/shippings/license-keys/index.ts +0 -26
- package/src/shippings/license-keys/store.ts +0 -77
- package/src/shippings/license-keys/update.ts +0 -117
- package/src/shippings/shipping-methods/destroy.ts +0 -111
- package/src/shippings/shipping-methods/fetch.ts +0 -62
- package/src/shippings/shipping-methods/index.ts +0 -28
- package/src/shippings/shipping-methods/store.ts +0 -123
- package/src/shippings/shipping-methods/update.ts +0 -120
- package/src/shippings/shipping-rates/destroy.ts +0 -102
- package/src/shippings/shipping-rates/fetch.ts +0 -320
- package/src/shippings/shipping-rates/index.ts +0 -36
- package/src/shippings/shipping-rates/store.ts +0 -102
- package/src/shippings/shipping-rates/update.ts +0 -139
- package/src/shippings/shipping-zones/destroy.ts +0 -111
- package/src/shippings/shipping-zones/fetch.ts +0 -96
- package/src/shippings/shipping-zones/index.ts +0 -26
- package/src/shippings/shipping-zones/store.ts +0 -74
- package/src/shippings/shipping-zones/update.ts +0 -156
- package/src/tax/destroy.ts +0 -54
- package/src/tax/fetch.ts +0 -20
- package/src/tax/index.ts +0 -24
- package/src/tax/store.ts +0 -69
- package/src/tax/update.ts +0 -119
- package/src/tests/coupons.test.ts +0 -90
- package/src/tests/customers.test.ts +0 -39
- package/src/tests/device.test.ts +0 -111
- package/src/tests/digitals.test.ts +0 -31
- package/src/tests/drivers.test.ts +0 -25
- package/src/tests/gift-cards.test.ts +0 -36
- package/src/tests/license.test.ts +0 -31
- package/src/tests/manufacturers.test.ts +0 -80
- package/src/tests/orders.test.ts +0 -25
- package/src/tests/payments.test.ts +0 -75
- package/src/tests/rates.test.ts +0 -367
- package/src/tests/receipts.test.ts +0 -287
- package/src/tests/restaurant.test.ts +0 -522
- package/src/tests/reviews.test.ts +0 -165
- package/src/tests/routes.test.ts +0 -51
- package/src/tests/setup.ts +0 -149
- package/src/tests/shipping.test.ts +0 -88
- package/src/tests/tax.test.ts +0 -25
- package/src/tests/units.test.ts +0 -110
- package/src/tests/variants.test.ts +0 -133
- package/src/tests/waitlist.test.ts +0 -303
- package/src/tests/zones.test.ts +0 -86
- package/src/types.ts +0 -312
- package/src/utils/typed-stats.ts +0 -67
- package/src/waitlists/index.ts +0 -19
- package/src/waitlists/products/destroy.ts +0 -65
- package/src/waitlists/products/export.ts +0 -113
- package/src/waitlists/products/fetch.ts +0 -362
- package/src/waitlists/products/index.ts +0 -33
- package/src/waitlists/products/store.ts +0 -112
- package/src/waitlists/products/update.ts +0 -125
- package/src/waitlists/restaurant/destroy.ts +0 -65
- package/src/waitlists/restaurant/export.ts +0 -107
- package/src/waitlists/restaurant/fetch.ts +0 -538
- package/src/waitlists/restaurant/index.ts +0 -43
- package/src/waitlists/restaurant/store.ts +0 -109
- package/src/waitlists/restaurant/update.ts +0 -198
- /package/dist/{src/coupons → coupons}/destroy.d.ts +0 -0
- /package/dist/{src/coupons → coupons}/fetch.d.ts +0 -0
- /package/dist/{src/coupons → coupons}/index.d.ts +0 -0
- /package/dist/{src/coupons → coupons}/store.d.ts +0 -0
- /package/dist/{src/coupons → coupons}/update.d.ts +0 -0
- /package/dist/{src/customers → customers}/destroy.d.ts +0 -0
- /package/dist/{src/customers → customers}/fetch.d.ts +0 -0
- /package/dist/{src/customers → customers}/index.d.ts +0 -0
- /package/dist/{src/customers → customers}/store.d.ts +0 -0
- /package/dist/{src/customers → customers}/update.d.ts +0 -0
- /package/dist/{src/devices → devices}/destroy.d.ts +0 -0
- /package/dist/{src/devices → devices}/export.d.ts +0 -0
- /package/dist/{src/devices → devices}/fetch.d.ts +0 -0
- /package/dist/{src/devices → devices}/index.d.ts +0 -0
- /package/dist/{src/devices → devices}/store.d.ts +0 -0
- /package/dist/{src/devices → devices}/update.d.ts +0 -0
- /package/dist/{src/errors → errors}/destroy.d.ts +0 -0
- /package/dist/{src/errors → errors}/fetch.d.ts +0 -0
- /package/dist/{src/errors → errors}/index.d.ts +0 -0
- /package/dist/{src/errors → errors}/update.d.ts +0 -0
- /package/dist/{src/gift-cards → gift-cards}/destroy.d.ts +0 -0
- /package/dist/{src/gift-cards → gift-cards}/fetch.d.ts +0 -0
- /package/dist/{src/gift-cards → gift-cards}/index.d.ts +0 -0
- /package/dist/{src/gift-cards → gift-cards}/store.d.ts +0 -0
- /package/dist/{src/gift-cards → gift-cards}/update.d.ts +0 -0
- /package/dist/{src/index.d.ts → index.d.ts} +0 -0
- /package/dist/{src/orders → orders}/destroy.d.ts +0 -0
- /package/dist/{src/orders → orders}/events.d.ts +0 -0
- /package/dist/{src/orders → orders}/export.d.ts +0 -0
- /package/dist/{src/orders → orders}/fetch.d.ts +0 -0
- /package/dist/{src/orders → orders}/guards.d.ts +0 -0
- /package/dist/{src/orders → orders}/index.d.ts +0 -0
- /package/dist/{src/orders → orders}/place-order.d.ts +0 -0
- /package/dist/{src/orders → orders}/store.d.ts +0 -0
- /package/dist/{src/orders → orders}/totals.d.ts +0 -0
- /package/dist/{src/orders → orders}/update.d.ts +0 -0
- /package/dist/{src/orders → orders}/webhook.d.ts +0 -0
- /package/dist/{src/payments → payments}/destroy.d.ts +0 -0
- /package/dist/{src/payments → payments}/fetch.d.ts +0 -0
- /package/dist/{src/payments → payments}/index.d.ts +0 -0
- /package/dist/{src/payments → payments}/store.d.ts +0 -0
- /package/dist/{src/payments → payments}/update.d.ts +0 -0
- /package/dist/{src/products → products}/categories/destroy.d.ts +0 -0
- /package/dist/{src/products → products}/categories/fetch.d.ts +0 -0
- /package/dist/{src/products → products}/categories/index.d.ts +0 -0
- /package/dist/{src/products → products}/categories/store.d.ts +0 -0
- /package/dist/{src/products → products}/categories/update.d.ts +0 -0
- /package/dist/{src/products → products}/index.d.ts +0 -0
- /package/dist/{src/products → products}/items/destroy.d.ts +0 -0
- /package/dist/{src/products → products}/items/fetch.d.ts +0 -0
- /package/dist/{src/products → products}/items/index.d.ts +0 -0
- /package/dist/{src/products → products}/items/store.d.ts +0 -0
- /package/dist/{src/products → products}/items/update.d.ts +0 -0
- /package/dist/{src/products → products}/manufacturers/destroy.d.ts +0 -0
- /package/dist/{src/products → products}/manufacturers/fetch.d.ts +0 -0
- /package/dist/{src/products → products}/manufacturers/index.d.ts +0 -0
- /package/dist/{src/products → products}/manufacturers/store.d.ts +0 -0
- /package/dist/{src/products → products}/manufacturers/update.d.ts +0 -0
- /package/dist/{src/products → products}/reviews/destroy.d.ts +0 -0
- /package/dist/{src/products → products}/reviews/fetch.d.ts +0 -0
- /package/dist/{src/products → products}/reviews/index.d.ts +0 -0
- /package/dist/{src/products → products}/reviews/store.d.ts +0 -0
- /package/dist/{src/products → products}/reviews/update.d.ts +0 -0
- /package/dist/{src/products → products}/units/destroy.d.ts +0 -0
- /package/dist/{src/products → products}/units/fetch.d.ts +0 -0
- /package/dist/{src/products → products}/units/index.d.ts +0 -0
- /package/dist/{src/products → products}/units/store.d.ts +0 -0
- /package/dist/{src/products → products}/units/update.d.ts +0 -0
- /package/dist/{src/products → products}/variants/destroy.d.ts +0 -0
- /package/dist/{src/products → products}/variants/fetch.d.ts +0 -0
- /package/dist/{src/products → products}/variants/index.d.ts +0 -0
- /package/dist/{src/products → products}/variants/store.d.ts +0 -0
- /package/dist/{src/products → products}/variants/update.d.ts +0 -0
- /package/dist/{src/receipts → receipts}/destroy.d.ts +0 -0
- /package/dist/{src/receipts → receipts}/fetch.d.ts +0 -0
- /package/dist/{src/receipts → receipts}/index.d.ts +0 -0
- /package/dist/{src/receipts → receipts}/store.d.ts +0 -0
- /package/dist/{src/receipts → receipts}/update.d.ts +0 -0
- /package/dist/{src/shippings → shippings}/delivery-routes/destroy.d.ts +0 -0
- /package/dist/{src/shippings → shippings}/delivery-routes/fetch.d.ts +0 -0
- /package/dist/{src/shippings → shippings}/delivery-routes/index.d.ts +0 -0
- /package/dist/{src/shippings → shippings}/delivery-routes/store.d.ts +0 -0
- /package/dist/{src/shippings → shippings}/delivery-routes/update.d.ts +0 -0
- /package/dist/{src/shippings → shippings}/digital-deliveries/destroy.d.ts +0 -0
- /package/dist/{src/shippings → shippings}/digital-deliveries/fetch.d.ts +0 -0
- /package/dist/{src/shippings → shippings}/digital-deliveries/index.d.ts +0 -0
- /package/dist/{src/shippings → shippings}/digital-deliveries/store.d.ts +0 -0
- /package/dist/{src/shippings → shippings}/digital-deliveries/update.d.ts +0 -0
- /package/dist/{src/shippings → shippings}/drivers/destroy.d.ts +0 -0
- /package/dist/{src/shippings → shippings}/drivers/fetch.d.ts +0 -0
- /package/dist/{src/shippings → shippings}/drivers/index.d.ts +0 -0
- /package/dist/{src/shippings → shippings}/drivers/store.d.ts +0 -0
- /package/dist/{src/shippings → shippings}/drivers/update.d.ts +0 -0
- /package/dist/{src/shippings → shippings}/index.d.ts +0 -0
- /package/dist/{src/shippings → shippings}/license-keys/destroy.d.ts +0 -0
- /package/dist/{src/shippings → shippings}/license-keys/fetch.d.ts +0 -0
- /package/dist/{src/shippings → shippings}/license-keys/index.d.ts +0 -0
- /package/dist/{src/shippings → shippings}/license-keys/store.d.ts +0 -0
- /package/dist/{src/shippings → shippings}/license-keys/update.d.ts +0 -0
- /package/dist/{src/shippings → shippings}/shipping-methods/destroy.d.ts +0 -0
- /package/dist/{src/shippings → shippings}/shipping-methods/fetch.d.ts +0 -0
- /package/dist/{src/shippings → shippings}/shipping-methods/index.d.ts +0 -0
- /package/dist/{src/shippings → shippings}/shipping-methods/store.d.ts +0 -0
- /package/dist/{src/shippings → shippings}/shipping-methods/update.d.ts +0 -0
- /package/dist/{src/shippings → shippings}/shipping-rates/destroy.d.ts +0 -0
- /package/dist/{src/shippings → shippings}/shipping-rates/fetch.d.ts +0 -0
- /package/dist/{src/shippings → shippings}/shipping-rates/index.d.ts +0 -0
- /package/dist/{src/shippings → shippings}/shipping-rates/store.d.ts +0 -0
- /package/dist/{src/shippings → shippings}/shipping-rates/update.d.ts +0 -0
- /package/dist/{src/shippings → shippings}/shipping-zones/destroy.d.ts +0 -0
- /package/dist/{src/shippings → shippings}/shipping-zones/fetch.d.ts +0 -0
- /package/dist/{src/shippings → shippings}/shipping-zones/index.d.ts +0 -0
- /package/dist/{src/shippings → shippings}/shipping-zones/store.d.ts +0 -0
- /package/dist/{src/shippings → shippings}/shipping-zones/update.d.ts +0 -0
- /package/dist/{src/tax → tax}/destroy.d.ts +0 -0
- /package/dist/{src/tax → tax}/fetch.d.ts +0 -0
- /package/dist/{src/tax → tax}/index.d.ts +0 -0
- /package/dist/{src/tax → tax}/store.d.ts +0 -0
- /package/dist/{src/tax → tax}/update.d.ts +0 -0
- /package/dist/{src/types.d.ts → types.d.ts} +0 -0
- /package/dist/{src/utils → utils}/typed-stats.d.ts +0 -0
- /package/dist/{src/waitlists → waitlists}/index.d.ts +0 -0
- /package/dist/{src/waitlists → waitlists}/products/destroy.d.ts +0 -0
- /package/dist/{src/waitlists → waitlists}/products/fetch.d.ts +0 -0
- /package/dist/{src/waitlists → waitlists}/products/index.d.ts +0 -0
- /package/dist/{src/waitlists → waitlists}/products/store.d.ts +0 -0
- /package/dist/{src/waitlists → waitlists}/products/update.d.ts +0 -0
- /package/dist/{src/waitlists → waitlists}/restaurant/destroy.d.ts +0 -0
- /package/dist/{src/waitlists → waitlists}/restaurant/fetch.d.ts +0 -0
- /package/dist/{src/waitlists → waitlists}/restaurant/index.d.ts +0 -0
- /package/dist/{src/waitlists → waitlists}/restaurant/store.d.ts +0 -0
- /package/dist/{src/waitlists → waitlists}/restaurant/update.d.ts +0 -0
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
import { db } from '@stacksjs/database'
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Delete a product item by ID
|
|
5
|
-
*
|
|
6
|
-
* @param id The ID of the product item to delete
|
|
7
|
-
* @returns True if the product item was deleted, false otherwise
|
|
8
|
-
*/
|
|
9
|
-
export async function destroy(id: number): Promise<boolean> {
|
|
10
|
-
try {
|
|
11
|
-
const result = await db
|
|
12
|
-
.deleteFrom('products')
|
|
13
|
-
.where('id', '=', id)
|
|
14
|
-
.executeTakeFirst()
|
|
15
|
-
|
|
16
|
-
return Number(result.numDeletedRows) > 0
|
|
17
|
-
}
|
|
18
|
-
catch (error) {
|
|
19
|
-
if (error instanceof Error) {
|
|
20
|
-
throw new TypeError(`Failed to delete product item: ${error.message}`)
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
throw error
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* Delete multiple product items by ID
|
|
29
|
-
*
|
|
30
|
-
* @param ids Array of product item IDs to delete
|
|
31
|
-
* @returns Number of product items deleted
|
|
32
|
-
*/
|
|
33
|
-
export async function bulkDestroy(ids: number[]): Promise<number> {
|
|
34
|
-
if (!ids.length)
|
|
35
|
-
return 0
|
|
36
|
-
|
|
37
|
-
try {
|
|
38
|
-
// Perform the delete operation
|
|
39
|
-
const result = await db
|
|
40
|
-
.deleteFrom('products')
|
|
41
|
-
.where('id', 'in', ids)
|
|
42
|
-
.executeTakeFirst()
|
|
43
|
-
|
|
44
|
-
// Return the number of deleted rows
|
|
45
|
-
return Number(result.numDeletedRows)
|
|
46
|
-
}
|
|
47
|
-
catch (error) {
|
|
48
|
-
if (error instanceof Error) {
|
|
49
|
-
throw new TypeError(`Failed to delete product items: ${error.message}`)
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
throw error
|
|
53
|
-
}
|
|
54
|
-
}
|
|
@@ -1,282 +0,0 @@
|
|
|
1
|
-
type ProductJsonResponse = ModelRow<typeof Product>
|
|
2
|
-
import type { StacksExpressionBuilder } from '@stacksjs/database'
|
|
3
|
-
import { db, sql } from '@stacksjs/database'
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Fetch a product item by ID
|
|
7
|
-
*/
|
|
8
|
-
export async function fetchById(id: number): Promise<ProductJsonResponse | undefined> {
|
|
9
|
-
const model = await db
|
|
10
|
-
.selectFrom('products')
|
|
11
|
-
.where('id', '=', id)
|
|
12
|
-
.selectAll()
|
|
13
|
-
.executeTakeFirst()
|
|
14
|
-
|
|
15
|
-
if (model) {
|
|
16
|
-
let manufacturer = null
|
|
17
|
-
let category = null
|
|
18
|
-
|
|
19
|
-
if (model.manufacturer_id) {
|
|
20
|
-
manufacturer = await db
|
|
21
|
-
.selectFrom('manufacturers')
|
|
22
|
-
.where('id', '=', model.manufacturer_id)
|
|
23
|
-
.selectAll()
|
|
24
|
-
.executeTakeFirst()
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
if (model.category_id) {
|
|
28
|
-
category = await db
|
|
29
|
-
.selectFrom('categories')
|
|
30
|
-
.where('id', '=', model.category_id)
|
|
31
|
-
.selectAll()
|
|
32
|
-
.executeTakeFirst()
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
return {
|
|
36
|
-
...model,
|
|
37
|
-
manufacturer,
|
|
38
|
-
category,
|
|
39
|
-
} as unknown as ProductJsonResponse
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
return undefined
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* Fetch all product items with their manufacturers and categories
|
|
47
|
-
*/
|
|
48
|
-
export async function fetchAll(): Promise<ProductJsonResponse[]> {
|
|
49
|
-
// Fetch all products
|
|
50
|
-
const models = await db.selectFrom('products').selectAll().execute()
|
|
51
|
-
|
|
52
|
-
// Get the IDs of all manufacturers and categories
|
|
53
|
-
const manufacturerIds = models.map((model: any) => model.manufacturer_id).filter((id: any) => id !== null && id !== undefined)
|
|
54
|
-
const categoryIds = models.map((model: any) => model.category_id).filter((id: any) => id !== null && id !== undefined)
|
|
55
|
-
|
|
56
|
-
let manufacturersQuery = db.selectFrom('manufacturers') as any
|
|
57
|
-
let categoriesQuery = db.selectFrom('categories') as any
|
|
58
|
-
|
|
59
|
-
if (manufacturerIds.length > 0) {
|
|
60
|
-
manufacturersQuery = manufacturersQuery.where('id', 'in', manufacturerIds)
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
if (categoryIds.length > 0) {
|
|
64
|
-
categoriesQuery = categoriesQuery.where('id', 'in', categoryIds)
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
// Fetch manufacturers and categories for these specific IDs using WHERE IN
|
|
68
|
-
const allManufacturers = await manufacturersQuery.selectAll().execute()
|
|
69
|
-
const allCategories = await categoriesQuery.selectAll().execute()
|
|
70
|
-
|
|
71
|
-
// Group manufacturers and categories by ID
|
|
72
|
-
const manufacturersById = allManufacturers.reduce((acc: any, manufacturer: any) => {
|
|
73
|
-
acc[manufacturer.id] = manufacturer
|
|
74
|
-
return acc
|
|
75
|
-
}, {} as Record<number, typeof allManufacturers[0]>)
|
|
76
|
-
|
|
77
|
-
const categoriesById = allCategories.reduce((acc: any, category: any) => {
|
|
78
|
-
acc[category.id] = category
|
|
79
|
-
return acc
|
|
80
|
-
}, {} as Record<number, typeof allCategories[0]>)
|
|
81
|
-
|
|
82
|
-
// Attach manufacturers and categories to each product
|
|
83
|
-
return models.map((model: any) => ({
|
|
84
|
-
...model,
|
|
85
|
-
manufacturer: model.manufacturer_id ? manufacturersById[model.manufacturer_id] : null,
|
|
86
|
-
category: model.category_id ? categoriesById[model.category_id] : null,
|
|
87
|
-
}))
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
/**
|
|
91
|
-
* Get products by manufacturer
|
|
92
|
-
*
|
|
93
|
-
* @param manufacturerId Manufacturer identifier
|
|
94
|
-
* @returns List of products for the specified manufacturer
|
|
95
|
-
*/
|
|
96
|
-
export async function getProductsByManufacturer(manufacturerId: number): Promise<ProductJsonResponse[]> {
|
|
97
|
-
try {
|
|
98
|
-
const products = await db
|
|
99
|
-
.selectFrom('products')
|
|
100
|
-
.selectAll()
|
|
101
|
-
.where('manufacturer_id', '=', manufacturerId)
|
|
102
|
-
.orderBy('name')
|
|
103
|
-
.execute()
|
|
104
|
-
|
|
105
|
-
return products as ProductJsonResponse[]
|
|
106
|
-
}
|
|
107
|
-
catch (error) {
|
|
108
|
-
if (error instanceof Error) {
|
|
109
|
-
throw new TypeError(`Failed to get products by manufacturer: ${error.message}`)
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
throw error
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
/**
|
|
117
|
-
* Get products by category
|
|
118
|
-
*
|
|
119
|
-
* @param categoryId Category identifier
|
|
120
|
-
* @returns List of products for the specified category
|
|
121
|
-
*/
|
|
122
|
-
export async function getProductsByCategory(categoryId: number): Promise<ProductJsonResponse[]> {
|
|
123
|
-
try {
|
|
124
|
-
const products = await db
|
|
125
|
-
.selectFrom('products')
|
|
126
|
-
.selectAll()
|
|
127
|
-
.where('category_id', '=', categoryId)
|
|
128
|
-
.orderBy('name')
|
|
129
|
-
.execute()
|
|
130
|
-
|
|
131
|
-
return products as ProductJsonResponse[]
|
|
132
|
-
}
|
|
133
|
-
catch (error) {
|
|
134
|
-
if (error instanceof Error) {
|
|
135
|
-
throw new TypeError(`Failed to get products by category: ${error.message}`)
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
throw error
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
/**
|
|
143
|
-
* Get product by UUID
|
|
144
|
-
*
|
|
145
|
-
* @param uuid Product UUID
|
|
146
|
-
* @returns Matching product or undefined
|
|
147
|
-
*/
|
|
148
|
-
export async function getProductByUuid(uuid: string): Promise<ProductJsonResponse | undefined> {
|
|
149
|
-
try {
|
|
150
|
-
const product = await db
|
|
151
|
-
.selectFrom('products')
|
|
152
|
-
.selectAll()
|
|
153
|
-
.where('uuid', '=', uuid)
|
|
154
|
-
.executeTakeFirst()
|
|
155
|
-
|
|
156
|
-
return product as ProductJsonResponse | undefined
|
|
157
|
-
}
|
|
158
|
-
catch (error) {
|
|
159
|
-
if (error instanceof Error) {
|
|
160
|
-
throw new TypeError(`Failed to get product by UUID: ${error.message}`)
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
throw error
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
/**
|
|
168
|
-
* Format product options for dropdown menus or selectors
|
|
169
|
-
*
|
|
170
|
-
* @returns Array of formatted product options with id, name, uuid, and price
|
|
171
|
-
*/
|
|
172
|
-
export async function formatProductOptions(): Promise<{ id: number, name: string, uuid: string, price: number }[]> {
|
|
173
|
-
try {
|
|
174
|
-
const results = await db
|
|
175
|
-
.selectFrom('products')
|
|
176
|
-
.select(['id', 'name', 'uuid', 'price'])
|
|
177
|
-
.orderBy('name')
|
|
178
|
-
.execute()
|
|
179
|
-
|
|
180
|
-
// Filter out any results with undefined/null values to match the return type
|
|
181
|
-
return results.filter((result: any) =>
|
|
182
|
-
result.name !== null
|
|
183
|
-
&& result.name !== undefined
|
|
184
|
-
&& result.uuid !== null
|
|
185
|
-
&& result.uuid !== undefined
|
|
186
|
-
&& result.price !== null
|
|
187
|
-
&& result.price !== undefined,
|
|
188
|
-
) as { id: number, name: string, uuid: string, price: number }[]
|
|
189
|
-
}
|
|
190
|
-
catch (error) {
|
|
191
|
-
if (error instanceof Error) {
|
|
192
|
-
throw new TypeError(`Failed to format product options: ${error.message}`)
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
throw error
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
/**
|
|
200
|
-
* Get products by price range
|
|
201
|
-
*
|
|
202
|
-
* @param minPrice Minimum price
|
|
203
|
-
* @param maxPrice Maximum price
|
|
204
|
-
* @returns List of products within the specified price range
|
|
205
|
-
*/
|
|
206
|
-
export async function getProductsByPriceRange(minPrice: number, maxPrice: number): Promise<ProductJsonResponse[]> {
|
|
207
|
-
try {
|
|
208
|
-
const products = await db
|
|
209
|
-
.selectFrom('products')
|
|
210
|
-
.selectAll()
|
|
211
|
-
.where('price', '>=', minPrice)
|
|
212
|
-
.where('price', '<=', maxPrice)
|
|
213
|
-
.orderBy('price')
|
|
214
|
-
.execute()
|
|
215
|
-
|
|
216
|
-
return products as ProductJsonResponse[]
|
|
217
|
-
}
|
|
218
|
-
catch (error) {
|
|
219
|
-
if (error instanceof Error) {
|
|
220
|
-
throw new TypeError(`Failed to get products by price range: ${error.message}`)
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
throw error
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
/**
|
|
228
|
-
* Top-selling products by units sold.
|
|
229
|
-
*
|
|
230
|
-
* Aggregates `order_items` (the line-item table — each row is one
|
|
231
|
-
* product on one order) grouping by product, summing quantity for the
|
|
232
|
-
* "units sold" rank and the line-item revenue (price × quantity) for
|
|
233
|
-
* the revenue side. Joins back to `products` so we can return the
|
|
234
|
-
* product name + current inventory inline; if a product was deleted
|
|
235
|
-
* after sales were recorded, that row is skipped (INNER JOIN).
|
|
236
|
-
*
|
|
237
|
-
* Revenue uses the price stored on `order_items` (price-at-sale)
|
|
238
|
-
* rather than the current `products.price`, so historical numbers
|
|
239
|
-
* stay correct even when prices change.
|
|
240
|
-
*/
|
|
241
|
-
export interface TopProductRow {
|
|
242
|
-
id: number
|
|
243
|
-
name: string
|
|
244
|
-
units_sold: number
|
|
245
|
-
revenue: number
|
|
246
|
-
inventory_count: number
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
export async function fetchTopProducts(limit: number = 5): Promise<TopProductRow[]> {
|
|
250
|
-
try {
|
|
251
|
-
const rows = await db
|
|
252
|
-
.selectFrom('order_items')
|
|
253
|
-
.innerJoin('products', 'products.id', 'order_items.product_id')
|
|
254
|
-
.select([
|
|
255
|
-
'products.id as id',
|
|
256
|
-
'products.name as name',
|
|
257
|
-
'products.inventory_count as inventory_count',
|
|
258
|
-
((eb: StacksExpressionBuilder) => eb.fn.sum('order_items.quantity').as('units_sold')) as any,
|
|
259
|
-
// Multiply line-item price × quantity inline. Falls back to the
|
|
260
|
-
// current product price if order_items.price is null (older data).
|
|
261
|
-
sql<number>`SUM(COALESCE(order_items.price, products.price) * order_items.quantity)`.as('revenue') as any,
|
|
262
|
-
])
|
|
263
|
-
.groupBy(['products.id', 'products.name', 'products.inventory_count'])
|
|
264
|
-
.orderBy('units_sold', 'desc')
|
|
265
|
-
.limit(limit)
|
|
266
|
-
.execute()
|
|
267
|
-
|
|
268
|
-
return rows.map((r: any) => ({
|
|
269
|
-
id: Number(r.id),
|
|
270
|
-
name: String(r.name ?? 'Unnamed product'),
|
|
271
|
-
units_sold: Number(r.units_sold ?? 0),
|
|
272
|
-
revenue: Number(r.revenue ?? 0),
|
|
273
|
-
inventory_count: Number(r.inventory_count ?? 0),
|
|
274
|
-
}))
|
|
275
|
-
}
|
|
276
|
-
catch (error) {
|
|
277
|
-
if (error instanceof Error) {
|
|
278
|
-
throw new TypeError(`Failed to fetch top products: ${error.message}`)
|
|
279
|
-
}
|
|
280
|
-
throw error
|
|
281
|
-
}
|
|
282
|
-
}
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
// Functions from destroy.ts
|
|
2
|
-
export {
|
|
3
|
-
bulkDestroy,
|
|
4
|
-
destroy,
|
|
5
|
-
} from './destroy'
|
|
6
|
-
|
|
7
|
-
// Functions from fetch.ts
|
|
8
|
-
export type { TopProductRow } from './fetch'
|
|
9
|
-
export {
|
|
10
|
-
fetchAll,
|
|
11
|
-
fetchById,
|
|
12
|
-
fetchTopProducts,
|
|
13
|
-
} from './fetch'
|
|
14
|
-
|
|
15
|
-
// Functions from store.ts
|
|
16
|
-
export {
|
|
17
|
-
bulkStore,
|
|
18
|
-
store,
|
|
19
|
-
} from './store'
|
|
20
|
-
|
|
21
|
-
// Functions from update.ts
|
|
22
|
-
export {
|
|
23
|
-
update,
|
|
24
|
-
updateAvailability,
|
|
25
|
-
updateInventory,
|
|
26
|
-
} from './update'
|
|
@@ -1,84 +0,0 @@
|
|
|
1
|
-
type ProductJsonResponse = ModelRow<typeof Product>
|
|
2
|
-
type NewProduct = NewModelData<typeof Product>
|
|
3
|
-
import { randomUUIDv7 } from 'bun'
|
|
4
|
-
import { db } from '@stacksjs/database'
|
|
5
|
-
import { fetchById } from './fetch'
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Create a new product item
|
|
9
|
-
*
|
|
10
|
-
* @param data The product item data to store
|
|
11
|
-
* @returns The newly created product item record
|
|
12
|
-
*/
|
|
13
|
-
export async function store(data: NewProduct): Promise<ProductJsonResponse> {
|
|
14
|
-
try {
|
|
15
|
-
const itemData = {
|
|
16
|
-
...data,
|
|
17
|
-
uuid: randomUUIDv7(),
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
const result = await db
|
|
21
|
-
.insertInto('products')
|
|
22
|
-
.values(itemData)
|
|
23
|
-
.executeTakeFirst()
|
|
24
|
-
|
|
25
|
-
if (!result)
|
|
26
|
-
throw new Error('Failed to create product item')
|
|
27
|
-
|
|
28
|
-
const insertId = Number(result.insertId) || Number(result.numInsertedOrUpdatedRows)
|
|
29
|
-
|
|
30
|
-
const model = await fetchById(insertId)
|
|
31
|
-
|
|
32
|
-
if (!model)
|
|
33
|
-
throw new Error('Failed to create product item')
|
|
34
|
-
|
|
35
|
-
return model
|
|
36
|
-
}
|
|
37
|
-
catch (error) {
|
|
38
|
-
if (error instanceof Error) {
|
|
39
|
-
throw new TypeError(`Failed to create product item: ${error.message}`)
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
throw error
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* Create multiple product items at once
|
|
48
|
-
*
|
|
49
|
-
* @param data Array of product item data to store
|
|
50
|
-
* @returns Number of product items created
|
|
51
|
-
*/
|
|
52
|
-
export async function bulkStore(data: NewProduct[]): Promise<number> {
|
|
53
|
-
if (!data.length)
|
|
54
|
-
return 0
|
|
55
|
-
|
|
56
|
-
let createdCount = 0
|
|
57
|
-
|
|
58
|
-
try {
|
|
59
|
-
await (db as any).transaction().execute(async (trx: any) => {
|
|
60
|
-
for (const item of data) {
|
|
61
|
-
const itemData = {
|
|
62
|
-
...item,
|
|
63
|
-
uuid: randomUUIDv7(),
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
await trx
|
|
67
|
-
.insertInto('products')
|
|
68
|
-
.values(itemData)
|
|
69
|
-
.execute()
|
|
70
|
-
|
|
71
|
-
createdCount++
|
|
72
|
-
}
|
|
73
|
-
})
|
|
74
|
-
|
|
75
|
-
return createdCount
|
|
76
|
-
}
|
|
77
|
-
catch (error) {
|
|
78
|
-
if (error instanceof Error) {
|
|
79
|
-
throw new TypeError(`Failed to create product items in bulk: ${error.message}`)
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
throw error
|
|
83
|
-
}
|
|
84
|
-
}
|