Coverage for api/database/integration_1c/entity_checker.py: 18%
38 statements
« prev ^ index » next coverage.py v7.6.2, created at 2024-10-10 03:02 +0300
« prev ^ index » next coverage.py v7.6.2, created at 2024-10-10 03:02 +0300
1from contextlib import suppress
2from typing import Any
4from mongodb import drivers_col, orgs_col, trailers_col, updates1c_col
5from sotrans_models.models.misc.document import (
6 DriverDocumentType,
7 TrailerDocumentType,
8 TruckDocumentType,
9)
10from sotrans_models.models.organizations import (
11 InnVerificationStatus,
12 OrganizationDBModel,
13)
14from sotrans_models.models.resources.drivers import DriverDBModel
15from sotrans_models.models.resources.trailers import TrailerDBModel
16from sotrans_models.models.resources.trucks import TruckDBModel
17from utils.check_carriers_resources import is_resource_model_verified
20async def check_resource_movable_to_updates(
21 entity: dict[str, Any],
22 collection_name: str,
23 current_doc: TrailerDocumentType
24 | TruckDocumentType
25 | DriverDocumentType
26 | None = None,
27):
28 if collection_name == trailers_col.collection_name:
29 required_doc_types = [
30 TrailerDocumentType.lease_contract,
31 TrailerDocumentType.sts,
32 ]
33 entity_type = TrailerDBModel
34 elif collection_name == drivers_col.collection_name:
35 required_doc_types = [
36 DriverDocumentType.drivers_license,
37 DriverDocumentType.passport,
38 ]
39 entity_type = DriverDBModel
40 else: # trucks
41 required_doc_types = [
42 TruckDocumentType.lease_contract,
43 TruckDocumentType.sts,
44 ]
45 entity_type = TruckDBModel
46 em = entity_type(**entity)
47 if em.documents:
48 for doc in em.documents:
49 with suppress(ValueError):
50 required_doc_types.remove(doc.type)
51 if not required_doc_types:
52 if await updates1c_col.collection.find_one(
53 {"collection": collection_name, "id": em.id}
54 ):
55 return False
56 else:
57 break
58 with suppress(ValueError):
59 required_doc_types.remove(current_doc)
60 if required_doc_types:
61 return False
63 org = await orgs_col.find_single(
64 "_id", entity.get(TrailerDBModel.organization_id)
65 )
66 if org is None:
67 return False
68 org_model = OrganizationDBModel(**org)
69 if not (
70 org_model.inn_verification_status
71 and org_model.inn_verification_status == InnVerificationStatus.success
72 and is_resource_model_verified(org_model)
73 ):
74 return False
75 return True