Skip to content

Commit

Permalink
Base merchant last transaction on POS device(s) latest transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
joemarct committed Jan 25, 2025
1 parent 347e8e8 commit 18e778f
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 10 deletions.
7 changes: 6 additions & 1 deletion main/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from watchtower.celery import app as celery_app
from main.models import *
from main.utils.address_validator import *
from paytacapos.models import Merchant
from paytacapos.models import Merchant, PosDevice
from main.utils.ipfs import (
get_ipfs_cid_from_url,
ipfs_gateways,
Expand Down Expand Up @@ -1688,6 +1688,11 @@ def parse_wallet_history(self, txid, wallet_handle, tx_fee=None, senders=[], rec
merchant.last_update = timezone.now()
merchant.save()

# update latest_transaction in POS devices
pos_devices = PosDevice.objects.filter(merchant=merchant)
for device in pos_devices:
device.populate_latest_transaction()

# for older token records
if (
txn and
Expand Down
8 changes: 6 additions & 2 deletions paytacapos/admin.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django.contrib import admin

from dynamic_raw_id.admin import DynamicRawIDMixin
from .models import *

from main.models import (
Expand Down Expand Up @@ -45,13 +45,17 @@ def queryset(self, request, queryset):
# Register your models here.

@admin.register(PosDevice)
class PosDeviceAdmin(admin.ModelAdmin):
class PosDeviceAdmin(DynamicRawIDMixin, admin.ModelAdmin):
search_fields = [
"wallet_hash",
"name",
"merchant__name",
]

dynamic_raw_id_fields = [
'latest_transaction'
]

list_display = [
"wallet_hash",
"posid",
Expand Down
20 changes: 20 additions & 0 deletions paytacapos/migrations/0034_posdevice_latest_transaction.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Generated by Django 3.0.14 on 2025-01-25 09:36

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('main', '0099_cashnonfungibletoken_fixed_supply'),
('paytacapos', '0033_auto_20241022_0535'),
]

operations = [
migrations.AddField(
model_name='posdevice',
name='latest_transaction',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='devices', to='main.Transaction'),
),
]
32 changes: 25 additions & 7 deletions paytacapos/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from django.utils import timezone
from django.contrib.postgres.fields import ArrayField
from psqlextra.query import PostgresQuerySet
from main.models import WalletHistory
from main.models import WalletHistory, Address, Transaction

from PIL import Image
import os
Expand Down Expand Up @@ -67,6 +67,12 @@ class PosDevice(models.Model):
null=True, blank=True,
)

latest_transaction = models.ForeignKey(
'main.Transaction',
on_delete=models.SET_NULL, related_name="devices",
null=True, blank=True
)

class Meta:
unique_together = (
("posid", "wallet_hash"),
Expand All @@ -85,6 +91,15 @@ def find_new_posid(cls, wallet_hash):
return i

return None

def populate_latest_transaction(self):
last_tx = Transaction.objects.filter(
wallet__wallet_hash=self.wallet_hash,
address__address_path__iregex=f"((0|1)/)?0*\d+{self.posid}"
).order_by('date_created').last()
if last_tx:
self.latest_transaction = last_tx
self.save()


class Location(models.Model):
Expand Down Expand Up @@ -188,14 +203,17 @@ def save(self, *args, **kwargs):

super().save(*args, **kwargs)


@property
def last_transaction_date(self):
wallet = WalletHistory.objects.filter(wallet__wallet_hash=self.wallet_hash)
last_tx = wallet.filter(record_type=WalletHistory.INCOMING).latest('date_created')
last_tx_date = None
if last_tx:
last_tx_date = str(last_tx.date_created)
pos_devices_check = self.devices.filter(latest_transaction__isnull=False)
if pos_devices_check.exists():
last_tx_date = pos_devices_check.latest('latest_transaction').latest_transaction.date_created
else:
wallet = WalletHistory.objects.filter(wallet__wallet_hash=self.wallet_hash)
last_tx = wallet.filter(record_type=WalletHistory.INCOMING).latest('date_created')
last_tx_date = None
if last_tx:
last_tx_date = str(last_tx.date_created)
return last_tx_date

def get_main_branch(self):
Expand Down

0 comments on commit 18e778f

Please sign in to comment.