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

atlp-rwanda / e-commerce-ones-and-zeroes-bn / fb2e3269-ba68-47bc-89e2-b805c292d315

19 Jul 2024 04:36PM UTC coverage: 89.874% (-0.6%) from 90.424%
fb2e3269-ba68-47bc-89e2-b805c292d315

push

circleci

web-flow
Merge pull request #68 from atlp-rwanda/fx-adding-pagination-and-fixig-order

[Fixes #187355124 #187355123] fixing checkout  and paginating list of…

254 of 298 branches covered (85.23%)

Branch coverage included in aggregate %.

10 of 15 new or added lines in 3 files covered. (66.67%)

1 existing line in 1 file now uncovered.

820 of 897 relevant lines covered (91.42%)

2.89 hits per line

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

85.53
/src/controllers/orderController.ts
1
import { Request, Response } from 'express';
2
import { db } from '../database/models';
1✔
3
import { v4 as uuidv4 } from 'uuid';
1✔
4
import stripe from '../helps/stripeConfig';
1✔
5

6
class OrderController {
7
  static async createOrder(req: Request, res: Response) {
8
    try {
6✔
9
      const { productId, quantity, addressId } = req.body;
6✔
10
      if (!productId || !quantity || quantity <= 0 || !addressId) {
6✔
11
        return res
1✔
12
          .status(400)
13
          .json({ message: 'Missing or Invalid product information' });
14
      }
15
      const address = await db.Address.findByPk(addressId);
5✔
16
      if (!address) {
5✔
17
        return res.status(404).json({ message: 'No such address found' });
1✔
18
      }
19
      const product = await db.Product.findByPk(productId);
4✔
20
      if (!product) {
4✔
21
        return res.status(404).json({ message: 'No such product found' });
1✔
22
      }
23
      if (product.dataValues.quantity < quantity) {
3✔
24
        return res
1✔
25
          .status(403)
26
          .json({ message: 'Insufficient quantity in stock' });
27
      }
28

29
      const paymentIntent = await stripe.paymentIntents.create({
2✔
30
        amount: product.dataValues.price * quantity,
31
        currency: 'rwf',
32
      });
33

UNCOV
34
      const order = await db.Order.create({
×
35
        orderId: uuidv4(),
36
        userId: (req as any).user.userId,
37
        cartId: null,
38
        addressId: address.dataValues.addressId,
39
        paymentIntentId: paymentIntent.id,
40
      });
41

NEW
42
      const orderProduct = await db.OrderProduct.create({
×
43
        orderProductId: uuidv4(),
44
        orderId: order.dataValues.orderId,
45
        productId: product.dataValues.productId,
46
        quantity: quantity,
47
      });
48

NEW
49
      return res.status(200).json({ order, paymentIntent, orderProduct });
×
50
    } catch (error: any) {
51
      return res.status(500).json({ message: 'Failed to create order' });
2✔
52
    }
53
  }
54

55
  static async getOrder(req: Request, res: Response) {
56
    try {
3✔
57
      const { orderId } = req.params;
3✔
58
      const order: any = await db.Order.findOne({
3✔
59
        where: {
60
          orderId: orderId,
61
        },
62
        include: [
63
          {
64
            model: db.Product,
65
            through: {
66
              model: db.OrderProduct,
67
              attributes: ['quantity'],
68
            },
69
          },
70
        ],
71
      });
72
      if (!order) {
2✔
73
        return res.status(404).json({ message: 'No such order found' });
1✔
74
      }
75
      return res.status(200).json({ order });
1✔
76
    } catch (error: any) {
77
      return res.status(500).json({ message: 'Failed to get orders' });
1✔
78
    }
79
  }
80

81
  static async getAllUserOrders(req: Request, res: Response) {
82
    try {
1✔
83
      const userId = (req as any).user.userId;
1✔
84
      const page = parseInt(req.query.page as string) || 1;
1!
85
      const pageSize = parseInt(req.query.pageSize as string) || 10; // Get pageSize from query params or default to 10
1!
86
      const offset = (page - 1) * pageSize;
1✔
87

88
      const orders = await db.Order.findAll({
1✔
89
        where: {
90
          userId: userId,
91
        },
92
        include: [
93
          {
94
            model: db.Product,
95
            through: {
96
              model: db.OrderProduct,
97
              attributes: ['quantity'],
98
            },
99
          },
100
        ],
101
        limit: pageSize,
102
        offset: offset,
103
      });
104

105
      const totalOrders = await db.Order.count({
1✔
106
        where: {
107
          userId: userId,
108
        },
109
      });
110

NEW
111
      const totalPages = Math.ceil(totalOrders / pageSize);
×
112

NEW
113
      res.status(200).json({
×
114
        orders,
115
        pagination: {
116
          currentPage: page,
117
          pageSize: pageSize,
118
          totalPages: totalPages,
119
          totalOrders: totalOrders,
120
        },
121
      });
122
    } catch (err: any) {
123
      return res.status(500).json({ message: 'Failed to get all user orders' });
1✔
124
    }
125
  }
126

127
  static async confirmOrderPayment(req: Request, res: Response) {
128
    try {
4✔
129
      const { orderId } = req.params;
4✔
130
      const order: any = await db.Order.findOne({
4✔
131
        where: {
132
          orderId: orderId,
133
        },
134
        include: [
135
          {
136
            model: db.Product,
137
            through: {
138
              model: db.OrderProduct,
139
              attributes: ['quantity'],
140
            },
141
          },
142
        ],
143
      });
144
      if (!order) {
3✔
145
        return res.status(404).json({ message: 'No such order found' });
1✔
146
      }
147
      const paymentIntent = await stripe.paymentIntents.retrieve(
2✔
148
        order.dataValues.paymentIntentId,
149
      );
150
      if (!paymentIntent) {
2✔
151
        return res.status(404).json({ message: 'No such paymentIntent found' });
1✔
152
      }
153
      if (paymentIntent.status !== 'succeeded') {
1!
154
        return res.status(403).json({ message: 'Order was not paid for' });
×
155
      }
156

157
      //check if order is associated with a cart
158
      if (order.dataValues.cartId) {
1✔
159
        //clear the cart after checkout
160
        const cart = await db.Cart.findByPk(order.dataValues.cartId);
1✔
161
        if (!cart) {
1!
162
          return res.status(404).json({ message: 'No such cart found' });
×
163
        }
164
        await db.CartProduct.destroy({
1✔
165
          where: {
166
            cartId: cart.dataValues.cartId,
167
          },
168
        });
169
      }
170
      order.dataValues.Products.forEach((product: any) => {
1✔
171
        product.decrement('quantity', {
1✔
172
          by: product.dataValues.OrderProduct.dataValues.quantity,
173
        });
174
      });
175
      order.update({ paid: true });
1✔
176

177
      return res.status(200).json({ message: 'Order was successfully paid' });
1✔
178
    } catch (error: any) {
179
      return res.status(500).json({ message: 'Failed to confirm payment' });
1✔
180
    }
181
  }
182
}
183

184
export default OrderController;
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