|
1 | 1 | import type {
|
2 |
| - OrderResponse, |
3 | 2 | OrderStats,
|
4 | 3 | OrderType,
|
5 | 4 | OrderTypeCount,
|
6 | 5 | StatusCount,
|
7 | 6 | } from '../../types'
|
8 | 7 | import { db } from '@stacksjs/database'
|
9 | 8 |
|
10 |
| -export interface FetchOrdersOptions { |
11 |
| - page?: number |
12 |
| - limit?: number |
13 |
| - search?: string |
14 |
| - status?: string |
15 |
| - order_type?: string |
16 |
| - customer_id?: number |
17 |
| - sortBy?: string |
18 |
| - sortOrder?: 'asc' | 'desc' |
19 |
| - from_date?: string |
20 |
| - to_date?: string |
21 |
| -} |
22 |
| - |
23 | 9 | /**
|
24 | 10 | * Fetch all orders from the database with their items
|
25 | 11 | */
|
@@ -57,26 +43,6 @@ export async function fetchById(id: number): Promise<OrderType | undefined> {
|
57 | 43 | return order
|
58 | 44 | }
|
59 | 45 |
|
60 |
| -/** |
61 |
| - * Fetch orders for a specific customer |
62 |
| - */ |
63 |
| -export async function fetchByCustomer(customerId: number, options: FetchOrdersOptions = {}): Promise<OrderResponse> { |
64 |
| - return fetchPaginated({ |
65 |
| - ...options, |
66 |
| - customer_id: customerId, |
67 |
| - }) |
68 |
| -} |
69 |
| - |
70 |
| -/** |
71 |
| - * Fetch orders by status |
72 |
| - */ |
73 |
| -export async function fetchByStatus(status: string, options: FetchOrdersOptions = {}): Promise<OrderResponse> { |
74 |
| - return fetchPaginated({ |
75 |
| - ...options, |
76 |
| - status, |
77 |
| - }) |
78 |
| -} |
79 |
| - |
80 | 46 | /**
|
81 | 47 | * Get order statistics
|
82 | 48 | */
|
@@ -137,3 +103,254 @@ export async function fetchStats(): Promise<OrderStats> {
|
137 | 103 | revenue: Number(revenue?.total || 0),
|
138 | 104 | }
|
139 | 105 | }
|
| 106 | + |
| 107 | +/** |
| 108 | + * Compare orders between different time periods |
| 109 | + * @param daysRange Number of days to look back (7, 30, 60, etc.) |
| 110 | + */ |
| 111 | +export async function compareOrdersByPeriod(daysRange: number = 30): Promise<{ |
| 112 | + current_period: number |
| 113 | + previous_period: number |
| 114 | + difference: number |
| 115 | + percentage_change: number |
| 116 | + days_range: number |
| 117 | +}> { |
| 118 | + const today = new Date() |
| 119 | + |
| 120 | + // Current period (last N days) |
| 121 | + const currentPeriodStart = new Date(today) |
| 122 | + currentPeriodStart.setDate(today.getDate() - daysRange) |
| 123 | + |
| 124 | + // Previous period (N days before the current period) |
| 125 | + const previousPeriodEnd = new Date(currentPeriodStart) |
| 126 | + previousPeriodEnd.setDate(previousPeriodEnd.getDate() - 1) |
| 127 | + |
| 128 | + const previousPeriodStart = new Date(previousPeriodEnd) |
| 129 | + previousPeriodStart.setDate(previousPeriodEnd.getDate() - daysRange) |
| 130 | + |
| 131 | + // Get orders for current period |
| 132 | + const currentPeriodOrders = await db |
| 133 | + .selectFrom('orders') |
| 134 | + .select(db.fn.count('id').as('count')) |
| 135 | + .where('created_at', '>=', currentPeriodStart.toISOString()) |
| 136 | + .where('created_at', '<=', today.toISOString()) |
| 137 | + .executeTakeFirst() |
| 138 | + |
| 139 | + // Get orders for previous period |
| 140 | + const previousPeriodOrders = await db |
| 141 | + .selectFrom('orders') |
| 142 | + .select(db.fn.count('id').as('count')) |
| 143 | + .where('created_at', '>=', previousPeriodStart.toISOString()) |
| 144 | + .where('created_at', '<=', previousPeriodEnd.toISOString()) |
| 145 | + .executeTakeFirst() |
| 146 | + |
| 147 | + const currentCount = Number(currentPeriodOrders?.count || 0) |
| 148 | + const previousCount = Number(previousPeriodOrders?.count || 0) |
| 149 | + const difference = currentCount - previousCount |
| 150 | + |
| 151 | + // Calculate percentage change, handling division by zero |
| 152 | + const percentageChange = previousCount !== 0 |
| 153 | + ? (difference / previousCount) * 100 |
| 154 | + : (currentCount > 0 ? 100 : 0) |
| 155 | + |
| 156 | + return { |
| 157 | + current_period: currentCount, |
| 158 | + previous_period: previousCount, |
| 159 | + difference, |
| 160 | + percentage_change: percentageChange, |
| 161 | + days_range: daysRange, |
| 162 | + } |
| 163 | +} |
| 164 | + |
| 165 | +/** |
| 166 | + * Calculate order values and metrics for different time periods |
| 167 | + * @param daysRange Number of days to look back (7, 30, 60, etc.) |
| 168 | + */ |
| 169 | +export async function calculateOrderMetrics(daysRange: number = 30): Promise<{ |
| 170 | + current_period: { |
| 171 | + total_orders: number |
| 172 | + total_revenue: number |
| 173 | + average_order_value: number |
| 174 | + orders_by_status: { status: string, count: number }[] |
| 175 | + orders_by_type: { order_type: string, count: number }[] |
| 176 | + } |
| 177 | + previous_period: { |
| 178 | + total_orders: number |
| 179 | + total_revenue: number |
| 180 | + average_order_value: number |
| 181 | + } |
| 182 | + comparison: { |
| 183 | + orders: { |
| 184 | + difference: number |
| 185 | + percentage: number |
| 186 | + is_increase: boolean |
| 187 | + } |
| 188 | + revenue: { |
| 189 | + difference: number |
| 190 | + percentage: number |
| 191 | + is_increase: boolean |
| 192 | + } |
| 193 | + average_order_value: { |
| 194 | + difference: number |
| 195 | + percentage: number |
| 196 | + is_increase: boolean |
| 197 | + } |
| 198 | + } |
| 199 | + days_range: number |
| 200 | +}> { |
| 201 | + const today = new Date() |
| 202 | + |
| 203 | + // Current period (last N days) |
| 204 | + const currentPeriodStart = new Date(today) |
| 205 | + currentPeriodStart.setDate(today.getDate() - daysRange) |
| 206 | + |
| 207 | + // Previous period (N days before the current period) |
| 208 | + const previousPeriodEnd = new Date(currentPeriodStart) |
| 209 | + previousPeriodEnd.setDate(previousPeriodEnd.getDate() - 1) |
| 210 | + |
| 211 | + const previousPeriodStart = new Date(previousPeriodEnd) |
| 212 | + previousPeriodStart.setDate(previousPeriodEnd.getDate() - daysRange) |
| 213 | + |
| 214 | + // Get values for current period |
| 215 | + const currentPeriodValues = await db |
| 216 | + .selectFrom('orders') |
| 217 | + .select([ |
| 218 | + db.fn.count('id').as('total_orders'), |
| 219 | + db.fn.sum('total_amount').as('total_revenue'), |
| 220 | + ]) |
| 221 | + .where('created_at', '>=', currentPeriodStart.toISOString()) |
| 222 | + .where('created_at', '<=', today.toISOString()) |
| 223 | + .executeTakeFirst() |
| 224 | + |
| 225 | + // Get values for previous period |
| 226 | + const previousPeriodValues = await db |
| 227 | + .selectFrom('orders') |
| 228 | + .select([ |
| 229 | + db.fn.count('id').as('total_orders'), |
| 230 | + db.fn.sum('total_amount').as('total_revenue'), |
| 231 | + ]) |
| 232 | + .where('created_at', '>=', previousPeriodStart.toISOString()) |
| 233 | + .where('created_at', '<=', previousPeriodEnd.toISOString()) |
| 234 | + .executeTakeFirst() |
| 235 | + |
| 236 | + // Get orders by status for current period |
| 237 | + const ordersByStatus = await db |
| 238 | + .selectFrom('orders') |
| 239 | + .select(['status', db.fn.count('id').as('count')]) |
| 240 | + .where('created_at', '>=', currentPeriodStart.toISOString()) |
| 241 | + .where('created_at', '<=', today.toISOString()) |
| 242 | + .groupBy('status') |
| 243 | + .execute() |
| 244 | + |
| 245 | + // Get orders by type for current period |
| 246 | + const ordersByType = await db |
| 247 | + .selectFrom('orders') |
| 248 | + .select(['order_type', db.fn.count('id').as('count')]) |
| 249 | + .where('created_at', '>=', currentPeriodStart.toISOString()) |
| 250 | + .where('created_at', '<=', today.toISOString()) |
| 251 | + .groupBy('order_type') |
| 252 | + .execute() |
| 253 | + |
| 254 | + // Calculate values for current period |
| 255 | + const currentTotalOrders = Number(currentPeriodValues?.total_orders || 0) |
| 256 | + const currentTotalRevenue = Number(currentPeriodValues?.total_revenue || 0) |
| 257 | + const currentAverageOrderValue = currentTotalOrders > 0 |
| 258 | + ? currentTotalRevenue / currentTotalOrders |
| 259 | + : 0 |
| 260 | + |
| 261 | + // Calculate values for previous period |
| 262 | + const previousTotalOrders = Number(previousPeriodValues?.total_orders || 0) |
| 263 | + const previousTotalRevenue = Number(previousPeriodValues?.total_revenue || 0) |
| 264 | + const previousAverageOrderValue = previousTotalOrders > 0 |
| 265 | + ? previousTotalRevenue / previousTotalOrders |
| 266 | + : 0 |
| 267 | + |
| 268 | + // Calculate differences |
| 269 | + const ordersDifference = currentTotalOrders - previousTotalOrders |
| 270 | + const revenueDifference = currentTotalRevenue - previousTotalRevenue |
| 271 | + const aovDifference = currentAverageOrderValue - previousAverageOrderValue |
| 272 | + |
| 273 | + // Calculate percentage changes |
| 274 | + const ordersPercentageChange = previousTotalOrders !== 0 |
| 275 | + ? (ordersDifference / previousTotalOrders) * 100 |
| 276 | + : (currentTotalOrders > 0 ? 100 : 0) |
| 277 | + |
| 278 | + const revenuePercentageChange = previousTotalRevenue !== 0 |
| 279 | + ? (revenueDifference / previousTotalRevenue) * 100 |
| 280 | + : (currentTotalRevenue > 0 ? 100 : 0) |
| 281 | + |
| 282 | + const aovPercentageChange = previousAverageOrderValue !== 0 |
| 283 | + ? (aovDifference / previousAverageOrderValue) * 100 |
| 284 | + : (currentAverageOrderValue > 0 ? 100 : 0) |
| 285 | + |
| 286 | + return { |
| 287 | + current_period: { |
| 288 | + total_orders: currentTotalOrders, |
| 289 | + total_revenue: currentTotalRevenue, |
| 290 | + average_order_value: currentAverageOrderValue, |
| 291 | + orders_by_status: ordersByStatus.map(item => ({ |
| 292 | + status: item.status, |
| 293 | + count: Number(item.count), |
| 294 | + })), |
| 295 | + orders_by_type: ordersByType.map(item => ({ |
| 296 | + order_type: item.order_type, |
| 297 | + count: Number(item.count), |
| 298 | + })), |
| 299 | + }, |
| 300 | + previous_period: { |
| 301 | + total_orders: previousTotalOrders, |
| 302 | + total_revenue: previousTotalRevenue, |
| 303 | + average_order_value: previousAverageOrderValue, |
| 304 | + }, |
| 305 | + comparison: { |
| 306 | + orders: { |
| 307 | + difference: ordersDifference, |
| 308 | + percentage: Math.abs(ordersPercentageChange), |
| 309 | + is_increase: ordersDifference >= 0, |
| 310 | + }, |
| 311 | + revenue: { |
| 312 | + difference: revenueDifference, |
| 313 | + percentage: Math.abs(revenuePercentageChange), |
| 314 | + is_increase: revenueDifference >= 0, |
| 315 | + }, |
| 316 | + average_order_value: { |
| 317 | + difference: aovDifference, |
| 318 | + percentage: Math.abs(aovPercentageChange), |
| 319 | + is_increase: aovDifference >= 0, |
| 320 | + }, |
| 321 | + }, |
| 322 | + days_range: daysRange, |
| 323 | + } |
| 324 | +} |
| 325 | + |
| 326 | +/** |
| 327 | + * Get daily order counts for a time period |
| 328 | + * @param daysRange Number of days to look back |
| 329 | + */ |
| 330 | +export async function fetchDailyOrderTrends(daysRange: number = 30): Promise<{ |
| 331 | + date: Date |
| 332 | + order_count: number |
| 333 | + revenue: number |
| 334 | +}[]> { |
| 335 | + const today = new Date() |
| 336 | + const startDate = new Date(today) |
| 337 | + startDate.setDate(today.getDate() - daysRange) |
| 338 | + |
| 339 | + // Query to get daily order counts and revenue |
| 340 | + const dailyOrders = await db |
| 341 | + .selectFrom('orders') |
| 342 | + .select([ |
| 343 | + db.fn.count('id').as('order_count'), |
| 344 | + db.fn.sum('total_amount').as('revenue'), |
| 345 | + 'created_at', |
| 346 | + ]) |
| 347 | + .where('created_at', '>=', startDate) |
| 348 | + .where('created_at', '<=', today) |
| 349 | + .execute() |
| 350 | + |
| 351 | + return dailyOrders.map(day => ({ |
| 352 | + date: day.created_at, |
| 353 | + order_count: Number(day.order_count || 0), |
| 354 | + revenue: Number(day.revenue || 0), |
| 355 | + })) |
| 356 | +} |
0 commit comments