Spaces:
Sleeping
Sleeping
File size: 6,046 Bytes
9fe25e8 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 | 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
|