Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

webhook update

Blue Mouse7b218a8cbb30
diff --git a/src/user/user.service.ts b/src/user/user.service.ts
index cf6d138a1..e23a9ea0d 100644
--- a/src/user/user.service.ts
+++ b/src/user/user.service.ts
@@ -29,9 +29,12 @@ export class UserService {
})
}
-
async update(id: string, update: Record<string, unknown>): Promise<any> {
- return this.usersRepository.update({id}, update)
+ return this.usersRepository.update({ id }, update)
+ }
+
+ async updateByEmail(email: string, update: Record<string, unknown>): Promise<any> {
+ return this.usersRepository.update({ email }, update)
}
async delete(id: string): Promise<any> {
diff --git a/src/webhook/webhook.controller.ts b/src/webhook/webhook.controller.ts
index 3ee2df6f6..ecff7dfc6 100644
--- a/src/webhook/webhook.controller.ts
+++ b/src/webhook/webhook.controller.ts
@@ -1,16 +1,22 @@
import {
- Controller, Body, Post, Headers, BadRequestException,
+ Controller, Body, Post, Headers, BadRequestException, NotFoundException,
} from '@nestjs/common'
import { ApiTags } from '@nestjs/swagger'
import * as _isEmpty from 'lodash/isEmpty'
+import * as _keys from 'lodash/keys'
+import * as _find from 'lodash/find'
-import { PlanCode } from '../user/entities/user.entity'
+import {
+ PlanCode, ACCOUNT_PLANS,
+} from '../user/entities/user.entity'
import { UserService } from '../user/user.service'
import { AppLoggerService } from '../logger/logger.service'
import { STRIPE_WH_SECRET, STRIPE_SECRET } from '../common/constants'
const stripe = require('stripe')(STRIPE_SECRET)
+const PLANS_LIST = _keys(ACCOUNT_PLANS)
+
@ApiTags('Webhook')
@Controller('webhook')
export class WebhookController {
@@ -57,6 +63,21 @@ export class WebhookController {
})
}
+ if (dataObject['billing_reason'] == 'subscription_update') {
+ const { customer_email } = dataObject
+ const { id } = dataObject.lines.data[0].price
+
+ const planID = _find(PLANS_LIST, (pid) => ACCOUNT_PLANS[pid].priceId === id)
+
+ if (_isEmpty(planID)) {
+ throw new NotFoundException('The provided price ID seems to be incorrect..')
+ }
+
+ await this.userService.updateByEmail(customer_email, {
+ planCode: planID,
+ })
+ }
+
break
}
case 'invoice.payment_failed': {