Skip to content

Commit 593440b

Browse files
chore: wip
1 parent 16c7c5f commit 593440b

File tree

11 files changed

+9
-304
lines changed

11 files changed

+9
-304
lines changed

storage/framework/core/commerce/src/coupons/fetch.ts

Lines changed: 0 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -40,94 +40,6 @@ export async function fetchAll(): Promise<CouponJsonResponse[]> {
4040
return coupons.map(processCouponData)
4141
}
4242

43-
/**
44-
* Fetch coupons with pagination, sorting, and filtering options
45-
*/
46-
export async function fetchPaginated(options: FetchCouponsOptions = {}): Promise<CouponResponse> {
47-
// Set default values
48-
const page = options.page || 1
49-
const limit = options.limit || 10
50-
51-
// Start building the query
52-
let query = db.selectFrom('coupons')
53-
let countQuery = db.selectFrom('coupons')
54-
55-
// Apply search filter if provided
56-
if (options.search) {
57-
const searchTerm = `%${options.search}%`
58-
const searchFilter = (eb: any) => eb.or([
59-
eb('code', 'like', searchTerm),
60-
eb('description', 'like', searchTerm),
61-
eb('discount_type', 'like', searchTerm),
62-
])
63-
64-
query = query.where(searchFilter)
65-
countQuery = countQuery.where(searchFilter)
66-
}
67-
68-
// Apply discount type filter if provided
69-
if (options.discount_type) {
70-
query = query.where('discount_type', '=', options.discount_type)
71-
countQuery = countQuery.where('discount_type', '=', options.discount_type)
72-
}
73-
74-
// Apply date range filter if provided
75-
if (options.from_date) {
76-
query = query.where('start_date', '>=', options.from_date)
77-
countQuery = countQuery.where('start_date', '>=', options.from_date)
78-
}
79-
80-
if (options.to_date) {
81-
query = query.where('end_date', '<=', options.to_date)
82-
countQuery = countQuery.where('end_date', '<=', options.to_date)
83-
}
84-
85-
// Apply product_id filter if provided
86-
if (options.product_id) {
87-
// This will filter coupons where the applicable_products JSON array contains the product_id
88-
// Note: This is a simplistic approach and may need adjustment based on database
89-
query = query.where('applicable_products', 'like', `%${options.product_id}%`)
90-
countQuery = countQuery.where('applicable_products', 'like', `%${options.product_id}%`)
91-
}
92-
93-
// Apply category_id filter if provided
94-
if (options.category_id) {
95-
// This will filter coupons where the applicable_categories JSON array contains the category_id
96-
query = query.where('applicable_categories', 'like', `%${options.category_id}%`)
97-
countQuery = countQuery.where('applicable_categories', 'like', `%${options.category_id}%`)
98-
}
99-
100-
// Get total count for pagination
101-
const countResult = await countQuery
102-
.select(eb => eb.fn.count('id').as('total'))
103-
.executeTakeFirst()
104-
105-
const total = Number(countResult?.total || 0)
106-
107-
// Apply pagination
108-
const coupons = await query
109-
.selectAll()
110-
.limit(limit)
111-
.offset((page - 1) * limit)
112-
.execute()
113-
114-
// Process coupon data (parse JSON fields)
115-
const processedCoupons = coupons.map(processCouponData)
116-
117-
// Calculate pagination info
118-
const totalPages = Math.ceil(total / limit)
119-
120-
return {
121-
data: processedCoupons,
122-
paging: {
123-
total_records: total,
124-
page,
125-
total_pages: totalPages,
126-
},
127-
next_cursor: page < totalPages ? page + 1 : null,
128-
}
129-
}
130-
13143
/**
13244
* Fetch a coupon by ID
13345
*/

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,13 @@ export {
99
fetchAll,
1010
fetchByCode,
1111
fetchById,
12-
fetchPaginated,
13-
fetchStats,
12+
fetchConversionRate,
1413
fetchCouponCounts,
1514
fetchCouponCountsByType,
1615
fetchRedemptionStats,
17-
fetchTopRedeemedCoupons,
1816
fetchRedemptionTrend,
19-
fetchConversionRate,
17+
fetchStats,
18+
fetchTopRedeemedCoupons,
2019
getActiveCouponsMoMChange,
2120
} from './fetch'
2221

