@@ -3,7 +3,6 @@ import { Ownership } from "@libs-private/data-models";
3
3
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
- import jwt from 'jsonwebtoken' ;
7
6
import { useGenericCRUDService } from "../genericCRUD" ;
8
7
import { Services } from "@libs-private/data-models" ;
9
8
@@ -35,7 +34,6 @@ export const useUsageService = (ctx: Context, ownership: Ownership) => {
35
34
try {
36
35
const headers = ( ctx . meta as any ) . request . headers ;
37
36
const secretKey = headers [ 'x-pica-secret' ] ;
38
- const authorization = headers [ 'authorization' ] ;
39
37
40
38
if ( ! secretKey ) {
41
39
return resultErr < 'service' > (
@@ -47,20 +45,9 @@ export const useUsageService = (ctx: Context, ownership: Ownership) => {
47
45
) ;
48
46
}
49
47
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 ;
62
50
const environment = secretKey . startsWith ( 'sk_live' ) ? 'live' : 'test' ;
63
- const clientId = decoded ?. payload ?. buildableId ;
64
51
const currentDate = new Date ( ) ;
65
52
const { daily : dailyKey , monthly : monthlyKey , yearly : yearlyKey } = formatDate ( currentDate ) ;
66
53
@@ -121,7 +108,6 @@ export const useUsageService = (ctx: Context, ownership: Ownership) => {
121
108
122
109
const headers = ( ctx . meta as any ) . request . headers ;
123
110
const secretKey = headers [ 'x-pica-secret' ] ;
124
- const authorization = headers [ 'authorization' ] ;
125
111
126
112
if ( ! secretKey ) {
127
113
return resultErr < 'service' > (
@@ -132,20 +118,9 @@ export const useUsageService = (ctx: Context, ownership: Ownership) => {
132
118
false
133
119
) ;
134
120
}
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 ;
149
124
150
125
const usage = ( await find < Usage > ( { query : { clientId } } ) ) . unwrap ( ) ;
151
126
0 commit comments