Skip to content

Commit

Permalink
add model for webhook configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
lilioid committed Jul 9, 2024
1 parent bc4fb9e commit 266f0aa
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 0 deletions.
73 changes: 73 additions & 0 deletions src/vinywaji/core/migrations/0002_webhookconfig.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# Generated by Django 5.0.6 on 2024-07-09 14:52

import django.db.models.deletion
import vinywaji.core.models
from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("vinywaji_core", "0001_initial"),
]

operations = [
migrations.CreateModel(
name="WebhookConfig",
fields=[
(
"id",
models.UUIDField(
default=vinywaji.core.models.uuid_default,
help_text="The ID of this webhook",
primary_key=True,
serialize=False,
),
),
(
"description",
models.CharField(
blank=True,
default="",
help_text="A free-form description which the user can give this webhook",
max_length=128,
),
),
(
"transaction_description",
models.CharField(
blank=True,
default="",
help_text="The description that will be added to the transaction when this webhook is triggered",
max_length=30,
),
),
(
"trigger_key",
models.CharField(
default=vinywaji.core.models.webhook_trigger_default,
editable=False,
help_text="The key required to trigger this webhook",
max_length=64,
),
),
(
"amount",
models.IntegerField(
help_text="How much money the triggered transaction records in euro-cent. Negative amounts represent purchases while positive amounts represent deposits."
),
),
(
"user",
models.ForeignKey(
editable=False,
help_text="The user who configured this webhook and who is impacted when it is called",
on_delete=django.db.models.deletion.CASCADE,
related_name="webhooks",
to=settings.AUTH_USER_MODEL,
),
),
],
),
]
57 changes: 57 additions & 0 deletions src/vinywaji/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,60 @@ def __str__(self):
else:
verb = "spent" if self.amount < 0 else "gained"
return f"{self.user.get_username()} {verb} {abs(self.amount)}€"


class WebhookConfig(models.Model):
"""
Users can create webhooks that trigger specific transactions.
This model stores the configuration of what each webhook exactly does.
"""

id = models.UUIDField(
primary_key=True, default=uuid_default, help_text="The ID of this webhook"
)
description = models.CharField(
max_length=128,
help_text="A free-form description which the user can give this webhook",
default="",
null=False,
blank=True,
)
transaction_description = models.CharField(
max_length=30,
help_text="The description that will be added to the transaction when this webhook is triggered",
default="",
null=False,
blank=True,
)
trigger_key = models.CharField(
max_length=64,
help_text="The key required to trigger this webhook",
default=webhook_trigger_default,
editable=False,
)
user = models.ForeignKey(
to="User",
on_delete=models.CASCADE,
related_name="webhooks",
editable=False,
help_text="The user who configured this webhook and who is impacted when it is called",
)
amount = models.IntegerField(
help_text="How much money the triggered transaction records in euro-cent. Negative amounts represent purchases while positive amounts represent deposits.",
)

def __str__(self):
return f"Webhook {self.id} (/{self.trigger_key})"

def get_absolute_url(self) -> str:
return reverse("webhook-trigger", kwargs={"pk": self.pk})

def trigger(self) -> Transaction:
"""
Trigger this webhook and create the configured transaction
"""
return Transaction.objects.create(
user=self.user,
description=self.transaction_description,
amount=self.amount,
)

0 comments on commit 266f0aa

Please sign in to comment.