| Total Complexity | 6 |
| Complexity/F | 6 |
| Lines of Code | 71 |
| Function Count | 1 |
| Duplicated Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
| 1 | import { Inject } from '@nestjs/common'; |
||
| 2 | import { FairCalendarView } from 'src/Application/FairCalendar/View/FairCalendarView'; |
||
| 3 | import { CooperativeNotFoundException } from '../Settings/Repository/CooperativeNotFoundException'; |
||
| 4 | import { ICooperativeRepository } from '../Settings/Repository/ICooperativeRepository'; |
||
| 5 | import { ICalendarOverview } from './ICalendarOverview'; |
||
| 6 | import { EventType } from './Event.entity'; |
||
| 7 | |||
| 8 | export class FairCalendarOverviewFactory { |
||
| 9 | constructor( |
||
| 10 | @Inject('ICooperativeRepository') |
||
| 11 | private readonly cooperativeRepository: ICooperativeRepository |
||
| 12 | ) {} |
||
| 13 | |||
| 14 | public async create(items: FairCalendarView[]): Promise<ICalendarOverview> { |
||
| 15 | const cooperative = await this.cooperativeRepository.find(); |
||
| 16 | if (!cooperative) { |
||
| 17 | throw new CooperativeNotFoundException(); |
||
| 18 | } |
||
| 19 | |||
| 20 | const overviewInDays: ICalendarOverview = { |
||
| 21 | mission: { |
||
| 22 | days: 0, |
||
| 23 | details: [] |
||
| 24 | }, |
||
| 25 | dojo: { |
||
| 26 | days: 0 |
||
| 27 | }, |
||
| 28 | formationConference: { |
||
| 29 | days: 0 |
||
| 30 | }, |
||
| 31 | leave: { |
||
| 32 | days: 0 |
||
| 33 | }, |
||
| 34 | support: { |
||
| 35 | days: 0 |
||
| 36 | }, |
||
| 37 | other: { |
||
| 38 | days: 0 |
||
| 39 | } |
||
| 40 | }; |
||
| 41 | |||
| 42 | for (const { time, type: itemType, project } of items) { |
||
| 43 | const type = itemType.startsWith('leave_') ? 'leave' : itemType; |
||
| 44 | const days = time / cooperative.getDayDuration(); |
||
| 45 | |||
| 46 | overviewInDays[type].days = |
||
| 47 | Math.round((overviewInDays[type].days + days) * 100) / 100; |
||
| 48 | |||
| 49 | if (type === EventType.MISSION) { |
||
| 50 | const missionDetail = overviewInDays[type].details.find( |
||
| 51 | ({ label }) => label === project.name |
||
| 52 | ); |
||
| 53 | |||
| 54 | if (missionDetail) { |
||
| 55 | missionDetail.days = |
||
| 56 | Math.round( |
||
| 57 | (missionDetail.days + Math.round(days * 100) / 100) * 100 |
||
| 58 | ) / 100; |
||
| 59 | } else { |
||
| 60 | overviewInDays[type].details.push({ |
||
| 61 | days, |
||
| 62 | label: project.name |
||
| 63 | }); |
||
| 64 | } |
||
| 65 | } |
||
| 66 | } |
||
| 67 | |||
| 68 | return overviewInDays; |
||
| 69 | } |
||
| 70 | } |
||
| 71 |