@@ -32,15 +32,19 @@ interface ChartDataPoint {
3232}
3333
3434const calculateAverage = ( values : { value : number } [ ] ) =>
35- values . length > 0 ? values . reduce ( ( sum , item ) => sum + item . value , 0 ) / values . length : 0 ;
35+ values . length > 0
36+ ? values . reduce ( ( sum , item ) => sum + item . value , 0 ) / values . length
37+ : 0 ;
3638
3739const calculateTrend = ( dataPoints : { date : string ; value : number } [ ] ) => {
38- if ( ! dataPoints ?. length ) return null ;
40+ if ( ! dataPoints ?. length ) {
41+ return null ;
42+ }
3943
4044 const midPoint = Math . floor ( dataPoints . length / 2 ) ;
4145 const firstHalf = dataPoints . slice ( 0 , midPoint ) ;
4246 const secondHalf = dataPoints . slice ( midPoint ) ;
43-
47+
4448 const previousAverage = calculateAverage ( firstHalf ) ;
4549 const currentAverage = calculateAverage ( secondHalf ) ;
4650
@@ -50,15 +54,24 @@ const calculateTrend = (dataPoints: { date: string; value: number }[]) => {
5054 : { type : 'neutral' as const , value : 0 } ;
5155 }
5256
53- const percentageChange = ( ( currentAverage - previousAverage ) / previousAverage ) * 100 ;
54-
55- if ( percentageChange > TREND_THRESHOLD ) return { type : 'up' as const , value : Math . abs ( percentageChange ) } ;
56- if ( percentageChange < - TREND_THRESHOLD ) return { type : 'down' as const , value : Math . abs ( percentageChange ) } ;
57+ const percentageChange =
58+ ( ( currentAverage - previousAverage ) / previousAverage ) * 100 ;
59+
60+ if ( percentageChange > TREND_THRESHOLD ) {
61+ return { type : 'up' as const , value : Math . abs ( percentageChange ) } ;
62+ }
63+ if ( percentageChange < - TREND_THRESHOLD ) {
64+ return { type : 'down' as const , value : Math . abs ( percentageChange ) } ;
65+ }
5766 return { type : 'neutral' as const , value : Math . abs ( percentageChange ) } ;
5867} ;
5968
60- const fetchChartData = async ( websiteIds : string [ ] ) : Promise < Record < string , ProcessedMiniChartData > > => {
61- if ( ! websiteIds . length ) return { } ;
69+ const fetchChartData = async (
70+ websiteIds : string [ ]
71+ ) : Promise < Record < string , ProcessedMiniChartData > > => {
72+ if ( ! websiteIds . length ) {
73+ return { } ;
74+ }
6275
6376 const chartQuery = `
6477 WITH
@@ -91,12 +104,17 @@ const fetchChartData = async (websiteIds: string[]): Promise<Record<string, Proc
91104 date ASC
92105 ` ;
93106
94- const queryResults = await chQuery < ChartDataPoint > ( chartQuery , { websiteIds } ) ;
107+ const queryResults = await chQuery < ChartDataPoint > ( chartQuery , {
108+ websiteIds,
109+ } ) ;
95110
96- const groupedData = websiteIds . reduce ( ( acc , id ) => {
97- acc [ id ] = [ ] ;
98- return acc ;
99- } , { } as Record < string , { date : string ; value : number } [ ] > ) ;
111+ const groupedData = websiteIds . reduce (
112+ ( acc , id ) => {
113+ acc [ id ] = [ ] ;
114+ return acc ;
115+ } ,
116+ { } as Record < string , { date : string ; value : number } [ ] >
117+ ) ;
100118
101119 for ( const row of queryResults ) {
102120 groupedData [ row . websiteId ] ?. push ( {
@@ -106,12 +124,12 @@ const fetchChartData = async (websiteIds: string[]): Promise<Record<string, Proc
106124 }
107125
108126 const processedData : Record < string , ProcessedMiniChartData > = { } ;
109-
127+
110128 for ( const websiteId of websiteIds ) {
111129 const dataPoints = groupedData [ websiteId ] || [ ] ;
112130 const totalViews = dataPoints . reduce ( ( sum , point ) => sum + point . value , 0 ) ;
113131 const trend = calculateTrend ( dataPoints ) ;
114-
132+
115133 processedData [ websiteId ] = {
116134 data : dataPoints ,
117135 totalViews,
@@ -137,7 +155,10 @@ export const websitesRouter = createTRPCRouter({
137155 ttl : CACHE_DURATION ,
138156 tables : [ 'websites' ] ,
139157 queryFn : ( ) => {
140- const whereClause = buildWebsiteFilter ( ctx . user . id , input . organizationId ) ;
158+ const whereClause = buildWebsiteFilter (
159+ ctx . user . id ,
160+ input . organizationId
161+ ) ;
141162 return ctx . db . query . websites . findMany ( {
142163 where : whereClause ,
143164 orderBy : ( table , { desc } ) => [ desc ( table . createdAt ) ] ,
@@ -148,22 +169,25 @@ export const websitesRouter = createTRPCRouter({
148169
149170 listWithCharts : protectedProcedure
150171 . input ( z . object ( { organizationId : z . string ( ) . optional ( ) } ) . default ( { } ) )
151- . query ( async ( { ctx, input } ) => {
172+ . query ( ( { ctx, input } ) => {
152173 const chartsListCacheKey = `listWithCharts:${ ctx . user . id } :${ input . organizationId || '' } ` ;
153-
174+
154175 return websiteCache . withCache ( {
155176 key : chartsListCacheKey ,
156177 ttl : CACHE_DURATION ,
157178 tables : [ 'websites' , 'member' ] ,
158179 queryFn : async ( ) => {
159- const whereClause = buildWebsiteFilter ( ctx . user . id , input . organizationId ) ;
160-
180+ const whereClause = buildWebsiteFilter (
181+ ctx . user . id ,
182+ input . organizationId
183+ ) ;
184+
161185 const websitesList = await ctx . db . query . websites . findMany ( {
162186 where : whereClause ,
163187 orderBy : ( table , { desc } ) => [ desc ( table . createdAt ) ] ,
164188 } ) ;
165189
166- const websiteIds = websitesList . map ( site => site . id ) ;
190+ const websiteIds = websitesList . map ( ( site ) => site . id ) ;
167191 const chartData = await fetchChartData ( websiteIds ) ;
168192
169193 return {
@@ -202,10 +226,17 @@ export const websitesRouter = createTRPCRouter({
202226 }
203227 }
204228
205- const billingCustomerId = await getBillingCustomerId ( ctx . user . id , input . organizationId ) ;
206- const creationLimitCheck = await checkAndTrackWebsiteCreation ( billingCustomerId ) ;
229+ const billingCustomerId = await getBillingCustomerId (
230+ ctx . user . id ,
231+ input . organizationId
232+ ) ;
233+ const creationLimitCheck =
234+ await checkAndTrackWebsiteCreation ( billingCustomerId ) ;
207235 if ( ! creationLimitCheck . allowed ) {
208- throw new TRPCError ( { code : 'BAD_REQUEST' , message : creationLimitCheck . error } ) ;
236+ throw new TRPCError ( {
237+ code : 'BAD_REQUEST' ,
238+ message : creationLimitCheck . error ,
239+ } ) ;
209240 }
210241
211242 const domainToCreate = buildFullDomain ( input . domain , input . subdomain ) ;
@@ -219,7 +250,9 @@ export const websitesRouter = createTRPCRouter({
219250 } ) ;
220251
221252 if ( duplicateWebsite ) {
222- const scopeDescription = input . organizationId ? 'in this organization' : 'for your account' ;
253+ const scopeDescription = input . organizationId
254+ ? 'in this organization'
255+ : 'for your account' ;
223256 throw new TRPCError ( {
224257 code : 'CONFLICT' ,
225258 message : `A website with the domain "${ domainToCreate } " already exists ${ scopeDescription } .` ,
@@ -257,7 +290,11 @@ export const websitesRouter = createTRPCRouter({
257290 update : protectedProcedure
258291 . input ( updateWebsiteSchema )
259292 . mutation ( async ( { ctx, input } ) => {
260- const websiteToUpdate = await authorizeWebsiteAccess ( ctx , input . id , 'update' ) ;
293+ const websiteToUpdate = await authorizeWebsiteAccess (
294+ ctx ,
295+ input . id ,
296+ 'update'
297+ ) ;
261298
262299 const [ updatedWebsite ] = await ctx . db
263300 . update ( websites )
@@ -287,8 +324,15 @@ export const websitesRouter = createTRPCRouter({
287324 delete : protectedProcedure
288325 . input ( z . object ( { id : z . string ( ) } ) )
289326 . mutation ( async ( { ctx, input } ) => {
290- const websiteToDelete = await authorizeWebsiteAccess ( ctx , input . id , 'delete' ) ;
291- const billingCustomerId = await getBillingCustomerId ( ctx . user . id , websiteToDelete . organizationId ) ;
327+ const websiteToDelete = await authorizeWebsiteAccess (
328+ ctx ,
329+ input . id ,
330+ 'delete'
331+ ) ;
332+ const billingCustomerId = await getBillingCustomerId (
333+ ctx . user . id ,
334+ websiteToDelete . organizationId
335+ ) ;
292336
293337 await Promise . all ( [
294338 ctx . db . delete ( websites ) . where ( eq ( websites . id , input . id ) ) ,
0 commit comments