@@ -26,4 +25,4 @@ export {
2625

2726
export {
2827
update,
29-
} from './update'
28+
} from './update'
Lines changed: 2 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { CustomersTable } from '../../types'
1+
import type { CustomerJsonResponse, CustomersTable } from '../../types'
22
import { db } from '@stacksjs/database'
33

44
export interface FetchCustomersOptions {
@@ -25,60 +25,10 @@ export interface PaginatedCustomers {
2525
/**
2626
* Fetch a customer by ID
2727
*/
28-
export async function fetchById(id: number): Promise<CustomersTable | undefined> {
28+
export async function fetchById(id: number): Promise<CustomerJsonResponse | undefined> {
2929
return await db
3030
.selectFrom('customers')
3131
.where('id', '=', id)
3232
.selectAll()
3333
.executeTakeFirst()
3434
}
35-
36-
/**
37-
* Fetch customers with simple pagination, sorting, and basic search
38-
* A minimal implementation that works well while planning for Algolia/Meilisearch
39-
*/
40-
export async function fetchPaginated(options: FetchCustomersOptions = {}): Promise<PaginatedCustomers> {
41-
// Set default values
42-
const page = options.page || 1
43-
const limit = options.limit || 10
44-
45-
// Start building the query
46-
let query = db.selectFrom('customers')
47-
let countQuery = db.selectFrom('customers')
48-
49-
// Simple name search if provided
50-
if (options.search && options.search.trim()) {
51-
const searchTerm = `%${options.search.trim()}%`
52-
query = query.where('name', 'like', searchTerm)
53-
countQuery = countQuery.where('name', 'like', searchTerm)
54-
}
55-
56-
// Get total count
57-
const countResult = await countQuery
58-
.select(eb => eb.fn.count('id').as('total'))
59-
.executeTakeFirst()
60-
61-
const total = Number(countResult?.total || 0)
62-
63-
// Basic pagination
64-
const customers = await query
65-
.selectAll()
66-
.limit(limit)
67-
.offset((page - 1) * limit)
68-
.execute()
69-
70-
// Calculate pagination info
71-
const totalPages = Math.ceil(total / limit)
72-
73-
return {
74-
customers,
75-
pagination: {
76-
total,
77-
currentPage: page,
78-
totalPages,
79-
limit,
80-
hasNextPage: page < totalPages,
81-
hasPrevPage: page > 1,
82-
},
83-
}
84-
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ export {
66
export {
77
fetchById,
88
type FetchCustomersOptions,
9-
fetchPaginated,
109
type PaginatedCustomers,
1110
} from './fetch'
1211

storage/framework/core/commerce/src/gift-cards/fetch.ts

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -14,51 +14,6 @@ export async function fetchAll(): Promise<GiftCardJsonResponse[]> {
1414
return giftCards
1515
}
1616

17-
/**
18-
* Fetch gift cards with pagination, sorting, and filtering options
19-
*/
20-
export async function fetchPaginated(options: FetchGiftCardsOptions = {}): Promise<GiftCardResponse> {
21-
// Set default values
22-
const page = options.page || 1
23-
const limit = options.limit || 10
24-
25-
// Start building the query
26-
let query = db.selectFrom('gift_cards')
27-
let countQuery = db.selectFrom('gift_cards')
28-
29-
if (options.max_balance !== undefined) {
30-
query = query.where('current_balance', '<=', options.max_balance)
31-
countQuery = countQuery.where('current_balance', '<=', options.max_balance)
32-
}
33-
34-
// Get total count for pagination
35-
const countResult = await countQuery
36-
.select(eb => eb.fn.count('id').as('total'))
37-
.executeTakeFirst()
38-
39-
const total = Number(countResult?.total || 0)
40-
41-
// Apply pagination
42-
const giftCards = await query
43-
.selectAll()
44-
.limit(limit)
45-
.offset((page - 1) * limit)
46-
.execute()
47-
48-
// Calculate pagination info
49-
const totalPages = Math.ceil(total / limit)
50-
51-
return {
52-
data: giftCards,
53-
paging: {
54-
total_records: total,
55-
page,
56-
total_pages: totalPages,
57-
},
58-
next_cursor: page < totalPages ? page + 1 : null,
59-
}
60-
}
61-
6217
/**
6318
* Fetch a gift card by ID
6419
*/

storage/framework/core/commerce/src/gift-cards/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ export {
66
} from './destroy'
77

88
export {
9+
calculateGiftCardValues,
910
checkBalance,
11+
compareActiveGiftCards,
1012
fetchActive,
13+
fetchAll,
1114
fetchByCode,
1215
fetchById,
13-
fetchPaginated,
1416
fetchStats,
1517
} from './fetch'
1618

storage/framework/core/commerce/src/manufacturer/fetch.ts

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,46 +2,6 @@ import type { ManufacturerJsonResponse } from '../../../../orm/src/models/Manufa
22
import type { FetchProductManufacturersOptions, ProductManufacturerResponse } from '../../types'
33
import { db } from '@stacksjs/database'
44

5-
/**
6-
* Fetch product manufacturers with pagination
7-
*/
8-
export async function fetchPaginated(options: FetchProductManufacturersOptions = {}): Promise<ProductManufacturerResponse> {
9-
// Set default values
10-
const page = options.page || 1
11-
const limit = options.limit || 10
12-
13-
// Start building the query
14-
const query = db.selectFrom('manufacturers')
15-
const countQuery = db.selectFrom('manufacturers')
16-
17-
// Get total count for pagination
18-
const countResult = await countQuery
19-
.select(eb => eb.fn.count('id').as('total'))
20-
.executeTakeFirst()
21-
22-
const total = Number(countResult?.total || 0)
23-
24-
// Apply pagination
25-
const manufacturers = await query
26-
.selectAll()
27-
.limit(limit)
28-
.offset((page - 1) * limit)
29-
.execute()
30-
31-
// Calculate pagination info
32-
const totalPages = Math.ceil(total / limit)
33-
34-
return {
35-
data: manufacturers,
36-
paging: {
37-
total_records: total,
38-
page,
39-
total_pages: totalPages,
40-
},
41-
next_cursor: page < totalPages ? page + 1 : null,
42-
}
43-
}
44-
455
/**
466
* Fetch a product manufacturer by ID
477
*/

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ export {
88
fetchById,
99
fetchByUuid,
1010
fetchFeatured,
11-
fetchPaginated,
1211
fetchWithProductCount,
1312
} from './fetch'
1413

storage/framework/core/commerce/src/orders/fetch.ts

Lines changed: 0 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -44,74 +44,6 @@ export async function fetchAll(): Promise<OrderType[]> {
4444
}))
4545
}
4646

47-
/**
48-
* Fetch orders with pagination, sorting, and filtering options
49-
*/
50-
export async function fetchPaginated(options: FetchOrdersOptions = {}): Promise<OrderResponse> {
51-
// Set default values
52-
const page = options.page || 1
53-
const limit = options.limit || 10
54-
55-
// Start building the query
56-
let query = db.selectFrom('orders')
57-
let countQuery = db.selectFrom('orders')
58-
59-
// Apply search filter if provided
60-
if (options.search) {
61-
const searchTerm = `%${options.search}%`
62-
const searchFilter = (eb: any) => eb.or([
63-
eb('id', 'like', searchTerm),
64-
eb('customer_id', '=', Number(options.search) || 0),
65-
eb('status', 'like', searchTerm),
66-
eb('order_type', 'like', searchTerm),
67-
])
68-
69-
query = query.where(searchFilter)
70-
countQuery = countQuery.where(searchFilter)
71-
}
72-
73-
// Get total count for pagination
74-
const countResult = await countQuery
75-
.select(eb => eb.fn.count('id').as('total'))
76-
.executeTakeFirst()
77-
78-
const total = Number(countResult?.total || 0)
79-
80-
// Apply pagination
81-
const orders = await query
82-
.selectAll()
83-
.limit(limit)
84-
.offset((page - 1) * limit)
85-
.execute()
86-
87-
// Fetch items for each order
88-
const ordersWithItems = await Promise.all(orders.map(async (order) => {
89-
const items = await db
90-
.selectFrom('order_items')
91-
.where('order_id', '=', order.id)
92-
.selectAll()
93-
.execute()
94-
95-
return {
96-
...order,
97-
items,
98-
}
99-
}))
100-
101-
// Calculate pagination info
102-
const totalPages = Math.ceil(total / limit)
103-
104-
return {
105-
data: ordersWithItems,
106-
paging: {
107-
total_records: total,
108-
page,
109-
total_pages: totalPages,
110-
},
111-
next_cursor: page < totalPages ? page + 1 : null,
112-
}
113-
}
114-
11547
/**
11648
* Fetch an order by ID
11749
*/

0 commit comments

Comments
 (0)