All files / postcardotp-backend/src/controllers paymentMethodController.js

89.65% Statements 26/29
100% Branches 4/4
100% Functions 4/4
89.65% Lines 26/29

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 551x 1x 1x   1x 2x 2x 2x 2x 1x   2x   1x       1x 1x 1x 1x       1x           1x 1x 1x 1x 1x           1x           1x 1x 1x 1x          
const PaymentMethodService = require("../services/paymentMethodService");
const { handleErrors } = require("../utils/errorHandler");
const successResponse = require("../utils/successResponse");
 
exports.getAllPaymentMethods = async (req, res, next) => {
  try {
    const userId = req.userId;
    const methods = await PaymentMethodService.getAllPaymentMethods(userId);
    if (!methods || methods.length === 0) {
      handleErrors(res, "No payment methods found", 404);
    }
    successResponse(res, methods, "Data fetched successfully", 200);
  } catch (err) {
    next(err);
  }
};
 
exports.getPaymentMethodById = async (req, res, next) => {
  try {
    const userId = req.userId;
    const method = await PaymentMethodService.getPaymentMethodById(
      userId,
      req.params.id,
    );
    successResponse(res, method, "Data fetched successfully", 201);
  } catch (err) {
    next(err);
  }
};
 
exports.createPaymentMethod = async (req, res, next) => {
  try {
    const { card_brand, card_number, exp_month, exp_year } = req.body;
    const userId = req.userId;
    const method = await PaymentMethodService.createPaymentMethod(userId, {
      card_brand,
      last4: card_number,
      exp_month,
      exp_year,
    });
    successResponse(res, method, "Payment method created successfully", 201);
  } catch (err) {
    next(err);
  }
};
 
exports.removePaymentMethod = async (req, res, next) => {
  try {
    await PaymentMethodService.removePaymentMethod(req.params.id);
    successResponse(res, {}, "Payment method removed successfully", 200);
  } catch (err) {
    next(err);
  }
};