From 07f4f1e0e6239f88875f54e7a4b783f41331a89f Mon Sep 17 00:00:00 2001 From: Jaimyn Mayer Date: Sat, 25 May 2024 00:17:05 +1000 Subject: [PATCH] added memberbucks product log with external_id to credit/debit api --- .../migrations/0018_auto_20240525_0016.py | 25 +++++++++++++ memberportal/access/models.py | 10 ------ memberportal/api_access/consumers.py | 35 ++++++++++++++++++- 3 files changed, 59 insertions(+), 11 deletions(-) create mode 100644 memberportal/access/migrations/0018_auto_20240525_0016.py diff --git a/memberportal/access/migrations/0018_auto_20240525_0016.py b/memberportal/access/migrations/0018_auto_20240525_0016.py new file mode 100644 index 00000000..a5b60d24 --- /dev/null +++ b/memberportal/access/migrations/0018_auto_20240525_0016.py @@ -0,0 +1,25 @@ +# Generated by Django 3.2.25 on 2024-05-24 14:16 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ("access", "0017_auto_20240523_1604"), + ] + + operations = [ + migrations.RemoveField( + model_name="memberbucksdevice", + name="supports_credits", + ), + migrations.RemoveField( + model_name="memberbucksdevice", + name="supports_debits", + ), + migrations.RemoveField( + model_name="memberbucksdevice", + name="supports_products", + ), + ] diff --git a/memberportal/access/models.py b/memberportal/access/models.py index f794a42a..134cac39 100644 --- a/memberportal/access/models.py +++ b/memberportal/access/models.py @@ -256,16 +256,6 @@ class Meta: verbose_name = "Memberbucks Device" verbose_name_plural = "Memberbucks Devices" - supports_debits = models.BooleanField( - "Supports the MM simple debits API.", default=False - ) - supports_credits = models.BooleanField( - "Supports the MM simple credits API.", default=False - ) - supports_products = models.BooleanField( - "Supports the MM products API.", default=False - ) - class Doors(AccessControlledDevice): type = "door" diff --git a/memberportal/api_access/consumers.py b/memberportal/api_access/consumers.py index 30d4c87e..64de1997 100644 --- a/memberportal/api_access/consumers.py +++ b/memberportal/api_access/consumers.py @@ -11,7 +11,11 @@ AccessControlledDeviceAPIKey, AccessControlledDevice, ) -from memberbucks.models import MemberBucks +from memberbucks.models import ( + MemberBucks, + MemberbucksProductPurchaseLog, + MemberbucksProduct, +) from profile.models import Profile, User from constance import config from django.core.exceptions import ObjectDoesNotExist @@ -481,6 +485,7 @@ def handle_other_packet(self, content): if content.get("command") == "debit" or content.get("command") == "credit": card_id = content.get("card_id") + product_external_id = content.get("product_external_id") amount = int(content.get("amount") or 0) description = content.get("description", f"{self.device.name} purchase.") command = content.get("command") @@ -556,6 +561,34 @@ def handle_other_packet(self, content): # We have a hard rate limit of one transaction every 3 seconds at most if time_dif > 3: amount = float(amount) if command == "credit" else float(amount * -1) + + if product_external_id: + try: + product = MemberbucksProduct.objects.get( + external_id=product_external_id + ) + except ObjectDoesNotExist: + self.send_json( + { + "command": command, + "reason": "invalid_product_external_id", + "success": False, + } + ) + logger.warning( + f"Tried to process {command} but product with external_id {product_external_id} does not exist." + ) + return True + purchase_log = MemberbucksProductPurchaseLog() + purchase_log.product = product + purchase_log.user = profile.user + purchase_log.cost_price = product.cost_price + purchase_log.price = amount + purchase_log.memberbucks_device = self.device + purchase_log.save() + + description = f"{profile.get_full_name()} ({profile.screen_name}) {product.name} purchased from {self.device.name} ({product.external_id_name}) for {amount}." + transaction = MemberBucks() transaction.amount = amount transaction.user = profile.user