fixup! WIP

This commit is contained in:
2025-12-09 15:46:46 +09:00
parent cec91d923c
commit 16be0f0fbe
5 changed files with 79 additions and 36 deletions

View File

@@ -11,6 +11,7 @@ from sqlalchemy import (
Text,
and_,
column,
func,
or_,
)
from sqlalchemy.orm import (
@@ -104,6 +105,37 @@ class Transaction(Base):
),
name="trx_type_transfer_no_self_transfers",
),
CheckConstraint(
func.coalesce(column("product_count"), 1) != 0,
name="trx_product_count_non_zero",
),
CheckConstraint(
func.coalesce(column("penalty_multiplier_percent"), 100) >= 100,
name="trx_penalty_multiplier_percent_min_100",
),
CheckConstraint(
func.coalesce(column("interest_rate_percent"), 0) >= 0,
name="trx_interest_rate_percent_non_negative",
),
CheckConstraint(
func.coalesce(column("amount"), 1) != 0,
name="trx_amount_non_zero",
),
CheckConstraint(
func.coalesce(column("per_product"), 1) > 0,
name="trx_per_product_positive",
),
CheckConstraint(
func.coalesce(column("penalty_threshold"), 0) <= 0,
name="trx_penalty_threshold_max_0",
),
CheckConstraint(
or_(
column("joint_transaction_id").is_(None),
column("joint_transaction_id") != column("id"),
),
name="trx_joint_transaction_id_not_self",
),
# Speed up product count calculation
Index("product_user_time", "product_id", "user_id", "time"),
# Speed up product owner calculation

View File

@@ -1,6 +1,6 @@
from __future__ import annotations
from typing import TYPE_CHECKING, Self
from typing import Self
from sqlalchemy import (
Integer,
@@ -19,12 +19,13 @@ class User(Base):
"""Internal database ID"""
name: Mapped[str] = mapped_column(String(20), unique=True)
"""
The PVV username of the user.
"""
"""The PVV username of the user."""
card: Mapped[str | None] = mapped_column(String(20))
"""The NTNU card number of the user."""
rfid: Mapped[str | None] = mapped_column(String(20))
"""The RFID tag of the user (if they have any, rare these days)."""
# name_re = r"[a-z]+"
# card_re = r"(([Nn][Tt][Nn][Uu])?[0-9]+)?"

View File

@@ -21,6 +21,13 @@ def joint_buy_product(
"""
Create buy product transactions for multiple users at once.
"""
if instigator not in users:
raise ValueError("Instigator must be in the list of users buying the product.")
if product_count <= 0:
raise ValueError("Product count must be positive.")
joint_transaction = Transaction.joint(
user_id=instigator.id,
product_id=product.id,