diff --git a/api/src/billing/billing.module.ts b/api/src/billing/billing.module.ts index 2c03b9f..48c4da2 100644 --- a/api/src/billing/billing.module.ts +++ b/api/src/billing/billing.module.ts @@ -10,6 +10,7 @@ import { AuthModule } from 'src/auth/auth.module' import { UsersModule } from 'src/users/users.module' import { GatewayModule } from 'src/gateway/gateway.module' import { PolarWebhookPayload, PolarWebhookPayloadSchema } from './schemas/polar-webhook-payload.schema' +import { Device, DeviceSchema } from '../gateway/schemas/device.schema' @Module({ imports: [ @@ -17,6 +18,7 @@ import { PolarWebhookPayload, PolarWebhookPayloadSchema } from './schemas/polar- { name: Plan.name, schema: PlanSchema }, { name: Subscription.name, schema: SubscriptionSchema }, { name: PolarWebhookPayload.name, schema: PolarWebhookPayloadSchema }, + { name: Device.name, schema: DeviceSchema }, ]), forwardRef(() => AuthModule), forwardRef(() => UsersModule), diff --git a/api/src/billing/billing.service.ts b/api/src/billing/billing.service.ts index cf6deb5..8ff3c93 100644 --- a/api/src/billing/billing.service.ts +++ b/api/src/billing/billing.service.ts @@ -11,6 +11,7 @@ import { User, UserDocument } from '../users/schemas/user.schema' import { CheckoutResponseDTO, PlanDTO } from './billing.dto' import { SMSDocument } from '../gateway/schemas/sms.schema' import { SMS } from '../gateway/schemas/sms.schema' +import { Device, DeviceDocument } from '../gateway/schemas/device.schema' import { validateEvent } from '@polar-sh/sdk/webhooks' import { PolarWebhookPayload, @@ -27,6 +28,7 @@ export class BillingService { private subscriptionModel: Model, @InjectModel(User.name) private userModel: Model, @InjectModel(SMS.name) private smsModel: Model, + @InjectModel(Device.name) private deviceModel: Model, @InjectModel(PolarWebhookPayload.name) private polarWebhookPayloadModel: Model, ) { @@ -311,7 +313,7 @@ export class BillingService { let plan: PlanDocument const subscription = await this.subscriptionModel.findOne({ - user, + user: user._id, isActive: true, }) @@ -329,12 +331,16 @@ export class BillingService { let hasReachedLimit = false let message = '' + // Get user's devices and then count SMS + const userDevices = await this.deviceModel.find({ user: userId }, '_id') + const deviceIds = userDevices.map(d => d._id) + const processedSmsToday = await this.smsModel.countDocuments({ - 'device.user': userId, + device: { $in: deviceIds }, createdAt: { $gte: new Date(new Date().setHours(0, 0, 0, 0)) }, }) const processedSmsLastMonth = await this.smsModel.countDocuments({ - 'device.user': userId, + device: { $in: deviceIds }, createdAt: { $gte: new Date(new Date().setMonth(new Date().getMonth() - 1)), }, @@ -421,13 +427,17 @@ export class BillingService { const plan = await this.planModel.findById(subscription.plan) + // First get all devices belonging to the user + const userDevices = await this.deviceModel.find({ user: userId }).select('_id') + const deviceIds = userDevices.map(device => device._id) + const processedSmsToday = await this.smsModel.countDocuments({ - 'device.user': userId, + device: { $in: deviceIds }, createdAt: { $gte: new Date(new Date().setHours(0, 0, 0, 0)) }, }) const processedSmsLastMonth = await this.smsModel.countDocuments({ - 'device.user': userId, + device: { $in: deviceIds }, createdAt: { $gte: new Date(new Date().setMonth(new Date().getMonth() - 1)), },