From 0488d9b81b454fd141424b030f27e8d8681dbabc Mon Sep 17 00:00:00 2001 From: Yoan VALLET <ext.sopra.yvallet@grandlyon.com> Date: Mon, 14 Sep 2020 18:51:37 +0200 Subject: [PATCH] feat: review fluidConfig service --- .../IndicatorContainerSwitcher.tsx | 2 +- .../KonnectorViewerContainer.tsx | 6 +-- .../Konnector/KonnectorForm.tsx | 5 +-- .../Konnector/KonnectorLoginForm.tsx | 5 +-- .../KonnectorViewer/KonnectorViewer.tsx | 5 +-- .../KonnectorViewer/KonnectorViewerCard.tsx | 5 +-- .../KonnectorViewer/KonnectorViewerList.tsx | 8 ++-- .../fluidConfig.model.ts} | 3 +- src/models/index.ts | 1 + .../consumptionPeriodSelectorService.ts | 12 ++++-- src/services/fluid.service.ts | 2 +- src/services/fluidConfig.service.ts | 25 +++++++++++ src/services/fluidConfigService.ts | 42 ------------------- src/services/konnectorStatusService.ts | 2 +- .../loadToCurrencyConverterService.ts | 4 +- src/services/triggers.service.ts | 4 +- 16 files changed, 57 insertions(+), 74 deletions(-) rename src/{services/IFluidConfig.ts => models/fluidConfig.model.ts} (69%) create mode 100644 src/services/fluidConfig.service.ts delete mode 100644 src/services/fluidConfigService.ts diff --git a/src/components/ContainerComponents/IndicatorsContainer/IndicatorContainerSwitcher.tsx b/src/components/ContainerComponents/IndicatorsContainer/IndicatorContainerSwitcher.tsx index 835686a75..7d9db61d6 100644 --- a/src/components/ContainerComponents/IndicatorsContainer/IndicatorContainerSwitcher.tsx +++ b/src/components/ContainerComponents/IndicatorsContainer/IndicatorContainerSwitcher.tsx @@ -6,7 +6,7 @@ import { ITimePeriod, } from 'services/dataConsumptionContracts' import { convertDateByTimeStep } from 'utils/date' -import FluidConfigService from 'services/fluidConfigService' +import FluidConfigService from 'services/fluidConfig.service' import FluidPerformanceIndicator from 'components/ContentComponents/PerformanceIndicator/FluidPerformanceIndicator' import KonnectorViewer from 'components/ContentComponents/KonnectorViewer/KonnectorViewer' diff --git a/src/components/ContainerComponents/KonnectorViewerContainer/KonnectorViewerContainer.tsx b/src/components/ContainerComponents/KonnectorViewerContainer/KonnectorViewerContainer.tsx index e9931fbc6..e3fe6d020 100644 --- a/src/components/ContainerComponents/KonnectorViewerContainer/KonnectorViewerContainer.tsx +++ b/src/components/ContainerComponents/KonnectorViewerContainer/KonnectorViewerContainer.tsx @@ -1,7 +1,7 @@ import React from 'react' import { translate } from 'cozy-ui/react/I18n' -import FluidConfigService from 'services/fluidConfigService' -import IFluidConfig from 'services/fluidConfigService' +import FluidConfigService from 'services/fluidConfig.service' +import { FluidConfig } from 'models' import KonnectorViewerList from 'components/ContentComponents/KonnectorViewer/KonnectorViewerList' interface KonnectorViewerContainerProps { @@ -13,7 +13,7 @@ const KonnectorViewerContainer: React.FC<KonnectorViewerContainerProps> = ({ isParam = false, t, }: KonnectorViewerContainerProps) => { - const fluidConfigs: IFluidConfig[] = new FluidConfigService().getFluidConfig() + const fluidConfigs: FluidConfig[] = new FluidConfigService().getFluidConfig() return ( <div className="kv-root"> <div className="kv-content"> diff --git a/src/components/ContentComponents/Konnector/KonnectorForm.tsx b/src/components/ContentComponents/Konnector/KonnectorForm.tsx index b27c1c845..3faeff9ed 100644 --- a/src/components/ContentComponents/Konnector/KonnectorForm.tsx +++ b/src/components/ContentComponents/Konnector/KonnectorForm.tsx @@ -1,14 +1,13 @@ import React from 'react' import { translate } from 'cozy-ui/react/I18n' -import IFluidConfig from 'services/fluidConfigService' -import { Konnector, Trigger } from 'doctypes' +import { Konnector, Trigger, FluidConfig } from 'models' import KonnectorLoginForm from 'components/ContentComponents/Konnector/KonnectorLoginForm' import KonnectorOAuthForm from 'components/ContentComponents/Konnector/KonnectorOAuthForm' interface KonnectorFormProps { - fluidConfig: IFluidConfig + fluidConfig: FluidConfig konnector: Konnector account: Account | null trigger: Trigger | null diff --git a/src/components/ContentComponents/Konnector/KonnectorLoginForm.tsx b/src/components/ContentComponents/Konnector/KonnectorLoginForm.tsx index 8ab2a1282..80b296cb3 100644 --- a/src/components/ContentComponents/Konnector/KonnectorLoginForm.tsx +++ b/src/components/ContentComponents/Konnector/KonnectorLoginForm.tsx @@ -2,10 +2,9 @@ import React, { useState, useEffect } from 'react' import { withClient, Client } from 'cozy-client' import { translate } from 'cozy-ui/react/I18n' -import { Account, AccountAuthData, Trigger } from 'models' +import { Account, AccountAuthData, Trigger, FluidConfig } from 'models' import AccountService from 'services/account.service' import ConnectionService from 'services/connection.service' -import IFluidConfig from 'services/fluidConfigService' import StyledIconButton from 'components/CommonKit/IconButton/StyledIconButton' import StyledButton from 'components/CommonKit/Button/StyledButton' @@ -17,7 +16,7 @@ import StyledAuthButton from 'components/CommonKit/Button/StyledAuthButton' import StyledIcon from 'components/CommonKit/Icon/StyledIcon' interface KonnectorLoginFormProps { - fluidConfig: IFluidConfig + fluidConfig: FluidConfig onSuccess: Function account: Account trigger: Trigger diff --git a/src/components/ContentComponents/KonnectorViewer/KonnectorViewer.tsx b/src/components/ContentComponents/KonnectorViewer/KonnectorViewer.tsx index e11cabe09..9d0d191da 100644 --- a/src/components/ContentComponents/KonnectorViewer/KonnectorViewer.tsx +++ b/src/components/ContentComponents/KonnectorViewer/KonnectorViewer.tsx @@ -1,14 +1,13 @@ import React, { useState, useEffect } from 'react' import { withClient, Client } from 'cozy-client' -import { Konnector } from 'models' +import { Konnector, FluidConfig } from 'models' import KonnectorService from 'services/konnector.service' import KonnectorViewerCard from 'components/ContentComponents/KonnectorViewer/KonnectorViewerCard' -import IFluidConfig from 'services/fluidConfigService' export interface KonnectorViewerProps { - fluidConfig: IFluidConfig + fluidConfig: FluidConfig client: Client isParam: boolean } diff --git a/src/components/ContentComponents/KonnectorViewer/KonnectorViewerCard.tsx b/src/components/ContentComponents/KonnectorViewer/KonnectorViewerCard.tsx index 98e5e6c6f..f949248f0 100644 --- a/src/components/ContentComponents/KonnectorViewer/KonnectorViewerCard.tsx +++ b/src/components/ContentComponents/KonnectorViewer/KonnectorViewerCard.tsx @@ -14,20 +14,19 @@ import StyledIcon from 'components/CommonKit/Icon/StyledIcon' import StyledIconButton from 'components/CommonKit/IconButton/StyledIconButton' import failurePicto from 'assets/png/picto/picto-failure.png' -import IFluidConfig from 'services/fluidConfigService' import KonnectorNotFound from 'components/ContentComponents/Konnector/KonnectorNotFound' import KonnectorForm from 'components/ContentComponents/Konnector/KonnectorForm' import KonnectorResult from 'components/ContentComponents/Konnector/KonnectorResult' import KonnectorLaunch from 'components/ContentComponents/Konnector/KonnectorLaunch' -import { Konnector, Trigger, TriggerState } from 'models' +import { Konnector, Trigger, TriggerState, FluidConfig } from 'models' import AccountService from 'services/account.service' import TriggerService from 'services/triggers.service' import { JobState } from 'enum/jobState.enum' interface KonnectorViewerCardProps { - fluidConfig: IFluidConfig + fluidConfig: FluidConfig konnector: Konnector client: Client isParam: boolean diff --git a/src/components/ContentComponents/KonnectorViewer/KonnectorViewerList.tsx b/src/components/ContentComponents/KonnectorViewer/KonnectorViewerList.tsx index dbfe1e7dd..3cf5f4893 100644 --- a/src/components/ContentComponents/KonnectorViewer/KonnectorViewerList.tsx +++ b/src/components/ContentComponents/KonnectorViewer/KonnectorViewerList.tsx @@ -1,11 +1,11 @@ import React, { useContext } from 'react' import KonnectorViewer from 'components/ContentComponents/KonnectorViewer/KonnectorViewer' -import IFluidConfig from 'services/fluidConfigService' +import { FluidConfig } from 'models' import { AppContext } from 'components/Contexts/AppContextProvider' export interface KonnectorViewerListProps { isParam: boolean - fluidConfigs: IFluidConfig[] + fluidConfigs: FluidConfig[] } const KonnectorViewerList: React.FC<KonnectorViewerListProps> = ({ @@ -15,12 +15,12 @@ const KonnectorViewerList: React.FC<KonnectorViewerListProps> = ({ const { fluidTypes } = useContext(AppContext) return ( <div> - {fluidConfigs.map((item: IFluidConfig, index: number) => { + {fluidConfigs.map((item: FluidConfig, index: number) => { return fluidTypes.includes(item.fluidTypeId) ? ( <KonnectorViewer isParam={isParam} key={index} fluidConfig={item} /> ) : null })} - {fluidConfigs.map((item: IFluidConfig, index: number) => { + {fluidConfigs.map((item: FluidConfig, index: number) => { return fluidTypes.includes(item.fluidTypeId) ? null : ( <KonnectorViewer isParam={isParam} key={index} fluidConfig={item} /> ) diff --git a/src/services/IFluidConfig.ts b/src/models/fluidConfig.model.ts similarity index 69% rename from src/services/IFluidConfig.ts rename to src/models/fluidConfig.model.ts index 9147fe93e..95fa4101b 100644 --- a/src/services/IFluidConfig.ts +++ b/src/models/fluidConfig.model.ts @@ -6,8 +6,7 @@ interface KonnectorConfig { cron?: string } -// eslint-disable-next-line @typescript-eslint/interface-name-prefix -export default interface IFluidConfig { +export interface FluidConfig { fluidTypeId: number name: string coefficient: number diff --git a/src/models/index.ts b/src/models/index.ts index e5120970c..8ee5577c6 100644 --- a/src/models/index.ts +++ b/src/models/index.ts @@ -1,4 +1,5 @@ export * from './account.model' +export * from './fluidConfig.model' export * from './konnector.model' export * from './trigger.model' export * from './userProfile.model' diff --git a/src/services/consumptionPeriodSelectorService.ts b/src/services/consumptionPeriodSelectorService.ts index 3ce691190..21f0d294b 100644 --- a/src/services/consumptionPeriodSelectorService.ts +++ b/src/services/consumptionPeriodSelectorService.ts @@ -2,10 +2,10 @@ import { DateTime } from 'luxon' import { FluidType } from 'enum/fluid.enum' import { ITimePeriod, TimeStep } from './dataConsumptionContracts' import Config from '../../config.json' -import IFluidConfig from 'services/fluidConfigService' +import { FluidConfig } from 'models' export default class ConsumptionPeriodSelector { - private readonly _dataDelayOffsetConfig: IFluidConfig[] + private readonly _dataDelayOffsetConfig: FluidConfig[] constructor() { this._dataDelayOffsetConfig = Config.fluidConfig @@ -108,7 +108,9 @@ export default class ConsumptionPeriodSelector { .minus({ days: 1 }) case TimeStep.MONTH: - return DateTime.local(date.year, date.month, 1).minus({ days: 1 }) + return DateTime.local(date.year, date.month, 1).minus({ + days: 1, + }) case TimeStep.YEAR: return DateTime.local(date.year, 1, 1).minus({ days: 1 }) @@ -166,7 +168,9 @@ export default class ConsumptionPeriodSelector { switch (timeStep) { case TimeStep.HALF_AN_HOUR || TimeStep.HOUR: comparisonTimePeriodStartDate = timePeriod.startDate.minus({ days: 1 }) - comparisonTimePeriodEndDate = timePeriod.endDate.minus({ days: 1 }) + comparisonTimePeriodEndDate = timePeriod.endDate.minus({ + days: 1, + }) break case TimeStep.DAY: diff --git a/src/services/fluid.service.ts b/src/services/fluid.service.ts index e527df36d..93e22c863 100644 --- a/src/services/fluid.service.ts +++ b/src/services/fluid.service.ts @@ -1,7 +1,7 @@ import { FluidType } from 'enum/fluid.enum' import { Client } from 'cozy-client' import { DateTime } from 'luxon' -import FluidConfigService from 'services/fluidConfigService' +import FluidConfigService from 'services/fluidConfig.service' import KonnectorService from 'services/konnector.service' import ConsumptionDataManager from 'services/consumptionDataManagerService' import AccountService from 'services/account.service' diff --git a/src/services/fluidConfig.service.ts b/src/services/fluidConfig.service.ts new file mode 100644 index 000000000..adb4de006 --- /dev/null +++ b/src/services/fluidConfig.service.ts @@ -0,0 +1,25 @@ +import Config from '../../config.json' +import { FluidConfig } from 'models' + +export default class FluidConfigService { + private readonly _fluidConfig: FluidConfig[] + + constructor() { + this._fluidConfig = Config.fluidConfig + } + + public getFluidConfig(): FluidConfig[] { + return this._fluidConfig + } + + /** + * + * @param min Minimum hour for cron + * @param max Maximum hour for cron + */ + public getCronArgs(min = 8, max = 9): string { + const randomHour = Math.floor(Math.random() * (max - min + 1) + min) + const randomMinutes = Math.floor(Math.random() * 59) + return `0 ${randomMinutes} ${randomHour} * * *` + } +} diff --git a/src/services/fluidConfigService.ts b/src/services/fluidConfigService.ts deleted file mode 100644 index a5c60c4aa..000000000 --- a/src/services/fluidConfigService.ts +++ /dev/null @@ -1,42 +0,0 @@ -import Config from '../../config.json' - -interface KonnectorConfig { - name: string - type: string - oauth: boolean - slug: string - cron?: string -} - -// eslint-disable-next-line @typescript-eslint/interface-name-prefix -export default interface IFluidConfig { - fluidTypeId: number - name: string - coefficient: number - dataDelayOffset: number - konnectorConfig: KonnectorConfig - siteLink: string -} - -export default class FluidConfig { - private readonly _fluidConfig: IFluidConfig[] - - constructor() { - this._fluidConfig = Config.fluidConfig - } - - public getFluidConfig(): IFluidConfig[] { - return this._fluidConfig - } - - /** - * - * @param min Minimum hour for cron - * @param max Maximum hour for cron - */ - public getCronArgs(min = 8, max = 9): string { - const randomHour = Math.floor(Math.random() * (max - min + 1) + min) - const randomMinutes = Math.floor(Math.random() * 59) - return `0 ${randomMinutes} ${randomHour} * * *` - } -} diff --git a/src/services/konnectorStatusService.ts b/src/services/konnectorStatusService.ts index d8229dc62..823c77dcc 100644 --- a/src/services/konnectorStatusService.ts +++ b/src/services/konnectorStatusService.ts @@ -1,5 +1,5 @@ import { Client } from 'cozy-client' -import FluidConfigService from 'services/fluidConfigService' +import FluidConfigService from 'services/fluidConfig.service' import AccountService from 'services/account.service' import { FluidType } from 'enum/fluid.enum' diff --git a/src/services/loadToCurrencyConverterService.ts b/src/services/loadToCurrencyConverterService.ts index 6299d8081..fd7506204 100644 --- a/src/services/loadToCurrencyConverterService.ts +++ b/src/services/loadToCurrencyConverterService.ts @@ -1,9 +1,9 @@ import { FluidType } from 'enum/fluid.enum' import Config from '../../config.json' -import IFluidConfig from 'services/fluidConfigService' +import { FluidConfig } from 'models' export default class LoadToCurrencyConverter { - private readonly _fluidConfig: IFluidConfig[] + private readonly _fluidConfig: FluidConfig[] constructor() { this._fluidConfig = Config.fluidConfig diff --git a/src/services/triggers.service.ts b/src/services/triggers.service.ts index 453db6908..ed4ba5d5f 100644 --- a/src/services/triggers.service.ts +++ b/src/services/triggers.service.ts @@ -8,7 +8,7 @@ import { } from 'models' import { buildAttributes } from 'cozy-harvest-lib/dist/helpers/triggers' import triggersMutations from 'cozy-harvest-lib/dist/connections/triggers' -import FluidConfig from 'services/fluidConfigService' +import FluidConfigService from 'services/fluidConfig.service' export default class TriggerService { private _client: Client @@ -18,7 +18,7 @@ export default class TriggerService { } private createTriggerAttributes(account: Account, konnector: Konnector) { - const fluidConfigService = new FluidConfig() + const fluidConfigService = new FluidConfigService() const cronArgs = fluidConfigService.getCronArgs() const triggerAttributes: TriggerAttributes = buildAttributes({ account: account, -- GitLab