@@ -16,7 +16,6 @@ import { User, UserDocument } from '../users/schemas/user.schema'
1616import { CheckoutResponseDTO , PlanDTO } from './billing.dto'
1717import { SMSDocument } from '../gateway/schemas/sms.schema'
1818import { SMS } from '../gateway/schemas/sms.schema'
19- import { Device , DeviceDocument } from '../gateway/schemas/device.schema'
2019import { validateEvent } from '@polar-sh/sdk/webhooks'
2120import {
2221 PolarWebhookPayload ,
@@ -41,7 +40,6 @@ export class BillingService {
4140 private subscriptionModel : Model < SubscriptionDocument > ,
4241 @InjectModel ( User . name ) private userModel : Model < UserDocument > ,
4342 @InjectModel ( SMS . name ) private smsModel : Model < SMSDocument > ,
44- @InjectModel ( Device . name ) private deviceModel : Model < DeviceDocument > ,
4543 @InjectModel ( PolarWebhookPayload . name )
4644 private polarWebhookPayloadModel : Model < PolarWebhookPayloadDocument > ,
4745 @InjectModel ( CheckoutSession . name )
@@ -69,17 +67,13 @@ export class BillingService {
6967 } )
7068 . populate ( 'plan' )
7169
72- // Get user's devices and usage data
73- const userDevices = await this . deviceModel . find ( { user : user . _id } , '_id' )
74- const deviceIds = userDevices . map ( ( d ) => d . _id )
75-
7670 const processedSmsToday = await this . smsModel . countDocuments ( {
77- device : { $in : deviceIds } ,
71+ user : user . _id ,
7872 createdAt : { $gte : new Date ( new Date ( ) . setHours ( 0 , 0 , 0 , 0 ) ) } ,
7973 } )
8074
8175 const processedSmsLastMonth = await this . smsModel . countDocuments ( {
82- device : { $in : deviceIds } ,
76+ user : user . _id ,
8377 createdAt : {
8478 $gte : new Date ( new Date ( ) . setMonth ( new Date ( ) . getMonth ( ) - 1 ) ) ,
8579 } ,
@@ -568,16 +562,12 @@ export class BillingService {
568562 let hasReachedLimit = false
569563 let message = ''
570564
571- // Get user's devices and then count SMS
572- const userDevices = await this . deviceModel . find ( { user : user . _id } , '_id' )
573- const deviceIds = userDevices . map ( ( d ) => d . _id )
574-
575565 const processedSmsToday = await this . smsModel . countDocuments ( {
576- device : { $in : deviceIds } ,
566+ user : user . _id ,
577567 createdAt : { $gte : new Date ( new Date ( ) . setHours ( 0 , 0 , 0 , 0 ) ) } ,
578568 } )
579569 const processedSmsLastMonth = await this . smsModel . countDocuments ( {
580- device : { $in : deviceIds } ,
570+ user : user . _id ,
581571 createdAt : {
582572 $gte : new Date ( new Date ( ) . setMonth ( new Date ( ) . getMonth ( ) - 1 ) ) ,
583573 } ,
@@ -724,19 +714,13 @@ export class BillingService {
724714
725715 const effectiveLimits = this . getEffectiveLimits ( subscription , plan )
726716
727- // First get all devices belonging to the user
728- const userDevices = await this . deviceModel
729- . find ( { user : new Types . ObjectId ( userId ) } )
730- . select ( '_id' )
731- const deviceIds = userDevices . map ( ( device ) => device . _id )
732-
733717 const processedSmsToday = await this . smsModel . countDocuments ( {
734- device : { $in : deviceIds } ,
718+ user : new Types . ObjectId ( userId ) ,
735719 createdAt : { $gte : new Date ( new Date ( ) . setHours ( 0 , 0 , 0 , 0 ) ) } ,
736720 } )
737721
738722 const processedSmsLastMonth = await this . smsModel . countDocuments ( {
739- device : { $in : deviceIds } ,
723+ user : new Types . ObjectId ( userId ) ,
740724 createdAt : {
741725 $gte : new Date ( new Date ( ) . setMonth ( new Date ( ) . getMonth ( ) - 1 ) ) ,
742726 } ,
0 commit comments