|
|
@@ -34,6 +34,7 @@ ESQUEMA DE BASE DE DATOS SQLITE (data.db)
|
|
|
- pin_hash TEXT NOT NULL (encriptado)
|
|
|
- kleincoins TEXT NOT NULL (encriptado, valor por defecto "0")
|
|
|
- created_at TEXT NOT NULL (fecha de creación en formato ISO 8601)
|
|
|
+- reward_progress INTEGER DEFAULT 0 NOT NULL CHECK (reward_progress >= 0 AND reward_progress <= 100)
|
|
|
(Guarda la información del usuario con su pin hasheado y kleincoins encriptadas)
|
|
|
|
|
|
2. Tabla: products
|
|
|
@@ -135,10 +136,10 @@ class UserDataService(BaseDataService):
|
|
|
)
|
|
|
conn.commit()
|
|
|
|
|
|
- user_id = cursor.fetchone()[0]
|
|
|
+ user_id = cursor.fetchone()
|
|
|
if user_id:
|
|
|
- logger.info(f"User added with ID: {user_id}")
|
|
|
- return user_id
|
|
|
+ logger.info(f"User added with ID: {user_id[0]}")
|
|
|
+ return user_id[0]
|
|
|
else:
|
|
|
logger.error("Failed to add user.")
|
|
|
return -1
|
|
|
@@ -164,7 +165,9 @@ class UserDataService(BaseDataService):
|
|
|
rut=user[3],
|
|
|
pin_hash=fernet.decrypt(user[4].encode()).decode(),
|
|
|
kleincoins=fernet.decrypt(user[5].encode()).decode(),
|
|
|
- created_at=user[6]
|
|
|
+ created_at=user[6],
|
|
|
+ permissions=user[7],
|
|
|
+ reward_progress=user[8]
|
|
|
) for user in users
|
|
|
]
|
|
|
|
|
|
@@ -183,7 +186,9 @@ class UserDataService(BaseDataService):
|
|
|
rut=user[3],
|
|
|
pin_hash=fernet.decrypt(user[4].encode()).decode(),
|
|
|
kleincoins=fernet.decrypt(user[5].encode()).decode(),
|
|
|
- created_at=user[6]
|
|
|
+ created_at=user[6],
|
|
|
+ permissions=user[7],
|
|
|
+ reward_progress=user[8]
|
|
|
)
|
|
|
return None
|
|
|
|
|
|
@@ -203,7 +208,9 @@ class UserDataService(BaseDataService):
|
|
|
rut=user[3],
|
|
|
pin_hash=user[4],
|
|
|
kleincoins=fernet.decrypt(user[5].encode()).decode(),
|
|
|
- created_at=user[6]
|
|
|
+ created_at=user[6],
|
|
|
+ permissions=user[7],
|
|
|
+ reward_progress=user[8]
|
|
|
)
|
|
|
return None
|
|
|
|
|
|
@@ -226,12 +233,30 @@ class UserDataService(BaseDataService):
|
|
|
if not result:
|
|
|
logger.error(f"User with ID {user_id} not found.")
|
|
|
return 0
|
|
|
+ logger.info(f"userID: {user_id}, permissions: {result[0]}")
|
|
|
result = result[0]
|
|
|
conn.close()
|
|
|
if result:
|
|
|
return result[0]
|
|
|
return 0
|
|
|
|
|
|
+ def set_reward_progress(self, user_id: int, progress: int) -> bool:
|
|
|
+ """Add progress to user's reward"""
|
|
|
+ conn = self._get_connection()
|
|
|
+ cursor = conn.cursor()
|
|
|
+ try:
|
|
|
+ cursor.execute("UPDATE users SET reward_progress = %s WHERE id = %s", (progress, user_id))
|
|
|
+ conn.commit()
|
|
|
+ success = cursor.rowcount > 0
|
|
|
+ if success:
|
|
|
+ logger.info(f"Reward progress updated for user {user_id}: {progress}")
|
|
|
+ return success
|
|
|
+ except psycopg2.IntegrityError as e:
|
|
|
+ logger.error(f"Failed to update reward progress: {e}")
|
|
|
+ return False
|
|
|
+ finally:
|
|
|
+ conn.close()
|
|
|
+
|
|
|
def get_by_rut(self, rut: str) -> Optional[User]:
|
|
|
"""Get user by RUT"""
|
|
|
conn = self._get_connection()
|
|
|
@@ -252,7 +277,9 @@ class UserDataService(BaseDataService):
|
|
|
rut=user[3],
|
|
|
pin_hash=fernet.decrypt(user[4].encode()).decode(),
|
|
|
kleincoins=fernet.decrypt(user[5].encode()).decode(),
|
|
|
- created_at=user[6]
|
|
|
+ created_at=user[6],
|
|
|
+ permissions=user[7],
|
|
|
+ reward_progress=user[8]
|
|
|
)
|
|
|
return None
|
|
|
#endregion
|
|
|
@@ -356,10 +383,10 @@ class BlacklistDataService(BaseDataService):
|
|
|
try:
|
|
|
cursor.execute("INSERT INTO blacklist (user_id) VALUES (%s) RETURNING id", (user_id,))
|
|
|
conn.commit()
|
|
|
- blacklist_id = cursor.fetchone()[0]
|
|
|
+ blacklist_id = cursor.fetchone()
|
|
|
if blacklist_id:
|
|
|
- logger.info(f"User with ID {user_id} added to blacklist.")
|
|
|
- return blacklist_id
|
|
|
+ logger.info(f"User with ID {blacklist_id[0]} added to blacklist.")
|
|
|
+ return blacklist_id[0]
|
|
|
else:
|
|
|
logger.error(f"Failed to add user with ID {user_id} to blacklist.")
|
|
|
return -1
|
|
|
@@ -487,10 +514,10 @@ class ProductDataService(BaseDataService):
|
|
|
(id, name, type, description, price, image, status)
|
|
|
)
|
|
|
conn.commit()
|
|
|
- product_id = cursor.fetchone()[0]
|
|
|
+ product_id = cursor.fetchone()
|
|
|
if product_id:
|
|
|
- logger.info(f"Product added with ID: {product_id}")
|
|
|
- return product_id
|
|
|
+ logger.info(f"Product added with ID: {product_id[0]}")
|
|
|
+ return product_id[0]
|
|
|
else:
|
|
|
logger.error("Failed to add product.")
|
|
|
return -1
|
|
|
@@ -1257,6 +1284,8 @@ def initialize_db():
|
|
|
kleincoins TEXT NOT NULL,
|
|
|
created_at TEXT NOT NULL,
|
|
|
permissions INTEGER DEFAULT 0 NOT NULL CHECK (permissions IN (0, 1, 2)) -- 0: Usuario normal, 1: Administrador, 2: Superusuario
|
|
|
+
|
|
|
+ reward_progress INTEGER DEFAULT 0 NOT NULL CHECK (reward_progress >= 0 AND reward_progress <= 100) -- Progreso de recompensas
|
|
|
);
|
|
|
""")
|
|
|
|