sql mongo updated
This commit is contained in:
@@ -1,5 +1,3 @@
|
||||
import datetime
|
||||
|
||||
from databases.no_sql_models.validations import (
|
||||
PasswordHistoryViaUser,
|
||||
DomainViaUser,
|
||||
@@ -9,19 +7,6 @@ from databases.no_sql_models.mongo_database import MongoQuery
|
||||
from api_library.date_time_actions.date_functions import system_arrow
|
||||
|
||||
|
||||
def validate_timestamp(doc):
|
||||
"""Validate and fix timestamp fields in MongoDB documents"""
|
||||
if not doc:
|
||||
return doc
|
||||
|
||||
timestamp_fields = ["modified_at", "created_at", "accessed_at", "timestamp"]
|
||||
for field in timestamp_fields:
|
||||
if field in doc and not isinstance(doc[field], (int, float)):
|
||||
# Convert to proper timestamp if it's not already
|
||||
doc[field] = system_arrow.to_timestamp(doc[field])
|
||||
return doc
|
||||
|
||||
|
||||
class MongoQueryIdentity:
|
||||
"""
|
||||
4ex. mongo_collection_name = str(Company.uu_id()) + "*" + str('UserPasswordHistory')
|
||||
@@ -30,12 +15,9 @@ class MongoQueryIdentity:
|
||||
def __init__(self, company_uuid, storage_reasoning: str = None):
|
||||
self.company_uuid = company_uuid
|
||||
self.mongo_collection_base = str(company_uuid)
|
||||
if storage_reasoning:
|
||||
self.mongo_collection_name = (
|
||||
str(company_uuid) + "*" + str(storage_reasoning)
|
||||
)
|
||||
else:
|
||||
self.mongo_collection_name = str(company_uuid) + "*" + str("Domain")
|
||||
self.mongo_collection_name = (
|
||||
str(company_uuid) + "*" + str(storage_reasoning or "Domain")
|
||||
)
|
||||
self.mongo_engine = MongoQuery(
|
||||
table_name=self.mongo_collection_name, database_name="mongo_database"
|
||||
)
|
||||
@@ -49,7 +31,6 @@ class MongoQueryIdentity:
|
||||
)
|
||||
|
||||
def create_domain_via_user(self, payload: DomainViaUser):
|
||||
self.use_collection("Domain")
|
||||
return self.mongo_engine.find_or_insert(
|
||||
field="user_uu_id",
|
||||
payload={
|
||||
@@ -61,7 +42,6 @@ class MongoQueryIdentity:
|
||||
)
|
||||
|
||||
def update_domain_via_user(self, payload: DomainViaUser):
|
||||
self.use_collection("Domain")
|
||||
return self.mongo_engine.update(
|
||||
match=payload.user_uu_id,
|
||||
payload={
|
||||
@@ -72,57 +52,25 @@ class MongoQueryIdentity:
|
||||
)
|
||||
|
||||
def get_domain_via_user(self, user_uu_id):
|
||||
self.use_collection("Domain")
|
||||
result = self.mongo_engine.filter_by(payload={"user_uu_id": user_uu_id})
|
||||
return [validate_timestamp(doc) for doc in result.data] if result else None
|
||||
return [self.validate_timestamp(doc) for doc in result.data] if result else None
|
||||
|
||||
def refresh_password_history_via_user(self, payload: PasswordHistoryViaUser):
|
||||
self.use_collection("PasswordHistory")
|
||||
password_history_item = self.mongo_engine.get_one(
|
||||
match=payload.user_uu_id, field="user_uu_id"
|
||||
)
|
||||
if not password_history_item:
|
||||
self.mongo_engine.insert(
|
||||
payload={
|
||||
"user_uu_id": str(payload.user_uu_id),
|
||||
"password_history": [],
|
||||
}
|
||||
)
|
||||
password_history_item = self.mongo_engine.get_one(
|
||||
match=payload.user_uu_id, field="user_uu_id"
|
||||
)
|
||||
password_history_list = password_history_item.get("password_history", [])
|
||||
hashed_password = payload.password_add.get("password")
|
||||
for password_in_history in password_history_list:
|
||||
if str(password_in_history.get("password")) == str(hashed_password):
|
||||
raise Exception(
|
||||
dict(
|
||||
status_code=400,
|
||||
detail="Password already used. Please enter a new password that you have not used last 3 times.",
|
||||
)
|
||||
)
|
||||
|
||||
if len(password_history_list) > 3:
|
||||
password_history_list.pop(0)
|
||||
|
||||
password_history_list.append(payload.password_add)
|
||||
return self.mongo_engine.update(
|
||||
field="user_uu_id",
|
||||
match=payload.user_uu_id,
|
||||
payload={
|
||||
"password_history": password_history_list,
|
||||
"password_history": payload.password_history,
|
||||
"access_history_detail": payload.access_history_detail,
|
||||
"modified_at": system_arrow.to_timestamp(system_arrow.now()),
|
||||
},
|
||||
field="user_uu_id",
|
||||
)
|
||||
|
||||
def get_password_history_via_user(self, user_uu_id):
|
||||
self.use_collection("UserPasswordHistory")
|
||||
result = self.mongo_engine.filter_by(payload={"user_uu_id": user_uu_id})
|
||||
return [validate_timestamp(doc) for doc in result.data] if result else None
|
||||
return [self.validate_timestamp(doc) for doc in result.data] if result else None
|
||||
|
||||
def update_access_history_via_user(self, payload: AccessHistoryViaUser):
|
||||
self.use_collection("AccessHistory")
|
||||
if already_dict := self.get_access_history_via_user(
|
||||
user_uu_id=payload.user_uu_id
|
||||
):
|
||||
@@ -148,6 +96,16 @@ class MongoQueryIdentity:
|
||||
)
|
||||
|
||||
def get_access_history_via_user(self, user_uu_id):
|
||||
self.use_collection("AccessHistory")
|
||||
result = self.mongo_engine.filter_by(payload={"user_uu_id": user_uu_id})
|
||||
return [validate_timestamp(doc) for doc in result.data] if result else None
|
||||
return [self.validate_timestamp(doc) for doc in result.data] if result else None
|
||||
|
||||
def validate_timestamp(doc):
|
||||
"""Validate and fix timestamp fields in MongoDB documents"""
|
||||
if not doc:
|
||||
return doc
|
||||
|
||||
timestamp_fields = ["modified_at", "created_at", "accessed_at", "timestamp"]
|
||||
for field in timestamp_fields:
|
||||
if field in doc and not isinstance(doc[field], (int, float)):
|
||||
doc[field] = system_arrow.to_timestamp(doc[field])
|
||||
return doc
|
||||
|
||||
Reference in New Issue
Block a user