1- import { Prisma } from "@prisma/client" ;
2- import { format , subDays } from "date-fns" ;
31import { z } from "zod" ;
4-
52import { createTRPCRouter , teamProcedure } from "~/server/api/trpc" ;
6- import { db } from "~/server/db " ;
3+ import { emailTimeSeries , reputationMetricsData } from "~/server/service/dashboard-service " ;
74
85export const dashboardRouter = createTRPCRouter ( {
96 emailTimeSeries : teamProcedure
@@ -15,88 +12,10 @@ export const dashboardRouter = createTRPCRouter({
1512 )
1613 . query ( async ( { ctx, input } ) => {
1714 const { team } = ctx ;
18- const days = input . days !== 7 ? 30 : 7 ;
19-
20- const startDate = new Date ( ) ;
21- startDate . setDate ( startDate . getDate ( ) - days ) ;
22- const isoStartDate = startDate . toISOString ( ) . split ( "T" ) [ 0 ] ;
23-
24- type DailyEmailUsage = {
25- date : string ;
26- sent : number ;
27- delivered : number ;
28- opened : number ;
29- clicked : number ;
30- bounced : number ;
31- complained : number ;
32- } ;
33-
34- const result = await db . $queryRaw < Array < DailyEmailUsage >> `
35- SELECT
36- date,
37- SUM(sent)::integer AS sent,
38- SUM(delivered)::integer AS delivered,
39- SUM(opened)::integer AS opened,
40- SUM(clicked)::integer AS clicked,
41- SUM(bounced)::integer AS bounced,
42- SUM(complained)::integer AS complained
43- FROM "DailyEmailUsage"
44- WHERE "teamId" = ${ team . id }
45- AND "date" >= ${ isoStartDate }
46- ${ input . domain ? Prisma . sql `AND "domainId" = ${ input . domain } ` : Prisma . sql `` }
47- GROUP BY "date"
48- ORDER BY "date" ASC
49- ` ;
50-
51- // Fill in any missing dates with 0 values
52- const filledResult : DailyEmailUsage [ ] = [ ] ;
53- const endDateObj = new Date ( ) ;
5415
55- for ( let i = days ; i > - 1 ; i -- ) {
56- const dateStr = subDays ( endDateObj , i )
57- . toISOString ( )
58- . split ( "T" ) [ 0 ] as string ;
59- const existingData = result . find ( ( r ) => r . date === dateStr ) ;
16+ const response = await emailTimeSeries ( { team, days : input . days , domain : input . domain } )
6017
61- if ( existingData ) {
62- filledResult . push ( {
63- ...existingData ,
64- date : format ( dateStr , "MMM dd" ) ,
65- } ) ;
66- } else {
67- filledResult . push ( {
68- date : format ( dateStr , "MMM dd" ) ,
69- sent : 0 ,
70- delivered : 0 ,
71- opened : 0 ,
72- clicked : 0 ,
73- bounced : 0 ,
74- complained : 0 ,
75- } ) ;
76- }
77- }
78-
79- const totalCounts = result . reduce (
80- ( acc , curr ) => {
81- acc . sent += curr . sent ;
82- acc . delivered += curr . delivered ;
83- acc . opened += curr . opened ;
84- acc . clicked += curr . clicked ;
85- acc . bounced += curr . bounced ;
86- acc . complained += curr . complained ;
87- return acc ;
88- } ,
89- {
90- sent : 0 ,
91- delivered : 0 ,
92- opened : 0 ,
93- clicked : 0 ,
94- bounced : 0 ,
95- complained : 0 ,
96- }
97- ) ;
98-
99- return { result : filledResult , totalCounts } ;
18+ return response
10019 } ) ,
10120
10221 reputationMetricsData : teamProcedure
@@ -107,34 +26,8 @@ export const dashboardRouter = createTRPCRouter({
10726 )
10827 . query ( async ( { ctx, input } ) => {
10928 const { team } = ctx ;
29+ const response = await reputationMetricsData ( { team, domain : input . domain } )
11030
111- const reputations = await db . cumulatedMetrics . findMany ( {
112- where : {
113- teamId : team . id ,
114- ...( input . domain ? { domainId : input . domain } : { } ) ,
115- } ,
116- } ) ;
117-
118- const results = reputations . reduce (
119- ( acc , curr ) => {
120- acc . delivered += Number ( curr . delivered ) ;
121- acc . hardBounced += Number ( curr . hardBounced ) ;
122- acc . complained += Number ( curr . complained ) ;
123- return acc ;
124- } ,
125- { delivered : 0 , hardBounced : 0 , complained : 0 }
126- ) ;
127-
128- const resultWithRates = {
129- ...results ,
130- bounceRate : results . delivered
131- ? ( results . hardBounced / results . delivered ) * 100
132- : 0 ,
133- complaintRate : results . delivered
134- ? ( results . complained / results . delivered ) * 100
135- : 0 ,
136- } ;
137-
138- return resultWithRates ;
31+ return response ;
13932 } ) ,
14033} ) ;
0 commit comments