• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

GEWIS / sudosos-backend / 25753937432

12 May 2026 09:17AM UTC coverage: 88.117% (-1.0%) from 89.089%
25753937432

push

github

web-flow
chore(deps): fix missing dependencies for running docs:dev (#911)

3925 of 4574 branches covered (85.81%)

Branch coverage included in aggregate %.

20093 of 22683 relevant lines covered (88.58%)

1125.83 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.03
/src/controller/stripe-controller.ts
1
/**
1✔
2
 *  SudoSOS back-end API service.
3
 *  Copyright (C) 2026 Study association GEWIS
4
 *
5
 *  This program is free software: you can redistribute it and/or modify
6
 *  it under the terms of the GNU Affero General Public License as published
7
 *  by the Free Software Foundation, either version 3 of the License, or
8
 *  (at your option) any later version.
9
 *
10
 *  This program is distributed in the hope that it will be useful,
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 *  GNU Affero General Public License for more details.
14
 *
15
 *  You should have received a copy of the GNU Affero General Public License
16
 *  along with this program.  If not, see <https://www.gnu.org/licenses/>.
17
 *
18
 *  @license
19
 */
1✔
20

21
/**
1✔
22
 * This is the module page of the stripe-controller.
23
 *
24
 * @module stripe
25
 */
1✔
26

27
import log4js, { Logger } from 'log4js';
28
import { Response } from 'express';
29
import Dinero from 'dinero.js';
30
import BaseController, { BaseControllerOptions } from './base-controller';
31
import Policy from './policy';
32
import { RequestWithToken } from '../middleware/token-middleware';
33
import StripeService from '../service/stripe-service';
34
import { StripeRequest } from './request/stripe-request';
35
import BalanceService from '../service/balance-service';
36

37
export default class StripeController extends BaseController {
1✔
38
  private logger: Logger = log4js.getLogger('StripeController');
1✔
39

40
  private stripeService: StripeService;
41

42
  /**
1✔
43
   * Create a new stripe controller instance
44
   * @param options
45
   */
1✔
46
  public constructor(options: BaseControllerOptions) {
1✔
47
    super(options);
1✔
48
    this.configureLogger(this.logger);
1✔
49
    this.stripeService = new StripeService();
1✔
50
  }
1✔
51

52
  /**
1✔
53
   * @inheritDoc
54
   */
1✔
55
  public getPolicy(): Policy {
1✔
56
    return {
1✔
57
      '/deposit': {
1✔
58
        POST: {
1✔
59
          policy: async (req) => this.roleManager.can(
1✔
60
            req.token.roles, 'create', 'all', 'StripeDeposit', ['*'],
3✔
61
          ),
62
          handler: this.createStripeDeposit.bind(this),
1✔
63
          body: { modelName: 'StripeRequest' },
1✔
64
        },
1✔
65
      },
1✔
66
    };
1✔
67
  }
1✔
68

69
  /**
1✔
70
   * POST /stripe/deposit
71
   * @summary Start the stripe deposit flow
72
   * @operationId deposit
73
   * @tags stripe - Operations of the stripe controller
74
   * @param {StripeRequest} request.body.required - The deposit that should be created
75
   * @return {StripePaymentIntentResponse} 200 - Payment Intent information
76
   * @return {string} 500 - Internal server error
77
   * @security JWT
78
   */
1✔
79
  public async createStripeDeposit(req: RequestWithToken, res: Response): Promise<void> {
1✔
80
    this.logger.trace('Create a new stripe deposit by user', req.token.user);
3✔
81
    const request = req.body as StripeRequest;
3✔
82

83
    try {
3✔
84
      const amount = Dinero(request.amount);
3✔
85
      const balance = await new BalanceService().getBalance(req.token.user.id);
3✔
86

87
      // Check if top-up satisfies minimum in accordance with TOS.
3✔
88
      if (!StripeService.validateStripeRequestMinimumAmount(balance, request)) {
3✔
89
        res.status(422).json({ error: 'Top-up amount is too low' });
1✔
90
        return;
1✔
91
      }
1✔
92

93
      // Check if top-up satisfies maximum in accordance with TOS.
2✔
94
      if (!StripeService.validateStripeRequestMaximumAmount(balance, request)) {
3✔
95
        res.status(422).json({ error: 'Top-up amount is too high' });
1✔
96
        return;
1✔
97
      }
1✔
98

99
      const { deposit, clientSecret } = await this.stripeService.createStripePaymentIntent(req.token.user, amount);
1✔
100
      res.status(200).json({
1✔
101
        id: deposit.id,
1✔
102
        createdAt: deposit.createdAt.toISOString(),
1✔
103
        updatedAt: deposit.updatedAt.toISOString(),
1✔
104
        stripeId: deposit.stripePaymentIntent.stripeId,
1✔
105
        clientSecret,
1✔
106
      });
1✔
107
    } catch (error) {
3!
108
      this.logger.error('Could not create Stripe payment intent:', error);
×
109
      res.status(500).send('Internal server error.');
×
110
    }
×
111
  }
3✔
112
}
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc