Browse Source

Merge pull request #100 from vernu/improve-billing-logic

improve billing service
pull/103/head
Israel Abebe 8 months ago
committed by GitHub
parent
commit
217c068bfb
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 2
      api/src/billing/billing.module.ts
  2. 20
      api/src/billing/billing.service.ts

2
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 { UsersModule } from 'src/users/users.module'
import { GatewayModule } from 'src/gateway/gateway.module' import { GatewayModule } from 'src/gateway/gateway.module'
import { PolarWebhookPayload, PolarWebhookPayloadSchema } from './schemas/polar-webhook-payload.schema' import { PolarWebhookPayload, PolarWebhookPayloadSchema } from './schemas/polar-webhook-payload.schema'
import { Device, DeviceSchema } from '../gateway/schemas/device.schema'
@Module({ @Module({
imports: [ imports: [
@ -17,6 +18,7 @@ import { PolarWebhookPayload, PolarWebhookPayloadSchema } from './schemas/polar-
{ name: Plan.name, schema: PlanSchema }, { name: Plan.name, schema: PlanSchema },
{ name: Subscription.name, schema: SubscriptionSchema }, { name: Subscription.name, schema: SubscriptionSchema },
{ name: PolarWebhookPayload.name, schema: PolarWebhookPayloadSchema }, { name: PolarWebhookPayload.name, schema: PolarWebhookPayloadSchema },
{ name: Device.name, schema: DeviceSchema },
]), ]),
forwardRef(() => AuthModule), forwardRef(() => AuthModule),
forwardRef(() => UsersModule), forwardRef(() => UsersModule),

20
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 { CheckoutResponseDTO, PlanDTO } from './billing.dto'
import { SMSDocument } from '../gateway/schemas/sms.schema' import { SMSDocument } from '../gateway/schemas/sms.schema'
import { SMS } 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 { validateEvent } from '@polar-sh/sdk/webhooks'
import { import {
PolarWebhookPayload, PolarWebhookPayload,
@ -27,6 +28,7 @@ export class BillingService {
private subscriptionModel: Model<SubscriptionDocument>, private subscriptionModel: Model<SubscriptionDocument>,
@InjectModel(User.name) private userModel: Model<UserDocument>, @InjectModel(User.name) private userModel: Model<UserDocument>,
@InjectModel(SMS.name) private smsModel: Model<SMSDocument>, @InjectModel(SMS.name) private smsModel: Model<SMSDocument>,
@InjectModel(Device.name) private deviceModel: Model<DeviceDocument>,
@InjectModel(PolarWebhookPayload.name) @InjectModel(PolarWebhookPayload.name)
private polarWebhookPayloadModel: Model<PolarWebhookPayloadDocument>, private polarWebhookPayloadModel: Model<PolarWebhookPayloadDocument>,
) { ) {
@ -311,7 +313,7 @@ export class BillingService {
let plan: PlanDocument let plan: PlanDocument
const subscription = await this.subscriptionModel.findOne({ const subscription = await this.subscriptionModel.findOne({
user,
user: user._id,
isActive: true, isActive: true,
}) })
@ -329,12 +331,16 @@ export class BillingService {
let hasReachedLimit = false let hasReachedLimit = false
let message = '' 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({ const processedSmsToday = await this.smsModel.countDocuments({
'device.user': userId,
device: { $in: deviceIds },
createdAt: { $gte: new Date(new Date().setHours(0, 0, 0, 0)) }, createdAt: { $gte: new Date(new Date().setHours(0, 0, 0, 0)) },
}) })
const processedSmsLastMonth = await this.smsModel.countDocuments({ const processedSmsLastMonth = await this.smsModel.countDocuments({
'device.user': userId,
device: { $in: deviceIds },
createdAt: { createdAt: {
$gte: new Date(new Date().setMonth(new Date().getMonth() - 1)), $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) 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({ const processedSmsToday = await this.smsModel.countDocuments({
'device.user': userId,
device: { $in: deviceIds },
createdAt: { $gte: new Date(new Date().setHours(0, 0, 0, 0)) }, createdAt: { $gte: new Date(new Date().setHours(0, 0, 0, 0)) },
}) })
const processedSmsLastMonth = await this.smsModel.countDocuments({ const processedSmsLastMonth = await this.smsModel.countDocuments({
'device.user': userId,
device: { $in: deviceIds },
createdAt: { createdAt: {
$gte: new Date(new Date().setMonth(new Date().getMonth() - 1)), $gte: new Date(new Date().setMonth(new Date().getMonth() - 1)),
}, },

Loading…
Cancel
Save