Skip to content

Commit 2fc5498

Browse files
chore: wip
1 parent 64ef867 commit 2fc5498

19 files changed

+149
-37
lines changed
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
import { db } from '@stacksjs/database'
2+
3+
/**
4+
* Delete an order by ID
5+
*
6+
* @param id The ID of the order to delete
7+
* @returns True if the order was deleted, false otherwise
8+
*/
9+
export async function destroy(id: number): Promise<boolean> {
10+
try {
11+
// Perform the delete operation
12+
const result = await db
13+
.deleteFrom('orders')
14+
.where('id', '=', id)
15+
.executeTakeFirst()
16+
17+
// Return true if any row was affected (deleted)
18+
return Number(result.numDeletedRows) > 0
19+
}
20+
catch (error) {
21+
if (error instanceof Error) {
22+
throw new TypeError(`Failed to delete order: ${error.message}`)
23+
}
24+
25+
throw error
26+
}
27+
}
28+
29+
/**
30+
* Soft delete an order by ID (updates status to CANCELED)
31+
*
32+
* @param id The ID of the order to soft delete
33+
* @returns True if the order was soft deleted, false otherwise
34+
*/
35+
export async function softDelete(id: number): Promise<boolean> {
36+
try {
37+
// Update the status to CANCELED instead of deleting
38+
const result = await db
39+
.updateTable('orders')
40+
.set({ status: 'CANCELED' })
41+
.where('id', '=', id)
42+
.executeTakeFirst()
43+
44+
// Return true if any row was affected (updated)
45+
return Number(result.numUpdatedRows) > 0
46+
}
47+
catch (error) {
48+
if (error instanceof Error) {
49+
throw new TypeError(`Failed to soft delete order: ${error.message}`)
50+
}
51+
52+
throw error
53+
}
54+
}
55+
56+
/**
57+
* Delete multiple orders by ID
58+
*
59+
* @param ids Array of order IDs to delete
60+
* @returns Number of orders deleted
61+
*/
62+
export async function bulkDestroy(ids: number[]): Promise<number> {
63+
if (!ids.length)
64+
return 0
65+
66+
try {
67+
// Perform the delete operation
68+
const result = await db
69+
.deleteFrom('orders')
70+
.where('id', 'in', ids)
71+
.executeTakeFirst()
72+
73+
// Return the number of deleted rows
74+
return Number(result.numDeletedRows)
75+
}
76+
catch (error) {
77+
if (error instanceof Error) {
78+
throw new TypeError(`Failed to delete orders: ${error.message}`)
79+
}
80+
81+
throw error
82+
}
83+
}
84+
85+
/**
86+
* Soft delete multiple orders by ID (updates status to CANCELED)
87+
*
88+
* @param ids Array of order IDs to soft delete
89+
* @returns Number of orders soft deleted
90+
*/
91+
export async function bulkSoftDelete(ids: number[]): Promise<number> {
92+
if (!ids.length)
93+
return 0
94+
95+
try {
96+
// Update the status to CANCELED instead of deleting
97+
const result = await db
98+
.updateTable('orders')
99+
.set({ status: 'CANCELED' })
100+
.where('id', 'in', ids)
101+
.executeTakeFirst()
102+
103+
// Return the number of updated rows
104+
return Number(result.numUpdatedRows)
105+
}
106+
catch (error) {
107+
if (error instanceof Error) {
108+
throw new TypeError(`Failed to soft delete orders: ${error.message}`)
109+
}
110+
111+
throw error
112+
}
113+
}

storage/framework/core/commerce/src/reviews/store.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import type { GiftCardRequestType, ProductRequestType } from '@stacksjs/orm'
2-
import type { GiftCardJsonResponse, NewGiftCard } from '../../../../orm/src/models/GiftCard'
3-
import { db } from '@stacksjs/database'
1+
import type { ProductRequestType } from '@stacksjs/orm'
42
import type { NewProductReview, ProductReviewJsonResponse } from '../../../../orm/src/models/ProductReview'
3+
import { db } from '@stacksjs/database'
54

