@@ -4,12 +4,23 @@ import { BResult } from "@event-inc/types";
4
4
import { Usage } from "@event-inc/types/usage" ;
5
5
import { resultErr , resultOk } from "@event-inc/utils" ;
6
6
import jwt from 'jsonwebtoken' ;
7
- import { format } from 'date-fns' ;
8
7
import { useGenericCRUDService } from "../genericCRUD" ;
9
8
import { Services } from "@libs-private/data-models" ;
10
9
11
10
const SERVICE_NAME = Services . Usage ;
12
11
12
+ const formatDate = ( date : Date ) => {
13
+ const year = date . getFullYear ( ) ;
14
+ const month = String ( date . getMonth ( ) + 1 ) . padStart ( 2 , '0' ) ;
15
+ const day = String ( date . getDate ( ) ) . padStart ( 2 , '0' ) ;
16
+
17
+ return {
18
+ daily : `${ year } -${ month } -${ day } ` ,
19
+ monthly : `${ year } -${ month } ` ,
20
+ yearly : `${ year } `
21
+ } ;
22
+ } ;
23
+
13
24
export const useUsageService = ( ctx : Context , ownership : Ownership ) => {
14
25
const { find, findAndUpdate, updateMany } = useGenericCRUDService ( ctx , SERVICE_NAME , ownership , {
15
26
DISABLE_ADDING_OWNERSHIP_CHECK : true ,
@@ -51,10 +62,7 @@ export const useUsageService = (ctx: Context, ownership: Ownership) => {
51
62
const environment = secretKey . startsWith ( 'sk_live' ) ? 'live' : 'test' ;
52
63
const clientId = decoded ?. payload ?. buildableId ;
53
64
const currentDate = new Date ( ) ;
54
-
55
- const dailyKey = format ( currentDate , 'yyyy-MM-dd' ) ;
56
- const monthlyKey = format ( currentDate , 'yyyy-MM' ) ;
57
- const yearlyKey = format ( currentDate , 'yyyy' ) ;
65
+ const { daily : dailyKey , monthly : monthlyKey , yearly : yearlyKey } = formatDate ( currentDate ) ;
58
66
59
67
// First, try to find the existing usage record
60
68
const existingUsage = ( await find < Usage > ( { query : { clientId } } ) ) . unwrap ( ) ;
0 commit comments