Coverage for api/database/integration_1c/savers.py: 41%
29 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
1import bson
2from database.integration_1c.entity_checker import (
3 check_resource_movable_to_updates,
4)
5from dbcc import MongoTableEngine
6from mongodb import db, drivers_col, trailers_col, trucks_col, updates1c_col
7from pymongo.results import UpdateResult
8from sotrans_models.models.orders.order import OrderDBModel
9from sotrans_models.models.organizations import OrganizationDBModel
10from sotrans_models.models.resources.drivers import DriverDBModel
11from sotrans_models.models.resources.trailers import TrailerDBModel
12from sotrans_models.models.resources.trucks import TruckDBModel
13from sotrans_models.models.updates1c import UpdateStatus
16async def save_entity_to_updates(collection_name: str, entity: dict):
17 entity.pop("_id", None)
18 document = {
19 "status_1c": "pending",
20 "reason_1c": None,
21 "code_1c": None,
22 "collection": collection_name,
23 **entity,
24 }
25 await updates1c_col.create(document)
28async def update_in_1c(
29 entity_id: bson.ObjectId,
30 collection_name: str,
31 update: DriverDBModel
32 | TrailerDBModel
33 | TruckDBModel
34 | OrganizationDBModel
35 | OrderDBModel,
36) -> bool:
37 result: UpdateResult = await updates1c_col.collection.update_one(
38 {
39 "status_1c": UpdateStatus.pending.value,
40 "id": entity_id,
41 "collection": collection_name,
42 },
43 {"$set": update.model_dump()},
44 )
45 return bool(result.modified_count)
48async def remove_from_1c(entity_id: bson.ObjectId, collection_name: str):
49 status_update_for_code: UpdateResult = (
50 await updates1c_col.collection.update_one(
51 {
52 "collection": collection_name,
53 "id": entity_id,
54 "code_1c": {"$ne": None, "$exists": True},
55 },
56 {"$set": {"status": UpdateStatus.deleted.value}},
57 )
58 )
59 if status_update_for_code.modified_count:
60 return
61 await updates1c_col.collection.delete_one(
62 {
63 "status": UpdateStatus.pending.value,
64 "collection": collection_name,
65 "id": entity_id,
66 }
67 )
70async def save_resource_updates_1c_for_organization(
71 organization_id: bson.ObjectId,
72):
73 for col in (drivers_col, trailers_col, trucks_col):
74 entities = await col.find_batch({"organization_id": organization_id})
75 for entity in entities:
76 if await check_resource_movable_to_updates(
77 entity, col.collection_name
78 ):
79 await save_entity_to_updates(col.collection_name, entity)