worker-service 0.2.0

Worker Service - A worker administration microservice that interoperates with the worker-matcher crate
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
//! Match scoring calculations
//!
//! This module combines individual matching algorithm scores into
//! overall match scores using configurable weights.

use crate::models::Worker;
use crate::config::MatchingConfig;
use super::{MatchResult, MatchScoreBreakdown};
use super::algorithms::{
    name_matching, dob_matching, gender_matching,
    address_matching, identifier_matching,
    tax_id_matching, document_matching,
};

/// Probabilistic scoring strategy
pub struct ProbabilisticScorer {
    /// Configuration for matching thresholds and weights
    config: MatchingConfig,
}

impl ProbabilisticScorer {
    /// Create a new probabilistic scorer with configuration
    pub fn new(config: MatchingConfig) -> Self {
        Self { config }
    }

    /// Calculate match score between two workers
    pub fn calculate_score(
        &self,
        worker: &Worker,
        candidate: &Worker,
    ) -> MatchResult {
        // Calculate individual component scores
        let name_score = name_matching::match_names(&worker.name, &candidate.name);

        let birth_date_score = dob_matching::match_birth_dates(
            worker.birth_date,
            candidate.birth_date,
        );

        let gender_score = gender_matching::match_gender(
            worker.gender,
            candidate.gender,
        );

        let address_score = address_matching::match_addresses(
            &worker.addresses,
            &candidate.addresses,
        );

        let identifier_score = identifier_matching::match_identifiers(
            &worker.identifiers,
            &candidate.identifiers,
        );

        let tax_id_score = tax_id_matching::match_tax_ids(worker, candidate);

        let document_score = document_matching::match_documents(
            &worker.documents,
            &candidate.documents,
        );

        // Tax ID exact match is a strong deterministic signal — short-circuit
        if tax_id_score >= 1.0 {
            return MatchResult {
                worker: candidate.clone(),
                score: 1.0,
                breakdown: MatchScoreBreakdown {
                    name_score, birth_date_score, gender_score,
                    address_score, identifier_score, tax_id_score, document_score,
                },
            };
        }

        // Document number exact match is also a strong signal — short-circuit
        if document_score >= 1.0 {
            return MatchResult {
                worker: candidate.clone(),
                score: 0.98,
                breakdown: MatchScoreBreakdown {
                    name_score, birth_date_score, gender_score,
                    address_score, identifier_score, tax_id_score, document_score,
                },
            };
        }

        // Weight factors for each component (probabilistic)
        const NAME_WEIGHT: f64 = 0.30;
        const DOB_WEIGHT: f64 = 0.25;
        const GENDER_WEIGHT: f64 = 0.10;
        const ADDRESS_WEIGHT: f64 = 0.10;
        const IDENTIFIER_WEIGHT: f64 = 0.10;
        const TAX_ID_WEIGHT: f64 = 0.10;
        const DOCUMENT_WEIGHT: f64 = 0.05;

        // Calculate weighted total score
        let total_score = (name_score * NAME_WEIGHT)
            + (birth_date_score * DOB_WEIGHT)
            + (gender_score * GENDER_WEIGHT)
            + (address_score * ADDRESS_WEIGHT)
            + (identifier_score * IDENTIFIER_WEIGHT)
            + (tax_id_score * TAX_ID_WEIGHT)
            + (document_score * DOCUMENT_WEIGHT);

        let breakdown = MatchScoreBreakdown {
            name_score,
            birth_date_score,
            gender_score,
            address_score,
            identifier_score,
            tax_id_score,
            document_score,
        };

        MatchResult {
            worker: candidate.clone(),
            score: total_score,
            breakdown,
        }
    }

    /// Check if a match score meets the threshold
    pub fn is_match(&self, score: f64) -> bool {
        score >= self.config.threshold_score
    }

    /// Classify match quality
    pub fn classify_match(&self, score: f64) -> MatchQuality {
        if score >= 0.95 {
            MatchQuality::Definite
        } else if score >= self.config.threshold_score {
            MatchQuality::Probable
        } else if score >= 0.50 {
            MatchQuality::Possible
        } else {
            MatchQuality::Unlikely
        }
    }
}

/// Deterministic scoring strategy
pub struct DeterministicScorer {
    /// Configuration for matching
    _config: MatchingConfig,
}

impl DeterministicScorer {
    /// Create a new deterministic scorer
    pub fn new(config: MatchingConfig) -> Self {
        Self { _config: config }
    }

