Skip to content

Commit 3e380fd

Browse files
chore: wip
1 parent 904cf17 commit 3e380fd

File tree

2 files changed

+181
-1
lines changed

2 files changed

+181
-1
lines changed
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
// Import dependencies
2+
import type { ProductUnitRequestType } from '@stacksjs/orm'
3+
import type { ProductUnitJsonResponse } from '../../../../orm/src/models/ProductUnit'
4+
import { db } from '@stacksjs/database'
5+
6+
/**
7+
* Update an existing product unit
8+
*
9+
* @param id The ID of the product unit to update
10+
* @param request Updated product unit data
11+
* @returns The updated product unit record
12+
*/
13+
export async function update(id: number, request: ProductUnitRequestType): Promise<ProductUnitJsonResponse | undefined> {
14+
// Validate the request data
15+
await request.validate()
16+
17+
try {
18+
// Prepare unit data for update
19+
const unitData = {
20+
name: request.get('name'),
21+
abbreviation: request.get('abbreviation'),
22+
type: request.get('type'),
23+
description: request.get('description'),
24+
is_default: request.get<boolean>('is_default'),
25+
updated_at: new Date(),
26+
}
27+
28+
// Update the product unit
29+
await db
30+
.updateTable('product_units')
31+
.set(unitData)
32+
.where('id', '=', id)
33+
.execute()
34+
35+
// If this unit is set as default, update all other units of the same type
36+
if (unitData.is_default === true) {
37+
await db
38+
.updateTable('product_units')
39+
.set({ is_default: false })
40+
.where('type', '=', unitData.type)
41+
.where('id', '!=', id)
42+
.execute()
43+
}
44+
45+
// Retrieve the updated product unit
46+
const unit = await db
47+
.selectFrom('product_units')
48+
.where('id', '=', id)
49+
.selectAll()
50+
.executeTakeFirst()
51+
52+
return unit
53+
}
54+
catch (error) {
55+
if (error instanceof Error) {
56+
throw new TypeError(`Failed to update product unit: ${error.message}`)
57+
}
58+
59+
throw error
60+
}
61+
}
62+
63+
/**
64+
* Update multiple product units at once
65+
*
66+
* @param updates Array of objects containing unit ID and update data
67+
* @returns Number of product units updated
68+
*/
69+
export async function bulkUpdate(updates: Array<{
70+
id: number
71+
data: ProductUnitRequestType
72+
}>): Promise<number> {
73+
if (!updates.length)
74+
return 0
75+
76+
let updatedCount = 0
77+
78+
try {
79+
// Process each product unit update
80+
await db.transaction().execute(async (trx) => {
81+
for (const { id, data } of updates) {
82+
// Validate update data
83+
await data.validate()
84+
85+
// Prepare unit data for update
86+
const unitData = {
87+
name: data.get<string>('name'),
88+
abbreviation: data.get<string>('abbreviation'),
89+
type: data.get<string>('type'),
90+
description: data.get<string>('description'),
91+
is_default: data.get<boolean>('is_default'),
92+
updated_at: new Date(),
93+
}
94+
95+
// Skip if no fields to update
96+
if (Object.keys(unitData).length === 0)
97+
continue
98+
99+
// Update the product unit
100+
const result = await trx
101+
.updateTable('product_units')
102+
.set(unitData)
103+
.where('id', '=', id)
104+
.executeTakeFirst()
105+
106+
// If this unit is set as default, update all other units of the same type
107+
if (unitData.is_default === true) {
108+
await trx
109+
.updateTable('product_units')
110+
.set({ is_default: false })
111+
.where('type', '=', unitData.type)
112+
.where('id', '!=', id)
113+
.execute()
114+
}
115+
116+
// Increment the counter if update was successful
117+
if (Number(result.numUpdatedRows) > 0)
118+
updatedCount++
119+
}
120+
})
121+
122+
return updatedCount
123+
}
124+
catch (error) {
125+
if (error instanceof Error) {
126+
throw new TypeError(`Failed to update product units in bulk: ${error.message}`)
127+
}
128+
129+
throw error
130+
}
131+
}
132+
133+
/**
134+
* Update the default status of a product unit
135+
*
136+
* @param id The ID of the product unit
137+
* @param isDefault The new default status value
138+
* @returns True if the status was updated successfully
139+
*/
140+
export async function updateDefaultStatus(id: number, isDefault: boolean): Promise<boolean> {
141+
try {
142+
// First get the unit type to update other units if needed
143+
const unit = await db
144+
.selectFrom('product_units')
145+
.select('type')
146+
.where('id', '=', id)
147+
.executeTakeFirst()
148+
149+
if (!unit) {
150+
return false
151+
}
152+
153+
const result = await db
154+
.updateTable('product_units')
155+
.set({
156+
is_default: isDefault,
157+
updated_at: new Date(),
158+
})
159+
.where('id', '=', id)
160+
.executeTakeFirst()
161+
162+
// If setting this unit as default, update all other units of the same type
163+
if (isDefault) {
164+
await db
165+
.updateTable('product_units')
166+
.set({ is_default: false })
167+
.where('type', '=', unit.type)
168+
.where('id', '!=', id)
169+
.execute()
170+
}
171+
172+
return Number(result.numUpdatedRows) > 0
173+
}
174+
catch (error) {
175+
if (error instanceof Error) {
176+
throw new TypeError(`Failed to update product unit default status: ${error.message}`)
177+
}
178+
179+
throw error
180+
}
181+
}

storage/framework/core/commerce/src/variants/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,4 @@ export {
1313
export {
1414
bulkUpdate,
1515
update,
16-
updateStatus,
1716
} from './update'

0 commit comments

Comments
 (0)