init commit

This commit is contained in:
2025-08-31 17:55:26 +03:00
commit 876ddec94a
78 changed files with 11999 additions and 0 deletions

View File

@@ -0,0 +1,79 @@
from datetime import datetime
from database import get_connection, is_postgres
class Clients:
def __init__(self):
# Parameter style: Postgres uses %s, SQLite uses ?
self.ph = "%s" if is_postgres() else "?"
def client_to_dict(self, row):
return {
"id": row[0],
"user_id": row[1],
"name": row[2],
"address": row[3],
"register_number": row[4],
"contact_person": row[5],
"phone": row[6],
"email": row[7],
"vat":row[8],
"created_at": row[9],
}
def create(self, user_id, name, address, register_number, contact_person, phone=None, email=None, vat=None):
created_at = datetime.now().isoformat()
with get_connection() as conn:
cur = conn.cursor()
cur.execute(
f"""
INSERT INTO clients (user_id, name, address, register_number, contact_person, phone, email, vat, created_at)
VALUES ({self.ph}, {self.ph}, {self.ph}, {self.ph}, {self.ph}, {self.ph}, {self.ph}, {self.ph}, {self.ph})
""",
(user_id, name, address, register_number, contact_person, phone, email, vat, created_at),
)
if hasattr(conn, "commit"):
conn.commit()
def get_all_by_user(self, user_id):
with get_connection() as conn:
cur = conn.cursor()
cur.execute(
f"SELECT * FROM clients WHERE user_id = {self.ph}",
(user_id,),
)
return [self.client_to_dict(row) for row in cur.fetchall()]
def get_by_id(self, client_id):
with get_connection() as conn:
cur = conn.cursor()
cur.execute(
f"SELECT * FROM clients WHERE id = {self.ph}",
(client_id,),
)
row = cur.fetchone()
return self.client_to_dict(row) if row else None
def update(self, client_id, name, address, register_number, contact_person, phone=None, email=None, vat = None):
with get_connection() as conn:
cur = conn.cursor()
cur.execute(
f"""
UPDATE clients
SET name={self.ph}, address={self.ph}, register_number={self.ph}, contact_person={self.ph},
phone={self.ph}, email={self.ph}, vat={self.ph}
WHERE id={self.ph}
""",
(name, address, register_number, contact_person, phone, email, vat, client_id),
)
if hasattr(conn, "commit"):
conn.commit()
def delete(self, client_id):
with get_connection() as conn:
cur = conn.cursor()
cur.execute(
f"DELETE FROM clients WHERE id={self.ph}",
(client_id,),
)
if hasattr(conn, "commit"):
conn.commit()

View File

@@ -0,0 +1,92 @@
from datetime import datetime
from database import get_connection, is_postgres
class Destinations:
def __init__(self):
# Parameter style: Postgres uses %s, SQLite uses ?
self.ph = "%s" if is_postgres() else "?"
def destination_to_dict(self, row):
destination = {
"id": row[0],
"user_id": row[1],
"name": row[2],
"address": row[3],
"latitude": row[4],
"longitude": row[5],
"created_at": row[6]
}
return destination
def create(self, user_id, name, address):
created_at = datetime.now().isoformat()
with get_connection() as conn:
cur = conn.cursor()
cur.execute(
f"""
INSERT INTO destinations (user_id, name, address, created_at)
VALUES ({self.ph}, {self.ph}, {self.ph}, {self.ph})
""",
(user_id, name, address, created_at),
)
if hasattr(conn, "commit"):
conn.commit()
return cur.lastrowid if hasattr(cur, "lastrowid") else None
def update(self, id, user_id, name, address):
with get_connection() as conn:
cur = conn.cursor()
cur.execute(
f"""
UPDATE destinations
SET user_id = {self.ph}, name = {self.ph}, address = {self.ph}
WHERE id = {self.ph}
""",
(user_id, name, address, id),
)
if hasattr(conn, "commit"):
conn.commit()
def add_gps_coordinates(self, id, latitude, longitude):
with get_connection() as conn:
cur = conn.cursor()
cur.execute(
f"""
UPDATE destinations
SET latitude = {self.ph}, longitude = {self.ph}
WHERE id = {self.ph}
""",
(latitude, longitude, id),
)
if hasattr(conn, "commit"):
conn.commit()
def delete(self, id):
with get_connection() as conn:
cur = conn.cursor()
cur.execute(
f"DELETE FROM destinations WHERE id = {self.ph}",
(id,),
)
if hasattr(conn, "commit"):
conn.commit()
def get_all_by_user(self, user_id):
with get_connection() as conn:
cur = conn.cursor()
cur.execute(
f"SELECT * FROM destinations WHERE user_id = {self.ph} ORDER BY created_at DESC",
(user_id,),
)
rows = cur.fetchall()
return [self.destination_to_dict(row) for row in rows]
def get_by_id(self, id):
with get_connection() as conn:
cur = conn.cursor()
cur.execute(
f"SELECT * FROM destinations WHERE id = {self.ph}",
(id,),
)
row = cur.fetchone()
return self.destination_to_dict(row) if row else None