65
/**
76
* Create a new gift card

storage/framework/core/orm/src/generate.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -749,7 +749,7 @@ export async function generateModelString(
749749
}
750750

751751
if (useUuid) {
752-
getFields += `get uuid(): string {
752+
getFields += `get uuid(): string | undefined {
753753
return this.attributes.uuid
754754
}\n\n`
755755

@@ -867,7 +867,7 @@ export async function generateModelString(
867867
fieldString += 'stripe_id?: string \n'
868868

869869
if (useUuid)
870-
fieldString += 'uuid: string \n'
870+
fieldString += 'uuid?: string \n'
871871

872872
if (useTimestamps) {
873873
fieldString += `

storage/framework/orm/src/models/Coupon.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export interface CouponsTable {
2929
end_date: string
3030
applicable_products?: string
3131
applicable_categories?: string
32-
uuid: string
32+
uuid?: string
3333

3434
created_at?: Date
3535

@@ -160,7 +160,7 @@ export class CouponModel {
160160
return this.attributes.id
161161
}
162162

163-
get uuid(): string {
163+
get uuid(): string | undefined {
164164
return this.attributes.uuid
165165
}
166166

storage/framework/orm/src/models/Customer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export interface CustomersTable {
2222
last_order?: string
2323
status: string | string[]
2424
avatar?: string
25-
uuid: string
25+
uuid?: string
2626

2727
created_at?: Date
2828

@@ -161,7 +161,7 @@ export class CustomerModel {
161161
return this.attributes.id
162162
}
163163

164-
get uuid(): string {
164+
get uuid(): string | undefined {
165165
return this.attributes.uuid
166166
}
167167

storage/framework/orm/src/models/Deployment.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export interface DeploymentsTable {
1919
execution_time: number
2020
deploy_script: string
2121
terminal_output: string
22-
uuid: string
22+
uuid?: string
2323

2424
created_at?: Date
2525

@@ -146,7 +146,7 @@ export class DeploymentModel {
146146
return this.attributes.id
147147
}
148148

149-
get uuid(): string {
149+
get uuid(): string | undefined {
150150
return this.attributes.uuid
151151
}
152152

storage/framework/orm/src/models/GiftCard.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export interface GiftCardsTable {
3030
expiry_date?: string
3131
last_used_date?: string
3232
template_id?: string
33-
uuid: string
33+
uuid?: string
3434

3535
created_at?: Date
3636

@@ -161,7 +161,7 @@ export class GiftCardModel {
161161
return this.attributes.id
162162
}
163163

164-
get uuid(): string {
164+
get uuid(): string | undefined {
165165
return this.attributes.uuid
166166
}
167167

storage/framework/orm/src/models/LoyaltyPoint.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export interface LoyaltyPointsTable {
1616
description?: string
1717
expiry_date?: string
1818
is_used?: boolean
19-
uuid: string
19+
uuid?: string
2020

2121
created_at?: Date
2222

@@ -135,7 +135,7 @@ export class LoyaltyPointModel {
135135
return this.attributes.id
136136
}
137137

138-
get uuid(): string {
138+
get uuid(): string | undefined {
139139
return this.attributes.uuid
140140
}
141141

storage/framework/orm/src/models/LoyaltyReward.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export interface LoyaltyRewardsTable {
2222
is_active?: boolean
2323
expiry_days?: number
2424
image_url?: string
25-
uuid: string
25+
uuid?: string
2626

2727
created_at?: Date
2828

@@ -149,7 +149,7 @@ export class LoyaltyRewardModel {
149149
return this.attributes.id
150150
}
151151

152-
get uuid(): string {
152+
get uuid(): string | undefined {
153153
return this.attributes.uuid
154154
}
155155

storage/framework/orm/src/models/Order.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export interface OrdersTable {
3131
special_instructions?: string
3232
estimated_delivery_time?: string
3333
applied_coupon_id?: string
34-
uuid: string
34+
uuid?: string
3535

3636
created_at?: Date
3737

@@ -170,7 +170,7 @@ export class OrderModel {
170170
return this.attributes.id
171171
}
172172

173-
get uuid(): string {
173+
get uuid(): string | undefined {
174174
return this.attributes.uuid
175175
}
176176

0 commit comments

Comments
 (0)