Skip to content

Commit 6bb7a04

Browse files
authored
feat: usage (#60)
1 parent a4b5623 commit 6bb7a04

File tree

3 files changed

+13
-17
lines changed

3 files changed

+13
-17
lines changed

libs-private/service-logic/services/usage/useUsageService.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,23 @@ import { BResult } from "@event-inc/types";
44
import { Usage } from "@event-inc/types/usage";
55
import { resultErr, resultOk } from "@event-inc/utils";
66
import jwt from 'jsonwebtoken';
7-
import { format } from 'date-fns';
87
import { useGenericCRUDService } from "../genericCRUD";
98
import { Services } from "@libs-private/data-models";
109

1110
const SERVICE_NAME = Services.Usage;
1211

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+
1324
export const useUsageService = (ctx: Context, ownership: Ownership) => {
1425
const { find, findAndUpdate, updateMany } = useGenericCRUDService(ctx, SERVICE_NAME, ownership, {
1526
DISABLE_ADDING_OWNERSHIP_CHECK: true,
@@ -51,10 +62,7 @@ export const useUsageService = (ctx: Context, ownership: Ownership) => {
5162
const environment = secretKey.startsWith('sk_live') ? 'live' : 'test';
5263
const clientId = decoded?.payload?.buildableId;
5364
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);
5866

5967
// First, try to find the existing usage record
6068
const existingUsage = (await find<Usage>({ query: { clientId } })).unwrap();

package-lock.json

Lines changed: 0 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@
5454
"analytics-node": "^6.2.0",
5555
"axios": "^1.2.0",
5656
"bcryptjs": "^2.4.3",
57-
"date-fns": "^4.1.0",
5857
"dotenv": "^16.4.5",
5958
"ethers": "^5.7.2",
6059
"jsonpack": "^1.1.5",

0 commit comments

Comments
 (0)