datasynth-core 2.3.0

Core domain models, traits, and distributions for synthetic enterprise data generation
Documentation
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
//! Evidence models per ISA 500.
//!
//! Audit evidence is all information used by the auditor to arrive at
//! conclusions on which the auditor's opinion is based.

use chrono::{DateTime, NaiveDate, Utc};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use uuid::Uuid;

use super::workpaper::Assertion;

/// Audit evidence representing supporting documentation.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AuditEvidence {
    /// Unique evidence ID
    pub evidence_id: Uuid,
    /// External reference
    pub evidence_ref: String,
    /// Engagement ID
    pub engagement_id: Uuid,
    /// Type of evidence
    pub evidence_type: EvidenceType,
    /// Source of evidence
    pub source_type: EvidenceSource,
    /// Evidence title
    pub title: String,
    /// Description
    pub description: String,

    // === Obtaining Information ===
    /// Date evidence was obtained
    pub obtained_date: NaiveDate,
    /// Who obtained the evidence
    pub obtained_by: String,
    /// File hash for integrity verification
    pub file_hash: Option<String>,
    /// File path or storage location
    pub file_path: Option<String>,
    /// File size in bytes
    pub file_size: Option<u64>,

    // === Reliability Assessment per ISA 500.A31 ===
    /// Reliability assessment
    pub reliability_assessment: ReliabilityAssessment,

    // === Relevance ===
    /// Assertions addressed by this evidence
    pub assertions_addressed: Vec<Assertion>,
    /// Account IDs impacted
    pub accounts_impacted: Vec<String>,
    /// Process areas covered
    pub process_areas: Vec<String>,

    // === Cross-References ===
    /// Linked workpaper IDs
    pub linked_workpapers: Vec<Uuid>,
    /// Related evidence IDs
    pub related_evidence: Vec<Uuid>,

    // === AI Extraction (optional) ===
    /// AI-extracted key terms
    pub ai_extracted_terms: Option<HashMap<String, String>>,
    /// AI extraction confidence
    pub ai_confidence: Option<f64>,
    /// AI summary
    pub ai_summary: Option<String>,

    // === Status ===
    /// Current status of this evidence item
    pub status: EvidenceStatus,

    // === Metadata ===
    #[serde(with = "crate::serde_timestamp::utc")]
    pub created_at: DateTime<Utc>,
    #[serde(with = "crate::serde_timestamp::utc")]
    pub updated_at: DateTime<Utc>,
}

impl AuditEvidence {
    /// Create new audit evidence.
    pub fn new(
        engagement_id: Uuid,
        evidence_type: EvidenceType,
        source_type: EvidenceSource,
        title: &str,
    ) -> Self {
        let now = Utc::now();
        Self {
            evidence_id: Uuid::new_v4(),
            evidence_ref: format!("EV-{}", Uuid::new_v4().simple()),
            engagement_id,
            evidence_type,
            source_type,
            title: title.into(),
            description: String::new(),
            obtained_date: now.date_naive(),
            obtained_by: String::new(),
            file_hash: None,
            file_path: None,
            file_size: None,
            reliability_assessment: ReliabilityAssessment::default(),
            assertions_addressed: Vec::new(),
            accounts_impacted: Vec::new(),
            process_areas: Vec::new(),
            linked_workpapers: Vec::new(),
            related_evidence: Vec::new(),
            ai_extracted_terms: None,
            ai_confidence: None,
            ai_summary: None,
            status: EvidenceStatus::Obtained,
            created_at: now,
            updated_at: now,
        }
    }

    /// Set the description.
    pub fn with_description(mut self, description: &str) -> Self {
        self.description = description.into();
        self
    }

    /// Set who obtained the evidence.
    pub fn with_obtained_by(mut self, obtained_by: &str, date: NaiveDate) -> Self {
        self.obtained_by = obtained_by.into();
        self.obtained_date = date;
        self
    }

    /// Set file information.
    pub fn with_file_info(mut self, path: &str, hash: &str, size: u64) -> Self {
        self.file_path = Some(path.into());
        self.file_hash = Some(hash.into());
        self.file_size = Some(size);
        self
    }

    /// Set reliability assessment.
    pub fn with_reliability(mut self, assessment: ReliabilityAssessment) -> Self {
        self.reliability_assessment = assessment;
        self
    }

    /// Add assertions addressed.
    pub fn with_assertions(mut self, assertions: Vec<Assertion>) -> Self {
        self.assertions_addressed = assertions;
        self
    }

