@stacksjs/commerce 0.70.53 → 0.70.55

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.
Files changed (165) hide show
  1. package/package.json +3 -2
  2. package/src/carts/destroy.ts +54 -0
  3. package/src/carts/fetch.ts +20 -0
  4. package/src/carts/index.ts +13 -0
  5. package/src/carts/store.ts +75 -0
  6. package/src/carts/update.ts +78 -0
  7. package/src/coupons/destroy.ts +49 -0
  8. package/src/coupons/fetch.ts +510 -0
  9. package/src/coupons/index.ts +30 -0
  10. package/src/coupons/store.ts +46 -0
  11. package/src/coupons/update.ts +147 -0
  12. package/src/customers/destroy.ts +62 -0
  13. package/src/customers/fetch.ts +20 -0
  14. package/src/customers/index.ts +17 -0
  15. package/src/customers/store.ts +45 -0
  16. package/src/customers/update.ts +44 -0
  17. package/src/devices/destroy.ts +54 -0
  18. package/src/devices/export.ts +98 -0
  19. package/src/devices/fetch.ts +125 -0
  20. package/src/devices/index.ts +31 -0
  21. package/src/devices/store.ts +82 -0
  22. package/src/devices/update.ts +125 -0
  23. package/src/errors/destroy.ts +54 -0
  24. package/src/errors/fetch.ts +197 -0
  25. package/src/errors/index.ts +28 -0
  26. package/src/errors/update.ts +116 -0
  27. package/src/gift-cards/destroy.ts +87 -0
  28. package/src/gift-cards/fetch.ts +431 -0
  29. package/src/gift-cards/index.ts +26 -0
  30. package/src/gift-cards/store.ts +56 -0
  31. package/src/gift-cards/update.ts +102 -0
  32. package/src/index.ts +78 -0
  33. package/src/orders/destroy.ts +111 -0
  34. package/src/orders/events.ts +121 -0
  35. package/src/orders/export.ts +158 -0
  36. package/src/orders/fetch.ts +379 -0
  37. package/src/orders/guards.ts +261 -0
  38. package/src/orders/index.ts +91 -0
  39. package/src/orders/place-order.ts +274 -0
  40. package/src/orders/store.ts +51 -0
  41. package/src/orders/totals.ts +190 -0
  42. package/src/orders/update.ts +164 -0
  43. package/src/orders/webhook.ts +312 -0
  44. package/src/payments/destroy.ts +56 -0
  45. package/src/payments/fetch.ts +288 -0
  46. package/src/payments/index.ts +16 -0
  47. package/src/payments/store.ts +63 -0
  48. package/src/payments/update.ts +52 -0
  49. package/src/product.ts +0 -0
  50. package/src/products/categories/destroy.ts +103 -0
  51. package/src/products/categories/fetch.ts +285 -0
  52. package/src/products/categories/index.ts +36 -0
  53. package/src/products/categories/store.ts +87 -0
  54. package/src/products/categories/update.ts +251 -0
  55. package/src/products/index.ts +35 -0
  56. package/src/products/items/destroy.ts +54 -0
  57. package/src/products/items/fetch.ts +282 -0
  58. package/src/products/items/index.ts +26 -0
  59. package/src/products/items/store.ts +84 -0
  60. package/src/products/items/update.ts +324 -0
  61. package/src/products/manufacturers/destroy.ts +56 -0
  62. package/src/products/manufacturers/fetch.ts +117 -0
  63. package/src/products/manufacturers/index.ts +24 -0
  64. package/src/products/manufacturers/store.ts +82 -0
  65. package/src/products/manufacturers/update.ts +113 -0
  66. package/src/products/reviews/destroy.ts +56 -0
  67. package/src/products/reviews/fetch.ts +63 -0
  68. package/src/products/reviews/index.ts +25 -0
  69. package/src/products/reviews/store.ts +46 -0
  70. package/src/products/reviews/update.ts +82 -0
  71. package/src/products/units/destroy.ts +56 -0
  72. package/src/products/units/fetch.ts +20 -0
  73. package/src/products/units/index.ts +13 -0
  74. package/src/products/units/store.ts +162 -0
  75. package/src/products/units/update.ts +159 -0
  76. package/src/products/variants/destroy.ts +56 -0
  77. package/src/products/variants/fetch.ts +20 -0
  78. package/src/products/variants/index.ts +21 -0
  79. package/src/products/variants/store.ts +136 -0
  80. package/src/products/variants/update.ts +104 -0
  81. package/src/receipts/destroy.ts +54 -0
  82. package/src/receipts/export.ts +0 -0
  83. package/src/receipts/fetch.ts +222 -0
  84. package/src/receipts/index.ts +24 -0
  85. package/src/receipts/printer.ts +69 -0
  86. package/src/receipts/store.ts +69 -0
  87. package/src/receipts/update.ts +124 -0
  88. package/src/shippings/delivery-routes/destroy.ts +54 -0
  89. package/src/shippings/delivery-routes/fetch.ts +42 -0
  90. package/src/shippings/delivery-routes/index.ts +26 -0
  91. package/src/shippings/delivery-routes/store.ts +69 -0
  92. package/src/shippings/delivery-routes/update.ts +120 -0
  93. package/src/shippings/digital-deliveries/destroy.ts +111 -0
  94. package/src/shippings/digital-deliveries/fetch.ts +20 -0
  95. package/src/shippings/digital-deliveries/index.ts +26 -0
  96. package/src/shippings/digital-deliveries/store.ts +76 -0
  97. package/src/shippings/digital-deliveries/update.ts +129 -0
  98. package/src/shippings/drivers/destroy.ts +78 -0
  99. package/src/shippings/drivers/fetch.ts +20 -0
  100. package/src/shippings/drivers/index.ts +24 -0
  101. package/src/shippings/drivers/store.ts +69 -0
  102. package/src/shippings/drivers/update.ts +117 -0
  103. package/src/shippings/index.ts +39 -0
  104. package/src/shippings/license-keys/destroy.ts +111 -0
  105. package/src/shippings/license-keys/fetch.ts +20 -0
  106. package/src/shippings/license-keys/index.ts +26 -0
  107. package/src/shippings/license-keys/store.ts +77 -0
  108. package/src/shippings/license-keys/update.ts +117 -0
  109. package/src/shippings/shipping-methods/destroy.ts +111 -0
  110. package/src/shippings/shipping-methods/fetch.ts +62 -0
  111. package/src/shippings/shipping-methods/index.ts +28 -0
  112. package/src/shippings/shipping-methods/store.ts +123 -0
  113. package/src/shippings/shipping-methods/update.ts +120 -0
  114. package/src/shippings/shipping-rates/destroy.ts +102 -0
  115. package/src/shippings/shipping-rates/fetch.ts +320 -0
  116. package/src/shippings/shipping-rates/index.ts +36 -0
  117. package/src/shippings/shipping-rates/store.ts +102 -0
  118. package/src/shippings/shipping-rates/update.ts +139 -0
  119. package/src/shippings/shipping-zones/destroy.ts +111 -0
  120. package/src/shippings/shipping-zones/fetch.ts +96 -0
  121. package/src/shippings/shipping-zones/index.ts +26 -0
  122. package/src/shippings/shipping-zones/store.ts +74 -0
  123. package/src/shippings/shipping-zones/update.ts +156 -0
  124. package/src/tax/destroy.ts +54 -0
  125. package/src/tax/fetch.ts +20 -0
  126. package/src/tax/index.ts +24 -0
  127. package/src/tax/store.ts +69 -0
  128. package/src/tax/update.ts +119 -0
  129. package/src/tests/coupons.test.ts +90 -0
  130. package/src/tests/customers.test.ts +39 -0
  131. package/src/tests/device.test.ts +111 -0
  132. package/src/tests/digitals.test.ts +31 -0
  133. package/src/tests/drivers.test.ts +25 -0
  134. package/src/tests/gift-cards.test.ts +36 -0
  135. package/src/tests/license.test.ts +31 -0
  136. package/src/tests/manufacturers.test.ts +80 -0
  137. package/src/tests/orders.test.ts +25 -0
  138. package/src/tests/payments.test.ts +75 -0
  139. package/src/tests/rates.test.ts +367 -0
  140. package/src/tests/receipts.test.ts +287 -0
  141. package/src/tests/restaurant.test.ts +522 -0
  142. package/src/tests/reviews.test.ts +165 -0
  143. package/src/tests/routes.test.ts +51 -0
  144. package/src/tests/setup.ts +149 -0
  145. package/src/tests/shipping.test.ts +88 -0
  146. package/src/tests/tax.test.ts +25 -0
  147. package/src/tests/units.test.ts +110 -0
  148. package/src/tests/variants.test.ts +133 -0
  149. package/src/tests/waitlist.test.ts +303 -0
  150. package/src/tests/zones.test.ts +86 -0
  151. package/src/types.ts +312 -0
  152. package/src/utils/typed-stats.ts +67 -0
  153. package/src/waitlists/index.ts +19 -0
  154. package/src/waitlists/products/destroy.ts +65 -0
  155. package/src/waitlists/products/export.ts +113 -0
  156. package/src/waitlists/products/fetch.ts +362 -0
  157. package/src/waitlists/products/index.ts +33 -0
  158. package/src/waitlists/products/store.ts +112 -0
  159. package/src/waitlists/products/update.ts +125 -0
  160. package/src/waitlists/restaurant/destroy.ts +65 -0
  161. package/src/waitlists/restaurant/export.ts +107 -0
  162. package/src/waitlists/restaurant/fetch.ts +538 -0
  163. package/src/waitlists/restaurant/index.ts +43 -0
  164. package/src/waitlists/restaurant/store.ts +109 -0
  165. package/src/waitlists/restaurant/update.ts +198 -0
