From 3f63005c60c7e64afadf4e9bc8c2228ba73774b3 Mon Sep 17 00:00:00 2001 From: isra el Date: Sat, 20 Apr 2024 14:06:40 +0300 Subject: [PATCH] feat(api): track received sms count stat --- api/src/gateway/gateway.service.ts | 19 +++++++++++++++++-- api/src/gateway/schemas/device.schema.ts | 3 +++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/api/src/gateway/gateway.service.ts b/api/src/gateway/gateway.service.ts index 4ec3a86..384cfbf 100644 --- a/api/src/gateway/gateway.service.ts +++ b/api/src/gateway/gateway.service.ts @@ -170,6 +170,16 @@ export class GatewayService { receivedAt: dto.receivedAt, }) + this.deviceModel + .findByIdAndUpdate(deviceId, { + $inc: { receivedSMSCount: 1 }, + }) + .exec() + .catch((e) => { + console.log('Failed to update receivedSMSCount') + console.log(e) + }) + // TODO: Implement webhook to forward received SMS to user's callback URL return sms @@ -207,15 +217,20 @@ export class GatewayService { const devices = await this.deviceModel.find({ user: user._id }) const apiKeys = await this.authService.getUserApiKeys(user) - const totalSMSCount = devices.reduce((acc, device) => { + const totalSentSMSCount = devices.reduce((acc, device) => { return acc + (device.sentSMSCount || 0) }, 0) + const totalReceivedSMSCount = devices.reduce((acc, device) => { + return acc + (device.receivedSMSCount || 0) + }, 0) + const totalDeviceCount = devices.length const totalApiKeyCount = apiKeys.length return { - totalSMSCount, + totalSentSMSCount, + totalReceivedSMSCount, totalDeviceCount, totalApiKeyCount, } diff --git a/api/src/gateway/schemas/device.schema.ts b/api/src/gateway/schemas/device.schema.ts index 4bc67f9..da2dc21 100644 --- a/api/src/gateway/schemas/device.schema.ts +++ b/api/src/gateway/schemas/device.schema.ts @@ -46,6 +46,9 @@ export class Device { @Prop({ type: Number, default: 0 }) sentSMSCount: number + + @Prop({ type: Number, default: 0 }) + receivedSMSCount: number } export const DeviceSchema = SchemaFactory.createForClass(Device)