Skip to content

Commit 50e1314

Browse files
committed
feat: get subscription portal url api
1 parent bd1e358 commit 50e1314

File tree

4 files changed

+28
-0
lines changed

4 files changed

+28
-0
lines changed

apps/api/src/users/resolvers/user.resolver.ts

+11
Original file line numberDiff line numberDiff line change
@@ -146,4 +146,15 @@ export class UserResolver extends BaseResolver(User, {
146146

147147
return { success: true }
148148
}
149+
150+
@Query(() => GraphQLString)
151+
@UseGuards(GraphqlGuard)
152+
async subscriptionPortalUrl(@UserId() userId: Types.ObjectId): Promise<string> {
153+
const user = await this.userService.findOne({ _id: userId })
154+
if (!user) {
155+
throw new Error('User not found')
156+
}
157+
const url = await this.subscriptionService.getSubscriptionPortal(user)
158+
return url
159+
}
149160
}

generated/graphql.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1725,6 +1725,7 @@ export interface UserDatabaseConnection {
17251725
export interface IQuery {
17261726
user(id: string): User | Promise<User>;
17271727
viewer(): User | Promise<User>;
1728+
subscriptionPortalUrl(): string | Promise<string>;
17281729
viewerContact(): ViewerContact | Promise<ViewerContact>;
17291730
accountCredential(id: string): AccountCredential | Promise<AccountCredential>;
17301731
accountCredentials(paging?: Nullable<CursorPaging>, filter?: Nullable<AccountCredentialFilter>, sorting?: Nullable<AccountCredentialSort[]>): AccountCredentialConnection | Promise<AccountCredentialConnection>;

generated/schema.graphql

+1
Original file line numberDiff line numberDiff line change
@@ -1175,6 +1175,7 @@ type Query {
11751175
id: ID!
11761176
): User!
11771177
viewer: User!
1178+
subscriptionPortalUrl: String!
11781179
viewerContact: ViewerContact!
11791180
accountCredential(
11801181
"""The id of the record to find."""

libs/common/src/subscriptions/subscription.service.ts

+15
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,19 @@ export class SubscriptionService {
100100
async getSubscription(subscriptionId: string): Promise<Stripe.Subscription> {
101101
return this.stripe.subscriptions.retrieve(subscriptionId)
102102
}
103+
104+
async getSubscriptionPortal(user: User): Promise<string> {
105+
if (!user.stripeCustomerId) {
106+
throw new Error('You do not have an active subscription')
107+
}
108+
const customer = await this.stripe.customers.retrieve(user.stripeCustomerId)
109+
if (!customer) {
110+
throw new Error(`Customer ${user.stripeCustomerId} not found`)
111+
}
112+
const portalSession = await this.stripe.billingPortal.sessions.create({
113+
customer: user.stripeCustomerId,
114+
return_url: 'https://chainjet.io/dashboard',
115+
})
116+
return portalSession.url
117+
}
103118
}

0 commit comments

Comments
 (0)