    /// Calculate match score using strict rules
    pub fn calculate_score(
        &self,
        worker: &Worker,
        candidate: &Worker,
    ) -> MatchResult {
        let mut total_score = 0.0;
        let mut points_available = 0.0;

        // Rule 0: Tax ID exact match = definite match
        let tax_id_score = tax_id_matching::match_tax_ids(worker, candidate);
        if tax_id_score >= 1.0 {
            return MatchResult {
                worker: candidate.clone(),
                score: 1.0,
                breakdown: MatchScoreBreakdown {
                    name_score: 0.0, birth_date_score: 0.0, gender_score: 0.0,
                    address_score: 0.0, identifier_score: 0.0,
                    tax_id_score, document_score: 0.0,
                },
            };
        }

        // Rule 1: Exact identifier match = definite match
        let identifier_score = identifier_matching::match_identifiers(
            &worker.identifiers,
            &candidate.identifiers,
        );

        if identifier_score >= 0.98 {
            return MatchResult {
                worker: candidate.clone(),
                score: 1.0,
                breakdown: MatchScoreBreakdown {
                    name_score: 0.0, birth_date_score: 0.0, gender_score: 0.0,
                    address_score: 0.0, identifier_score,
                    tax_id_score, document_score: 0.0,
                },
            };
        }

        // Rule 1b: Document number exact match = definite match
        let document_score = document_matching::match_documents(
            &worker.documents,
            &candidate.documents,
        );

        if document_score >= 1.0 {
            return MatchResult {
                worker: candidate.clone(),
                score: 1.0,
                breakdown: MatchScoreBreakdown {
                    name_score: 0.0, birth_date_score: 0.0, gender_score: 0.0,
                    address_score: 0.0, identifier_score,
                    tax_id_score, document_score,
                },
            };
        }

        // Rule 2: Name + DOB + Gender must all match
        let name_score = name_matching::match_names(&worker.name, &candidate.name);
        let dob_score = dob_matching::match_birth_dates(
            worker.birth_date,
            candidate.birth_date,
        );
        let gender_score = gender_matching::match_gender(
            worker.gender,
            candidate.gender,
        );

        points_available += 3.0;

        if name_score >= 0.90 {
            total_score += 1.0;
        }

        if dob_score >= 0.95 {
            total_score += 1.0;
        }

        if gender_score >= 1.0 {
            total_score += 1.0;
        }

        // Rule 3: Address is optional but adds confidence
        let address_score = address_matching::match_addresses(
            &worker.addresses,
            &candidate.addresses,
        );

        if !worker.addresses.is_empty() && !candidate.addresses.is_empty() {
            points_available += 1.0;
            if address_score >= 0.80 {
                total_score += 1.0;
            }
        }

        // Calculate final score as percentage of available points
        let final_score = if points_available > 0.0 {
            total_score / points_available
        } else {
            0.0
        };

        let breakdown = MatchScoreBreakdown {
            name_score,
            birth_date_score: dob_score,
            gender_score,
            address_score,
            identifier_score,
            tax_id_score,
            document_score,
        };

        MatchResult {
            worker: candidate.clone(),
            score: final_score,
            breakdown,
        }
    }

    /// Check if a match score meets deterministic criteria
    pub fn is_match(&self, score: f64) -> bool {
        score >= 0.75 // Require at least 3/4 rules to match
    }
}

/// Match quality classification
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MatchQuality {
    /// Definite match (score >= 0.95)
    Definite,
    /// Probable match (score >= threshold)
    Probable,
    /// Possible match (score >= 0.50)
    Possible,
    /// Unlikely match (score < 0.50)
    Unlikely,
}

