koprogo_api/application/use_cases/
work_report_use_cases.rs

1use crate::application::dto::{
2    AddDocumentDto, AddPhotoDto, CreateWorkReportDto, PageRequest, UpdateWorkReportDto,
3    WarrantyStatusDto, WorkReportFilters, WorkReportListResponseDto, WorkReportResponseDto,
4};
5use crate::application::ports::WorkReportRepository;
6use crate::domain::entities::WorkReport;
7use chrono::DateTime;
8use std::sync::Arc;
9use uuid::Uuid;
10
11pub struct WorkReportUseCases {
12    repository: Arc<dyn WorkReportRepository>,
13}
14
15impl WorkReportUseCases {
16    pub fn new(repository: Arc<dyn WorkReportRepository>) -> Self {
17        Self { repository }
18    }
19
20    pub async fn create_work_report(
21        &self,
22        dto: CreateWorkReportDto,
23    ) -> Result<WorkReportResponseDto, String> {
24        let organization_id = Uuid::parse_str(&dto.organization_id)
25            .map_err(|_| "Invalid organization_id format".to_string())?;
26        let building_id = Uuid::parse_str(&dto.building_id)
27            .map_err(|_| "Invalid building_id format".to_string())?;
28
29        let work_date = DateTime::parse_from_rfc3339(&dto.work_date)
30            .map_err(|_| "Invalid work_date format".to_string())?
31            .with_timezone(&chrono::Utc);
32
33        let completion_date = if let Some(ref date_str) = dto.completion_date {
34            Some(
35                DateTime::parse_from_rfc3339(date_str)
36                    .map_err(|_| "Invalid completion_date format".to_string())?
37                    .with_timezone(&chrono::Utc),
38            )
39        } else {
40            None
41        };
42
43        let work_report = WorkReport::new(
44            organization_id,
45            building_id,
46            dto.title,
47            dto.description,
48            dto.work_type,
49            dto.contractor_name,
50            work_date,
51            dto.cost,
52            dto.warranty_type.clone(),
53        );
54
55        let mut work_report = work_report;
56        work_report.contractor_contact = dto.contractor_contact;
57        work_report.completion_date = completion_date;
58        work_report.invoice_number = dto.invoice_number;
59        work_report.notes = dto.notes;
60
61        let created = self.repository.create(&work_report).await?;
62        Ok(self.to_response_dto(&created))
63    }
64
65    pub async fn get_work_report(&self, id: Uuid) -> Result<Option<WorkReportResponseDto>, String> {
66        let work_report = self.repository.find_by_id(id).await?;
67        Ok(work_report.map(|w| self.to_response_dto(&w)))
68    }
69
70    pub async fn list_work_reports_by_building(
71        &self,
72        building_id: Uuid,
73    ) -> Result<Vec<WorkReportResponseDto>, String> {
74        let work_reports = self.repository.find_by_building(building_id).await?;
75        Ok(work_reports
76            .iter()
77            .map(|w| self.to_response_dto(w))
78            .collect())
79    }
80
81    pub async fn list_work_reports_by_organization(
82        &self,
83        organization_id: Uuid,
84    ) -> Result<Vec<WorkReportResponseDto>, String> {
85        let work_reports = self
86            .repository
87            .find_by_organization(organization_id)
88            .await?;
89        Ok(work_reports
90            .iter()
91            .map(|w| self.to_response_dto(w))
92            .collect())
93    }
94
95    pub async fn list_work_reports_paginated(
96        &self,
97        page_request: &PageRequest,
98        filters: &WorkReportFilters,
99    ) -> Result<WorkReportListResponseDto, String> {
100        let (work_reports, total) = self
101            .repository
102            .find_all_paginated(page_request, filters)
103            .await?;
104
105        let dtos = work_reports
106            .iter()
107            .map(|w| self.to_response_dto(w))
108            .collect();
109
110        Ok(WorkReportListResponseDto {
111            work_reports: dtos,
112            total,
113            page: page_request.page,
114            page_size: page_request.per_page,
115        })
116    }
117
118    pub async fn get_active_warranties(
119        &self,
120        building_id: Uuid,
121    ) -> Result<Vec<WarrantyStatusDto>, String> {
122        let work_reports = self
123            .repository
124            .find_with_active_warranty(building_id)
125            .await?;
126
127        Ok(work_reports
128            .iter()
129            .map(|w| WarrantyStatusDto {
130                work_report_id: w.id.to_string(),
131                title: w.title.clone(),
132                warranty_type: w.warranty_type.clone(),
133                warranty_expiry: w.warranty_expiry.to_rfc3339(),
134                is_valid: w.is_warranty_valid(),
135                days_remaining: w.warranty_days_remaining(),
136            })
137            .collect())
138    }
139
140    pub async fn get_expiring_warranties(
141        &self,
142        building_id: Uuid,
143        days: i32,
144    ) -> Result<Vec<WarrantyStatusDto>, String> {
145        let work_reports = self
146            .repository
147            .find_with_expiring_warranty(building_id, days)
148            .await?;
149
150        Ok(work_reports
151            .iter()
152            .map(|w| WarrantyStatusDto {
153                work_report_id: w.id.to_string(),
154                title: w.title.clone(),
155                warranty_type: w.warranty_type.clone(),
156                warranty_expiry: w.warranty_expiry.to_rfc3339(),
157                is_valid: w.is_warranty_valid(),
158                days_remaining: w.warranty_days_remaining(),
159            })
160            .collect())
161    }
162
163    pub async fn update_work_report(
164        &self,
165        id: Uuid,
166        dto: UpdateWorkReportDto,
167    ) -> Result<WorkReportResponseDto, String> {
168        let mut work_report = self
169            .repository
170            .find_by_id(id)
171            .await?
172            .ok_or_else(|| "Work report not found".to_string())?;
173
174        if let Some(title) = dto.title {
175            work_report.title = title;
176        }
177        if let Some(description) = dto.description {
178            work_report.description = description;
179        }
180        if let Some(work_type) = dto.work_type {
181            work_report.work_type = work_type;
182        }
183        if let Some(contractor_name) = dto.contractor_name {
184            work_report.contractor_name = contractor_name;
185        }
186        if let Some(contractor_contact) = dto.contractor_contact {
187            work_report.contractor_contact = Some(contractor_contact);
188        }
189        if let Some(work_date_str) = dto.work_date {
190            let work_date = DateTime::parse_from_rfc3339(&work_date_str)
191                .map_err(|_| "Invalid work_date format".to_string())?
192                .with_timezone(&chrono::Utc);
193            work_report.work_date = work_date;
194        }
195        if let Some(completion_date_str) = dto.completion_date {
196            let completion_date = DateTime::parse_from_rfc3339(&completion_date_str)
197                .map_err(|_| "Invalid completion_date format".to_string())?
198                .with_timezone(&chrono::Utc);
199            work_report.completion_date = Some(completion_date);
200        }
201        if let Some(cost) = dto.cost {
202            work_report.cost = cost;
203        }
204        if let Some(invoice_number) = dto.invoice_number {
205            work_report.invoice_number = Some(invoice_number);
206        }
207        if let Some(notes) = dto.notes {
208            work_report.notes = Some(notes);
209        }
210        if let Some(warranty_type) = dto.warranty_type {
211            work_report.warranty_type = warranty_type;
212            // Recalculate warranty expiry when type changes
213            work_report.warranty_expiry = match work_report.warranty_type {
214                crate::domain::entities::WarrantyType::None => chrono::Utc::now(),
215                crate::domain::entities::WarrantyType::Standard => {
216                    work_report.work_date + chrono::Duration::days(2 * 365)
217                }
218                crate::domain::entities::WarrantyType::Decennial => {
219                    work_report.work_date + chrono::Duration::days(10 * 365)
220                }
221                crate::domain::entities::WarrantyType::Extended => {
222                    work_report.work_date + chrono::Duration::days(3 * 365)
223                }
224                crate::domain::entities::WarrantyType::Custom { years } => {
225                    work_report.work_date + chrono::Duration::days(years as i64 * 365)
226                }
227            };
228        }
229
230        work_report.updated_at = chrono::Utc::now();
231
232        let updated = self.repository.update(&work_report).await?;
233        Ok(self.to_response_dto(&updated))
234    }
235
236    pub async fn add_photo(
237        &self,
238        id: Uuid,
239        dto: AddPhotoDto,
240    ) -> Result<WorkReportResponseDto, String> {
241        let mut work_report = self
242            .repository
243            .find_by_id(id)
244            .await?
245            .ok_or_else(|| "Work report not found".to_string())?;
246
247        work_report.add_photo(dto.photo_path);
248
249        let updated = self.repository.update(&work_report).await?;
250        Ok(self.to_response_dto(&updated))
251    }
252
253    pub async fn add_document(
254        &self,
255        id: Uuid,
256        dto: AddDocumentDto,
257    ) -> Result<WorkReportResponseDto, String> {
258        let mut work_report = self
259            .repository
260            .find_by_id(id)
261            .await?
262            .ok_or_else(|| "Work report not found".to_string())?;
263
264        work_report.add_document(dto.document_path);
265
266        let updated = self.repository.update(&work_report).await?;
267        Ok(self.to_response_dto(&updated))
268    }
269
270    pub async fn delete_work_report(&self, id: Uuid) -> Result<bool, String> {
271        self.repository.delete(id).await
272    }
273
274    fn to_response_dto(&self, work_report: &WorkReport) -> WorkReportResponseDto {
275        WorkReportResponseDto {
276            id: work_report.id.to_string(),
277            organization_id: work_report.organization_id.to_string(),
278            building_id: work_report.building_id.to_string(),
279            title: work_report.title.clone(),
280            description: work_report.description.clone(),
281            work_type: work_report.work_type.clone(),
282            contractor_name: work_report.contractor_name.clone(),
283            contractor_contact: work_report.contractor_contact.clone(),
284            work_date: work_report.work_date.to_rfc3339(),
285            completion_date: work_report.completion_date.as_ref().map(|d| d.to_rfc3339()),
286            cost: work_report.cost,
287            invoice_number: work_report.invoice_number.clone(),
288            photos: work_report.photos.clone(),
289            documents: work_report.documents.clone(),
290            notes: work_report.notes.clone(),
291            warranty_type: work_report.warranty_type.clone(),
292            warranty_expiry: work_report.warranty_expiry.to_rfc3339(),
293            is_warranty_valid: work_report.is_warranty_valid(),
294            warranty_days_remaining: work_report.warranty_days_remaining(),
295            created_at: work_report.created_at.to_rfc3339(),
296            updated_at: work_report.updated_at.to_rfc3339(),
297        }
298    }
299}
300
301#[cfg(test)]
302mod tests {
303    use super::*;
304    use crate::application::dto::{CreateWorkReportDto, UpdateWorkReportDto, WorkReportFilters};
305    use crate::application::ports::WorkReportRepository;
306    use crate::domain::entities::{WarrantyType, WorkReport, WorkType};
307    use async_trait::async_trait;
308    use chrono::Utc;
309    use std::sync::Arc;
310    use tokio::sync::Mutex;
311    use uuid::Uuid;
312
313    /// In-memory mock for WorkReportRepository
314    struct MockWorkReportRepository {
315        reports: Mutex<Vec<WorkReport>>,
316    }
317
318    impl MockWorkReportRepository {
319        fn new() -> Self {
320            Self {
321                reports: Mutex::new(Vec::new()),
322            }
323        }
324
325        fn with_reports(reports: Vec<WorkReport>) -> Self {
326            Self {
327                reports: Mutex::new(reports),
328            }
329        }
330    }
331
332    #[async_trait]
333    impl WorkReportRepository for MockWorkReportRepository {
334        async fn create(&self, work_report: &WorkReport) -> Result<WorkReport, String> {
335            let mut reports = self.reports.lock().await;
336            reports.push(work_report.clone());
337            Ok(work_report.clone())
338        }
339
340        async fn find_by_id(&self, id: Uuid) -> Result<Option<WorkReport>, String> {
341            let reports = self.reports.lock().await;
342            Ok(reports.iter().find(|r| r.id == id).cloned())
343        }
344
345        async fn find_by_building(&self, building_id: Uuid) -> Result<Vec<WorkReport>, String> {
346            let reports = self.reports.lock().await;
347            Ok(reports
348                .iter()
349                .filter(|r| r.building_id == building_id)
350                .cloned()
351                .collect())
352        }
353
354        async fn find_by_organization(
355            &self,
356            organization_id: Uuid,
357        ) -> Result<Vec<WorkReport>, String> {
358            let reports = self.reports.lock().await;
359            Ok(reports
360                .iter()
361                .filter(|r| r.organization_id == organization_id)
362                .cloned()
363                .collect())
364        }
365
366        async fn find_all_paginated(
367            &self,
368            _page_request: &crate::application::dto::PageRequest,
369            _filters: &WorkReportFilters,
370        ) -> Result<(Vec<WorkReport>, i64), String> {
371            let reports = self.reports.lock().await;
372            let total = reports.len() as i64;
373            Ok((reports.clone(), total))
374        }
375
376        async fn find_with_active_warranty(
377            &self,
378            building_id: Uuid,
379        ) -> Result<Vec<WorkReport>, String> {
380            let reports = self.reports.lock().await;
381            Ok(reports
382                .iter()
383                .filter(|r| r.building_id == building_id && r.is_warranty_valid())
384                .cloned()
385                .collect())
386        }
387
388        async fn find_with_expiring_warranty(
389            &self,
390            building_id: Uuid,
391            days: i32,
392        ) -> Result<Vec<WorkReport>, String> {
393            let reports = self.reports.lock().await;
394            Ok(reports
395                .iter()
396                .filter(|r| {
397                    r.building_id == building_id
398                        && r.is_warranty_valid()
399                        && r.warranty_days_remaining() <= days as i64
400                })
401                .cloned()
402                .collect())
403        }
404
405        async fn update(&self, work_report: &WorkReport) -> Result<WorkReport, String> {
406            let mut reports = self.reports.lock().await;
407            if let Some(pos) = reports.iter().position(|r| r.id == work_report.id) {
408                reports[pos] = work_report.clone();
409            }
410            Ok(work_report.clone())
411        }
412
413        async fn delete(&self, id: Uuid) -> Result<bool, String> {
414            let mut reports = self.reports.lock().await;
415            let len_before = reports.len();
416            reports.retain(|r| r.id != id);
417            Ok(reports.len() < len_before)
418        }
419    }
420
421    fn make_org_and_building() -> (Uuid, Uuid) {
422        (Uuid::new_v4(), Uuid::new_v4())
423    }
424
425    #[tokio::test]
426    async fn test_create_work_report() {
427        let (org_id, building_id) = make_org_and_building();
428        let repo = Arc::new(MockWorkReportRepository::new());
429        let uc = WorkReportUseCases::new(repo);
430
431        let work_date = Utc::now().to_rfc3339();
432        let dto = CreateWorkReportDto {
433            organization_id: org_id.to_string(),
434            building_id: building_id.to_string(),
435            title: "Elevator repair".to_string(),
436            description: "Cable replacement".to_string(),
437            work_type: WorkType::Repair,
438            contractor_name: "Schindler".to_string(),
439            contractor_contact: None,
440            work_date,
441            completion_date: None,
442            cost: 2500.0,
443            invoice_number: Some("INV-001".to_string()),
444            notes: None,
445            warranty_type: WarrantyType::Standard,
446        };
447
448        let result = uc.create_work_report(dto).await;
449        assert!(result.is_ok());
450        let resp = result.unwrap();
451        assert_eq!(resp.title, "Elevator repair");
452        assert_eq!(resp.cost, 2500.0);
453        assert_eq!(resp.contractor_name, "Schindler");
454        assert!(resp.is_warranty_valid);
455    }
456
457    #[tokio::test]
458    async fn test_update_work_report() {
459        let (org_id, building_id) = make_org_and_building();
460        let report = WorkReport::new(
461            org_id,
462            building_id,
463            "Old title".to_string(),
464            "Old desc".to_string(),
465            WorkType::Maintenance,
466            "Contractor A".to_string(),
467            Utc::now(),
468            1000.0,
469            WarrantyType::None,
470        );
471        let report_id = report.id;
472
473        let repo = Arc::new(MockWorkReportRepository::with_reports(vec![report]));
474        let uc = WorkReportUseCases::new(repo);
475
476        let dto = UpdateWorkReportDto {
477            title: Some("Updated title".to_string()),
478            description: None,
479            work_type: None,
480            contractor_name: None,
481            contractor_contact: None,
482            work_date: None,
483            completion_date: None,
484            cost: Some(1500.0),
485            invoice_number: None,
486            notes: None,
487            warranty_type: None,
488        };
489
490        let result = uc.update_work_report(report_id, dto).await;
491        assert!(result.is_ok());
492        let resp = result.unwrap();
493        assert_eq!(resp.title, "Updated title");
494        assert_eq!(resp.cost, 1500.0);
495    }
496
497    #[tokio::test]
498    async fn test_list_work_reports_by_building() {
499        let (org_id, building_id) = make_org_and_building();
500        let other_building = Uuid::new_v4();
501
502        let r1 = WorkReport::new(
503            org_id,
504            building_id,
505            "Report 1".into(),
506            "Desc".into(),
507            WorkType::Repair,
508            "C1".into(),
509            Utc::now(),
510            100.0,
511            WarrantyType::None,
512        );
513        let r2 = WorkReport::new(
514            org_id,
515            building_id,
516            "Report 2".into(),
517            "Desc".into(),
518            WorkType::Maintenance,
519            "C2".into(),
520            Utc::now(),
521            200.0,
522            WarrantyType::None,
523        );
524        let r3 = WorkReport::new(
525            org_id,
526            other_building,
527            "Report 3".into(),
528            "Desc".into(),
529            WorkType::Emergency,
530            "C3".into(),
531            Utc::now(),
532            300.0,
533            WarrantyType::None,
534        );
535
536        let repo = Arc::new(MockWorkReportRepository::with_reports(vec![r1, r2, r3]));
537        let uc = WorkReportUseCases::new(repo);
538
539        let result = uc.list_work_reports_by_building(building_id).await;
540        assert!(result.is_ok());
541        let reports = result.unwrap();
542        assert_eq!(reports.len(), 2);
543        assert!(reports
544            .iter()
545            .all(|r| r.building_id == building_id.to_string()));
546    }
547
548    #[tokio::test]
549    async fn test_get_active_warranties() {
550        let (org_id, building_id) = make_org_and_building();
551
552        // Active warranty (Standard = 2 years from now)
553        let r1 = WorkReport::new(
554            org_id,
555            building_id,
556            "Active warranty".into(),
557            "Desc".into(),
558            WorkType::Renovation,
559            "C1".into(),
560            Utc::now(),
561            5000.0,
562            WarrantyType::Standard,
563        );
564        // No warranty
565        let r2 = WorkReport::new(
566            org_id,
567            building_id,
568            "No warranty".into(),
569            "Desc".into(),
570            WorkType::Maintenance,
571            "C2".into(),
572            Utc::now(),
573            100.0,
574            WarrantyType::None,
575        );
576
577        let repo = Arc::new(MockWorkReportRepository::with_reports(vec![r1, r2]));
578        let uc = WorkReportUseCases::new(repo);
579
580        let result = uc.get_active_warranties(building_id).await;
581        assert!(result.is_ok());
582        let warranties = result.unwrap();
583        assert_eq!(warranties.len(), 1);
584        assert_eq!(warranties[0].title, "Active warranty");
585        assert!(warranties[0].is_valid);
586        assert!(warranties[0].days_remaining > 700);
587    }
588
589    #[tokio::test]
590    async fn test_get_expiring_warranties() {
591        let (org_id, building_id) = make_org_and_building();
592
593        // Warranty expiring in ~30 days (custom 0-year warranty set to expire soon)
594        let mut r1 = WorkReport::new(
595            org_id,
596            building_id,
597            "Expiring soon".into(),
598            "Desc".into(),
599            WorkType::Repair,
600            "C1".into(),
601            Utc::now(),
602            1000.0,
603            WarrantyType::Standard,
604        );
605        // Override warranty_expiry to 20 days from now
606        r1.warranty_expiry = Utc::now() + chrono::Duration::days(20);
607
608        // Warranty valid for a long time (decennial)
609        let r2 = WorkReport::new(
610            org_id,
611            building_id,
612            "Long warranty".into(),
613            "Desc".into(),
614            WorkType::Renovation,
615            "C2".into(),
616            Utc::now(),
617            50000.0,
618            WarrantyType::Decennial,
619        );
620
621        let repo = Arc::new(MockWorkReportRepository::with_reports(vec![r1, r2]));
622        let uc = WorkReportUseCases::new(repo);
623
624        let result = uc.get_expiring_warranties(building_id, 30).await;
625        assert!(result.is_ok());
626        let expiring = result.unwrap();
627        assert_eq!(expiring.len(), 1);
628        assert_eq!(expiring[0].title, "Expiring soon");
629        assert!(expiring[0].days_remaining <= 30);
630    }
631}