View File

@@ -0,0 +1,193 @@
from datetime import datetime
from database import get_connection, is_postgres
class OrdersIn:
def __init__(self):
# Parameter placeholder per backend
self.ph = "%s" if is_postgres() else "?"
def order_to_dict(self, row):
return {
"id": row[0],
"order_number": row[1],
"user_id": row[2],
"client_id": row[3],
"products_description": row[4],
"ldb_quantity": row[5],
"kg_quantity": row[6],
"track_reg_number": row[7],
"trailer_reg_number": row[8],
"received_price": row[9],
"created_at": row[10],
}
def order_point_to_dict(self, row):
return {
"id": row[0],
"order_id": row[1],
"destination_id": row[2],
"informatins": row[3],
"point_data": row[4],
"point_hour": row[5],
'point_type': row[6],
}
def create_order(self, data):
created_at = datetime.now().isoformat()
with get_connection() as conn:
cur = conn.cursor()
returning = " RETURNING id" if is_postgres() else ""
cur.execute(
f"""
INSERT INTO orders_in
(user_id, client_id, products_description, received_price, order_number,
ldb_quantity, kg_quantity, track_reg_number, trailer_reg_number, created_at)
VALUES ({self.ph}, {self.ph}, {self.ph}, {self.ph}, {self.ph}, {self.ph}, {self.ph}, {self.ph}, {self.ph}, {self.ph}){returning}
""",
(
data["user_id"],
data["client_id"],
data['products_description'],
data["received_price"],
data["order_number"],
data["ldb_quantity"],
data["kg_quantity"],
data["track_reg_number"],
data["trailer_reg_number"],
created_at,
),
)
new_id = cur.fetchone()[0] if is_postgres() else getattr(cur, "lastrowid", None)
if hasattr(conn, "commit"):
conn.commit()
return new_id
def update_order(self, data):
with get_connection() as conn:
cur = conn.cursor()
cur.execute(
f"""
UPDATE orders_in SET
user_id = {self.ph}, client_id = {self.ph}, products_description = {self.ph},
received_price = {self.ph}, order_number = {self.ph},
ldb_quantity = {self.ph}, kg_quantity = {self.ph}, track_reg_number = {self.ph},
trailer_reg_number = {self.ph}
WHERE id = {self.ph}
""",
(
data["user_id"],
data["client_id"],
data["products_description"],
data["received_price"],
data["order_number"],
data["ldb_quantity"],
data["kg_quantity"],
data["track_reg_number"],
data["trailer_reg_number"],
data["id"],
),
)
if hasattr(conn, "commit"):
conn.commit()
def delete_order(self, order_id):
with get_connection() as conn:
cur = conn.cursor()
cur.execute(f"DELETE FROM orders_in WHERE id = {self.ph}", (order_id,))
if hasattr(conn, "commit"):
conn.commit()
def get_order_by_id(self, order_id):
with get_connection() as conn:
cur = conn.cursor()
cur.execute(f"SELECT * FROM orders_in WHERE id = {self.ph}", (order_id,))
row = cur.fetchone()
return self.order_to_dict(row) if row else None
def get_orders_by_user(self, user_id):
with get_connection() as conn:
cur = conn.cursor()
cur.execute(
f"SELECT * FROM orders_in WHERE user_id = {self.ph} ORDER BY created_at DESC",
(user_id,),
)
rows = cur.fetchall()
return [self.order_to_dict(row) for row in rows]
def create_order_point(self, data):
with get_connection() as conn:
cur = conn.cursor()
returning = " RETURNING id" if is_postgres() else ""
cur.execute(
f"""
INSERT INTO order_in_points
(order_id, destination_id, informatins, point_data, point_hour, point_type)
VALUES ({self.ph}, {self.ph}, {self.ph}, {self.ph}, {self.ph}, {self.ph}){returning}
""",
(
data["order_id"],
data["destination_id"],
data["informatins"],
data["point_data"],
data["point_hour"],
data['point_type'],
),
)
new_id = cur.fetchone()[0] if is_postgres() else getattr(cur, "lastrowid", None)
if hasattr(conn, "commit"):
conn.commit()
return new_id
def update_order_point(self, data):
with get_connection() as conn:
cur = conn.cursor()
cur.execute(
f"""
UPDATE order_in_points SET
order_id = {self.ph}, destination_id = {self.ph}, informatins = {self.ph},
point_data = {self.ph}, point_hour = {self.ph}, point_type = {self.ph}
WHERE id = {self.ph}
""",
(
data["order_id"],
data["destination_id"],
data["informatins"],
data["point_data"],
data["point_hour"],
data['point_type'],
data["id"],
),
)
if hasattr(conn, "commit"):
conn.commit()
def delete_order_point(self, point_id):
with get_connection() as conn:
cur = conn.cursor()
cur.execute(f"DELETE FROM order_in_points WHERE id = {self.ph}", (point_id,))
if hasattr(conn, "commit"):
conn.commit()
def delete_points_by_order_id(self, order_id):
with get_connection() as conn:
cur = conn.cursor()
cur.execute(f"DELETE FROM order_in_points WHERE order_id = {self.ph}", (order_id,))
if hasattr(conn, "commit"):
conn.commit()
def get_order_point_by_id(self, point_id):
with get_connection() as conn:
cur = conn.cursor()
cur.execute(f"SELECT * FROM order_in_points WHERE id = {self.ph}", (point_id,))
row = cur.fetchone()
return self.order_point_to_dict(row) if row else None
def get_order_points_by_order(self, order_id):
with get_connection() as conn:
cur = conn.cursor()
cur.execute(
f"SELECT * FROM order_in_points WHERE order_id = {self.ph} ORDER BY id",
(order_id,),
)
rows = cur.fetchall()
return [self.order_point_to_dict(row) for row in rows]