    /// Add AI extraction results.
    pub fn with_ai_extraction(
        mut self,
        terms: HashMap<String, String>,
        confidence: f64,
        summary: &str,
    ) -> Self {
        self.ai_extracted_terms = Some(terms);
        self.ai_confidence = Some(confidence);
        self.ai_summary = Some(summary.into());
        self
    }

    /// Link to a workpaper.
    pub fn link_workpaper(&mut self, workpaper_id: Uuid) {
        if !self.linked_workpapers.contains(&workpaper_id) {
            self.linked_workpapers.push(workpaper_id);
            self.updated_at = Utc::now();
        }
    }

    /// Get the overall reliability level.
    pub fn overall_reliability(&self) -> ReliabilityLevel {
        self.reliability_assessment.overall_reliability
    }

    /// Check if this is high-quality evidence.
    pub fn is_high_quality(&self) -> bool {
        matches!(
            self.reliability_assessment.overall_reliability,
            ReliabilityLevel::High
        ) && matches!(
            self.source_type,
            EvidenceSource::ExternalThirdParty | EvidenceSource::AuditorPrepared
        )
    }
}

/// Type of audit evidence.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
pub enum EvidenceType {
    /// Confirmation from third party
    Confirmation,
    /// Client-prepared document
    #[default]
    Document,
    /// Auditor-prepared analysis
    Analysis,
    /// Screenshot or system extract
    SystemExtract,
    /// Contract or agreement
    Contract,
    /// Bank statement
    BankStatement,
    /// Invoice
    Invoice,
    /// Email correspondence
    Email,
    /// Meeting minutes
    MeetingMinutes,
    /// Management representation
    ManagementRepresentation,
    /// Legal letter
    LegalLetter,
    /// Specialist report
    SpecialistReport,
    /// Physical inventory observation
    PhysicalObservation,
    /// Recalculation spreadsheet
    Recalculation,
}

/// Source of audit evidence per ISA 500.A31.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
pub enum EvidenceSource {
    /// External source, directly from third party
    ExternalThirdParty,
    /// External source, provided by client
    #[default]
    ExternalClientProvided,
    /// Internal source, client prepared
    InternalClientPrepared,
    /// Auditor prepared
    AuditorPrepared,
}

impl EvidenceSource {
    /// Get the inherent reliability of this source type.
    pub fn inherent_reliability(&self) -> ReliabilityLevel {
        match self {
            Self::ExternalThirdParty => ReliabilityLevel::High,
            Self::AuditorPrepared => ReliabilityLevel::High,
            Self::ExternalClientProvided => ReliabilityLevel::Medium,
            Self::InternalClientPrepared => ReliabilityLevel::Low,
        }
    }

    /// Get a description for ISA documentation.
    pub fn description(&self) -> &'static str {
        match self {
            Self::ExternalThirdParty => "Obtained directly from independent external source",
            Self::AuditorPrepared => "Prepared by the auditor",
            Self::ExternalClientProvided => "External evidence provided by client",
            Self::InternalClientPrepared => "Prepared internally by client personnel",
        }
    }
}

/// Status of an evidence item through its lifecycle.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
pub enum EvidenceStatus {
    /// Evidence has been requested but not yet received
    Requested,
    /// Evidence has been obtained
    #[default]
    Obtained,
    /// Evidence is under review
    UnderReview,
    /// Evidence has been accepted
    Accepted,
    /// Evidence has been rejected
    Rejected,
    /// Evidence has been superseded by newer evidence
    Superseded,
}

/// Reliability assessment per ISA 500.A31.
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct ReliabilityAssessment {
    /// Independence of source
    pub independence_of_source: ReliabilityLevel,
    /// Effectiveness of related controls
    pub effectiveness_of_controls: ReliabilityLevel,
    /// Qualifications of information provider
    pub qualifications_of_provider: ReliabilityLevel,
    /// Objectivity of information provider
    pub objectivity_of_provider: ReliabilityLevel,
    /// Overall reliability conclusion
    pub overall_reliability: ReliabilityLevel,
    /// Assessment notes
    pub notes: String,
}

