Skip to content

Commit

Permalink
added memberbucks product log with external_id to credit/debit api
Browse files Browse the repository at this point in the history
  • Loading branch information
jabelone committed May 24, 2024
1 parent ae92046 commit 07f4f1e
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 11 deletions.
25 changes: 25 additions & 0 deletions memberportal/access/migrations/0018_auto_20240525_0016.py
Original file line number Diff line number Diff line change
@@ -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",
),
]
10 changes: 0 additions & 10 deletions memberportal/access/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
35 changes: 34 additions & 1 deletion memberportal/api_access/consumers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 07f4f1e

Please sign in to comment.