Coverage for api/database/verification.py: 12%

58 statements  

« prev     ^ index     » next       coverage.py v7.6.2, created at 2024-10-10 03:02 +0300

1from bson import ObjectId 

2from logging_config import logger 

3from mongodb import drivers_col, trailers_col, trucks_col 

4from sotrans_models.models.misc.verification import VerificationStatus 

5from sotrans_models.models.organizations import ( 

6 InnVerificationStatus, 

7 OrganizationDBModel, 

8) 

9from sotrans_models.models.resources.drivers import DriverStatus 

10from sotrans_models.models.resources.trailers import TrailerDBModel 

11from sotrans_models.models.resources.trucks import ( 

12 BodySettingsModel, 

13 TruckDBModel, 

14 TruckStatus, 

15 TruckType, 

16) 

17from utils.check_carriers_resources import ( 

18 is_body_appropriate, 

19 is_resource_verified, 

20) 

21 

22 

23async def has_combination(org_id: ObjectId) -> bool: 

24 drivers = await drivers_col.find_batch({"organization_id": org_id}) 

25 if not drivers: 

26 return False 

27 for driver in drivers: 

28 if is_resource_verified(driver): 

29 break 

30 else: 

31 return False 

32 trucks = await trucks_col.find_batch({"organization_id": org_id}) 

33 if not trucks: 

34 return False 

35 for truck in trucks: 

36 if truck["truck_type"] == TruckType.van_truck and is_resource_verified( 

37 truck 

38 ): 

39 return True 

40 for truck in trucks: 

41 if truck[ 

42 "truck_type" 

43 ] == TruckType.tractor_unit and is_resource_verified(truck): 

44 break 

45 else: 

46 return False 

47 trailers = await trailers_col.find_batch({"organization_id": org_id}) 

48 if not trailers: 

49 return False 

50 for trailer in trailers: 

51 if is_resource_verified(trailer): 

52 return True 

53 return False 

54 

55 

56async def check_full_verification(organization: OrganizationDBModel): 

57 return ( 

58 organization.inn_verification_status == InnVerificationStatus.success 

59 and organization.verification 

60 and organization.verification.status == VerificationStatus.accepted 

61 and await has_combination(organization.id) 

62 ) 

63 

64 

65async def can_apply_order( 

66 org_id: ObjectId, body_settings: BodySettingsModel 

67) -> bool: 

68 drivers = await drivers_col.find_batch({"organization_id": org_id}) 

69 for driver in drivers: 

70 if ( 

71 is_resource_verified(driver) 

72 and driver["status"] == DriverStatus.ready 

73 ): 

74 break 

75 else: 

76 return False 

77 trucks = await trucks_col.find_batch({"organization_id": org_id}) 

78 for truck in trucks: 

79 truck_model = TruckDBModel(**truck) 

80 if ( 

81 truck_model.truck_type == TruckType.van_truck 

82 and is_resource_verified(truck) 

83 and truck_model.status == TruckStatus.ready 

84 and is_body_appropriate(truck_model.body, body_settings) 

85 ): 

86 return True 

87 for truck in trucks: 

88 truck_model = TruckDBModel(**truck) 

89 if ( 

90 truck_model.truck_type == TruckType.tractor_unit 

91 and is_resource_verified(truck) 

92 and truck_model.status == TruckStatus.ready 

93 ): 

94 break 

95 else: 

96 return False 

97 trailers = await trailers_col.find_batch({"organization_id": org_id}) 

98 for trailer in trailers: 

99 trailer_model = TrailerDBModel(**trailer) 

100 if ( 

101 is_resource_verified(trailer) 

102 and trailer_model.status == TruckStatus.ready 

103 and is_body_appropriate(trailer_model.body, body_settings) 

104 ): 

105 return True 

106 return False