diff --git a/api/models.py b/api/models.py index 10131d1d..15eaf3ef 100644 --- a/api/models.py +++ b/api/models.py @@ -44,8 +44,8 @@ class Order(models.Model): UPI = 15, 'Updated invoice' DIS = 16, 'In dispute' MLD = 17, 'Maker lost dispute' - # TLD = 18, 'Taker lost dispute' - # EXP = 19, 'Expired' + TLD = 18, 'Taker lost dispute' + EXP = 19, 'Expired' # order info status = models.PositiveSmallIntegerField(choices=Status.choices, null=False, default=int(Status.WFB)) @@ -77,7 +77,7 @@ class Order(models.Model): # buyer payment LN invoice has_invoice = models.BooleanField(default=False, null=False) # has invoice and is valid invoice = models.CharField(max_length=300, unique=False, null=True, default=None) - + class Profile(models.Model): user = models.OneToOneField(User,on_delete=models.CASCADE) diff --git a/api/views.py b/api/views.py index 33555171..a5fc6ef4 100644 --- a/api/views.py +++ b/api/views.py @@ -1,4 +1,4 @@ -from rest_framework import status +from rest_framework import status, serializers from rest_framework.generics import CreateAPIView, ListAPIView from rest_framework.views import APIView from rest_framework import viewsets @@ -101,19 +101,30 @@ class OrderView(viewsets.ViewSet): data = ListOrderSerializer(order).data nickname = request.user.username - #To do fix: data['status_message'] = Order.Status.get(order.status).label - data['status_message'] = Order.Status.WFB.label # Hardcoded WFB, should use order.status value. + # Add booleans if user is maker, taker, partipant, buyer or seller + data['is_maker'] = str(order.maker) == nickname + data['is_taker'] = str(order.taker) == nickname + data['is_participant'] = data['is_maker'] or data['is_taker'] + data['is_buyer'] = (data['is_maker'] and order.type == int(Order.Types.BUY)) or (data['is_taker'] and order.type == int(Order.Types.SELL)) + data['is_seller'] = (data['is_maker'] and order.type == int(Order.Types.SELL)) or (data['is_taker'] and order.type == int(Order.Types.BUY)) - # Check if requester is participant in the order and add boolean to response - data['is_participant'] = (str(order.maker) == nickname or str(order.taker) == nickname) + # If not a participant and order is not public, forbid. + if not data['is_participant'] and order.status != int(Order.Status.PUB): + return Response({'bad_request':'Not allowed to see this order'},status.HTTP_403_FORBIDDEN) + + # return nicks too data['maker_nick'] = str(order.maker) data['taker_nick'] = str(order.taker) + + #To do fix: data['status_message'] = Order.Status.get(order.status).label + # Needs to serialize the order.status into the message. + data['status_message'] = Order.Status.WFB.label # Hardcoded WFB, should use order.status value. if data['is_participant']: return Response(data, status=status.HTTP_200_OK) else: - # Non participants should not see the status or who is the taker - for key in ('status','status_message','taker','taker_nick'): + # Non participants should not see the status, who is the taker, etc + for key in ('status','status_message','taker','taker_nick','is_maker','is_taker','is_buyer','is_seller'): del data[key] return Response(data, status=status.HTTP_200_OK) @@ -139,6 +150,8 @@ class OrderView(viewsets.ViewSet): order.taker = self.request.user order.status = int(Order.Status.TAK) + + #TODO REPLY WITH HODL INVOICE data = ListOrderSerializer(order).data # An invoice came in! update it diff --git a/frontend/src/components/OrderPage.js b/frontend/src/components/OrderPage.js index d53ca945..afda6b6e 100644 --- a/frontend/src/components/OrderPage.js +++ b/frontend/src/components/OrderPage.js @@ -2,6 +2,17 @@ import React, { Component } from "react"; import { Paper, Button , Grid, Typography, List, ListItem, ListItemText, ListItemAvatar, Avatar, Divider} from "@material-ui/core" import { Link } from 'react-router-dom' +function msToTime(duration) { + var seconds = Math.floor((duration / 1000) % 60), + minutes = Math.floor((duration / (1000 * 60)) % 60), + hours = Math.floor((duration / (1000 * 60 * 60)) % 24); + + minutes = (minutes < 10) ? "0" + minutes : minutes; + seconds = (seconds < 10) ? "0" + seconds : seconds; + + return hours + "h " + minutes + "m " + seconds + "s"; +} + function getCookie(name) { let cookieValue = null; if (document.cookie && document.cookie !== '') { @@ -54,6 +65,9 @@ export default class OrderPage extends Component { makerNick: data.maker_nick, takerId: data.taker, takerNick: data.taker_nick, + isBuyer:data.buyer, + isSeller:data.seller, + expiresAt:data.expires_at, }); }); } @@ -90,16 +104,43 @@ export default class OrderPage extends Component { - + - + + + {this.state.isParticipant ? + <> + {this.state.takerNick!='None' ? + <> + + + + + + + + : + <> + + + + + + } + + :"" + } + @@ -116,31 +157,17 @@ export default class OrderPage extends Component { } - {this.state.isParticipant ? - <> - - - - - { this.state.takerNick!='None' ? - <> - - - - - - : ""} - - :"" - } + + + + + + - +