Fakultas Ilmu Komputer UI

Commit 9ce7205b authored by Kefas Satrio Bangkit Solidedantyo's avatar Kefas Satrio Bangkit Solidedantyo
Browse files

Merge branch 'PBI-8-AddTransactionStatus' into 'staging'

Add transaction status in cart

See merge request !51
parents ac880e8a 3b81cab2
Pipeline #73593 passed with stages
in 10 minutes and 19 seconds
......@@ -2,6 +2,7 @@ from django.contrib.auth.models import Group
from rest_framework import serializers, exceptions
from dj_rest_auth.serializers import LoginSerializer
from dj_rest_auth.registration.serializers import RegisterSerializer
from payment.serializers import CartSerializer
from .models import CustomUser
......@@ -13,6 +14,10 @@ class CustomUserDetailsSerializer(serializers.ModelSerializer):
result['role'] = instance.groups.first().name
if instance.is_staff:
result['role'] = 'admin'
if instance.cart.exists():
result['cart'] = CartSerializer(instance.cart.last()).data
else:
result['cart'] = {}
return result
class Meta:
......
from django.db import models
from django.utils.translation import gettext_lazy as _
class TransactionStatus(models.TextChoices):
SUCCESS = "success", _("Sucess")
PENDING = "pending", _("Pending")
UNPAID = "unpaid", _("Unpaid")
ERROR = "error", _("Error")
# Generated by Django 3.1 on 2021-04-26 14:58
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('payment', '0003_cart_user'),
]
operations = [
migrations.AddField(
model_name='cart',
name='transaction_status',
field=models.CharField(choices=[('success', 'Sucess'), ('pending', 'Pending'), ('unpaid', 'Unpaid'), ('error', 'Error')], default='unpaid', max_length=128),
),
]
from django.db import models
from constants.transaction_status import TransactionStatus as TRANSACTION_STATUS
from dietela_program.models import DietelaProgram
from nutritionists.models import Nutritionist
from authentication.models import CustomUser
......@@ -19,6 +20,8 @@ class Cart(models.Model):
related_name="cart",
null=True
)
transaction_status = models.CharField(max_length=128,\
choices=TRANSACTION_STATUS.choices, default=TRANSACTION_STATUS.UNPAID)
def __str__(self):
return f"{self.user} - {self.program.unique_code} - {self.nutritionist}"
from rest_framework import serializers
from dietela_program.serializers import DietelaProgramSerializer
from nutritionists.serializers import NutritionistSerializer
from authentication.serializers import CustomUserDetailsSerializer
from .models import Cart
......@@ -15,5 +14,4 @@ class CartSerializer(serializers.ModelSerializer):
result = super().to_representation(instance)
result['program'] = DietelaProgramSerializer(instance.program).data
result['nutritionist'] = NutritionistSerializer(instance.nutritionist).data
result['user'] = CustomUserDetailsSerializer(instance.user).data
return result
......@@ -4,6 +4,7 @@ from rest_framework.test import APITestCase
from django.test import TestCase
from rest_framework import status
from rest_framework.response import Response
from constants.transaction_status import TransactionStatus as TRANSACTION_STATUS
from .models import Cart
from .serializers import CartSerializer
from .views import PaymentViewSet
......@@ -19,6 +20,7 @@ class CartTests(APITestCase):
@classmethod
def setUpTestData(cls):
cls.BASE_URL = "/payment/cart/"
cls.SET_TRANSACTION_STATUS_URL = "/payment/set-transaction-status/"
cls.dietela_program_1 = DietelaProgram.objects.create(
unique_code="PRG1",
......@@ -32,6 +34,12 @@ class CartTests(APITestCase):
price=300000.00,
)
cls.dietela_program_3 = DietelaProgram.objects.create(
unique_code="PRG3",
name="Program 3",
price=300000.00,
)
cls.nutritionist_1 = Nutritionist.objects.create(
id=1,
full_name_and_degree="Test, S.Gz",
......@@ -53,6 +61,11 @@ class CartTests(APITestCase):
nutritionist = cls.nutritionist_1,
)
cls.cart_3 = Cart.objects.create(
program = cls.dietela_program_3,
nutritionist = cls.nutritionist_1,
)
def test_get_all_carts(self):
response = self.client.get(self.BASE_URL)
......@@ -89,9 +102,40 @@ class CartTests(APITestCase):
response = self.client.post(
url, data, format='json')
self.assertEqual(json.loads(response.content).get('program').get('unique_code'), 'PRG1')
self.assertEqual(Cart.objects.count(), 3)
self.assertEqual(json.loads(response.content).get('transaction_status'), 'unpaid')
self.assertEqual(Cart.objects.count(), 4)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
def test_set_transaction_status_with_status_code_200(self):
url = self.SET_TRANSACTION_STATUS_URL
data = {
'cart_id': self.cart_3.id,
'status_code': 200
}
response = self.client.post(url, data, format='json')
self.assertEqual(response.status_code, status.HTTP_200_OK)
def test_set_transaction_status_with_status_code_201(self):
url = self.SET_TRANSACTION_STATUS_URL
data = {
'cart_id': self.cart_3.id,
'status_code': 201
}
response = self.client.post(url, data, format='json')
self.assertEqual(response.status_code, status.HTTP_200_OK)
def test_set_transaction_status_with_other_status_code(self):
url = self.SET_TRANSACTION_STATUS_URL
data = {
'cart_id': self.cart_3.id,
'status_code': 202
}
response = self.client.post(url, data, format='json')
self.assertEqual(response.status_code, status.HTTP_200_OK)
@patch('payment.views.json.loads')
class TestPaymentIntegration(TestCase):
......
from rest_framework import routers
from .views import CartViewSet, PaymentViewSet
from .views import CartViewSet, PaymentViewSet, TransactionStatusViewSet
router = routers.SimpleRouter()
router.register('payment/cart', CartViewSet)
router.register('payment/midtrans', PaymentViewSet, \
basename="Payment")
router.register('payment/set-transaction-status', TransactionStatusViewSet, \
basename="TransactionStatus")
urlpatterns = []
......
......@@ -3,6 +3,7 @@ import os
from rest_framework import viewsets, status
from rest_framework.response import Response
import requests
from constants.transaction_status import TransactionStatus as TRANSACTION_STATUS
from dietela_program.serializers import DietelaProgramSerializer
from dietela_program.models import DietelaProgram
from .serializers import CartSerializer
......@@ -72,3 +73,22 @@ class PaymentViewSet(viewsets.ViewSet):
return Response(json_response, status=status.HTTP_400_BAD_REQUEST)
return Response(json_response, status=status.HTTP_200_OK)
class TransactionStatusViewSet(viewsets.ViewSet):
@staticmethod
def create(request):
cart_id = request.data['cart_id']
transaction_status_code = request.data['status_code']
cart = Cart.objects.get(pk=cart_id)
if transaction_status_code == 200:
cart.transaction_status = TRANSACTION_STATUS.SUCCESS
elif transaction_status_code == 201:
cart.transaction_status = TRANSACTION_STATUS.PENDING
else:
cart.transaction_status = TRANSACTION_STATUS.ERROR
cart.save()
return Response(status=status.HTTP_200_OK)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment