datasynth-core 2.4.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
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
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
//! Internal audit function and report models per ISA 610.
//!
//! ISA 610 governs the use of the work of internal auditors. The external auditor
//! must evaluate the internal audit function's objectivity, competence, and whether
//! a systematic and disciplined approach has been applied.

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

/// Reporting line of the internal audit function.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
pub enum ReportingLine {
    /// Reports to the Audit Committee
    #[default]
    AuditCommittee,
    /// Reports to the Board of Directors
    Board,
    /// Reports to the Chief Financial Officer
    CFO,
    /// Reports to the Chief Executive Officer
    CEO,
}

/// ISA 610 assessment of the overall effectiveness of the internal audit function.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
pub enum IaAssessment {
    /// Internal audit is fully effective across all dimensions
    FullyEffective,
    /// Internal audit is largely effective with minor gaps
    #[default]
    LargelyEffective,
    /// Internal audit is partially effective with notable gaps
    PartiallyEffective,
    /// Internal audit is not effective
    Ineffective,
}

/// Objectivity rating of internal audit personnel.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
pub enum ObjectivityRating {
    /// High objectivity — strong independence safeguards in place
    #[default]
    High,
    /// Moderate objectivity — some independence concerns exist
    Moderate,
    /// Low objectivity — significant independence concerns exist
    Low,
}

/// Competence rating of the internal audit function.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
pub enum CompetenceRating {
    /// High competence — well-qualified staff with relevant expertise
    High,
    /// Moderate competence — adequate but some skill gaps exist
    #[default]
    Moderate,
    /// Low competence — significant skill gaps exist
    Low,
}

/// Extent to which the external auditor relies on the internal audit function's work.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
pub enum RelianceExtent {
    /// No reliance — external auditor performs all work independently
    NoReliance,
    /// Limited reliance — minor use of internal audit work
    #[default]
    LimitedReliance,
    /// Significant reliance — substantial use of internal audit work
    SignificantReliance,
    /// Full reliance — maximum use of internal audit work permitted by ISA 610
    FullReliance,
}

/// Overall rating assigned by the internal auditor to the audited area.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
pub enum IaReportRating {
    /// Area is operating satisfactorily
    #[default]
    Satisfactory,
    /// Area needs improvement in certain respects
    NeedsImprovement,
    /// Area is unsatisfactory with material control weaknesses
    Unsatisfactory,
}

/// Lifecycle status of an internal audit report.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
pub enum IaReportStatus {
    /// Report is in draft form, pending management review
    #[default]
    Draft,
    /// Report has been finalised and issued
    Final,
    /// Report has been retracted
    Retracted,
}

/// Priority level assigned to an internal audit recommendation.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
pub enum RecommendationPriority {
    /// Critical — immediate action required to address a significant control failure
    Critical,
    /// High — urgent action required
    High,
    /// Medium — action required within a reasonable timeframe
    #[default]
    Medium,
    /// Low — action desirable but not urgent
    Low,
}

/// Status of a management action plan in response to a recommendation.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
pub enum ActionPlanStatus {
    /// Action plan has not yet been started
    #[default]
    Open,
    /// Action plan is in progress
    InProgress,
    /// Action plan has been fully implemented
    Implemented,
    /// Action plan has passed its target date without implementation
    Overdue,
}

/// External auditor's assessment of the reliability of a specific piece of internal audit work.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
pub enum IaWorkAssessment {
    /// Work is reliable and can be used without significant modification
    Reliable,
    /// Work is partially reliable — some additional procedures required
    #[default]
    PartiallyReliable,
    /// Work is not reliable — cannot be used by the external auditor
    Unreliable,
}

/// The internal audit function of the entity being audited (ISA 610).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct InternalAuditFunction {
    /// Unique function ID
    pub function_id: Uuid,
    /// Human-readable reference (format: "IAF-{first 8 hex chars of function_id}")
    pub function_ref: String,
    /// Engagement this assessment relates to
    pub engagement_id: Uuid,

    // === Organisation ===
    /// Name of the internal audit department
    pub department_name: String,
    /// Reporting line of the head of internal audit
    pub reporting_line: ReportingLine,
    /// Name of the head of internal audit
    pub head_of_ia: String,
    /// Professional qualifications held by the head of internal audit
    pub head_of_ia_qualifications: Vec<String>,
    /// Number of internal audit staff (FTE)
    pub staff_count: u32,
    /// Percentage of total risk universe covered by the annual audit plan
    pub annual_plan_coverage: f64,
    /// Whether a formal quality assurance and improvement programme exists
    pub quality_assurance: bool,

    // === ISA 610 Assessment ===
    /// Overall assessment of the internal audit function per ISA 610
    pub isa_610_assessment: IaAssessment,
    /// Assessment of objectivity
    pub objectivity_rating: ObjectivityRating,
    /// Assessment of technical competence
    pub competence_rating: CompetenceRating,
    /// Whether a systematic and disciplined approach is applied
    pub systematic_discipline: bool,

    // === Reliance Decision ===
    /// Extent to which the external auditor plans to rely on internal audit work
    pub reliance_extent: RelianceExtent,
    /// Specific audit areas where reliance will be placed
    pub reliance_areas: Vec<String>,
    /// Whether direct assistance from internal audit staff will be used
    pub direct_assistance: bool,

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

impl InternalAuditFunction {
    /// Create a new internal audit function record with sensible defaults.
    pub fn new(
        engagement_id: Uuid,
        department_name: impl Into<String>,
        head_of_ia: impl Into<String>,
    ) -> Self {
        let now = Utc::now();
        let id = Uuid::new_v4();
        let function_ref = format!("IAF-{}", &id.simple().to_string()[..8]);
        Self {
            function_id: id,
            function_ref,
            engagement_id,
            department_name: department_name.into(),
            reporting_line: ReportingLine::AuditCommittee,
            head_of_ia: head_of_ia.into(),
            head_of_ia_qualifications: Vec::new(),
            staff_count: 0,
            annual_plan_coverage: 0.0,
            quality_assurance: false,
            isa_610_assessment: IaAssessment::LargelyEffective,
            objectivity_rating: ObjectivityRating::High,
            competence_rating: CompetenceRating::Moderate,
            systematic_discipline: true,
            reliance_extent: RelianceExtent::LimitedReliance,
            reliance_areas: Vec::new(),
            direct_assistance: false,
            created_at: now,
            updated_at: now,
        }
    }
}

/// A recommendation raised in an internal audit report.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct IaRecommendation {
    /// Unique recommendation ID
    pub recommendation_id: Uuid,
    /// Description of the recommendation
    pub description: String,
    /// Priority level
    pub priority: RecommendationPriority,
    /// Management's response to the recommendation
    pub management_response: Option<String>,
}

/// Management's action plan in response to an internal audit recommendation.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ActionPlan {
    /// Unique plan ID
    pub plan_id: Uuid,
    /// The recommendation this plan addresses
    pub recommendation_id: Uuid,
    /// Description of the planned action
    pub description: String,
    /// Party responsible for implementing the action
    pub responsible_party: String,
    /// Target implementation date
    pub target_date: NaiveDate,
    /// Current status of the action plan
    pub status: ActionPlanStatus,
}

/// An internal audit report for a specific audit area (ISA 610).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct InternalAuditReport {
    /// Unique report ID
    pub report_id: Uuid,
    /// Human-readable reference (format: "IAR-{first 8 hex chars of report_id}")
    pub report_ref: String,
    /// Engagement this report is associated with
    pub engagement_id: Uuid,
    /// The internal audit function that produced this report
    pub ia_function_id: Uuid,

    // === Report Header ===
    /// Report title
    pub report_title: String,
    /// Audit area covered by the report
    pub audit_area: String,
    /// Date the report was issued
    pub report_date: NaiveDate,
    /// Start of the period covered by the audit
    pub period_start: NaiveDate,
    /// End of the period covered by the audit
    pub period_end: NaiveDate,

    // === Scope & Methodology ===
    /// Description of the audit scope
    pub scope_description: String,
    /// Methodology applied during the audit
    pub methodology: String,

    // === Findings & Ratings ===
    /// Overall rating of the audited area
    pub overall_rating: IaReportRating,
    /// Total number of findings raised
    pub findings_count: u32,
    /// Number of high-risk findings
    pub high_risk_findings: u32,
    /// Recommendations raised in the report
    pub recommendations: Vec<IaRecommendation>,
    /// Management action plans in response to the recommendations
    pub management_action_plans: Vec<ActionPlan>,

    // === Status ===
    /// Current lifecycle status of the report
    pub status: IaReportStatus,

    // === External Auditor's Assessment ===
    /// External auditor's assessment of the reliability of this report's work
    pub external_auditor_assessment: Option<IaWorkAssessment>,

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