@@ -0,0 +1,117 @@
1
+ import { db } from '@stacksjs/database'
2
+ // Import dependencies
3
+ import { formatDate } from '@stacksjs/orm'
4
+ type DriverJsonResponse = ModelRow<typeof Driver>
5
+ type DriverUpdate = UpdateModelData<typeof Driver>
6
+
7
+ /**
8
+ * Update a driver
9
+ *
10
+ * @param id The id of the driver to update
11
+ * @param data The driver data to update
12
+ * @returns The updated driver record
13
+ */
14
+ export async function update(id: number, data: DriverUpdate): Promise<DriverJsonResponse> {
15
+ try {
16
+ if (!id)
17
+ throw new Error('Driver ID is required for update')
18
+
19
+ const result = await db
20
+ .updateTable('drivers')
21
+ .set({
22
+ ...data,
23
+ updated_at: formatDate(new Date()),
24
+ })
25
+ .where('id', '=', id)
26
+ .returningAll()
27
+ .executeTakeFirst()
28
+
29
+ if (!result)
30
+ throw new Error('Failed to update driver')
31
+
32
+ return result as DriverJsonResponse
33
+ }
34
+ catch (error) {
35
+ if (error instanceof Error) {
36
+ throw new TypeError(`Failed to update driver: ${error.message}`)
37
+ }
38
+
39
+ throw error
40
+ }
41
+ }
42
+
43
+ /**
44
+ * Update a driver's status
45
+ *
46
+ * @param id The ID of the driver
47
+ * @param status The new status (active, on_delivery, on_break)
48
+ * @returns The updated driver with the new status
49
+ */
50
+ export async function updateStatus(
51
+ id: number,
52
+ status: 'active' | 'on_delivery' | 'on_break',
53
+ ): Promise<DriverJsonResponse> {
54
+ try {
55
+ const result = await db
56
+ .updateTable('drivers')
57
+ .set({
58
+ status,
59
+ updated_at: formatDate(new Date()),
60
+ })
61
+ .where('id', '=', id)
62
+ .returningAll()
63
+ .executeTakeFirst()
64
+
65
+ if (!result)
66
+ throw new Error('Failed to update driver status')
67
+
68
+ return result as DriverJsonResponse
69
+ }
70
+ catch (error) {
71
+ if (error instanceof Error) {
72
+ throw new TypeError(`Failed to update driver status: ${error.message}`)
73
+ }
74
+
75
+ throw error
76
+ }
77
+ }
78
+
79
+ /**
80
+ * Update driver's contact information
81
+ *
82
+ * @param id The ID of the driver
83
+ * @param phone The updated phone number
84
+ * @returns The updated driver
85
+ */
86
+ export async function updateContact(
87
+ id: number,
88
+ phone?: string,
89
+ ): Promise<DriverJsonResponse> {
90
+ try {
91
+ const updateData: Record<string, any> = {
92
+ updated_at: formatDate(new Date()),
93
+ }
94
+
95
+ if (phone !== undefined)
96
+ updateData.phone = phone
97
+
98
+ const result = await db
99
+ .updateTable('drivers')
100
+ .set(updateData)
101
+ .where('id', '=', id)
102
+ .returningAll()
103
+ .executeTakeFirst()
104
+
105
+ if (!result)
106
+ throw new Error('Failed to update contact information')
107
+
108
+ return result as DriverJsonResponse
109
+ }
110
+ catch (error) {
111
+ if (error instanceof Error) {
112
+ throw new TypeError(`Failed to update contact information: ${error.message}`)
113
+ }
114
+
115
+ throw error
116
+ }
117
+ }
@@ -0,0 +1,39 @@
1
+ import * as routes from './delivery-routes'
2
+ import * as digital from './digital-deliveries'
3
+ import * as drivers from './drivers'
4
+ import * as licenses from './license-keys'
5
+ import * as methods from './shipping-methods'
6
+ import * as rates from './shipping-rates'
7
+ import * as zones from './shipping-zones'
8
+
9
+ export {
10
+ digital,
11
+ drivers,
12
+ licenses,
13
+ methods,
14
+ rates,
15
+ routes,
16
+ zones,
17
+ }
18
+
19
+ interface ShippingsNamespace {
20
+ digital: typeof digital
21
+ drivers: typeof drivers
22
+ licenses: typeof licenses
23
+ methods: typeof methods
24
+ rates: typeof rates
25
+ routes: typeof routes
26
+ zones: typeof zones
27
+ }
28
+
29
+ const shippings: ShippingsNamespace = {
30
+ digital,
31
+ drivers,
32
+ licenses,
33
+ methods,
34
+ rates,
35
+ routes,
36
+ zones,
37
+ }
38
+
39
+ export default shippings
@@ -0,0 +1,111 @@
1
+ import { db } from '@stacksjs/database'
2
+
3
+ /**
4
+ * Delete a license key by ID
5
+ *
6
+ * @param id The ID of the license key to delete
7
+ * @returns True if the license key was deleted, false otherwise
8
+ */
9
+ export async function destroy(id: number): Promise<boolean> {
10
+ try {
11
+ const result = await db
12
+ .deleteFrom('license_keys')
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 license key: ${error.message}`)
21
+ }
22
+
23
+ throw error
24
+ }
25
+ }
26
+
27
+ /**
28
+ * Soft delete a license key by ID (updates status to 'inactive')
29
+ *
30
+ * @param id The ID of the license key to soft delete
31
+ * @returns True if the license key was soft deleted, false otherwise
32
+ */
33
+ export async function softDelete(id: number): Promise<boolean> {
34
+ try {
35
+ // Update the status to 'inactive' instead of deleting
36
+ const result = await db
37
+ .updateTable('license_keys')
38
+ .set({ status: 'inactive' })
39
+ .where('id', '=', id)
40
+ .executeTakeFirst()
41
+
42
+ // Return true if any row was affected (updated)
43
+ return Number(result.numUpdatedRows) > 0
44
+ }
45
+ catch (error) {
46
+ if (error instanceof Error) {
47
+ throw new TypeError(`Failed to soft delete license key: ${error.message}`)
48
+ }
49
+
50
+ throw error
51
+ }
52
+ }
53
+
54
+ /**
55
+ * Delete multiple license keys by ID
56
+ *
57
+ * @param ids Array of license key IDs to delete
58
+ * @returns Number of license keys deleted
59
+ */
60
+ export async function bulkDestroy(ids: number[]): Promise<number> {
61
+ if (!ids.length)
62
+ return 0
63
+
64
+ try {
65
+ // Perform the delete operation
66
+ const result = await db
67
+ .deleteFrom('license_keys')
68
+ .where('id', 'in', ids)
69
+ .executeTakeFirst()
70
+
71
+ // Return the number of deleted rows
72
+ return Number(result.numDeletedRows)
73
+ }
74
+ catch (error) {
75
+ if (error instanceof Error) {
76
+ throw new TypeError(`Failed to delete license keys: ${error.message}`)
77
+ }
78
+
79
+ throw error
80
+ }
81
+ }
82
+
83
+ /**
84
+ * Soft delete multiple license keys by ID (updates status to 'inactive')
85
+ *
86
+ * @param ids Array of license key IDs to soft delete
87
+ * @returns Number of license keys soft deleted
88
+ */
89
+ export async function bulkSoftDelete(ids: number[]): Promise<number> {
90
+ if (!ids.length)
91
+ return 0
92
+
93
+ try {
94
+ // Update the status to 'inactive' instead of deleting
95
+ const result = await db
96
+ .updateTable('license_keys')
97
+ .set({ status: 'inactive' })
98
+ .where('id', 'in', ids)
99
+ .executeTakeFirst()
100
+
101
+ // Return the number of updated rows
102
+ return Number(result.numUpdatedRows)
103
+ }
104
+ catch (error) {
105
+ if (error instanceof Error) {
106
+ throw new TypeError(`Failed to soft delete license keys: ${error.message}`)
107
+ }
108
+
109
+ throw error
110
+ }
111
+ }
@@ -0,0 +1,20 @@
1
+ type LicenseKeyJsonResponse = ModelRow<typeof LicenseKey>
2
+ import { db } from '@stacksjs/database'
3
+
4
+ /**
5
+ * Fetch a shipping method by ID
6
+ */
7
+ export async function fetchById(id: number): Promise<LicenseKeyJsonResponse | undefined> {
8
+ return await db
9
+ .selectFrom('license_keys')
10
+ .where('id', '=', id)
11
+ .selectAll()
12
+ .executeTakeFirst() as LicenseKeyJsonResponse | undefined
13
+ }
14
+
15
+ /**
16
+ * Fetch all digital deliveries
17
+ */
18
+ export async function fetchAll(): Promise<LicenseKeyJsonResponse[]> {
19
+ return await db.selectFrom('license_keys').selectAll().execute() as LicenseKeyJsonResponse[]
20
+ }
@@ -0,0 +1,26 @@
1
+ // Functions from destroy.ts
2
+ export {
3
+ bulkDestroy,
4
+ bulkSoftDelete,
5
+ destroy,
6
+ softDelete,
7
+ } from './destroy'
8
+
9
+ // Functions from fetch.ts
10
+ export {
11
+ fetchAll,
12
+ fetchById,
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
+ updateExpiration,
25
+ updateStatus,
26
+ } from './update'
@@ -0,0 +1,77 @@
1
+ // Import dependencies
2
+ type LicenseKeyJsonResponse = ModelRow<typeof LicenseKey>
3
+ type NewLicenseKey = NewModelData<typeof LicenseKey>
4
+ import { randomUUIDv7 } from 'bun'
5
+ import { db } from '@stacksjs/database'
6
+ import { fetchById } from './fetch'
7
+
8
+ /**
9
+ * Create a new license key
10
+ *
11
+ * @param data The license key data to store
12
+ * @returns The newly created license key record
13
+ */
14
+ export async function store(data: NewLicenseKey): Promise<LicenseKeyJsonResponse> {
15
+ try {
16
+ const licenseData = {
17
+ ...data,
18
+ uuid: randomUUIDv7(),
19
+ }
20
+
21
+ const result = await db
22
+ .insertInto('license_keys')
23
+ .values(licenseData)
24
+ .executeTakeFirst()
25
+
26
+ if (!result)
27
+ throw new Error('Failed to create license key')
28
+
29
+ const insertedId = Number(result.insertId) || Number(result.numInsertedOrUpdatedRows)
30
+
31
+ const licenseKey = await fetchById(insertedId)
32
+
33
+ if (!licenseKey)
34
+ throw new Error('Failed to create license key')
35
+
36
+ return licenseKey
37
+ }
38
+ catch (error) {
39
+ if (error instanceof Error) {
40
+ throw new TypeError(`Failed to create license key: ${error.message}`)
41
+ }
42
+
43
+ throw error
44
+ }
45
+ }
46
+
47
+ /**
48
+ * Create multiple license keys at once
49
+ *
50
+ * @param data Array of license key data to store
51
+ * @returns Number of license keys created
52
+ */
53
+ export async function bulkStore(data: NewLicenseKey[]): Promise<number> {
54
+ if (!data.length)
55
+ return 0
56
+
57
+ try {
58
+ const licenseDataArray = data.map(item => ({
59
+ ...item,
60
+ uuid: randomUUIDv7(),
61
+ }))
62
+
63
+ const result = await db
64
+ .insertInto('license_keys')
65
+ .values(licenseDataArray)
66
+ .executeTakeFirst()
67
+
68
+ return Number(result.numInsertedOrUpdatedRows)
69
+ }
70
+ catch (error) {
71
+ if (error instanceof Error) {
72
+ throw new TypeError(`Failed to create license keys in bulk: ${error.message}`)
73
+ }
74
+
75
+ throw error
76
+ }
77
+ }
@@ -0,0 +1,117 @@
1
+ import { db } from '@stacksjs/database'
2
+ // Import dependencies
3
+ import { formatDate } from '@stacksjs/orm'
4
+ type LicenseKeyJsonResponse = ModelRow<typeof LicenseKey>
5
+ type LicenseKeyUpdate = UpdateModelData<typeof LicenseKey>
6
+
7
+ /**
8
+ * Update a license key
9
+ *
10
+ * @param id The ID of the license key
11
+ * @param data The license key data to update
12
+ * @returns The updated license key record
13
+ */
14
+ export async function update(id: number, data: LicenseKeyUpdate): Promise<LicenseKeyJsonResponse> {
15
+ try {
16
+ if (!id)
17
+ throw new Error('License key ID is required for update')
18
+
19
+ const result = await db
20
+ .updateTable('license_keys')
21
+ .set({
22
+ ...data,
23
+ updated_at: formatDate(new Date()),
24
+ })
25
+ .where('id', '=', id)
26
+ .returningAll()
27
+ .executeTakeFirst()
28
+
29
+ if (!result)
30
+ throw new Error('Failed to update license key')
31
+
32
+ return result as LicenseKeyJsonResponse
33
+ }
34
+ catch (error) {
35
+ if (error instanceof Error) {
36
+ throw new TypeError(`Failed to update license key: ${error.message}`)
37
+ }
38
+
39
+ throw error
40
+ }
41
+ }
42
+
43
+ /**
44
+ * Update a license key's status
45
+ *
46
+ * @param id The ID of the license key
47
+ * @param status The new status
48
+ * @returns The updated license key with the new status
49
+ */
50
+ export async function updateStatus(
51
+ id: number,
52
+ status: string | string[],
53
+ ): Promise<LicenseKeyJsonResponse> {
54
+ try {
55
+ const result = await db
56
+ .updateTable('license_keys')
57
+ .set({
58
+ status,
59
+ updated_at: formatDate(new Date()),
60
+ })
61
+ .where('id', '=', id)
62
+ .returningAll()
63
+ .executeTakeFirst()
64
+
65
+ if (!result)
66
+ throw new Error('Failed to update license key status')
67
+
68
+ return result as LicenseKeyJsonResponse
69
+ }
70
+ catch (error) {
71
+ if (error instanceof Error) {
72
+ throw new TypeError(`Failed to update license key status: ${error.message}`)
73
+ }
74
+
75
+ throw error
76
+ }
77
+ }
78
+
79
+ /**
80
+ * Update expiration information for a license key
81
+ *
82
+ * @param id The ID of the license key
83
+ * @param expiryDate The updated expiry date
84
+ * @returns The updated license key
85
+ */
86
+ export async function updateExpiration(
87
+ id: number,
88
+ expiryDate?: string | Date,
89
+ ): Promise<LicenseKeyJsonResponse> {
90
+ try {
91
+ const updateData: Record<string, any> = {
92
+ updated_at: formatDate(new Date()),
93
+ }
94
+
95
+ if (expiryDate !== undefined)
96
+ updateData.expiry_date = expiryDate
97
+
98
+ const result = await db
99
+ .updateTable('license_keys')
100
+ .set(updateData)
101
+ .where('id', '=', id)
102
+ .returningAll()
103
+ .executeTakeFirst()
104
+
105
+ if (!result)
106
+ throw new Error('Failed to update expiration information')
107
+
108
+ return result as LicenseKeyJsonResponse
109
+ }
110
+ catch (error) {
111
+ if (error instanceof Error) {
112
+ throw new TypeError(`Failed to update expiration information: ${error.message}`)
113
+ }
114
+
115
+ throw error
116
+ }
117
+ }
@@ -0,0 +1,111 @@
1
+ import { db } from '@stacksjs/database'
2
+
3
+ /**
4
+ * Delete a shipping method by ID
5
+ *
6
+ * @param id The ID of the shipping method to delete
7
+ * @returns True if the shipping method was deleted, false otherwise
8
+ */
9
+ export async function destroy(id: number): Promise<boolean> {
10
+ try {
11
+ const result = await db
12
+ .deleteFrom('shipping_methods')
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 shipping method: ${error.message}`)
21
+ }
22
+
23
+ throw error
24
+ }
25
+ }
26
+
27
+ /**
28
+ * Soft delete a shipping method by ID (updates status to 'inactive')
29
+ *
30
+ * @param id The ID of the shipping method to soft delete
31
+ * @returns True if the shipping method was soft deleted, false otherwise
32
+ */
33
+ export async function softDelete(id: number): Promise<boolean> {
34
+ try {
35
+ // Update the status to 'inactive' instead of deleting
36
+ const result = await db
37
+ .updateTable('shipping_methods')
38
+ .set({ status: 'inactive' })
39
+ .where('id', '=', id)
40
+ .executeTakeFirst()
41
+
42
+ // Return true if any row was affected (updated)
43
+ return Number(result.numUpdatedRows) > 0
44
+ }
45
+ catch (error) {
46
+ if (error instanceof Error) {
47
+ throw new TypeError(`Failed to soft delete shipping method: ${error.message}`)
48
+ }
49
+
50
+ throw error
51
+ }
52
+ }
53
+
54
+ /**
55
+ * Delete multiple shipping methods by ID
56
+ *
57
+ * @param ids Array of shipping method IDs to delete
58
+ * @returns Number of shipping methods deleted
59
+ */
60
+ export async function bulkDestroy(ids: number[]): Promise<number> {
61
+ if (!ids.length)
62
+ return 0
63
+
64
+ try {
65
+ // Perform the delete operation
66
+ const result = await db
67
+ .deleteFrom('shipping_methods')
68
+ .where('id', 'in', ids)
69
+ .executeTakeFirst()
70
+
71
+ // Return the number of deleted rows
72
+ return Number(result.numDeletedRows)
73
+ }
74
+ catch (error) {
75
+ if (error instanceof Error) {
76
+ throw new TypeError(`Failed to delete shipping methods: ${error.message}`)
77
+ }
78
+
79
+ throw error
80
+ }
81
+ }
82
+
83
+ /**
84
+ * Soft delete multiple shipping methods by ID (updates status to 'inactive')
85
+ *
86
+ * @param ids Array of shipping method IDs to soft delete
87
+ * @returns Number of shipping methods soft deleted
88
+ */
89
+ export async function bulkSoftDelete(ids: number[]): Promise<number> {
90
+ if (!ids.length)
91
+ return 0
92
+
93
+ try {
94
+ // Update the status to 'inactive' instead of deleting
95
+ const result = await db
96
+ .updateTable('shipping_methods')
97
+ .set({ status: 'inactive' })
98
+ .where('id', 'in', ids)
99
+ .executeTakeFirst()
100
+
101
+ // Return the number of updated rows
102
+ return Number(result.numUpdatedRows)
103
+ }
104
+ catch (error) {
105
+ if (error instanceof Error) {
106
+ throw new TypeError(`Failed to soft delete shipping methods: ${error.message}`)
107
+ }
108
+
109
+ throw error
110
+ }
111
+ }
@@ -0,0 +1,62 @@
1
+ type ShippingMethodJsonResponse = ModelRow<typeof ShippingMethod>
2
+ import { db } from '@stacksjs/database'
3
+
4
+ /**
5
+ * Fetch a shipping method by ID
6
+ */
7
+ export async function fetchById(id: number): Promise<ShippingMethodJsonResponse | undefined> {
8
+ const model = await db
9
+ .selectFrom('shipping_methods')
10
+ .where('id', '=', id)
11
+ .selectAll()
12
+ .executeTakeFirst()
13
+
14
+ if (model) {
15
+ const shippingZones = await db.selectFrom('shipping_zones').where('shipping_method_id', '=', id).selectAll().execute()
16
+
17
+ return {
18
+ ...model,
19
+ shippingZones,
20
+ } as unknown as ShippingMethodJsonResponse
21
+ }
22
+
23
+ return undefined
24
+ }
25
+
26
+ /**
27
+ * Fetch all shipping methods with their shipping zones
28
+ */
29
+ export async function fetchAll(): Promise<ShippingMethodJsonResponse[]> {
30
+ // Fetch all shipping methods
31
+ const models = await db.selectFrom('shipping_methods').selectAll().execute()
32
+
33
+ // Get the IDs of all shipping methods
34
+ const shippingMethodIds = models.map((model: any) => model.id)
35
+
36
+ let shippingQuery = db.selectFrom('shipping_zones') as any
37
+
38
+ if (shippingMethodIds.length > 0) {
39
+ shippingQuery = shippingQuery.where('shipping_method_id', 'in', shippingMethodIds)
40
+ }
41
+
42
+ // Fetch shipping zones for these specific shipping methods using WHERE IN
43
+ const allShippingZones = await shippingQuery.selectAll().execute()
44
+
45
+ // Group shipping zones by shipping method ID
46
+ const shippingZonesByMethodId = allShippingZones.reduce((acc: any, zone: any) => {
47
+ const methodId = zone.shipping_method_id
48
+ if (methodId !== null && methodId !== undefined) {
49
+ if (!acc[methodId]) {
50
+ acc[methodId] = []
51
+ }
52
+ acc[methodId].push(zone)
53
+ }
54
+ return acc
55
+ }, {} as Record<number, typeof allShippingZones>)
56
+
57
+ // Attach shipping zones to each shipping method
58
+ return models.map((model: any) => ({
59
+ ...model,
60
+ shipping_zones: shippingZonesByMethodId[model.id] || [],
61
+ }))
62
+ }
@@ -0,0 +1,28 @@
1
+ // Functions from destroy.ts
2
+ export {
3
+ bulkDestroy,
4
+ bulkSoftDelete,
5
+ destroy,
6
+ softDelete,
7
+ } from './destroy'
8
+
9
+ // Functions from fetch.ts
10
+ export {
11
+ fetchAll,
12
+ fetchById,
13
+ } from './fetch'
14
+
15
+ // Functions from store.ts
16
+ export {
17
+ bulkStore,
18
+ formatShippingOptions,
19
+ getActiveShippingMethods,
20
+ store,
21
+ } from './store'
22
+
23
+ // Functions from update.ts
24
+ export {
25
+ update,
26
+ updatePricing,
27
+ updateStatus,
28
+ } from './update'