Skip to content

Commit c45e218

Browse files
chore: wip
1 parent fc76f62 commit c45e218

File tree

1 file changed

+103
-0
lines changed
  • storage/framework/core/commerce/src/categories

1 file changed

+103
-0
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
import { db } from '@stacksjs/database'
2+
import { fetchById } from './fetch'
3+
4+
/**
5+
* Delete a category by ID
6+
* @param id The ID of the category to delete
7+
* @returns A boolean indicating whether the deletion was successful
8+
*/
9+
export async function remove(id: number): Promise<boolean> {
10+
// First check if the category exists
11+
const category = await fetchById(id)
12+
13+
if (!category) {
14+
throw new Error(`Category with ID ${id} not found`)
15+
}
16+
17+
// Delete the category
18+
const result = await db
19+
.deleteFrom('categories')
20+
.where('id', '=', id)
21+
.executeTakeFirst()
22+
23+
return !!result
24+
}
25+
26+
/**
27+
* Bulk delete multiple categories
28+
* @param ids Array of category IDs to delete
29+
* @returns Number of categories successfully deleted
30+
*/
31+
export async function bulkRemove(ids: number[]): Promise<number> {
32+
if (!ids.length) {
33+
return 0
34+
}
35+
36+
// Delete all categories in the array
37+
const result = await db
38+
.deleteFrom('categories')
39+
.where('id', 'in', ids)
40+
.executeTakeFirst()
41+
42+
return Number(result?.numDeletedRows) || 0
43+
}
44+
45+
/**
46+
* Delete child categories of a parent category
47+
* @param parentId The ID of the parent category
48+
* @returns Number of categories deleted
49+
*/
50+
export async function removeChildCategories(parentId: string): Promise<number> {
51+
// Delete categories with the specified parent_category_id
52+
const result = await db
53+
.deleteFrom('categories')
54+
.where('parent_category_id', '=', parentId)
55+
.executeTakeFirst()
56+
57+
return Number(result?.numDeletedRows) || 0
58+
}
59+
60+
/**
61+
* Deactivate a category (set is_active to false)
62+
* @param id The ID of the category to deactivate
63+
* @returns A boolean indicating whether the deactivation was successful
64+
*/
65+
export async function deactivate(id: number): Promise<boolean> {
66+
// First check if the category exists
67+
const category = await fetchById(id)
68+
69+
if (!category) {
70+
throw new Error(`Category with ID ${id} not found`)
71+
}
72+
73+
// Update the category status
74+
const result = await db
75+
.updateTable('categories')
76+
.set({
77+
is_active: false,
78+
updated_at: new Date().toISOString(),
79+
})
80+
.where('id', '=', id)
81+
.executeTakeFirst()
82+
83+
return !!result
84+
}
85+
86+
/**
87+
* Deactivate all child categories of a parent category
88+
* @param parentId The ID of the parent category
89+
* @returns Number of categories deactivated
90+
*/
91+
export async function deactivateChildCategories(parentId: string): Promise<number> {
92+
// Update categories with the specified parent_category_id
93+
const result = await db
94+
.updateTable('categories')
95+
.set({
96+
is_active: false,
97+
updated_at: new Date().toISOString(),
98+
})
99+
.where('parent_category_id', '=', parentId)
100+
.execute()
101+
102+
return Number(result.length) || 0
103+
}

0 commit comments

Comments
 (0)