| 18 | @UseGuards(GraphqlGuard) |
| 19 | @UseInterceptors(AuthorizerInterceptor(User)) |
| 20 | export class UserResolver extends BaseResolver(User, { |
| 21 | guards: [GraphqlGuard], |
| 22 | UpdateDTOClass: UpdateUserInput, |
| 23 | read: { many: { disabled: true } }, |
| 24 | create: { disabled: true }, |
| 25 | update: { many: { disabled: true } }, |
| 26 | delete: { disabled: true }, |
| 27 | }) { |
| 28 | private readonly logger = new Logger(UserResolver.name) |
| 29 | |
| 30 | constructor( |
| 31 | protected readonly userService: UserService, |
| 32 | protected readonly subscriptionService: SubscriptionService, |
| 33 | ) { |
| 34 | super(userService) |
| 35 | } |
| 36 | |
| 37 | @Query(() => User) |
| 38 | @UseGuards(GraphqlGuard) |
| 39 | viewer(@UserId() userId: Types.ObjectId): Promise<User | null> { |
| 40 | return this.userService.findOne({ _id: userId }) |
| 41 | } |
| 42 | |
| 43 | @Mutation(() => UserCheckoutSessionPayload) |
| 44 | async createCheckoutSession( |
| 45 | @UserId() userId: ObjectId, |
| 46 | @Args({ name: 'priceId', type: () => GraphQLString }) priceId: string, |
| 47 | ): Promise<{ sessionId: string }> { |
| 48 | if (!userId) { |
| 49 | throw new Error('Not logged in') |
| 50 | } |
| 51 | const user = await this.userService.findOne({ _id: userId }) |
| 52 | if (!user) { |
| 53 | throw new Error('User not found') |
| 54 | } |
| 55 | this.logger.log(`Creating checkout session for user ${user.id} with price ${priceId}`) |
| 56 | const successUrl = `${process.env.FRONTEND_ENDPOINT}/dashboard` |
| 57 | const cancelUrl = `${process.env.FRONTEND_ENDPOINT}/dashboard` |
| 58 | const sessionId = await this.subscriptionService.createCheckoutSession(user, priceId, successUrl, cancelUrl) |
| 59 | return { sessionId } |
| 60 | } |
| 61 | |
| 62 | @Mutation(() => ResultPayload) |
| 63 | async resumeSubscription(@UserId() userId: ObjectId): Promise<{ success: boolean }> { |
| 64 | if (!userId) { |
| 65 | throw new Error('Not logged in') |
| 66 | } |
| 67 | const user = await this.userService.findOne({ _id: userId }) |
| 68 | if (!user) { |
| 69 | throw new Error('User not found') |
| 70 | } |
| 71 | this.logger.log(`Resuming subscription ${user.stripeSubscriptionId} for user ${user.id}`) |
| 72 | await this.subscriptionService.resumeSubscription(user) |
| 73 | await this.userService.updateOneNative({ _id: user._id }, { $unset: { nextPlan: 'free' } }) |
| 74 | return { success: true } |
| 75 | } |
| 76 | |
| 77 | @Mutation(() => ResultPayload) |
nothing calls this directly
no test coverage detected