impl InternalAuditReport {
    /// Create a new internal audit report.
    pub fn new(
        engagement_id: Uuid,
        ia_function_id: Uuid,
        report_title: impl Into<String>,
        audit_area: impl Into<String>,
        report_date: NaiveDate,
        period_start: NaiveDate,
        period_end: NaiveDate,
    ) -> Self {
        let now = Utc::now();
        let id = Uuid::new_v4();
        let report_ref = format!("IAR-{}", &id.simple().to_string()[..8]);
        Self {
            report_id: id,
            report_ref,
            engagement_id,
            ia_function_id,
            report_title: report_title.into(),
            audit_area: audit_area.into(),
            report_date,
            period_start,
            period_end,
            scope_description: String::new(),
            methodology: String::new(),
            overall_rating: IaReportRating::Satisfactory,
            findings_count: 0,
            high_risk_findings: 0,
            recommendations: Vec::new(),
            management_action_plans: Vec::new(),
            status: IaReportStatus::Draft,
            external_auditor_assessment: None,
            created_at: now,
            updated_at: now,
        }
    }
}

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

    fn sample_date(year: i32, month: u32, day: u32) -> NaiveDate {
        NaiveDate::from_ymd_opt(year, month, day).unwrap()
    }

    #[test]
    fn test_new_ia_function() {
        let eng = Uuid::new_v4();
        let iaf = InternalAuditFunction::new(eng, "Group Internal Audit", "Jane Smith");

        assert_eq!(iaf.engagement_id, eng);
        assert_eq!(iaf.department_name, "Group Internal Audit");
        assert_eq!(iaf.head_of_ia, "Jane Smith");
        assert_eq!(iaf.reporting_line, ReportingLine::AuditCommittee);
        assert_eq!(iaf.isa_610_assessment, IaAssessment::LargelyEffective);
        assert_eq!(iaf.objectivity_rating, ObjectivityRating::High);
        assert_eq!(iaf.competence_rating, CompetenceRating::Moderate);
        assert_eq!(iaf.reliance_extent, RelianceExtent::LimitedReliance);
        assert!(iaf.systematic_discipline);
        assert!(!iaf.direct_assistance);
        assert!(iaf.function_ref.starts_with("IAF-"));
        assert_eq!(iaf.function_ref.len(), 12); // "IAF-" + 8 hex chars
    }

    #[test]
    fn test_new_ia_report() {
        let eng = Uuid::new_v4();
        let func = Uuid::new_v4();
        let report = InternalAuditReport::new(
            eng,
            func,
            "Procurement Process Review",
            "Procurement",
            sample_date(2025, 3, 31),
            sample_date(2025, 1, 1),
            sample_date(2025, 12, 31),
        );

        assert_eq!(report.engagement_id, eng);
        assert_eq!(report.ia_function_id, func);
        assert_eq!(report.report_title, "Procurement Process Review");
        assert_eq!(report.audit_area, "Procurement");
        assert_eq!(report.overall_rating, IaReportRating::Satisfactory);
        assert_eq!(report.status, IaReportStatus::Draft);
        assert_eq!(report.findings_count, 0);
        assert!(report.recommendations.is_empty());
        assert!(report.external_auditor_assessment.is_none());
        assert!(report.report_ref.starts_with("IAR-"));
        assert_eq!(report.report_ref.len(), 12); // "IAR-" + 8 hex chars
    }

    #[test]
    fn test_reporting_line_serde() {
        let variants = [
            ReportingLine::AuditCommittee,
            ReportingLine::Board,
            ReportingLine::CFO,
            ReportingLine::CEO,
        ];
        for v in variants {
            let json = serde_json::to_string(&v).unwrap();
            let rt: ReportingLine = serde_json::from_str(&json).unwrap();
            assert_eq!(v, rt);
        }
        assert_eq!(
            serde_json::to_string(&ReportingLine::AuditCommittee).unwrap(),
            "\"audit_committee\""
        );
    }

    #[test]
    fn test_ia_assessment_serde() {
        let variants = [
            IaAssessment::FullyEffective,
            IaAssessment::LargelyEffective,
            IaAssessment::PartiallyEffective,
            IaAssessment::Ineffective,
        ];
        for v in variants {
            let json = serde_json::to_string(&v).unwrap();
            let rt: IaAssessment = serde_json::from_str(&json).unwrap();
            assert_eq!(v, rt);
        }
        assert_eq!(
            serde_json::to_string(&IaAssessment::FullyEffective).unwrap(),
            "\"fully_effective\""
        );
    }

    #[test]
    fn test_reliance_extent_serde() {
        let variants = [
            RelianceExtent::NoReliance,
            RelianceExtent::LimitedReliance,
            RelianceExtent::SignificantReliance,
            RelianceExtent::FullReliance,
        ];
        for v in variants {
            let json = serde_json::to_string(&v).unwrap();
            let rt: RelianceExtent = serde_json::from_str(&json).unwrap();
            assert_eq!(v, rt);
        }
        assert_eq!(
            serde_json::to_string(&RelianceExtent::SignificantReliance).unwrap(),
            "\"significant_reliance\""
        );
    }

    #[test]
    fn test_ia_report_status_serde() {
        let variants = [
            IaReportStatus::Draft,
            IaReportStatus::Final,
            IaReportStatus::Retracted,
        ];
        for v in variants {
            let json = serde_json::to_string(&v).unwrap();
            let rt: IaReportStatus = serde_json::from_str(&json).unwrap();
            assert_eq!(v, rt);
        }
        assert_eq!(
            serde_json::to_string(&IaReportStatus::Final).unwrap(),
            "\"final\""
        );
    }

    #[test]
    fn test_ia_report_rating_serde() {
        let variants = [
            IaReportRating::Satisfactory,
            IaReportRating::NeedsImprovement,
            IaReportRating::Unsatisfactory,
        ];
        for v in variants {
            let json = serde_json::to_string(&v).unwrap();
            let rt: IaReportRating = serde_json::from_str(&json).unwrap();
            assert_eq!(v, rt);
        }
        assert_eq!(
            serde_json::to_string(&IaReportRating::NeedsImprovement).unwrap(),
            "\"needs_improvement\""
        );
    }

    #[test]
    fn test_recommendation_priority_serde() {
        let variants = [
            RecommendationPriority::Critical,
            RecommendationPriority::High,
            RecommendationPriority::Medium,
            RecommendationPriority::Low,
        ];
        for v in variants {
            let json = serde_json::to_string(&v).unwrap();
            let rt: RecommendationPriority = serde_json::from_str(&json).unwrap();
            assert_eq!(v, rt);
        }
        assert_eq!(
            serde_json::to_string(&RecommendationPriority::Critical).unwrap(),
            "\"critical\""
        );
    }

    #[test]
    fn test_action_plan_status_serde() {
        let variants = [
            ActionPlanStatus::Open,
            ActionPlanStatus::InProgress,
            ActionPlanStatus::Implemented,
            ActionPlanStatus::Overdue,
        ];
        for v in variants {
            let json = serde_json::to_string(&v).unwrap();
            let rt: ActionPlanStatus = serde_json::from_str(&json).unwrap();
            assert_eq!(v, rt);
        }
        assert_eq!(
            serde_json::to_string(&ActionPlanStatus::InProgress).unwrap(),
            "\"in_progress\""
        );
    }

    #[test]
    fn test_ia_work_assessment_serde() {
        let variants = [
            IaWorkAssessment::Reliable,
            IaWorkAssessment::PartiallyReliable,
            IaWorkAssessment::Unreliable,
        ];
        for v in variants {
            let json = serde_json::to_string(&v).unwrap();
            let rt: IaWorkAssessment = serde_json::from_str(&json).unwrap();
            assert_eq!(v, rt);
        }
        assert_eq!(
            serde_json::to_string(&IaWorkAssessment::PartiallyReliable).unwrap(),
            "\"partially_reliable\""
        );
    }
}