impl ReliabilityAssessment {
    /// Create a new reliability assessment.
    pub fn new(
        independence: ReliabilityLevel,
        controls: ReliabilityLevel,
        qualifications: ReliabilityLevel,
        objectivity: ReliabilityLevel,
        notes: &str,
    ) -> Self {
        let overall = Self::calculate_overall(independence, controls, qualifications, objectivity);
        Self {
            independence_of_source: independence,
            effectiveness_of_controls: controls,
            qualifications_of_provider: qualifications,
            objectivity_of_provider: objectivity,
            overall_reliability: overall,
            notes: notes.into(),
        }
    }

    /// Calculate overall reliability from components.
    fn calculate_overall(
        independence: ReliabilityLevel,
        controls: ReliabilityLevel,
        qualifications: ReliabilityLevel,
        objectivity: ReliabilityLevel,
    ) -> ReliabilityLevel {
        let scores = [
            independence.score(),
            controls.score(),
            qualifications.score(),
            objectivity.score(),
        ];
        let avg = scores.iter().sum::<u8>() / 4;
        ReliabilityLevel::from_score(avg)
    }

    /// Create a high reliability assessment.
    pub fn high(notes: &str) -> Self {
        Self::new(
            ReliabilityLevel::High,
            ReliabilityLevel::High,
            ReliabilityLevel::High,
            ReliabilityLevel::High,
            notes,
        )
    }

    /// Create a medium reliability assessment.
    pub fn medium(notes: &str) -> Self {
        Self::new(
            ReliabilityLevel::Medium,
            ReliabilityLevel::Medium,
            ReliabilityLevel::Medium,
            ReliabilityLevel::Medium,
            notes,
        )
    }

    /// Create a low reliability assessment.
    pub fn low(notes: &str) -> Self {
        Self::new(
            ReliabilityLevel::Low,
            ReliabilityLevel::Low,
            ReliabilityLevel::Low,
            ReliabilityLevel::Low,
            notes,
        )
    }
}

/// Reliability level for evidence assessment.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
pub enum ReliabilityLevel {
    /// High reliability
    High,
    /// Medium reliability
    #[default]
    Medium,
    /// Low reliability
    Low,
}

impl ReliabilityLevel {
    /// Get numeric score.
    pub fn score(&self) -> u8 {
        match self {
            Self::High => 3,
            Self::Medium => 2,
            Self::Low => 1,
        }
    }

    /// Create from score.
    pub fn from_score(score: u8) -> Self {
        match score {
            0..=1 => Self::Low,
            2 => Self::Medium,
            _ => Self::High,
        }
    }
}

/// Evidence sufficiency evaluation per ISA 500.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EvidenceSufficiency {
    /// Assertion being evaluated
    pub assertion: Assertion,
    /// Account or area
    pub account_or_area: String,
    /// Evidence pieces collected
    pub evidence_count: u32,
    /// Total reliability score
    pub total_reliability_score: f64,
    /// Risk level being addressed
    pub risk_level: super::engagement::RiskLevel,
    /// Is evidence sufficient?
    pub is_sufficient: bool,
    /// Sufficiency conclusion notes
    pub conclusion_notes: String,
}

#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
    use super::*;

    #[test]
    fn test_evidence_creation() {
        let evidence = AuditEvidence::new(
            Uuid::new_v4(),
            EvidenceType::Confirmation,
            EvidenceSource::ExternalThirdParty,
            "Bank Confirmation",
        );

        assert_eq!(evidence.evidence_type, EvidenceType::Confirmation);
        assert_eq!(evidence.source_type, EvidenceSource::ExternalThirdParty);
    }

    #[test]
    fn test_reliability_assessment() {
        let assessment = ReliabilityAssessment::new(
            ReliabilityLevel::High,
            ReliabilityLevel::Medium,
            ReliabilityLevel::High,
            ReliabilityLevel::Medium,
            "External confirmation with good controls",
        );

        assert_eq!(assessment.overall_reliability, ReliabilityLevel::Medium);
    }

    #[test]
    fn test_source_reliability() {
        assert_eq!(
            EvidenceSource::ExternalThirdParty.inherent_reliability(),
            ReliabilityLevel::High
        );
        assert_eq!(
            EvidenceSource::InternalClientPrepared.inherent_reliability(),
            ReliabilityLevel::Low
        );
    }

    #[test]
    fn test_evidence_quality() {
        let evidence = AuditEvidence::new(
            Uuid::new_v4(),
            EvidenceType::Confirmation,
            EvidenceSource::ExternalThirdParty,
            "Bank Confirmation",
        )
        .with_reliability(ReliabilityAssessment::high("Direct confirmation"));

        assert!(evidence.is_high_quality());
    }
}