View File

@@ -0,0 +1,209 @@
from datetime import datetime
from database import get_connection, is_postgres
class OrdersOut:
def __init__(self):
# Parameter placeholder per backend
self.ph = "%s" if is_postgres() else "?"
def order_to_dict(self, row):
return {
"id": row[0],
"order_number": row[1],
"user_id": row[2],
"client_id": row[3],
"transporter_id": row[4],
"products_description": row[5],
"ldb_quantity": row[6],
"kg_quantity": row[7],
"track_reg_number": row[8],
"trailer_reg_number": row[9],
"received_price": row[10],
"paid_price": row[11],
"created_at": row[12],
"status": row[13]
}
def order_point_to_dict(self, row):
return {
"id": row[0],
"order_id": row[1],
"destination_id": row[2],
"informatins": row[3],
"point_data": row[4],
"point_hour": row[5],
'point_type': row[6],
}
def create_order(self, data):
created_at = datetime.now().isoformat()
with get_connection() as conn:
cursor = conn.cursor()
returning = " RETURNING id" if is_postgres() else ""
cursor.execute(
f"""
INSERT INTO orders_out
(user_id, client_id, transporter_id, products_description, received_price, paid_price, order_number,
ldb_quantity, kg_quantity, track_reg_number, trailer_reg_number, created_at)
VALUES ({self.ph}, {self.ph}, {self.ph}, {self.ph}, {self.ph}, {self.ph}, {self.ph}, {self.ph}, {self.ph}, {self.ph}, {self.ph}, {self.ph}){returning}
""",
(
data["user_id"],
data["client_id"],
data["transporter_id"],
data['products_description'],
data["received_price"],
data["paid_price"],
data["order_number"],
data["ldb_quantity"],
data["kg_quantity"],
data["track_reg_number"],
data["trailer_reg_number"],
created_at,
),
)
new_id = cursor.fetchone()[0] if is_postgres() else getattr(cursor, "lastrowid", None)
if hasattr(conn, "commit"):
conn.commit()
return new_id
def update_order(self, data):
with get_connection() as conn:
cursor = conn.cursor()
cursor.execute(
f"""
UPDATE orders_out SET client_id = {self.ph}, transporter_id = {self.ph}, products_description = {self.ph},
received_price = {self.ph}, paid_price = {self.ph}, order_number = {self.ph},
ldb_quantity = {self.ph}, kg_quantity = {self.ph}, track_reg_number = {self.ph},
trailer_reg_number = {self.ph}
WHERE id = {self.ph}
""",
(
data["client_id"],
data["transporter_id"],
data["products_description"],
data["received_price"],
data["paid_price"],
data["order_number"],
data["ldb_quantity"],
data["kg_quantity"],
data["track_reg_number"],
data["trailer_reg_number"],
data["id"],
),
)
if hasattr(conn, "commit"):
conn.commit()
def cancel_order(self, order_id):
with get_connection() as conn:
cursor = conn.cursor()
cursor.execute(
f"UPDATE orders_out SET order_status = {self.ph} WHERE id = {self.ph}",
('cancelled', order_id,),
)
if hasattr(conn, "commit"):
conn.commit()
def delete_order(self, order_id):
with get_connection() as conn:
cursor = conn.cursor()
cursor.execute(f"DELETE FROM orders_out WHERE id = {self.ph}", (order_id,))
if hasattr(conn, "commit"):
conn.commit()
def get_order_by_id(self, order_id):
with get_connection() as conn:
cursor = conn.cursor()
cursor.execute(f"SELECT * FROM orders_out WHERE id = {self.ph}", (order_id,))
row = cursor.fetchone()
return self.order_to_dict(row) if row else None
def get_orders_by_user(self, user_id):
with get_connection() as conn:
cursor = conn.cursor()
cursor.execute(
f"SELECT * FROM orders_out WHERE user_id = {self.ph} ORDER BY created_at DESC",
(user_id,),
)
rows = cursor.fetchall()
return [self.order_to_dict(row) for row in rows]
def create_order_point(self, data):
with get_connection() as conn:
cursor = conn.cursor()
returning = " RETURNING id" if is_postgres() else ""
cursor.execute(
f"""
INSERT INTO order_out_points
(order_id, destination_id, informatins, point_data, point_hour, point_type)
VALUES ({self.ph}, {self.ph}, {self.ph}, {self.ph}, {self.ph}, {self.ph}){returning}
""",
(
data["order_id"],
data["destination_id"],
data["informatins"],
data["point_data"],
data["point_hour"],
data['point_type']
),
)
# keep behavior similar: no return expected, but commit consistently
if is_postgres():
_ = cursor.fetchone() # consume RETURNING if present
if hasattr(conn, "commit"):
conn.commit()
def update_order_point(self, data):
with get_connection() as conn:
cursor = conn.cursor()
cursor.execute(
f"""
UPDATE order_out_points SET
order_id = {self.ph}, destination_id = {self.ph}, informatins = {self.ph}, point_data = {self.ph}, point_hour = {self.ph}, point_type = {self.ph}
WHERE id = {self.ph}
""",
(
data["order_id"],
data["destination_id"],
data["informatins"],
data["point_data"],
data["point_hour"],
data['point_type'],
data["id"],
),
)
if hasattr(conn, "commit"):
conn.commit()
def delete_order_point(self, point_id):
with get_connection() as conn:
cursor = conn.cursor()
cursor.execute(f"DELETE FROM order_out_points WHERE id = {self.ph}", (point_id,))
if hasattr(conn, "commit"):
conn.commit()
def delete_points_by_order_id(self, order_id):
with get_connection() as conn:
cursor = conn.cursor()
cursor.execute(f"DELETE FROM order_out_points WHERE order_id = {self.ph}", (order_id,))
if hasattr(conn, "commit"):
conn.commit()
def get_order_point_by_id(self, point_id):
with get_connection() as conn:
cursor = conn.cursor()
cursor.execute(f"SELECT * FROM order_out_points WHERE id = {self.ph}", (point_id,))
row = cursor.fetchone()
return self.order_point_to_dict(row) if row else None
def get_order_points_by_order(self, order_id):
with get_connection() as conn:
cursor = conn.cursor()
cursor.execute(
f"SELECT * FROM order_out_points WHERE order_id = {self.ph} ORDER BY id",
(order_id,),
)
rows = cursor.fetchall()
return [self.order_point_to_dict(row) for row in rows]

