def send_email(operation: str, document_id: str, user_id: str) -> None: document = cast(Dict, mongo.find_document(document_id)) users = mongo.get_users_with_permissions_to_document( document_id, document["company"], user_id) pool = Pool(processes=5) pool.starmap_async(send_single_mail, [(document_id, operation, user) for user in users])
def get_users_with_permissions(document_id: str, user_id: str) -> List: document = mongo.find_document(document_id) if document is None: raise HTTPException("Document not found", 404) users = mongo.get_users_with_permissions_to_document( document["_id"], document["company"], user_id) return users
def update_document_content(document_id: str, user_id: str, content: Any) -> Dict: user = cast(Dict, mongo.find_user_by_id(user_id)) document = mongo.find_document(document_id) if not document or user["company"] != document["company"]: raise HTTPException("You have no permission for that", 403) mongo.update_document(document_id, "content", content) return {}
def get_document(document_id: str, user_identifier: str) -> Dict: if mongo.check_user_permissions(user_identifier, document_id): document_content = mongo.find_document(document_id) if document_content: return { "content": document_content["content"], "id": str(document_content["_id"]), "status": document_content["status"], } raise HTTPException(f"Document with {document_id} not found", 404)
def archive_document(document_id: str, user_id: str) -> Dict: document: Dict = cast(Dict, mongo.find_document(document_id)) user: Dict = cast(Dict, mongo.find_user_by_id(user_id)) if document["company"] != user["company"]: raise HTTPException( "You should be member of company created this document!", 403) if document["status"] != Status.SIGNING.value or len( document["signed"]) < 2: raise HTTPException("Sign document before archive!", 409) mongo.update_document(document_id, "status", Status.ARCHIVE.value) send_email("archive", document_id, user_id) return {}
def create_invite(user_identifier: str, body: Dict) -> Dict: user = mongo.find_user_by_name(body["username"]) if user is None or str(user["_id"]) == user_identifier: raise HTTPException("Invalid identification data!", 403) document = cast(Dict, mongo.find_document(body["document"])) if (document["company"] != cast( Dict, mongo.find_user_by_id(user_identifier))["company"]): raise HTTPException("You should have permissions for this action!", 403) if not mongo.create_invite(user["_id"], body["document"]): raise HTTPException("Document not found!", 404) send_single_mail(str(document["_id"]), "invite to accept", user) return {}
def approve_document(document_id: str, user_id: str) -> Dict: document = mongo.find_document(document_id) user: Dict = cast(Dict, mongo.find_user_by_id(user_id)) if not document: raise HTTPException(f"Document with {document_id} not found", 404) if user["role"] not in [Role.LAWYER.value, Role.ECONOMIST.value]: raise HTTPException("Invalid role for approving document!", 409) if user_id in document["approved"]: raise HTTPException("Document already approved by you!", 409) document["approved"].append(user_id) mongo.update_document(document_id, "approved", document["approved"]) mongo.update_document(document_id, "status", Status.AGREED.value) send_email("approve", document_id, user_id) return {}
def sign_document(document_id: str, user_id: str) -> Dict: document = cast(Dict, mongo.find_document(document_id)) user = cast(Dict, mongo.find_user_by_id(user_id)) if user["role"] != Role.GENERAL_DIRECTOR.value: raise HTTPException("Signing validation failed!", 409) if document["status"] not in [Status.AGREED.value, Status.SIGNING.value]: raise HTTPException("You can't execute such command!", 409) if not mongo.is_approved_by_company(document_id, user["company"]): raise HTTPException("Wait until document approved by your company!", 409) if user_id in document["signed"] or len(document["signed"]) >= 3: raise HTTPException("Already signed!", 409) document["signed"].append(user_id) mongo.update_document(document_id, "signed", document["signed"]) mongo.update_document(document_id, "status", Status.SIGNING.value) send_email("sign", document_id, user_id) return {}
from typing import Dict, Optional, Tuple, cast from http_exception import HTTPException from mongodb_handler import mongo from services.email_sender import send_single_mail def get_invites(user_id: str) -> Dict: if (user_invite := mongo.select_invite(user_id)) is None: return {} else: document_id = str(user_invite["invite_document"]) document = cast(Dict, mongo.find_document(document_id)) return { "document_name": document["document_name"], "document_id": str(document["_id"]), "invite_id": str(user_invite["_id"]), } def create_invite(user_identifier: str, body: Dict) -> Dict: user = mongo.find_user_by_name(body["username"]) if user is None or str(user["_id"]) == user_identifier: raise HTTPException("Invalid identification data!", 403) document = cast(Dict, mongo.find_document(body["document"])) if (document["company"] != cast( Dict, mongo.find_user_by_id(user_identifier))["company"]): raise HTTPException("You should have permissions for this action!", 403)