intrgrating suggestions after betta 1
This commit is contained in:
81
transportmanager/server/models/company_users.py
Normal file
81
transportmanager/server/models/company_users.py
Normal file
@@ -0,0 +1,81 @@
|
||||
from datetime import datetime
|
||||
from database import get_connection, is_postgres
|
||||
|
||||
class CompanyUsers:
|
||||
def __init__(self):
|
||||
self.ph = "%s" if is_postgres() else "?"
|
||||
|
||||
def access_to_dict(self, row):
|
||||
access = {
|
||||
'id': row[0],
|
||||
'company_user_id': row[1],
|
||||
'clients': row[2],
|
||||
'transporters': row[3],
|
||||
'destinations': row[4],
|
||||
'orders_in': row[5],
|
||||
'orders_out': row[6],
|
||||
'report':row[7],
|
||||
'created_at': row[8]
|
||||
}
|
||||
return access
|
||||
|
||||
def insert_company_user_access(self, access_data):
|
||||
created_at = datetime.now().isoformat()
|
||||
with get_connection() as conn:
|
||||
cursor = conn.cursor()
|
||||
returning = "RETURNING id" if is_postgres() else ""
|
||||
query = f"""
|
||||
INSERT INTO company_user_access (
|
||||
company_user_id, clients, transporters, destinations, orders_in, orders_out, report, created_at
|
||||
) VALUES ({self.ph}, {self.ph}, {self.ph}, {self.ph}, {self.ph}, {self.ph}, {self.ph}, {self.ph}) {returning}
|
||||
"""
|
||||
cursor.execute(
|
||||
query,
|
||||
(
|
||||
access_data['company_user_id'],
|
||||
access_data['clients'],
|
||||
access_data['transporters'],
|
||||
access_data['destinations'],
|
||||
access_data['orders_in'],
|
||||
access_data['orders_out'],
|
||||
access_data['report'],
|
||||
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 get_access_by_company_user_id(self, company_user_id):
|
||||
with get_connection() as conn:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute(f"SELECT * FROM company_user_access WHERE company_user_id = {self.ph}", (company_user_id,))
|
||||
row = cursor.fetchone()
|
||||
return self.access_to_dict(row) if row else None
|
||||
|
||||
def update_company_user_access(self, data):
|
||||
with get_connection() as conn:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute(
|
||||
f"""
|
||||
UPDATE company_user_access
|
||||
SET clients = {self.ph}, transporters = {self.ph}, destinations = {self.ph}, orders_in = {self.ph}, orders_out = {self.ph}, report = {self.ph}
|
||||
WHERE company_user_id = {self.ph}
|
||||
""",
|
||||
(
|
||||
data['clients'],
|
||||
data['transporters'],
|
||||
data['destinations'],
|
||||
data['orders_in'],
|
||||
data['orders_out'],
|
||||
data['report'],
|
||||
data['company_user_id']
|
||||
)
|
||||
)
|
||||
if hasattr(conn, "commit"):
|
||||
conn.commit()
|
||||
@@ -19,6 +19,8 @@ class OrdersIn:
|
||||
"trailer_reg_number": row[8],
|
||||
"received_price": row[9],
|
||||
"created_at": row[10],
|
||||
"file":row[11],
|
||||
"expenses": row[12],
|
||||
}
|
||||
|
||||
def order_point_to_dict(self, row):
|
||||
@@ -41,8 +43,8 @@ class OrdersIn:
|
||||
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}
|
||||
ldb_quantity, kg_quantity, track_reg_number, trailer_reg_number, created_at, file_name, expenses)
|
||||
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"],
|
||||
@@ -55,6 +57,8 @@ class OrdersIn:
|
||||
data["track_reg_number"],
|
||||
data["trailer_reg_number"],
|
||||
created_at,
|
||||
data["file"],
|
||||
data["expenses"]
|
||||
),
|
||||
)
|
||||
new_id = cur.fetchone()[0] if is_postgres() else getattr(cur, "lastrowid", None)
|
||||
@@ -71,7 +75,7 @@ class OrdersIn:
|
||||
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}
|
||||
trailer_reg_number = {self.ph}, file_name = {self.ph}, expenses = {self.ph}
|
||||
WHERE id = {self.ph}
|
||||
""",
|
||||
(
|
||||
@@ -84,6 +88,8 @@ class OrdersIn:
|
||||
data["kg_quantity"],
|
||||
data["track_reg_number"],
|
||||
data["trailer_reg_number"],
|
||||
data['file'],
|
||||
data['expenses'],
|
||||
data["id"],
|
||||
),
|
||||
)
|
||||
|
||||
@@ -22,7 +22,10 @@ class Users:
|
||||
'created_at': row[12],
|
||||
'otp_code': row[13],
|
||||
'otp_expiration': row[14],
|
||||
'user_role': row[15]
|
||||
'user_role': row[15],
|
||||
'company_id': row[16],
|
||||
'active': row[17],
|
||||
'temporary_password': row[18]
|
||||
}
|
||||
return user
|
||||
|
||||
@@ -70,6 +73,28 @@ class Users:
|
||||
if hasattr(conn, "commit"):
|
||||
conn.commit()
|
||||
return inserted_id
|
||||
|
||||
def insert_company_user(self, name, email, password_hash, company_id):
|
||||
created_at = datetime.now().isoformat()
|
||||
company_id = company_id
|
||||
user_role = 'company_user'
|
||||
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, user_role, company_id
|
||||
) VALUES ({self.ph}, {self.ph}, {self.ph}, {self.ph}, {self.ph}, {self.ph}) {returning}
|
||||
"""
|
||||
cursor.execute(query, (name, email, password_hash, created_at, user_role, company_id))
|
||||
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:
|
||||
@@ -190,5 +215,33 @@ class Users:
|
||||
""",
|
||||
(smtp_host, smtp_port, smtp_user, user_id)
|
||||
)
|
||||
if hasattr(conn, "commit"):
|
||||
conn.commit()
|
||||
|
||||
def deactivate_user(self, user_id):
|
||||
with get_connection() as conn:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute(
|
||||
f"""
|
||||
UPDATE users
|
||||
SET active = {self.ph}
|
||||
WHERE id = {self.ph}
|
||||
""",
|
||||
(0, user_id)
|
||||
)
|
||||
if hasattr(conn, "commit"):
|
||||
conn.commit()
|
||||
|
||||
def update_temp_pass(self, user_id):
|
||||
with get_connection() as conn:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute(
|
||||
f"""
|
||||
UPDATE users
|
||||
SET temporary_passwrd = {self.ph}
|
||||
WHERE id = {self.ph}
|
||||
""",
|
||||
(0, user_id)
|
||||
)
|
||||
if hasattr(conn, "commit"):
|
||||
conn.commit()
|
||||
Reference in New Issue
Block a user