File size: 2,618 Bytes
d084ead
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""

One-time script to seed/update initial employee accounts.



Usage:

    python auth/seed_users.py



- Creates the user if the username AND email are new.

- Updates username + password if the email already exists (handles renames).

- Safe to re-run at any time.

"""

import sys
import os

# Allow importing from project root
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from dotenv import load_dotenv
load_dotenv()

from auth.database import engine, Base, SessionLocal
import auth.models  # register models with Base so create_all sees them
from auth.models import User
from auth.service import create_user, hash_password

Base.metadata.create_all(bind=engine)

INITIAL_USERS = [
    {
        "username": "Befree_employee1",
        "password": "Emp1_Test@2026!",
        "full_name": "Employee One",
        "email": "emp1@befree.com",
        "role": "tester",
    },
    {
        "username": "Befree_employee2",
        "password": "Emp2_Secure#849",
        "full_name": "Employee Two",
        "email": "emp2@befree.com",
        "role": "tester",
    },
    {
        "username": "Befree_employee3",
        "password": "Emp3_Login$572",
        "full_name": "Employee Three",
        "email": "emp3@befree.com",
        "role": "tester",
    }
]

for u in INITIAL_USERS:
    # Each user gets its own session so a failure on one doesn't cascade
    with SessionLocal() as db:
        try:
            # Check if a user with this email already exists
            existing_by_email = db.query(User).filter(User.email == u["email"]).first()

            if existing_by_email:
                # Update username and password (handles the employee1 -> Befree_employee1 rename)
                old_username = existing_by_email.username
                existing_by_email.username = u["username"]
                existing_by_email.hashed_password = hash_password(u["password"])
                existing_by_email.full_name = u["full_name"]
                existing_by_email.role = u["role"]
                db.commit()
                print(f"UPDATE Renamed: {old_username!r} -> {u['username']!r} ({u['role']})")
            else:
                # Brand new user
                user = create_user(db, **u)
                print(f"OK    Created: {user.username} ({user.role})")

        except Exception as e:
            db.rollback()
            print(f"SKIP  Failed:  {u['username']} - {e}")

print(
    "\nDone. Share credentials with employees and ask them to change passwords after first login."
)