View File

@@ -0,0 +1,130 @@
from datetime import datetime, timedelta
from database import get_connection, is_postgres
class Subscription:
def __init__(self):
# Parameter placeholder per backend
self.ph = "%s" if is_postgres() else "?"
def subscription_to_dict(self, row):
return {
"id": row[0],
"user_id": row[1], #company id
"plan": row[2],
"start_date": row[3],
"end_date": row[4],
"status": row[5],
"register_number": row[6],
"created_at": row[7],
}
def create(self, user_id, plan, start_date, end_date, register_number, status="active"):
created_at = datetime.now().isoformat()
with get_connection() as conn:
cursor = conn.cursor()
returning = " RETURNING id" if is_postgres() else ""
cursor.execute(
f"""
INSERT INTO subscriptions (user_id, plan, start_date, end_date, status, register_number, created_at)
VALUES ({self.ph}, {self.ph}, {self.ph}, {self.ph}, {self.ph}, {self.ph}, {self.ph}){returning}
""",
(user_id, plan, start_date, end_date, status, register_number, created_at),
)
new_id = cursor.fetchone()[0] if is_postgres() else getattr(cursor, "lastrowid", None)
if hasattr(conn, "commit"):
conn.commit()
return new_id
def get_by_user_id(self, user_id):
with get_connection() as conn:
cursor = conn.cursor()
cursor.execute(
f"""
SELECT * FROM subscriptions
WHERE user_id = {self.ph}
ORDER BY start_date DESC
""",
(user_id,),
)
rows = cursor.fetchall()
return [self.subscription_to_dict(row) for row in rows]
def get_by_id(self, subscription_id):
with get_connection() as conn:
cursor = conn.cursor()
cursor.execute(
f"""
SELECT * FROM subscriptions
WHERE id = {self.ph}
""",
(subscription_id,),
)
row = cursor.fetchone()
return self.subscription_to_dict(row) if row else None
def update_status(self, subscription_id, new_status):
with get_connection() as conn:
cursor = conn.cursor()
cursor.execute(
f"""
UPDATE subscriptions
SET status = {self.ph}
WHERE id = {self.ph}
""",
(new_status, subscription_id),
)
if hasattr(conn, "commit"):
conn.commit()
return cursor.rowcount if hasattr(cursor, "rowcount") else 0
def delete(self, subscription_id):
with get_connection() as conn:
cursor = conn.cursor()
cursor.execute(
f"""
DELETE FROM subscriptions
WHERE id = {self.ph}
""",
(subscription_id,),
)
if hasattr(conn, "commit"):
conn.commit()
return cursor.rowcount if hasattr(cursor, "rowcount") else 0
def get_first_2_months_subscription_for_register_number(self, register_number):
with get_connection() as conn:
cursor = conn.cursor()
cursor.execute(
f"""
SELECT * FROM subscriptions
WHERE register_number = {self.ph} AND plan = 'first_2_months' AND status = 'active'
""",
(register_number,),
)
row = cursor.fetchone()
return self.subscription_to_dict(row) if row else None
def get_all(self):
with get_connection() as conn:
cursor = conn.cursor()
cursor.execute(
"""
SELECT * FROM subscriptions
ORDER BY created_at DESC
"""
)
rows = cursor.fetchall()
return [self.subscription_to_dict(row) for row in rows]
def update_subscription_statuses(self):
now = datetime.now()
subscriptions = self.get_all()
for sub in subscriptions:
end_date = datetime.fromisoformat(sub["end_date"])
days_left = (end_date - now).days
if days_left < 0 and sub["status"] != "expired":
self.update_status(sub["id"], "expired")
elif 0 <= days_left <= 5 and sub["status"] != "less_than_5_days":
self.update_status(sub["id"], "less_than_5_days")

