@@ -88,104 +88,6 @@ export default {
8888
8989 switch ( event . type ) {
9090
91- case 'customer.subscription.created' :
92-
93- const subscriptionCreated = event . data . object ;
94- const { key, buildKitIntegrationLimit, buildKitUsageLimit, chatUsageLimit } = analyzeSubscription ( event . data . object ?. items ?. data ) ;
95-
96- if ( key && subscriptionCreated ?. status === 'active' ) {
97- const billing = {
98- throughput : parseInt ( process . env . DEFAULT_CLIENT_THROUGHPUT ) || 500 ,
99- buildKitIntegrationLimit,
100- buildKitUsageLimit,
101- chatUsageLimit,
102- provider : 'stripe' ,
103- customerId : subscriptionCreated ?. customer ,
104- subscription : {
105- id : subscriptionCreated . id ,
106- endDate : subscriptionCreated . current_period_end ,
107- valid : true ,
108- key,
109- }
110- }
111-
112- const client = await ctx . broker . call ( 'v1.clients.updateBillingByCustomerId' , {
113- customerId : subscriptionCreated ?. customer ,
114- billing
115- } ) ;
116-
117- await makeHttpNetworkCall ( {
118- url : TRACK_EVENT ,
119- method : 'POST' ,
120- data : {
121- path : 't' ,
122- data : {
123- event : 'Created Subscription' ,
124- properties : {
125- ...subscriptionCreated ,
126- version : "pica-1.0.0"
127- } ,
128- context,
129- userId : client ?. author ?. _id
130- }
131- }
132- } ) ;
133- }
134-
135- break ;
136-
137- case 'customer.subscription.updated' :
138- const subscriptionUpdated = event . data . object ;
139-
140- const {
141- key : subscriptionKey ,
142- buildKitIntegrationLimit : updatedBuildKitIntegrationLimit ,
143- buildKitUsageLimit : updatedBuildKitUsageLimit ,
144- chatUsageLimit : updatedChatUsageLimit
145- } = analyzeSubscription ( subscriptionUpdated ?. items ?. data ) ;
146-
147- const billing = {
148- throughput : parseInt ( process . env . DEFAULT_CLIENT_THROUGHPUT ) || 500 ,
149- buildKitIntegrationLimit : updatedBuildKitIntegrationLimit ,
150- buildKitUsageLimit : updatedBuildKitUsageLimit ,
151- chatUsageLimit : updatedChatUsageLimit ,
152- provider : 'stripe' ,
153- customerId : subscriptionUpdated ?. customer ,
154- subscription : {
155- id : subscriptionUpdated ?. id ,
156- endDate : subscriptionUpdated ?. current_period_end ,
157- valid : true ,
158- key : subscriptionKey ,
159- } ,
160- } ;
161-
162- const updatedClient = await ctx . broker . call (
163- 'v1.clients.updateBillingByCustomerId' ,
164- {
165- customerId : subscriptionUpdated ?. customer ,
166- billing,
167- }
168- ) ;
169-
170- await makeHttpNetworkCall ( {
171- url : TRACK_EVENT ,
172- method : 'POST' ,
173- data : {
174- path : 't' ,
175- data : {
176- event : 'Updated Subscription' ,
177- properties : {
178- ...subscriptionUpdated ,
179- version : "pica-1.0.0"
180- } ,
181- context,
182- userId : updatedClient ?. author ?. _id ,
183- }
184- }
185- } ) ;
186-
187- break ;
188-
18991 case 'customer.subscription.deleted' :
19092 const subscriptionDeleted = event . data . object ;
19193
@@ -213,6 +115,7 @@ export default {
213115 customerId : subscriptionCreated ?. customer ,
214116 subscription : {
215117 id : subscriptionCreated ?. id ,
118+ startDate : subscriptionCreated ?. current_period_start ,
216119 endDate : subscriptionCreated ?. current_period_end ,
217120 key : 'sub::free' ,
218121 valid : true ,
@@ -315,13 +218,29 @@ export default {
315218 invoicePaymentSucceeded ?. subscription as string
316219 ) ;
317220
318- const succeededInvoiceClient = await ctx . broker . call (
319- 'v1.clients.updateOnInvoicePaymentSuccess' ,
320- {
321- customerId : invoicePaymentSucceeded ?. customer ,
221+ const { key, buildKitIntegrationLimit, buildKitUsageLimit, chatUsageLimit } = analyzeSubscription ( subscription ?. items ?. data ) ;
222+
223+
224+ const billing = {
225+ throughput : parseInt ( process . env . DEFAULT_CLIENT_THROUGHPUT ) || 500 ,
226+ buildKitIntegrationLimit,
227+ buildKitUsageLimit,
228+ chatUsageLimit,
229+ provider : 'stripe' ,
230+ customerId : subscription ?. customer ,
231+ subscription : {
232+ id : subscription ?. id ,
233+ startDate : subscription ?. current_period_start ,
322234 endDate : subscription ?. current_period_end ,
235+ valid : true ,
236+ key,
323237 }
324- ) ;
238+ }
239+
240+ const updatedClient = await ctx . broker . call ( 'v1.clients.updateBillingByCustomerId' , {
241+ customerId : subscription ?. customer ,
242+ billing
243+ } )
325244
326245 await makeHttpNetworkCall ( {
327246 url : TRACK_EVENT ,
@@ -335,7 +254,7 @@ export default {
335254 version : "pica-1.0.0"
336255 } ,
337256 context,
338- userId : succeededInvoiceClient ?. author ?. _id ,
257+ userId : updatedClient ?. author ?. _id ,
339258 }
340259 }
341260 } ) ;
0 commit comments