Skip to content

Commit 55aa773

Browse files
chore: wip
1 parent 496046d commit 55aa773

File tree

5 files changed

+58
-9
lines changed

5 files changed

+58
-9
lines changed

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

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -380,9 +380,6 @@ export async function fetchCouponCountsByType(): Promise<Record<string, CouponCo
380380
return result
381381
}
382382

383-
/**
384-
* Fetch coupon redemption statistics based on usage_count
385-
*/
386383
export async function fetchRedemptionStats(): Promise<CouponRedemptionStats> {
387384
// Total redemptions (sum of all usage_count)
388385
const totalResult = await db
@@ -547,3 +544,56 @@ export async function fetchConversionRate(): Promise<{
547544
total_redeemed: totalRedeemed,
548545
}
549546
}
547+
548+
/**
549+
* Calculate the month-over-month change in active coupons
550+
*/
551+
export async function getActiveCouponsMoMChange(): Promise<{
552+
current_month: number
553+
previous_month: number
554+
difference: number
555+
percentage_change: number
556+
}> {
557+
const today = new Date()
558+
559+
// Current month (start of current month to today)
560+
const currentMonthStart = new Date(today.getFullYear(), today.getMonth(), 1)
561+
562+
// Previous month
563+
const previousMonthStart = new Date(today.getFullYear(), today.getMonth() - 1, 1)
564+
const previousMonthEnd = new Date(today.getFullYear(), today.getMonth(), 0)
565+
566+
// Get active coupons for current month
567+
const currentMonthActive = await db
568+
.selectFrom('coupons')
569+
.select(db.fn.count('id').as('count'))
570+
.where('is_active', '=', true)
571+
.where('start_date', '<=', today)
572+
.where('end_date', '>=', currentMonthStart)
573+
.executeTakeFirst()
574+
575+
// Get active coupons for previous month
576+
const previousMonthActive = await db
577+
.selectFrom('coupons')
578+
.select(db.fn.count('id').as('count'))
579+
.where('is_active', '=', true)
580+
.where('start_date', '<=', previousMonthEnd)
581+
.where('end_date', '>=', previousMonthStart)
582+
.executeTakeFirst()
583+
584+
const currentCount = Number(currentMonthActive?.count || 0)
585+
const previousCount = Number(previousMonthActive?.count || 0)
586+
const difference = currentCount - previousCount
587+
588+
// Calculate percentage change, handling division by zero
589+
const percentageChange = previousCount !== 0
590+
? (difference / previousCount) * 100
591+
: (currentCount > 0 ? 100 : 0) // If previous month was 0, and current is > 0, that's a 100% increase
592+
593+
return {
594+
current_month: currentCount,
595+
previous_month: previousCount,
596+
difference,
597+
percentage_change: percentageChange,
598+
}
599+
}

storage/framework/core/commerce/types.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,6 @@ export interface FetchProductManufacturersOptions {
287287
search?: string
288288
}
289289

290-
291290
export interface FetchCouponsOptions {
292291
page?: number
293292
limit?: number
@@ -330,4 +329,4 @@ export interface CouponRedemptionStats {
330329
month: number
331330
year: number
332331
by_type: Record<string, number>
333-
}
332+
}

storage/framework/core/components/stepper/vite.config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ export default defineConfig(({ mode }) => {
6464
entryFileNames: 'index.js',
6565
preserveModules: false,
6666
globals: {
67-
vue: 'Vue',
67+
'vue': 'Vue',
6868
'@heroicons/vue': 'HeroIcons',
69-
unocss: 'UnoCSS',
69+
'unocss': 'UnoCSS',
7070
},
7171
},
7272
],

storage/framework/server-auto-imports.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,4 +262,4 @@
262262
"transactionRequest": true,
263263
"userRequest": true
264264
}
265-
}
265+
}

storage/framework/types/server-auto-imports.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,4 +262,4 @@ declare global {
262262
const teamRequest: typeof import('./../requests')['teamRequest']
263263
const transactionRequest: typeof import('./../requests')['transactionRequest']
264264
const userRequest: typeof import('./../requests')['userRequest']
265-
}
265+
}

0 commit comments

Comments
 (0)