impl MatchQuality {
    /// Get string representation
    pub fn as_str(&self) -> &'static str {
        match self {
            MatchQuality::Definite => "definite",
            MatchQuality::Probable => "probable",
            MatchQuality::Possible => "possible",
            MatchQuality::Unlikely => "unlikely",
        }
    }

    /// Check if this quality indicates a match
    pub fn is_match(&self) -> bool {
        matches!(self, MatchQuality::Definite | MatchQuality::Probable)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::models::{HumanName, Gender};
    use chrono::NaiveDate;

    fn create_test_config() -> MatchingConfig {
        MatchingConfig {
            threshold_score: 0.85,
            exact_match_score: 1.0,
            fuzzy_match_score: 0.8,
        }
    }

    fn create_test_worker(name: &str, dob: Option<NaiveDate>) -> Worker {
        Worker {
            id: uuid::Uuid::new_v4(),
            identifiers: vec![],
            active: true,
            name: HumanName {
                use_type: None,
                family: name.to_string(),
                given: vec!["John".to_string()],
                prefix: vec![],
                suffix: vec![],
            },
            additional_names: vec![],
            telecom: vec![],
            gender: Gender::Male,
            worker_type: None,
            birth_date: dob,
            tax_id: None,
            documents: vec![],
            emergency_contacts: vec![],
            deceased: false,
            deceased_datetime: None,
            addresses: vec![],
            marital_status: None,
            multiple_birth: None,
            photo: vec![],
            managing_organization: None,
            links: vec![],
            created_at: chrono::Utc::now(),
            updated_at: chrono::Utc::now(),
        }
    }

    #[test]
    fn test_exact_match_scores_high() {
        let config = create_test_config();
        let scorer = ProbabilisticScorer::new(config);

        let dob = NaiveDate::from_ymd_opt(1980, 1, 15);
        let worker1 = create_test_worker("Smith", dob);
        let worker2 = create_test_worker("Smith", dob);

        let result = scorer.calculate_score(&worker1, &worker2);

        // With NAME (0.30) + DOB (0.25) + GENDER (0.10) = 0.65
        // No address, identifiers, tax_id, or documents, so those contribute 0
        assert!(result.score >= 0.60, "Exact match on name/dob/gender should score >= 0.60, got {}", result.score);
        assert!(!scorer.is_match(result.score)); // 0.65 < threshold of 0.85
        assert_eq!(scorer.classify_match(result.score), MatchQuality::Possible);
    }

    #[test]
    fn test_fuzzy_match_scores_moderate() {
        let config = create_test_config();
        let scorer = ProbabilisticScorer::new(config);

        let dob1 = NaiveDate::from_ymd_opt(1980, 1, 15);
        let dob2 = NaiveDate::from_ymd_opt(1980, 1, 16); // One day off

        let worker1 = create_test_worker("Smith", dob1);
        let worker2 = create_test_worker("Smyth", dob2); // Spelling variant

        let result = scorer.calculate_score(&worker1, &worker2);

        assert!(result.score > 0.60, "Fuzzy match should score > 0.60, got {}", result.score);
        assert!(result.score < 0.80);
    }

    #[test]
    fn test_no_match_scores_low() {
        let config = create_test_config();
        let scorer = ProbabilisticScorer::new(config);

        let dob1 = NaiveDate::from_ymd_opt(1980, 1, 15);
        let dob2 = NaiveDate::from_ymd_opt(1990, 6, 20);

        let worker1 = create_test_worker("Smith", dob1);
        let worker2 = create_test_worker("Johnson", dob2);

        let result = scorer.calculate_score(&worker1, &worker2);

        assert!(result.score < 0.50, "Non-match should score < 0.50, got {}", result.score);
        assert!(!scorer.is_match(result.score));
    }

    #[test]
    fn test_deterministic_exact_match() {
        let config = create_test_config();
        let scorer = DeterministicScorer::new(config);

        let dob = NaiveDate::from_ymd_opt(1980, 1, 15);
        let worker1 = create_test_worker("Smith", dob);
        let worker2 = create_test_worker("Smith", dob);

        let result = scorer.calculate_score(&worker1, &worker2);

        assert!(result.score >= 0.75, "Exact match should meet deterministic threshold");
        assert!(scorer.is_match(result.score));
    }

    #[test]
    fn test_match_quality_classification() {
        assert_eq!(ProbabilisticScorer::new(create_test_config())
            .classify_match(0.98), MatchQuality::Definite);

        assert_eq!(ProbabilisticScorer::new(create_test_config())
            .classify_match(0.87), MatchQuality::Probable);

        assert_eq!(ProbabilisticScorer::new(create_test_config())
            .classify_match(0.60), MatchQuality::Possible);

        assert_eq!(ProbabilisticScorer::new(create_test_config())
            .classify_match(0.30), MatchQuality::Unlikely);
    }

    #[test]
    fn test_probabilistic_all_fields_match() {
        let config = create_test_config();
        let scorer = ProbabilisticScorer::new(config);

        let dob = NaiveDate::from_ymd_opt(1980, 1, 15);
        let mut worker1 = create_test_worker("Smith", dob);
        let mut worker2 = create_test_worker("Smith", dob);

        // Add matching addresses
        let addr = crate::models::Address {
            use_type: None,
            line1: Some("123 Main St".into()),
            line2: None,
            city: Some("Springfield".into()),
            state: Some("IL".into()),
            postal_code: Some("62701".into()),
            country: None,
        };
        worker1.addresses = vec![addr.clone()];
        worker2.addresses = vec![addr];

        // Add matching identifiers
        let id = crate::models::Identifier::mrn("hospital-a".into(), "MRN-001".into());
        worker1.identifiers = vec![id.clone()];
        worker2.identifiers = vec![id];

        let result = scorer.calculate_score(&worker1, &worker2);
        assert!(result.score > 0.80, "All fields matching should score very high, got {}", result.score);
    }

    #[test]
    fn test_probabilistic_no_fields_match() {
        let config = create_test_config();
        let scorer = ProbabilisticScorer::new(config);

        let mut worker1 = create_test_worker("Smith", NaiveDate::from_ymd_opt(1980, 1, 15));
        worker1.gender = Gender::Male;
        let mut worker2 = create_test_worker("Johnson", NaiveDate::from_ymd_opt(1995, 8, 22));
        worker2.gender = Gender::Female;

        let result = scorer.calculate_score(&worker1, &worker2);
        assert!(result.score < 0.30, "No matching fields should score very low, got {}", result.score);
        assert!(!scorer.is_match(result.score));
    }

    #[test]
    fn test_probabilistic_partial_match() {
        let config = create_test_config();
        let scorer = ProbabilisticScorer::new(config);

        // Same name but different DOB
        let worker1 = create_test_worker("Smith", NaiveDate::from_ymd_opt(1980, 1, 15));
        let worker2 = create_test_worker("Smith", NaiveDate::from_ymd_opt(1990, 6, 20));

        let result = scorer.calculate_score(&worker1, &worker2);
        assert!(result.score > 0.30, "Name match alone should contribute some score, got {}", result.score);
        assert!(result.score < 0.80, "Only name match should not score too high, got {}", result.score);
    }

    #[test]
    fn test_deterministic_tax_id_match_short_circuits() {
        let config = create_test_config();
        let scorer = DeterministicScorer::new(config);

        let mut worker1 = create_test_worker("Smith", NaiveDate::from_ymd_opt(1980, 1, 15));
        worker1.tax_id = Some("123-45-6789".into());
        let mut worker2 = create_test_worker("Jones", NaiveDate::from_ymd_opt(1995, 12, 1));
        worker2.tax_id = Some("123-45-6789".into());

        let result = scorer.calculate_score(&worker1, &worker2);
        assert_eq!(result.score, 1.0, "Tax ID match should short-circuit to 1.0");
        assert_eq!(result.breakdown.tax_id_score, 1.0);
    }

    #[test]
    fn test_deterministic_identifier_match() {
        let config = create_test_config();
        let scorer = DeterministicScorer::new(config);

        let id = crate::models::Identifier::ssn("123-45-6789".into());
        let mut worker1 = create_test_worker("Smith", NaiveDate::from_ymd_opt(1980, 1, 15));
        worker1.identifiers = vec![id.clone()];
        let mut worker2 = create_test_worker("Jones", NaiveDate::from_ymd_opt(1995, 12, 1));
        worker2.identifiers = vec![id];

        let result = scorer.calculate_score(&worker1, &worker2);
        assert_eq!(result.score, 1.0, "Exact identifier match should short-circuit to 1.0");
    }

    #[test]
    fn test_score_boundary_0_95() {
        let scorer = ProbabilisticScorer::new(create_test_config());
        assert_eq!(scorer.classify_match(0.95), MatchQuality::Definite);
        assert_eq!(scorer.classify_match(0.949), MatchQuality::Probable);
    }

    #[test]
    fn test_score_boundary_0_70() {
        let config = MatchingConfig {
            threshold_score: 0.70,
            exact_match_score: 1.0,
            fuzzy_match_score: 0.8,
        };
        let scorer = ProbabilisticScorer::new(config);
        assert!(scorer.is_match(0.70), "Score at threshold should be a match");
        assert!(!scorer.is_match(0.69), "Score below threshold should not be a match");
        assert_eq!(scorer.classify_match(0.70), MatchQuality::Probable);
    }
}