4
4
from cg .constants .priority import Priority
5
5
from cg .models .orders .constants import OrderType
6
6
from cg .services .orders .constants import ORDER_TYPE_WORKFLOW_MAP
7
+ from cg .services .orders .validation .models .existing_sample import ExistingSample
7
8
from cg .services .orders .validation .models .order import Order
8
9
from cg .services .orders .validation .models .order_with_cases import OrderWithCases
10
+ from cg .services .orders .validation .models .order_with_samples import OrderWithSamples
11
+ from cg .services .orders .validation .models .sample import Sample as ValidationSample
12
+ from cg .store .models import Application , Sample
13
+ from cg .store .store import Store
9
14
10
- DUE_TIME_BY_PRIORITY : dict [Priority , timedelta . days ] = {
15
+ DUE_TIME_BY_PRIORITY : dict [Priority , timedelta ] = {
11
16
Priority .express : timedelta (days = 7 ),
12
17
Priority .priority : timedelta (days = 14 ),
13
18
Priority .standard : timedelta (days = 21 ),
@@ -21,7 +26,23 @@ def contains_existing_data(order: OrderWithCases) -> bool:
21
26
return any (not case .is_new or case .enumerated_existing_samples for case in order .cases )
22
27
23
28
24
- def get_ticket_tags (order : Order , order_type : OrderType ) -> list [str ]:
29
+ def contains_external_data (order : Order , status_db : Store ) -> bool :
30
+ """Check if any existing or new sample from the given order is external."""
31
+ existing_samples : list [Sample ] = get_existing_samples (order = order , status_db = status_db )
32
+ new_samples : list [ValidationSample ] = get_new_samples (order = order )
33
+
34
+ if any ([sample .is_external for sample in existing_samples ]):
35
+ return True
36
+
37
+ for sample in new_samples :
38
+ application : Application | None = status_db .get_application_by_tag (sample .application )
39
+ if application and application .is_external :
40
+ return True
41
+
42
+ return False
43
+
44
+
45
+ def get_ticket_tags (order : Order , order_type : OrderType , status_db : Store ) -> list [str ]:
25
46
"""Generate ticket tags based on the order and order type"""
26
47
27
48
tags : list [str ] = [ORDER_TYPE_WORKFLOW_MAP [order_type ]]
@@ -30,6 +51,9 @@ def get_ticket_tags(order: Order, order_type: OrderType) -> list[str]:
30
51
if contains_existing_data (order ):
31
52
tags .append ("existing-data" )
32
53
54
+ if contains_external_data (order = order , status_db = status_db ):
55
+ tags .append ("external-data" )
56
+
33
57
return tags
34
58
35
59
@@ -54,3 +78,38 @@ def get_due_by_date(priority: Priority) -> date:
54
78
"""Get the ticket due by date based on the order priority."""
55
79
due_by : datetime = datetime .now () + DUE_TIME_BY_PRIORITY [priority ]
56
80
return due_by .date ()
81
+
82
+
83
+ def get_existing_samples (order : Order , status_db : Store ) -> list [Sample ]:
84
+ existing_samples : list [Sample ] = []
85
+
86
+ if isinstance (order , OrderWithCases ):
87
+ existing_samples .extend (
88
+ [
89
+ sample
90
+ for (_ , case ) in order .enumerated_existing_cases
91
+ for sample in status_db .get_samples_by_case_id (case .internal_id )
92
+ ]
93
+ )
94
+
95
+ existing_samples .extend (
96
+ [
97
+ sample
98
+ for (_ , case ) in order .enumerated_new_cases
99
+ for (_ , existing_sample ) in case .enumerated_existing_samples
100
+ if (sample := status_db .get_sample_by_internal_id (existing_sample .internal_id ))
101
+ ]
102
+ )
103
+
104
+ return existing_samples
105
+
106
+
107
+ def get_new_samples (order : Order ) -> list [ValidationSample ]:
108
+ new_samples : list [ValidationSample ] = []
109
+
110
+ if isinstance (order , OrderWithCases ):
111
+ new_samples .extend ([sample for (_ , _ , sample ) in order .enumerated_new_samples ])
112
+ elif isinstance (order , OrderWithSamples ):
113
+ new_samples .extend (order .samples )
114
+
115
+ return new_samples
0 commit comments