View File

@@ -0,0 +1,87 @@
from datetime import datetime
from database import get_connection, is_postgres
class Transporters:
def __init__(self):
# Parameter placeholder depending on backend
self.ph = "%s" if is_postgres() else "?"
def transporter_to_dict(self, row):
return {
"id": row[0],
"user_id": row[1],
"name": row[2],
"address": row[3],
"register_number": row[4],
"contact_person": row[5],
"phone": row[6],
"email": row[7],
"vat": row[8],
"created_at": row[9]
}
def create_transporter(self, name, address, register_number, contact_person, phone, email, vat, user_id):
created_at = datetime.now().isoformat()
returning = " RETURNING id" if is_postgres() else ""
with get_connection() as conn:
cursor = conn.cursor()
cursor.execute(
f"""
INSERT INTO transporters
(name, address, register_number, contact_person, phone, email, vat, created_at, user_id)
VALUES ({self.ph}, {self.ph}, {self.ph}, {self.ph}, {self.ph}, {self.ph}, {self.ph}, {self.ph}, {self.ph}){returning}
""",
(name, address, register_number, contact_person, phone, email, vat, created_at, user_id),
)
transporter_id = cursor.fetchone()[0] if is_postgres() else cursor.lastrowid
if hasattr(conn, "commit"):
conn.commit()
return transporter_id
def get_all_transporters_by_user(self, user_id):
with get_connection() as conn:
cursor = conn.cursor()
cursor.execute(
f"SELECT * FROM transporters WHERE user_id = {self.ph} ORDER BY created_at DESC",
(user_id,),
)
rows = cursor.fetchall()
return [self.transporter_to_dict(row) for row in rows]
def get_transporter_by_id(self, transporter_id):
with get_connection() as conn:
cursor = conn.cursor()
cursor.execute(
f"SELECT * FROM transporters WHERE id = {self.ph}",
(transporter_id,),
)
row = cursor.fetchone()
return self.transporter_to_dict(row) if row else None
def update_transporter(self, transporter_id, name, address, register_number, contact_person, phone, email, vat):
with get_connection() as conn:
cursor = conn.cursor()
cursor.execute(
f"""
UPDATE transporters SET
name = {self.ph},
address = {self.ph},
register_number = {self.ph},
contact_person = {self.ph},
phone = {self.ph},
email = {self.ph},
vat = {self.ph}
WHERE id = {self.ph}
""",
(name, address, register_number, contact_person, phone, email, vat, transporter_id),
)
if hasattr(conn, "commit"):
conn.commit()
def delete_transporter(self, transporter_id):
with get_connection() as conn:
cursor = conn.cursor()
cursor.execute(f"DELETE FROM transporters WHERE id = {self.ph}", (transporter_id,))
if hasattr(conn, "commit"):
conn.commit()

View File

@@ -0,0 +1,194 @@
from datetime import datetime
from database import get_connection, is_postgres
class Users:
def __init__(self):
self.ph = "%s" if is_postgres() else "?"
def user_to_dict(self, row):
user = {
'id': row[0],
'name': row[1],
'contact_name': row[2],
'email': row[3],
'password_hash': row[4],
'phone': row[5],
'register_number': row[6],
'vat':row[7],
'address': row[8],
'logo_filename': row[9],
'terms': row[10],
'first_order_number': row[11],
'created_at': row[12],
'otp_code': row[13],
'otp_expiration': row[14],
'user_role': row[15]
}
return user
def email_to_dict(self, row):
email = {
'id': row[0],
'user_id': row[1],
'smtp_host': row[2],
'smtp_port': row[3],
'smtp_user': row[4],
'created_at': row[5]
}
return email
def get_user_by_email(self, email):
with get_connection() as conn:
cursor = conn.cursor()
cursor.execute(f"SELECT * FROM users WHERE email = {self.ph}", (email,))
row = cursor.fetchone()
return self.user_to_dict(row) if row else None
def get_user_by_id(self, user_id):
with get_connection() as conn:
cursor = conn.cursor()
cursor.execute(f"SELECT * FROM users WHERE id = {self.ph}", (user_id,))
row = cursor.fetchone()
return self.user_to_dict(row) if row else None
def insert_user(self, name, email, password_hash):
created_at = datetime.now().isoformat()
with get_connection() as conn:
cursor = conn.cursor()
returning = "RETURNING id" if is_postgres() else ""
query = f"""
INSERT INTO users (
name, email, password_hash, created_at
) VALUES ({self.ph}, {self.ph}, {self.ph}, {self.ph}) {returning}
"""
cursor.execute(query, (name, email, password_hash, created_at))
inserted_id = None
if is_postgres():
inserted_id = cursor.fetchone()[0]
else:
inserted_id = cursor.lastrowid
if hasattr(conn, "commit"):
conn.commit()
return inserted_id
def update_user_otp(self, user_id, otp_code, expiration):
with get_connection() as conn:
cursor = conn.cursor()
cursor.execute(
f"""
UPDATE users
SET otp_code = {self.ph}, otp_expiration = {self.ph}
WHERE id = {self.ph}
""",
(otp_code, expiration.isoformat(), user_id)
)
if hasattr(conn, "commit"):
conn.commit()
def clear_user_otp(self, user_id):
with get_connection() as conn:
cursor = conn.cursor()
cursor.execute(
f"""
UPDATE users
SET otp_code = NULL, otp_expiration = NULL
WHERE id = {self.ph}
""",
(user_id,)
)
if hasattr(conn, "commit"):
conn.commit()
def update_user_password(self, user_id, new_password_hash):
with get_connection() as conn:
cursor = conn.cursor()
cursor.execute(
f"""
UPDATE users
SET password_hash = {self.ph}
WHERE id = {self.ph}
""",
(new_password_hash, user_id)
)
if hasattr(conn, "commit"):
conn.commit()
def update_user(self, data):
with get_connection() as conn:
cursor = conn.cursor()
cursor.execute(
f"""
UPDATE users
SET name = {self.ph}, contact_name = {self.ph}, email = {self.ph}, phone = {self.ph}, register_number = {self.ph}, vat = {self.ph}, address = {self.ph}, logo_filename = {self.ph}, terms = {self.ph}, first_order_number = {self.ph}
WHERE id = {self.ph}
""",
(
data['name'],
data['contact_name'],
data['email'],
data['phone'],
data['register_number'],
data['vat'],
data['address'],
data['logo_filename'],
data['terms'],
data['first_order_number'],
data['user_id']
)
)
if hasattr(conn, "commit"):
conn.commit()
def update_user_logo(self, data):
with get_connection() as conn:
cursor = conn.cursor()
cursor.execute(
f"""
UPDATE users SET logo_filename = {self.ph} WHERE id = {self.ph}
""",
(data['logo_filename'], data['user_id'])
)
if hasattr(conn, "commit"):
conn.commit()
def get_all_users_with_role(self, role='user'):
with get_connection() as conn:
cursor = conn.cursor()
cursor.execute(f"SELECT * FROM users WHERE user_role = {self.ph}", (role,))
rows = cursor.fetchall()
return [self.user_to_dict(row) for row in rows]
#--- email credentials ---
def insert_email_credentials(self, user_id, smtp_host, smtp_port, smtp_user):
created_at = datetime.now().isoformat()
with get_connection() as conn:
cursor = conn.cursor()
cursor.execute(
f"""
INSERT INTO email (
user_id, smtp_host, smtp_port, smtp_user, created_at
) VALUES ({self.ph}, {self.ph}, {self.ph}, {self.ph}, {self.ph})
""",
(user_id, smtp_host, smtp_port, smtp_user, created_at)
)
if hasattr(conn, "commit"):
conn.commit()
def get_email_credentials(self, user_id):
with get_connection() as conn:
cursor = conn.cursor()
cursor.execute(f"SELECT * FROM email WHERE user_id = {self.ph}", (user_id,))
row = cursor.fetchone()
return self.email_to_dict(row) if row else None
def update_email_credentials(self, user_id, smtp_host, smtp_port, smtp_user):
with get_connection() as conn:
cursor = conn.cursor()
cursor.execute(
f"""
UPDATE email SET smtp_host={self.ph}, smtp_port={self.ph}, smtp_user={self.ph} WHERE id = {self.ph}
""",
(smtp_host, smtp_port, smtp_user, user_id)
)
if hasattr(conn, "commit"):
conn.commit()