Repository: fossasia/open-event-server

Issue:
'Completed' attribute missing from order-statistics API
**I'm submitting a ...**  (check one with "x")
- [x] bug report
- [ ] feature request
- [ ] support request => Please do not submit support requests here, instead ask your query in out Gitter channel at https://gitter.im/fossasia/open-event-orga-server

**Current behavior:**
Right now the response contains the following fields
 ```
       "placed": 0,
        "draft": 0,
        "cancelled": 0,
        "total": 0,
        "expired": 0,
        "pending": 0
```
The attribute `completed` is missing from the response. 
Image for reference : https://user-images.githubusercontent.com/13910561/29230756-f5dabe20-7f01-11e7-8c5f-58ee69c38e65.png

**Expected behavior:**
`completed` attribute should be there.


Discussion:

Resolving patch:
diff --git a/app/api/order_statistics/tickets.py b/app/api/order_statistics/tickets.py
--- a/app/api/order_statistics/tickets.py
+++ b/app/api/order_statistics/tickets.py
@@ -45,13 +45,16 @@ def tickets_count(self, obj):
             OrderTicket.ticket_id == obj_id, Order.status == 'expired').scalar()
         placed = db.session.query(func.sum(OrderTicket.quantity.label('sum'))).join(Order.order_tickets).filter(
             OrderTicket.ticket_id == obj_id, Order.status == 'placed').scalar()
+        completed = db.session.query(func.sum(OrderTicket.quantity.label('sum'))).join(Order.order_tickets).filter(
+            OrderTicket.ticket_id == obj_id, Order.status == 'completed').scalar()
         result = {
             'total': total or 0,
             'draft': draft or 0,
             'cancelled': cancelled or 0,
             'pending': pending or 0,
             'expired': expired or 0,
-            'placed': placed or 0
+            'placed': placed or 0,
+            'completed': completed or 0
         }
         return result
 
@@ -68,13 +71,16 @@ def orders_count(self, obj):
                                                                                      Order.status == 'expired'))
         placed = get_count(db.session.query(Order).join(Order.order_tickets).filter(OrderTicket.ticket_id == obj_id,
                                                                                     Order.status == 'placed'))
+        completed = get_count(db.session.query(Order).join(Order.order_tickets).filter(OrderTicket.ticket_id == obj_id,
+                                                                                       Order.status == 'completed'))
         result = {
             'total': total or 0,
             'draft': draft or 0,
             'cancelled': cancelled or 0,
             'pending': pending or 0,
             'expired': expired or 0,
-            'placed': placed or 0
+            'placed': placed or 0,
+            'completed': completed or 0
         }
         return result
 
@@ -92,13 +98,16 @@ def sales_count(self, obj):
             OrderTicket.ticket_id == obj_id, Order.status == 'expired').scalar()
         placed = db.session.query(func.sum(Order.amount.label('sum'))).join(Order.order_tickets).filter(
             OrderTicket.ticket_id == obj_id, Order.status == 'placed').scalar()
+        completed = db.session.query(func.sum(Order.amount.label('sum'))).join(Order.order_tickets).filter(
+            OrderTicket.ticket_id == obj_id, Order.status == 'completed').scalar()
         result = {
             'total': total or 0,
             'draft': draft or 0,
             'cancelled': cancelled or 0,
             'pending': pending or 0,
             'expired': expired or 0,
-            'placed': placed or 0
+            'placed': placed or 0,
+            'completed': completed or 0
         }
         return result
 


Test patch: