Spaces:
Paused
Paused
Upload models.py
Browse files- app/db/models.py +256 -256
app/db/models.py
CHANGED
|
@@ -1,257 +1,257 @@
|
|
| 1 |
-
from sqlalchemy import Column, Integer, String, Boolean, DateTime, Float, ForeignKey, ARRAY, JSON, Table, Enum
|
| 2 |
-
from sqlalchemy.orm import relationship, mapped_column, Mapped
|
| 3 |
-
from sqlalchemy.dialects.postgresql import JSONB
|
| 4 |
-
from datetime import datetime
|
| 5 |
-
from typing import List, Optional
|
| 6 |
-
from .database import Base
|
| 7 |
-
import enum
|
| 8 |
-
|
| 9 |
-
# Association tables for many-to-many relationships
|
| 10 |
-
user_roles = Table(
|
| 11 |
-
'user_roles',
|
| 12 |
-
Base.metadata,
|
| 13 |
-
Column('user_id', Integer, ForeignKey('users.id')),
|
| 14 |
-
Column('role_id', Integer, ForeignKey('roles.id'))
|
| 15 |
-
)
|
| 16 |
-
|
| 17 |
-
# Role model
|
| 18 |
-
class Role(Base):
|
| 19 |
-
__tablename__ = "roles"
|
| 20 |
-
|
| 21 |
-
id: Mapped[int] = mapped_column(primary_key=True)
|
| 22 |
-
name: Mapped[str] = mapped_column(String, unique=True, index=True)
|
| 23 |
-
description: Mapped[str] = mapped_column(String)
|
| 24 |
-
permissions: Mapped[List[str]] = mapped_column(ARRAY(String), default=list)
|
| 25 |
-
created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)
|
| 26 |
-
updated_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
|
| 27 |
-
|
| 28 |
-
# Relationships
|
| 29 |
-
users = relationship("User", secondary=user_roles, back_populates="roles")
|
| 30 |
-
|
| 31 |
-
# Branch model
|
| 32 |
-
class Branch(Base):
|
| 33 |
-
__tablename__ = "branches"
|
| 34 |
-
|
| 35 |
-
id: Mapped[int] = mapped_column(primary_key=True)
|
| 36 |
-
name: Mapped[str] = mapped_column(String, unique=True, index=True)
|
| 37 |
-
address: Mapped[str] = mapped_column(String)
|
| 38 |
-
phone: Mapped[str] = mapped_column(String)
|
| 39 |
-
email: Mapped[str] = mapped_column(String)
|
| 40 |
-
is_active: Mapped[bool] = mapped_column(Boolean, default=True)
|
| 41 |
-
created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)
|
| 42 |
-
updated_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
|
| 43 |
-
|
| 44 |
-
# Relationships
|
| 45 |
-
users = relationship("User", back_populates="branch")
|
| 46 |
-
products = relationship("Product", back_populates="branch")
|
| 47 |
-
orders = relationship("Order", back_populates="branch")
|
| 48 |
-
staff_activities = relationship("StaffActivity", back_populates="branch")
|
| 49 |
-
staff_metrics = relationship("PerformanceMetric", back_populates="branch")
|
| 50 |
-
|
| 51 |
-
async def ensure_tables():
|
| 52 |
-
"""Ensure all tables exist in the database"""
|
| 53 |
-
async with async_engine.begin() as conn:
|
| 54 |
-
await conn.run_sync(Base.metadata.create_all)
|
| 55 |
-
|
| 56 |
-
class User(Base):
|
| 57 |
-
__tablename__ = "users"
|
| 58 |
-
|
| 59 |
-
id: Mapped[int] = mapped_column(primary_key=True)
|
| 60 |
-
email: Mapped[str] = mapped_column(String, unique=True, index=True)
|
| 61 |
-
username: Mapped[str] = mapped_column(String, unique=True, index=True)
|
| 62 |
-
full_name: Mapped[str] = mapped_column(String)
|
| 63 |
-
hashed_password: Mapped[str] = mapped_column(String)
|
| 64 |
-
is_active: Mapped[bool] = mapped_column(Boolean, default=True)
|
| 65 |
-
is_superuser: Mapped[bool] = mapped_column(Boolean, default=False)
|
| 66 |
-
branch_id: Mapped[Optional[int]] = mapped_column(ForeignKey("branches.id"))
|
| 67 |
-
created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)
|
| 68 |
-
|
| 69 |
-
# Update roles relationship to use many-to-many
|
| 70 |
-
roles = relationship("Role", secondary=user_roles, back_populates="users")
|
| 71 |
-
|
| 72 |
-
# Other relationships
|
| 73 |
-
branch = relationship("Branch", back_populates="users")
|
| 74 |
-
products = relationship("Product", back_populates="seller")
|
| 75 |
-
orders = relationship("Order", back_populates="customer")
|
| 76 |
-
notifications = relationship("Notification", back_populates="user")
|
| 77 |
-
sessions = relationship("Session", back_populates="user", cascade="all, delete-orphan")
|
| 78 |
-
activities = relationship("StaffActivity", back_populates="user")
|
| 79 |
-
performance_metrics = relationship("PerformanceMetric", back_populates="user")
|
| 80 |
-
|
| 81 |
-
class Product(Base):
|
| 82 |
-
__tablename__ = "products"
|
| 83 |
-
|
| 84 |
-
id: Mapped[int] = mapped_column(primary_key=True)
|
| 85 |
-
name: Mapped[str] = mapped_column(String, index=True)
|
| 86 |
-
description: Mapped[str] = mapped_column(String)
|
| 87 |
-
price: Mapped[float] = mapped_column(Float)
|
| 88 |
-
category: Mapped[str] = mapped_column(String, index=True)
|
| 89 |
-
inventory_count: Mapped[int] = mapped_column(Integer)
|
| 90 |
-
seller_id: Mapped[int] = mapped_column(ForeignKey("users.id"))
|
| 91 |
-
branch_id: Mapped[int] = mapped_column(ForeignKey("branches.id"))
|
| 92 |
-
brand_id: Mapped[Optional[int]] = mapped_column(ForeignKey("brands.id"), nullable=True)
|
| 93 |
-
created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)
|
| 94 |
-
updated_at: Mapped[datetime] = mapped_column(
|
| 95 |
-
DateTime,
|
| 96 |
-
default=datetime.utcnow,
|
| 97 |
-
onupdate=datetime.utcnow
|
| 98 |
-
)
|
| 99 |
-
|
| 100 |
-
# Relationships
|
| 101 |
-
seller = relationship("User", back_populates="products")
|
| 102 |
-
branch = relationship("Branch", back_populates="products")
|
| 103 |
-
brand = relationship("Brand", back_populates="products")
|
| 104 |
-
order_items = relationship("OrderItem", back_populates="product")
|
| 105 |
-
|
| 106 |
-
class Order(Base):
|
| 107 |
-
__tablename__ = "orders"
|
| 108 |
-
|
| 109 |
-
id: Mapped[int] = mapped_column(primary_key=True)
|
| 110 |
-
customer_id: Mapped[int] = mapped_column(ForeignKey("users.id"))
|
| 111 |
-
branch_id: Mapped[int] = mapped_column(ForeignKey("branches.id"))
|
| 112 |
-
total_amount: Mapped[float] = mapped_column(Float)
|
| 113 |
-
status: Mapped[str] = mapped_column(String)
|
| 114 |
-
created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)
|
| 115 |
-
updated_at: Mapped[datetime] = mapped_column(
|
| 116 |
-
DateTime,
|
| 117 |
-
default=datetime.utcnow,
|
| 118 |
-
onupdate=datetime.utcnow
|
| 119 |
-
)
|
| 120 |
-
|
| 121 |
-
# Relationships
|
| 122 |
-
customer = relationship("User", back_populates="orders")
|
| 123 |
-
branch = relationship("Branch", back_populates="orders")
|
| 124 |
-
items = relationship("OrderItem", back_populates="order", cascade="all, delete-orphan")
|
| 125 |
-
|
| 126 |
-
class OrderItem(Base):
|
| 127 |
-
__tablename__ = "order_items"
|
| 128 |
-
|
| 129 |
-
id: Mapped[int] = mapped_column(primary_key=True)
|
| 130 |
-
order_id: Mapped[int] = mapped_column(ForeignKey("orders.id"))
|
| 131 |
-
product_id: Mapped[int] = mapped_column(ForeignKey("products.id"))
|
| 132 |
-
quantity: Mapped[int] = mapped_column(Integer)
|
| 133 |
-
price: Mapped[float] = mapped_column(Float)
|
| 134 |
-
|
| 135 |
-
# Relationships
|
| 136 |
-
order = relationship("Order", back_populates="items")
|
| 137 |
-
product = relationship("Product", back_populates="order_items")
|
| 138 |
-
|
| 139 |
-
class Notification(Base):
|
| 140 |
-
__tablename__ = "notifications"
|
| 141 |
-
|
| 142 |
-
id: Mapped[int] = mapped_column(primary_key=True)
|
| 143 |
-
user_id: Mapped[int] = mapped_column(ForeignKey("users.id"))
|
| 144 |
-
title: Mapped[str] = mapped_column(String)
|
| 145 |
-
message: Mapped[str] = mapped_column(String)
|
| 146 |
-
type: Mapped[str] = mapped_column(String)
|
| 147 |
-
data: Mapped[Optional[dict]] = mapped_column(JSONB)
|
| 148 |
-
created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)
|
| 149 |
-
read: Mapped[bool] = mapped_column(Boolean, default=False)
|
| 150 |
-
|
| 151 |
-
# Relationship
|
| 152 |
-
user = relationship("User", back_populates="notifications")
|
| 153 |
-
|
| 154 |
-
class Event(Base):
|
| 155 |
-
__tablename__ = "events"
|
| 156 |
-
|
| 157 |
-
id: Mapped[int] = mapped_column(primary_key=True)
|
| 158 |
-
user_id: Mapped[int] = mapped_column(ForeignKey("users.id"))
|
| 159 |
-
title: Mapped[str] = mapped_column(String)
|
| 160 |
-
description: Mapped[str] = mapped_column(String)
|
| 161 |
-
start_time: Mapped[datetime] = mapped_column(DateTime)
|
| 162 |
-
end_time: Mapped[datetime] = mapped_column(DateTime)
|
| 163 |
-
attendees: Mapped[List[str]] = mapped_column(ARRAY(String), default=list)
|
| 164 |
-
is_all_day: Mapped[bool] = mapped_column(Boolean, default=False)
|
| 165 |
-
reminder_minutes: Mapped[int] = mapped_column(Integer)
|
| 166 |
-
status: Mapped[str] = mapped_column(String)
|
| 167 |
-
attendee_responses: Mapped[dict] = mapped_column(JSONB, default=dict)
|
| 168 |
-
created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)
|
| 169 |
-
updated_at: Mapped[datetime] = mapped_column(
|
| 170 |
-
DateTime,
|
| 171 |
-
default=datetime.utcnow,
|
| 172 |
-
onupdate=datetime.utcnow
|
| 173 |
-
)
|
| 174 |
-
# Fields for recurring events
|
| 175 |
-
is_recurring: Mapped[bool] = mapped_column(Boolean, default=False)
|
| 176 |
-
recurrence_pattern: Mapped[Optional[str]] = mapped_column(String)
|
| 177 |
-
recurrence_group: Mapped[Optional[str]] = mapped_column(String)
|
| 178 |
-
recurrence_end_date: Mapped[Optional[datetime]] = mapped_column(DateTime)
|
| 179 |
-
parent_event_id: Mapped[Optional[int]] = mapped_column(Integer)
|
| 180 |
-
sequence_number: Mapped[Optional[int]] = mapped_column(Integer)
|
| 181 |
-
reminder_sent: Mapped[bool] = mapped_column(Boolean, default=False)
|
| 182 |
-
|
| 183 |
-
# Relationship
|
| 184 |
-
user = relationship("User")
|
| 185 |
-
|
| 186 |
-
class Session(Base):
|
| 187 |
-
__tablename__ = "sessions"
|
| 188 |
-
|
| 189 |
-
id: Mapped[int] = mapped_column(primary_key=True)
|
| 190 |
-
user_id: Mapped[int] = mapped_column(ForeignKey("users.id"))
|
| 191 |
-
session_token: Mapped[str] = mapped_column(String, unique=True, index=True)
|
| 192 |
-
last_activity: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)
|
| 193 |
-
created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)
|
| 194 |
-
expires_at: Mapped[datetime] = mapped_column(DateTime)
|
| 195 |
-
is_active: Mapped[bool] = mapped_column(Boolean, default=True)
|
| 196 |
-
iteration_active: Mapped[bool] = mapped_column(Boolean, default=True)
|
| 197 |
-
|
| 198 |
-
# Relationship
|
| 199 |
-
user = relationship("User", back_populates="sessions")
|
| 200 |
-
|
| 201 |
-
class ActivityType(str, enum.Enum):
|
| 202 |
-
LOGIN = "login"
|
| 203 |
-
LOGOUT = "logout"
|
| 204 |
-
SALE = "sale"
|
| 205 |
-
VOID = "void"
|
| 206 |
-
REFUND = "refund"
|
| 207 |
-
INVENTORY = "inventory"
|
| 208 |
-
CUSTOMER_SERVICE = "customer_service"
|
| 209 |
-
|
| 210 |
-
class StaffActivity(Base):
|
| 211 |
-
__tablename__ = "staff_activities"
|
| 212 |
-
|
| 213 |
-
id: Mapped[int] = mapped_column(primary_key=True)
|
| 214 |
-
user_id: Mapped[int] = mapped_column(ForeignKey("users.id"))
|
| 215 |
-
branch_id: Mapped[int] = mapped_column(ForeignKey("branches.id"))
|
| 216 |
-
activity_type: Mapped[str] = mapped_column(String)
|
| 217 |
-
details: Mapped[dict] = mapped_column(JSON)
|
| 218 |
-
created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)
|
| 219 |
-
duration: Mapped[Optional[float]] = mapped_column(Float, nullable=True) # Duration in minutes if applicable
|
| 220 |
-
performance_score: Mapped[Optional[float]] = mapped_column(Float, nullable=True) # Score based on activity type
|
| 221 |
-
|
| 222 |
-
# Relationships
|
| 223 |
-
user = relationship("User", back_populates="activities")
|
| 224 |
-
branch = relationship("Branch", back_populates="staff_activities")
|
| 225 |
-
|
| 226 |
-
class PerformanceMetric(Base):
|
| 227 |
-
__tablename__ = "performance_metrics"
|
| 228 |
-
|
| 229 |
-
id: Mapped[int] = mapped_column(primary_key=True)
|
| 230 |
-
user_id: Mapped[int] = mapped_column(ForeignKey("users.id"))
|
| 231 |
-
branch_id: Mapped[int] = mapped_column(ForeignKey("branches.id"))
|
| 232 |
-
metric_date: Mapped[datetime] = mapped_column(DateTime)
|
| 233 |
-
total_sales: Mapped[float] = mapped_column(Float, default=0)
|
| 234 |
-
transaction_count: Mapped[int] = mapped_column(Integer, default=0)
|
| 235 |
-
average_transaction_value: Mapped[float] = mapped_column(Float, default=0)
|
| 236 |
-
void_count: Mapped[int] = mapped_column(Integer, default=0)
|
| 237 |
-
customer_interaction_count: Mapped[int] = mapped_column(Integer, default=0)
|
| 238 |
-
login_time: Mapped[float] = mapped_column(Float, default=0) # Total minutes logged in
|
| 239 |
-
efficiency_score: Mapped[float] = mapped_column(Float, default=0) # Calculated score based on metrics
|
| 240 |
-
|
| 241 |
-
# Relationships
|
| 242 |
-
user = relationship("User", back_populates="performance_metrics")
|
| 243 |
-
branch = relationship("Branch", back_populates="staff_metrics")
|
| 244 |
-
|
| 245 |
-
class Brand(Base):
|
| 246 |
-
__tablename__ = "brands"
|
| 247 |
-
|
| 248 |
-
id: Mapped[int] = mapped_column(primary_key=True)
|
| 249 |
-
name: Mapped[str] = mapped_column(String, unique=True, index=True)
|
| 250 |
-
category: Mapped[str] = mapped_column(String)
|
| 251 |
-
established_date: Mapped[datetime] = mapped_column(DateTime)
|
| 252 |
-
is_active: Mapped[bool] = mapped_column(Boolean, default=True)
|
| 253 |
-
created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)
|
| 254 |
-
updated_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
|
| 255 |
-
|
| 256 |
-
# Relationships
|
| 257 |
products = relationship("Product", back_populates="brand")
|
|
|
|
| 1 |
+
from sqlalchemy import Column, Integer, String, Boolean, DateTime, Float, ForeignKey, ARRAY, JSON, Table, Enum
|
| 2 |
+
from sqlalchemy.orm import relationship, mapped_column, Mapped
|
| 3 |
+
from sqlalchemy.dialects.postgresql import JSONB
|
| 4 |
+
from datetime import datetime
|
| 5 |
+
from typing import List, Optional
|
| 6 |
+
from .database import Base, async_engine
|
| 7 |
+
import enum
|
| 8 |
+
|
| 9 |
+
# Association tables for many-to-many relationships
|
| 10 |
+
user_roles = Table(
|
| 11 |
+
'user_roles',
|
| 12 |
+
Base.metadata,
|
| 13 |
+
Column('user_id', Integer, ForeignKey('users.id')),
|
| 14 |
+
Column('role_id', Integer, ForeignKey('roles.id'))
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
# Role model
|
| 18 |
+
class Role(Base):
|
| 19 |
+
__tablename__ = "roles"
|
| 20 |
+
|
| 21 |
+
id: Mapped[int] = mapped_column(primary_key=True)
|
| 22 |
+
name: Mapped[str] = mapped_column(String, unique=True, index=True)
|
| 23 |
+
description: Mapped[str] = mapped_column(String)
|
| 24 |
+
permissions: Mapped[List[str]] = mapped_column(ARRAY(String), default=list)
|
| 25 |
+
created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)
|
| 26 |
+
updated_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
|
| 27 |
+
|
| 28 |
+
# Relationships
|
| 29 |
+
users = relationship("User", secondary=user_roles, back_populates="roles")
|
| 30 |
+
|
| 31 |
+
# Branch model
|
| 32 |
+
class Branch(Base):
|
| 33 |
+
__tablename__ = "branches"
|
| 34 |
+
|
| 35 |
+
id: Mapped[int] = mapped_column(primary_key=True)
|
| 36 |
+
name: Mapped[str] = mapped_column(String, unique=True, index=True)
|
| 37 |
+
address: Mapped[str] = mapped_column(String)
|
| 38 |
+
phone: Mapped[str] = mapped_column(String)
|
| 39 |
+
email: Mapped[str] = mapped_column(String)
|
| 40 |
+
is_active: Mapped[bool] = mapped_column(Boolean, default=True)
|
| 41 |
+
created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)
|
| 42 |
+
updated_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
|
| 43 |
+
|
| 44 |
+
# Relationships
|
| 45 |
+
users = relationship("User", back_populates="branch")
|
| 46 |
+
products = relationship("Product", back_populates="branch")
|
| 47 |
+
orders = relationship("Order", back_populates="branch")
|
| 48 |
+
staff_activities = relationship("StaffActivity", back_populates="branch")
|
| 49 |
+
staff_metrics = relationship("PerformanceMetric", back_populates="branch")
|
| 50 |
+
|
| 51 |
+
async def ensure_tables():
|
| 52 |
+
"""Ensure all tables exist in the database"""
|
| 53 |
+
async with async_engine.begin() as conn:
|
| 54 |
+
await conn.run_sync(Base.metadata.create_all)
|
| 55 |
+
|
| 56 |
+
class User(Base):
|
| 57 |
+
__tablename__ = "users"
|
| 58 |
+
|
| 59 |
+
id: Mapped[int] = mapped_column(primary_key=True)
|
| 60 |
+
email: Mapped[str] = mapped_column(String, unique=True, index=True)
|
| 61 |
+
username: Mapped[str] = mapped_column(String, unique=True, index=True)
|
| 62 |
+
full_name: Mapped[str] = mapped_column(String)
|
| 63 |
+
hashed_password: Mapped[str] = mapped_column(String)
|
| 64 |
+
is_active: Mapped[bool] = mapped_column(Boolean, default=True)
|
| 65 |
+
is_superuser: Mapped[bool] = mapped_column(Boolean, default=False)
|
| 66 |
+
branch_id: Mapped[Optional[int]] = mapped_column(ForeignKey("branches.id"))
|
| 67 |
+
created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)
|
| 68 |
+
|
| 69 |
+
# Update roles relationship to use many-to-many
|
| 70 |
+
roles = relationship("Role", secondary=user_roles, back_populates="users")
|
| 71 |
+
|
| 72 |
+
# Other relationships
|
| 73 |
+
branch = relationship("Branch", back_populates="users")
|
| 74 |
+
products = relationship("Product", back_populates="seller")
|
| 75 |
+
orders = relationship("Order", back_populates="customer")
|
| 76 |
+
notifications = relationship("Notification", back_populates="user")
|
| 77 |
+
sessions = relationship("Session", back_populates="user", cascade="all, delete-orphan")
|
| 78 |
+
activities = relationship("StaffActivity", back_populates="user")
|
| 79 |
+
performance_metrics = relationship("PerformanceMetric", back_populates="user")
|
| 80 |
+
|
| 81 |
+
class Product(Base):
|
| 82 |
+
__tablename__ = "products"
|
| 83 |
+
|
| 84 |
+
id: Mapped[int] = mapped_column(primary_key=True)
|
| 85 |
+
name: Mapped[str] = mapped_column(String, index=True)
|
| 86 |
+
description: Mapped[str] = mapped_column(String)
|
| 87 |
+
price: Mapped[float] = mapped_column(Float)
|
| 88 |
+
category: Mapped[str] = mapped_column(String, index=True)
|
| 89 |
+
inventory_count: Mapped[int] = mapped_column(Integer)
|
| 90 |
+
seller_id: Mapped[int] = mapped_column(ForeignKey("users.id"))
|
| 91 |
+
branch_id: Mapped[int] = mapped_column(ForeignKey("branches.id"))
|
| 92 |
+
brand_id: Mapped[Optional[int]] = mapped_column(ForeignKey("brands.id"), nullable=True)
|
| 93 |
+
created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)
|
| 94 |
+
updated_at: Mapped[datetime] = mapped_column(
|
| 95 |
+
DateTime,
|
| 96 |
+
default=datetime.utcnow,
|
| 97 |
+
onupdate=datetime.utcnow
|
| 98 |
+
)
|
| 99 |
+
|
| 100 |
+
# Relationships
|
| 101 |
+
seller = relationship("User", back_populates="products")
|
| 102 |
+
branch = relationship("Branch", back_populates="products")
|
| 103 |
+
brand = relationship("Brand", back_populates="products")
|
| 104 |
+
order_items = relationship("OrderItem", back_populates="product")
|
| 105 |
+
|
| 106 |
+
class Order(Base):
|
| 107 |
+
__tablename__ = "orders"
|
| 108 |
+
|
| 109 |
+
id: Mapped[int] = mapped_column(primary_key=True)
|
| 110 |
+
customer_id: Mapped[int] = mapped_column(ForeignKey("users.id"))
|
| 111 |
+
branch_id: Mapped[int] = mapped_column(ForeignKey("branches.id"))
|
| 112 |
+
total_amount: Mapped[float] = mapped_column(Float)
|
| 113 |
+
status: Mapped[str] = mapped_column(String)
|
| 114 |
+
created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)
|
| 115 |
+
updated_at: Mapped[datetime] = mapped_column(
|
| 116 |
+
DateTime,
|
| 117 |
+
default=datetime.utcnow,
|
| 118 |
+
onupdate=datetime.utcnow
|
| 119 |
+
)
|
| 120 |
+
|
| 121 |
+
# Relationships
|
| 122 |
+
customer = relationship("User", back_populates="orders")
|
| 123 |
+
branch = relationship("Branch", back_populates="orders")
|
| 124 |
+
items = relationship("OrderItem", back_populates="order", cascade="all, delete-orphan")
|
| 125 |
+
|
| 126 |
+
class OrderItem(Base):
|
| 127 |
+
__tablename__ = "order_items"
|
| 128 |
+
|
| 129 |
+
id: Mapped[int] = mapped_column(primary_key=True)
|
| 130 |
+
order_id: Mapped[int] = mapped_column(ForeignKey("orders.id"))
|
| 131 |
+
product_id: Mapped[int] = mapped_column(ForeignKey("products.id"))
|
| 132 |
+
quantity: Mapped[int] = mapped_column(Integer)
|
| 133 |
+
price: Mapped[float] = mapped_column(Float)
|
| 134 |
+
|
| 135 |
+
# Relationships
|
| 136 |
+
order = relationship("Order", back_populates="items")
|
| 137 |
+
product = relationship("Product", back_populates="order_items")
|
| 138 |
+
|
| 139 |
+
class Notification(Base):
|
| 140 |
+
__tablename__ = "notifications"
|
| 141 |
+
|
| 142 |
+
id: Mapped[int] = mapped_column(primary_key=True)
|
| 143 |
+
user_id: Mapped[int] = mapped_column(ForeignKey("users.id"))
|
| 144 |
+
title: Mapped[str] = mapped_column(String)
|
| 145 |
+
message: Mapped[str] = mapped_column(String)
|
| 146 |
+
type: Mapped[str] = mapped_column(String)
|
| 147 |
+
data: Mapped[Optional[dict]] = mapped_column(JSONB)
|
| 148 |
+
created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)
|
| 149 |
+
read: Mapped[bool] = mapped_column(Boolean, default=False)
|
| 150 |
+
|
| 151 |
+
# Relationship
|
| 152 |
+
user = relationship("User", back_populates="notifications")
|
| 153 |
+
|
| 154 |
+
class Event(Base):
|
| 155 |
+
__tablename__ = "events"
|
| 156 |
+
|
| 157 |
+
id: Mapped[int] = mapped_column(primary_key=True)
|
| 158 |
+
user_id: Mapped[int] = mapped_column(ForeignKey("users.id"))
|
| 159 |
+
title: Mapped[str] = mapped_column(String)
|
| 160 |
+
description: Mapped[str] = mapped_column(String)
|
| 161 |
+
start_time: Mapped[datetime] = mapped_column(DateTime)
|
| 162 |
+
end_time: Mapped[datetime] = mapped_column(DateTime)
|
| 163 |
+
attendees: Mapped[List[str]] = mapped_column(ARRAY(String), default=list)
|
| 164 |
+
is_all_day: Mapped[bool] = mapped_column(Boolean, default=False)
|
| 165 |
+
reminder_minutes: Mapped[int] = mapped_column(Integer)
|
| 166 |
+
status: Mapped[str] = mapped_column(String)
|
| 167 |
+
attendee_responses: Mapped[dict] = mapped_column(JSONB, default=dict)
|
| 168 |
+
created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)
|
| 169 |
+
updated_at: Mapped[datetime] = mapped_column(
|
| 170 |
+
DateTime,
|
| 171 |
+
default=datetime.utcnow,
|
| 172 |
+
onupdate=datetime.utcnow
|
| 173 |
+
)
|
| 174 |
+
# Fields for recurring events
|
| 175 |
+
is_recurring: Mapped[bool] = mapped_column(Boolean, default=False)
|
| 176 |
+
recurrence_pattern: Mapped[Optional[str]] = mapped_column(String)
|
| 177 |
+
recurrence_group: Mapped[Optional[str]] = mapped_column(String)
|
| 178 |
+
recurrence_end_date: Mapped[Optional[datetime]] = mapped_column(DateTime)
|
| 179 |
+
parent_event_id: Mapped[Optional[int]] = mapped_column(Integer)
|
| 180 |
+
sequence_number: Mapped[Optional[int]] = mapped_column(Integer)
|
| 181 |
+
reminder_sent: Mapped[bool] = mapped_column(Boolean, default=False)
|
| 182 |
+
|
| 183 |
+
# Relationship
|
| 184 |
+
user = relationship("User")
|
| 185 |
+
|
| 186 |
+
class Session(Base):
|
| 187 |
+
__tablename__ = "sessions"
|
| 188 |
+
|
| 189 |
+
id: Mapped[int] = mapped_column(primary_key=True)
|
| 190 |
+
user_id: Mapped[int] = mapped_column(ForeignKey("users.id"))
|
| 191 |
+
session_token: Mapped[str] = mapped_column(String, unique=True, index=True)
|
| 192 |
+
last_activity: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)
|
| 193 |
+
created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)
|
| 194 |
+
expires_at: Mapped[datetime] = mapped_column(DateTime)
|
| 195 |
+
is_active: Mapped[bool] = mapped_column(Boolean, default=True)
|
| 196 |
+
iteration_active: Mapped[bool] = mapped_column(Boolean, default=True)
|
| 197 |
+
|
| 198 |
+
# Relationship
|
| 199 |
+
user = relationship("User", back_populates="sessions")
|
| 200 |
+
|
| 201 |
+
class ActivityType(str, enum.Enum):
|
| 202 |
+
LOGIN = "login"
|
| 203 |
+
LOGOUT = "logout"
|
| 204 |
+
SALE = "sale"
|
| 205 |
+
VOID = "void"
|
| 206 |
+
REFUND = "refund"
|
| 207 |
+
INVENTORY = "inventory"
|
| 208 |
+
CUSTOMER_SERVICE = "customer_service"
|
| 209 |
+
|
| 210 |
+
class StaffActivity(Base):
|
| 211 |
+
__tablename__ = "staff_activities"
|
| 212 |
+
|
| 213 |
+
id: Mapped[int] = mapped_column(primary_key=True)
|
| 214 |
+
user_id: Mapped[int] = mapped_column(ForeignKey("users.id"))
|
| 215 |
+
branch_id: Mapped[int] = mapped_column(ForeignKey("branches.id"))
|
| 216 |
+
activity_type: Mapped[str] = mapped_column(String)
|
| 217 |
+
details: Mapped[dict] = mapped_column(JSON)
|
| 218 |
+
created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)
|
| 219 |
+
duration: Mapped[Optional[float]] = mapped_column(Float, nullable=True) # Duration in minutes if applicable
|
| 220 |
+
performance_score: Mapped[Optional[float]] = mapped_column(Float, nullable=True) # Score based on activity type
|
| 221 |
+
|
| 222 |
+
# Relationships
|
| 223 |
+
user = relationship("User", back_populates="activities")
|
| 224 |
+
branch = relationship("Branch", back_populates="staff_activities")
|
| 225 |
+
|
| 226 |
+
class PerformanceMetric(Base):
|
| 227 |
+
__tablename__ = "performance_metrics"
|
| 228 |
+
|
| 229 |
+
id: Mapped[int] = mapped_column(primary_key=True)
|
| 230 |
+
user_id: Mapped[int] = mapped_column(ForeignKey("users.id"))
|
| 231 |
+
branch_id: Mapped[int] = mapped_column(ForeignKey("branches.id"))
|
| 232 |
+
metric_date: Mapped[datetime] = mapped_column(DateTime)
|
| 233 |
+
total_sales: Mapped[float] = mapped_column(Float, default=0)
|
| 234 |
+
transaction_count: Mapped[int] = mapped_column(Integer, default=0)
|
| 235 |
+
average_transaction_value: Mapped[float] = mapped_column(Float, default=0)
|
| 236 |
+
void_count: Mapped[int] = mapped_column(Integer, default=0)
|
| 237 |
+
customer_interaction_count: Mapped[int] = mapped_column(Integer, default=0)
|
| 238 |
+
login_time: Mapped[float] = mapped_column(Float, default=0) # Total minutes logged in
|
| 239 |
+
efficiency_score: Mapped[float] = mapped_column(Float, default=0) # Calculated score based on metrics
|
| 240 |
+
|
| 241 |
+
# Relationships
|
| 242 |
+
user = relationship("User", back_populates="performance_metrics")
|
| 243 |
+
branch = relationship("Branch", back_populates="staff_metrics")
|
| 244 |
+
|
| 245 |
+
class Brand(Base):
|
| 246 |
+
__tablename__ = "brands"
|
| 247 |
+
|
| 248 |
+
id: Mapped[int] = mapped_column(primary_key=True)
|
| 249 |
+
name: Mapped[str] = mapped_column(String, unique=True, index=True)
|
| 250 |
+
category: Mapped[str] = mapped_column(String)
|
| 251 |
+
established_date: Mapped[datetime] = mapped_column(DateTime)
|
| 252 |
+
is_active: Mapped[bool] = mapped_column(Boolean, default=True)
|
| 253 |
+
created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)
|
| 254 |
+
updated_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
|
| 255 |
+
|
| 256 |
+
# Relationships
|
| 257 |
products = relationship("Product", back_populates="brand")
|