import { getAggregationPeriodValue } from 'app/shared/services/aggregationPeriods/aggregationPeriods.service'; import { get } from 'lodash'; import { map } from 'rxjs/operators'; import { MaerskApiClient } from '../../../common/clients/maerskApi.client'; import { TceAggregation, TceAggregationDto, VesselsEarningsParams } from '../store/earnings.types'; import { FiltersState } from '../../../shared/store/filters/filters.types'; import { VesselsRequestParams } from '../store/vessels/vessels.types'; export class EarningsService { private apiClient: MaerskApiClient; constructor(apiClient) { this.apiClient = apiClient; } static createBenchmark(benchmark) { return { date: benchmark ? benchmark.date : null, spot: get(benchmark, 'spot') ? benchmark.spot : null, year: get(benchmark, 'year') ? benchmark.year : null, }; } createAggregation(data: TceAggregationDto[]): TceAggregation[] { return data.map((item, index) => ({ id: index + 1, date: item.date, commercial: item.tceResult, benchmark: EarningsService.createBenchmark(item.benchmark), })); } mapVesselsEarnings(earnings) { const mapVesselEarnings = (data) => { return data.every(value => !value) ? [] : data.map((item) => { return { id: item.id, partner: item.partner && { id: item.partner.id, name: item.partner.name, abbr: item.partner.axCode, }, name: item.name, poolPoints: item.poolPoints, bunkerAdjustmentPerDay: item.bunkerAdjustmentPerDay, earnings: item.earnings, bunkerAdjustment: item.bunkerAdjustment, scrubberBonus: item.scrubberBonus, wafDta: item.wafDta, specialCompensations: item.specialCompensations, tce: item.tce, warnings: item.warnings || null, hasScrubber: item.hasScrubber || false, vesselCode: item.vesselCode, }; }); }; const mapAverageVesselEarnings = (earnings) => { return { id: earnings.id, names: earnings.names, poolPoints: earnings.poolPoints, bunkerAdjustmentPerDay: earnings.bunkerAdjustmentPerDay, earnings: earnings.earnings, bunkerAdjustment: earnings.bunkerAdjustment, scrubberBonus: earnings.scrubberBonus, wafDta: earnings.wafDta, specialCompensations: earnings.specialCompensations, tce: earnings.tce, modelAverageTce: earnings.modelAverageTce, }; }; return [ mapAverageVesselEarnings(earnings.averagePoolEarnings), earnings.averageFilteredEarnings && mapAverageVesselEarnings(earnings.averageFilteredEarnings), mapVesselEarnings(earnings.vesselsEarnings), ]; } getAggregations(filters: FiltersState) { const requestFilters: VesselsEarningsParams = { poolId: filters.poolId, year: filters.year, aggregate: getAggregationPeriodValue(filters.aggregationPeriodId), partnerAxCodes: filters.poolPartnerAxCodes, groupIds: filters.groupIds, omitGroupIds: filters.omitGroupIds, }; return this.apiClient.getAggregations(requestFilters).pipe(map(this.createAggregation)); } getVesselsData(filters: FiltersState, selectedAggregationDate: string) { const aggregationValue = getAggregationPeriodValue(filters.aggregationPeriodId); const earningsRequestParams: VesselsRequestParams = { poolId: filters.poolId, date: selectedAggregationDate, aggregate: aggregationValue, }; if (filters.poolPartnerAxCodes.length) { earningsRequestParams.partnerAxCodes = filters.poolPartnerAxCodes; } if (filters.groupIds.length) { earningsRequestParams.groupIds = filters.groupIds; } if (filters.omitGroupIds.length) { earningsRequestParams.omitGroupIds = filters.omitGroupIds; } return this.apiClient .getVesselsEarnings(earningsRequestParams) .pipe(map(this.mapVesselsEarnings)); } }