Download apps/backend/src/api/routes/billing.controller.ts from Leon4gr45/postio: direct link, hf CLI and curl.
- Browser
- Download file 5.96 kB
-
https://huggingface.co/spaces/Leon4gr45/postio/resolve/main/apps/backend/src/api/routes/billing.controller.ts
- Command line
-
hf download hf://spaces/Leon4gr45/postio/apps/backend/src/api/routes/billing.controller.ts
-
curl -L -o billing.controller.ts https://huggingface.co/spaces/Leon4gr45/postio/resolve/main/apps/backend/src/api/routes/billing.controller.ts
5.96 kB
| import { Body, Controller, Get, HttpException, Param, Post, Req } from '@nestjs/common'; | |
| import { SubscriptionService } from '@gitroom/nestjs-libraries/database/prisma/subscriptions/subscription.service'; | |
| import { StripeService } from '@gitroom/nestjs-libraries/services/stripe.service'; | |
| import { GetOrgFromRequest } from '@gitroom/nestjs-libraries/user/org.from.request'; | |
| import { Organization, User } from '@prisma/client'; | |
| import { BillingSubscribeDto } from '@gitroom/nestjs-libraries/dtos/billing/billing.subscribe.dto'; | |
| import { ApiTags } from '@nestjs/swagger'; | |
| import { GetUserFromRequest } from '@gitroom/nestjs-libraries/user/user.from.request'; | |
| import { NotificationService } from '@gitroom/nestjs-libraries/database/prisma/notifications/notification.service'; | |
| import { Request } from 'express'; | |
| import { AuthService } from '@gitroom/helpers/auth/auth.service'; | |
| ('Billing') | |
| ('/billing') | |
| export class BillingController { | |
| constructor( | |
| private _subscriptionService: SubscriptionService, | |
| private _stripeService: StripeService, | |
| private _notificationService: NotificationService | |
| ) {} | |
| ('/check/:id') | |
| async checkId( | |
| () org: Organization, | |
| ('id') body: string | |
| ) { | |
| return { | |
| status: await this._stripeService.checkSubscription(org.id, body), | |
| }; | |
| } | |
| ('/check-discount') | |
| async checkDiscount(() org: Organization) { | |
| return { | |
| offerCoupon: !(await this._stripeService.checkDiscount(org.paymentId)) | |
| ? false | |
| : AuthService.signJWT({ discount: true }), | |
| }; | |
| } | |
| ('/apply-discount') | |
| async applyDiscount(() org: Organization) { | |
| await this._stripeService.applyDiscount(org.paymentId); | |
| } | |
| ('/finish-trial') | |
| async finishTrial(() org: Organization) { | |
| try { | |
| await this._stripeService.finishTrial(org.paymentId); | |
| } catch (err) {} | |
| return { | |
| finish: true, | |
| }; | |
| } | |
| ('/is-trial-finished') | |
| async isTrialFinished(() org: Organization) { | |
| return { | |
| finished: !org.isTrailing, | |
| }; | |
| } | |
| ('/embedded') | |
| embedded( | |
| () org: Organization, | |
| () user: User, | |
| () body: BillingSubscribeDto, | |
| () req: Request | |
| ) { | |
| const uniqueId = req?.cookies?.track; | |
| return this._stripeService.embedded( | |
| uniqueId, | |
| org.id, | |
| user.id, | |
| body, | |
| org.allowTrial | |
| ); | |
| } | |
| ('/subscribe') | |
| subscribe( | |
| () org: Organization, | |
| () user: User, | |
| () body: BillingSubscribeDto, | |
| () req: Request | |
| ) { | |
| const uniqueId = req?.cookies?.track; | |
| return this._stripeService.subscribe( | |
| uniqueId, | |
| org.id, | |
| user.id, | |
| body, | |
| org.allowTrial | |
| ); | |
| } | |
| ('/portal') | |
| async modifyPayment(() org: Organization) { | |
| const customer = await this._stripeService.getCustomerByOrganizationId( | |
| org.id | |
| ); | |
| const { url } = await this._stripeService.createBillingPortalLink(customer); | |
| return { | |
| portal: url, | |
| }; | |
| } | |
| ('/') | |
| getCurrentBilling(() org: Organization) { | |
| return this._subscriptionService.getSubscriptionByOrganizationId(org.id); | |
| } | |
| ('/cancel') | |
| async cancel( | |
| () org: Organization, | |
| () user: User, | |
| () body: { feedback: string } | |
| ) { | |
| await this._notificationService.sendEmail( | |
| process.env.EMAIL_FROM_ADDRESS, | |
| 'Subscription Cancelled', | |
| `Organization ${org.name} has cancelled their subscription because: ${body.feedback}`, | |
| user.email | |
| ); | |
| return this._stripeService.setToCancel(org.id); | |
| } | |
| ('/prorate') | |
| prorate( | |
| () org: Organization, | |
| () body: BillingSubscribeDto | |
| ) { | |
| return this._stripeService.prorate(org.id, body); | |
| } | |
| ('/charges') | |
| async getCharges( | |
| () user: User, | |
| () org: Organization | |
| ) { | |
| if (!user.isSuperAdmin) { | |
| throw new HttpException('Unauthorized', 400); | |
| } | |
| return this._stripeService.getCharges(org.id); | |
| } | |
| ('/refund-charges') | |
| async refundCharges( | |
| () user: User, | |
| () org: Organization, | |
| () body: { chargeIds: string[] } | |
| ) { | |
| if (!user.isSuperAdmin) { | |
| throw new HttpException('Unauthorized', 400); | |
| } | |
| return this._stripeService.refundCharges(org.id, body.chargeIds); | |
| } | |
| ('/cancel-subscription') | |
| async cancelSubscription( | |
| () user: User, | |
| () org: Organization | |
| ) { | |
| if (!user.isSuperAdmin) { | |
| throw new HttpException('Unauthorized', 400); | |
| } | |
| return this._stripeService.cancelSubscription(org.id); | |
| } | |
| ('/chatbase-refund/preview') | |
| chatbaseRefundPreview(() org: Organization) { | |
| return this._stripeService.chatbaseRefundPreview(org.id); | |
| } | |
| ('/chatbase-refund') | |
| async chatbaseRefund( | |
| () user: User, | |
| () org: Organization | |
| ) { | |
| const refund = await this._stripeService.chatbaseRefund(org.id); | |
| if (refund.refunded) { | |
| await this._notificationService.sendEmail( | |
| process.env.EMAIL_FROM_ADDRESS, | |
| 'Refund issued from Chatbase', | |
| `Organization ${org.name} received a refund of ${refund.amount} ${refund.currency} and their subscription was cancelled`, | |
| user.email | |
| ); | |
| } | |
| return refund; | |
| } | |
| ('/add-subscription') | |
| async addSubscription( | |
| () body: { subscription: string }, | |
| () user: User, | |
| () org: Organization | |
| ) { | |
| if (!user.isSuperAdmin) { | |
| throw new Error('Unauthorized'); | |
| } | |
| await this._subscriptionService.addSubscription( | |
| org.id, | |
| user.id, | |
| body.subscription | |
| ); | |
| } | |
| } | |