Skip to content

Commit 9f6637b

Browse files
authored
refactor: usage (#61)
1 parent 6bb7a04 commit 9f6637b

File tree

1 file changed

+5
-30
lines changed

1 file changed

+5
-30
lines changed

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

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { Ownership } from "@libs-private/data-models";
33
import { BResult } from "@event-inc/types";
44
import { Usage } from "@event-inc/types/usage";
55
import { resultErr, resultOk } from "@event-inc/utils";
6-
import jwt from 'jsonwebtoken';
76
import { useGenericCRUDService } from "../genericCRUD";
87
import { Services } from "@libs-private/data-models";
98

@@ -35,7 +34,6 @@ export const useUsageService = (ctx: Context, ownership: Ownership) => {
3534
try {
3635
const headers = (ctx.meta as any).request.headers;
3736
const secretKey = headers['x-pica-secret'];
38-
const authorization = headers['authorization'];
3937

4038
if (!secretKey) {
4139
return resultErr<'service'>(
@@ -47,20 +45,9 @@ export const useUsageService = (ctx: Context, ownership: Ownership) => {
4745
);
4846
}
4947

50-
if (!authorization) {
51-
return resultErr<'service'>(
52-
false,
53-
'service_4000',
54-
'User is not authenticated',
55-
'buildable-core',
56-
false
57-
);
58-
}
59-
60-
const authToken = authorization.split(' ')[1];
61-
const decoded = jwt.decode(authToken, { complete: true }) as any;
48+
// Get the buildableId from ctx.meta.buildable._id
49+
const clientId = (ctx.meta as any).buildable._id;
6250
const environment = secretKey.startsWith('sk_live') ? 'live' : 'test';
63-
const clientId = decoded?.payload?.buildableId;
6451
const currentDate = new Date();
6552
const { daily: dailyKey, monthly: monthlyKey, yearly: yearlyKey } = formatDate(currentDate);
6653

@@ -121,7 +108,6 @@ export const useUsageService = (ctx: Context, ownership: Ownership) => {
121108

122109
const headers = (ctx.meta as any).request.headers;
123110
const secretKey = headers['x-pica-secret'];
124-
const authorization = headers['authorization'];
125111

126112
if (!secretKey) {
127113
return resultErr<'service'>(
@@ -132,20 +118,9 @@ export const useUsageService = (ctx: Context, ownership: Ownership) => {
132118
false
133119
);
134120
}
135-
136-
if (!authorization) {
137-
return resultErr<'service'>(
138-
false,
139-
'service_4000',
140-
'User is not authenticated',
141-
'buildable-core',
142-
false
143-
);
144-
}
145-
146-
const authToken = authorization.split(' ')[1];
147-
const decoded = jwt.decode(authToken, { complete: true }) as any;
148-
const clientId = decoded?.payload?.buildableId;
121+
122+
// Get the buildableId from ctx.meta.buildable._id
123+
const clientId = (ctx.meta as any).buildable?._id;
149124

150125
const usage = (await find<Usage>({ query: { clientId } })).unwrap();
151126

0 commit comments

Comments
 (0)