Files / app /core /seeding.py
lea97338's picture
Upload 100 files (#1)
680fa2b
Raw
History Blame Contribute Delete
6.05 kB
from sqlalchemy.orm import Session
from backend.app.models.users import User, WorkerProfile, CustomerProfile
from backend.app.models.jobs import Job
from backend.app.models.bookings import Booking
from backend.app.models.wallet import WalletTransaction
from backend.app.models.roster import RosterWorker
from backend.app.models.alerts import AdminAlert
from backend.app.core.security import get_password_hash
def seed_db(db: Session):
"""Pre-populates the database with initial viva seed data if empty."""
# Check if DB is already seeded
if db.query(User).first() is not None:
print("Database already seeded.")
return False
print("Seeding database...")
# 1. Create Default Admin
admin = User(
name="Chandan Admin",
phone="9999999999",
role="admin",
password_hash=get_password_hash("admin123")
)
db.add(admin)
# 2. Create Default Customer
customer = User(
name="Harsh",
phone="9876543210",
role="customer",
password_hash=get_password_hash("pass123")
)
db.add(customer)
db.flush()
cust_profile = CustomerProfile(user_id=customer.id, wallet_balance=8200)
db.add(cust_profile)
# 3. Create Default Mediator
mediator = User(
name="Rafiq Thekedar",
phone="9876599999",
role="mediator",
password_hash=get_password_hash("pass123")
)
db.add(mediator)
db.flush()
med_cust_profile = CustomerProfile(user_id=mediator.id, wallet_balance=5000)
db.add(med_cust_profile)
# 4. Create Seed Workers
workers_data = [
("Ramesh Kumar", "9876500001", "Mason", 650, 4.8, 126, 97, 67, 34, True, True),
("Imran Ali", "9876500002", "Electrician", 800, 4.7, 88, 95, 35, 45, True, True),
("Sita Devi", "9876500003", "Painter", 700, 4.9, 112, 99, 58, 72, True, True),
("Babulal Meena", "9876500004", "Helper", 500, 4.4, 46, 91, 26, 67, True, False),
("Karan Singh", "9876500005", "Carpenter", 750, 4.6, 73, 92, 74, 58, False, True),
("Mohan Lal", "9876500006", "Plumber", 720, 4.5, 65, 93, 46, 28, True, True),
]
workers = []
for name, phone, skill, rate, rating, completed, completion, x, y, online, verified in workers_data:
user = User(
name=name,
phone=phone,
role="worker",
password_hash=get_password_hash("pass123")
)
db.add(user)
db.flush()
# Calculate distance
import math
dist = round(math.sqrt((x - 50)**2 + (y - 50)**2) / 15, 1) or 1.2
prof = WorkerProfile(
user_id=user.id,
skill=skill,
rate=rate,
rating=rating,
distance=dist,
online=online,
verified=verified,
completed_jobs=completed,
completion_rate=completion,
map_x=x,
map_y=y
)
db.add(prof)
workers.append(user)
db.flush()
# 5. Create Seed Jobs
jobs_data = [
("Mason", "Mansarovar, Jaipur", 1300, "POSTED", "Boundary wall repair and cement finishing."),
("Painter", "Vaishali Nagar", 2800, "ACCEPTED", "Two rooms wall putty and primer."),
("Electrician", "Malviya Nagar", 900, "IN_PROGRESS", "Switch board repair and fan installation."),
]
jobs = []
for skill, loc, budget, status, desc in jobs_data:
job = Job(
customer_id=customer.id,
skill_required=skill,
location=loc,
budget=budget,
description=desc,
status=status
)
db.add(job)
jobs.append(job)
db.flush()
# 6. Create Seed Bookings
bookings_data = [
("B-2401", "Boundary wall repair", workers[0].id, 1300, "ACCEPTED", jobs[0].id),
("B-2402", "Switch board repair", workers[1].id, 900, "IN_PROGRESS", jobs[2].id),
]
for code, title, worker_id, amount, status, job_id in bookings_data:
booking = Booking(
code=code,
customer_id=customer.id,
worker_id=worker_id,
amount=amount,
status=status,
job_id=job_id
)
db.add(booking)
db.flush()
# 7. Create Seed Wallet Transactions
tx_data = [
("Wallet top-up via UPI", 5000, "credit"),
("Escrow hold for B-2401", -1300, "hold"),
("Escrow hold for B-2402", -900, "hold"),
("Refund from cancelled booking", 600, "credit"),
]
for label, amount, tx_type in tx_data:
tx = WalletTransaction(
user_id=customer.id,
label=label,
amount=amount,
type=tx_type
)
db.add(tx)
# 8. Create Seed Roster Workers
roster_data = [
("Rafiq", "Helper", "IVR only", "Available", 1200),
("Sunita", "Painter", "Verified", "On job", 820),
("Dinesh", "Mason", "Verified", "Available", 1440),
]
for name, skill, phone_status, status, commission in roster_data:
roster_worker = RosterWorker(
mediator_id=mediator.id,
name=name,
skill=skill,
phone_status=phone_status,
status=status,
commission=commission
)
db.add(roster_worker)
# 9. Create Seed Admin Alerts
alerts_data = [
("Dispute", "B-2397 quality issue pending admin review", "red"),
("Fraud", "Duplicate Aadhaar hash detected for two accounts", "orange"),
("IVR", "23 non-smartphone workers received daily job alerts", "gray"),
]
for type_, text, severity in alerts_data:
alert = AdminAlert(
type=type_,
text=text,
severity=severity,
status="active"
)
db.add(alert)
db.commit()
print("Database seeded successfully!")
return True