diff --git a/src/components/Analysis/AnalysisConsumption.tsx b/src/components/Analysis/AnalysisConsumption.tsx index 1ab5dfc159c5803aada7323327e1726940452290..acc8cd7c3a1f662dded519795197469327f2c884 100644 --- a/src/components/Analysis/AnalysisConsumption.tsx +++ b/src/components/Analysis/AnalysisConsumption.tsx @@ -76,7 +76,7 @@ const AnalysisConsumption: React.FC<AnalysisConsumptionProps> = ({ async function loadAverageComsumption() { const profileTypeEntityService = new ProfileTypeEntityService(client) const profileType: ProfileType | null = await profileTypeEntityService.getProfileType( - analysisDate.plus({ month: -1 }) + analysisDate.minus({ month: 1 }).startOf('month') ) if (profileType !== null) { const profileTypeService: ProfileTypeService = new ProfileTypeService( diff --git a/src/components/ProfileType/ProfileTypeFinished.tsx b/src/components/ProfileType/ProfileTypeFinished.tsx index fa1a89fd95cd35c77ccc6793ffee330785797518..c130bc8649637ddd0d2a11a0ee12eca8aafdd01f 100644 --- a/src/components/ProfileType/ProfileTypeFinished.tsx +++ b/src/components/ProfileType/ProfileTypeFinished.tsx @@ -12,10 +12,13 @@ import finishIcon from 'assets/icons/visu/profileType/finish.svg' import ProfileTypeService from 'services/profileType.service' import useExploration from 'components/Hooks/useExploration' import { AppStore } from 'store' +import { TimePeriod } from 'models' import { UserExplorationID } from 'enum/userExploration.enum' import { UsageEventType } from 'enum/usageEvent.enum' import { useClient } from 'cozy-client' import UsageEventService from 'services/usageEvent.service' +import ProfileTypeEntityService from 'services/profileTypeEntity.service' +import { DateTime } from 'luxon' interface ProfileTypeFinishedProps { profileType: ProfileType @@ -37,13 +40,53 @@ const ProfileTypeFinished: React.FC<ProfileTypeFinishedProps> = ({ const [isSaved, setIsSaved] = useState<boolean>(false) const [, setValidExploration] = useExploration() + const profile = useSelector((state: AppStore) => state.ecolyo.profile) useEffect(() => { - if (!isSaved) { - const consistentProfileType = ProfileTypeService.checkConsistency( + async function checkForExistingProfileType() { + const consistentProfileType: ProfileType = ProfileTypeService.checkConsistency( profileType ) - setIsSaved(true) + const chosenPeriod: TimePeriod = { + startDate: profileType.updateDate.setZone('utc', { + keepLocalTime: true, + }), + endDate: DateTime.local().setZone('utc', { + keepLocalTime: true, + }), + } + const profileTypeEntityService = new ProfileTypeEntityService(client) + const myProfileTypes: + | ProfileType[] + | null = await profileTypeEntityService.getAllProfileTypes(chosenPeriod) + if (myProfileTypes !== null) { + const destroyPT = await profileTypeEntityService.deleteProfileTypes( + myProfileTypes + ) + if (destroyPT) { + dispatch(newProfileTypeEntry(consistentProfileType)) + setIsSaved(true) + dispatch( + updateProfile({ + isProfileTypeCompleted: true, + }) + ) + } else { + console.log('ERROR') + } + } else { + dispatch(newProfileTypeEntry(consistentProfileType)) + setIsSaved(true) + dispatch( + updateProfile({ + isProfileTypeCompleted: true, + }) + ) + } + } + + if (!isSaved) { + checkForExistingProfileType() if ( currentChallenge && currentChallenge.exploration.id === UserExplorationID.EXPLORATION001 @@ -53,18 +96,12 @@ const ProfileTypeFinished: React.FC<ProfileTypeFinishedProps> = ({ UsageEventService.addEvent(client, { type: UsageEventType.PROFILE_SET_EVENT, }) - - dispatch( - updateProfile({ - isProfileTypeCompleted: true, - }) - ) - dispatch(newProfileTypeEntry(consistentProfileType)) } }, [ dispatch, profileType, isSaved, + profile.isProfileTypeCompleted, currentChallenge, setValidExploration, client, diff --git a/src/components/ProfileType/ProfileTypeFormDateSelection.tsx b/src/components/ProfileType/ProfileTypeFormDateSelection.tsx new file mode 100644 index 0000000000000000000000000000000000000000..356be9445de3d2cd836d80c15c976f4d812708e1 --- /dev/null +++ b/src/components/ProfileType/ProfileTypeFormDateSelection.tsx @@ -0,0 +1,249 @@ +/* eslint-disable @typescript-eslint/no-explicit-any */ + +import React, { useCallback, useEffect, useState } from 'react' +import 'components/ProfileType/profileTypeForm.scss' +import { useI18n } from 'cozy-ui/transpiled/react/I18n' +import ProfileTypeProgress from 'components/ProfileType/ProfileTypeProgress' +import ProfileTypeNavigation from 'components/ProfileType/ProfileTypeNavigation' +import { ProfileTypeStepForm } from 'enum/profileType.enum' +import { + ProfileType, + ProfileTypeAnswer, + ProfileTypeAnswerChoices, +} from 'models/profileType.model' +import { DateTime } from 'luxon' +import { MenuItem, Select } from '@material-ui/core' + +interface ProfileTypeFormDateSelectionProps { + step: ProfileTypeStepForm + viewedStep: ProfileTypeStepForm + profileType: ProfileType + answerType: ProfileTypeAnswer + setNextStep: Function + setPrevioustStep: Function + isProfileTypeComplete: boolean +} + +interface SelectionMonth { + label: string + value: number +} + +const ProfileTypeFormDateSelection: React.FC<ProfileTypeFormDateSelectionProps> = ({ + step, + viewedStep, + profileType, + answerType, + setNextStep, + setPrevioustStep, + isProfileTypeComplete, +}: ProfileTypeFormDateSelectionProps) => { + const { t } = useI18n() + const [selectedMonth, setSelectedMonth] = useState<any>({ + label: DateTime.now().toLocaleString({ month: 'long' }), + value: DateTime.now().month, // Date.getMonth starts at 0 + }) + const [selectedYear, setSelectedYear] = useState<number>(DateTime.now().year) + const [answer, setAnswer] = useState<ProfileTypeAnswerChoices>('') + + const selectMonths = [ + { + label: 'Janvier', + value: '01', + }, + { + label: 'Février', + value: '02', + }, + { + label: 'Mars', + value: '03', + }, + { + label: 'Avril', + value: '04', + }, + { + label: 'Mai', + value: '05', + }, + { + label: 'Juin', + value: '06', + }, + { + label: 'Juillet', + value: '07', + }, + { + label: 'Aout', + value: '08', + }, + { + label: 'Septembre', + value: '09', + }, + { + label: 'Octobre', + value: '10', + }, + { + label: 'Novembre', + value: '11', + }, + { + label: 'Décembre', + value: '12', + }, + ] + + const selectYears = [] + const curYear = DateTime.now().year + const limitYears = curYear - 10 + + for (let i = curYear; i >= limitYears; i--) { + selectYears.push(i) + } + + function getMonthFullName(month: number) { + switch (month) { + case 1: + return 'Janvier' + case 2: + return 'Février' + case 3: + return 'Mars' + case 4: + return 'Avril' + case 5: + return 'Mai' + case 6: + return 'Juin' + case 7: + return 'Juillet' + case 8: + return 'Aout' + case 9: + return 'Septembre' + case 10: + return 'Octobre' + case 11: + return 'Novembre' + case 12: + return 'Décembre' + default: + return null + } + } + + const handlePrevious = useCallback(() => { + setPrevioustStep(profileType) + }, [profileType, setPrevioustStep]) + + const handleNext = useCallback(() => { + profileType[answerType.attribute] = answer + setNextStep(profileType) + }, [profileType, setNextStep, answer, answerType.attribute]) + + function handleSelectMonth(event: any) { + setSelectedMonth({ + value: event.target.value, + label: getMonthFullName(parseInt(event.target.value)), + }) + const isoString: string = selectedYear + '-' + selectedMonth.value + '-01' + setAnswer(DateTime.fromISO(isoString)) + } + + function handleSelectYear(event: any) { + setSelectedYear(parseInt(event.target.value)) + const isoString: string = selectedYear + '-' + selectedMonth.value + '-01' + setAnswer(DateTime.fromISO(isoString)) + } + + useEffect(() => { + if (step < viewedStep || isProfileTypeComplete) { + const isoString: string = selectedYear + '-' + selectedMonth.value + '-01' + setAnswer(DateTime.fromISO(isoString)) + profileType[answerType.attribute] = DateTime.fromISO(isoString) + } + }, [ + step, + viewedStep, + profileType, + answerType, + isProfileTypeComplete, + selectedYear, + selectedMonth.value, + ]) + + return ( + <> + <div className={'profile-form-container'}> + <ProfileTypeProgress step={step} /> + <div className={'profile-question-label'}> + {t( + `profile_type.${ProfileTypeStepForm[step].toLowerCase()}.question` + )} + </div> + {answer !== null ? ( + <div className="select-container"> + <div className="date-select"> + <Select + native={false} + labelId="selectMonthDate" + className="month" + defaultValue={selectedMonth.value} + onChange={e => handleSelectMonth(e)} + > + {/* if current year, only show past and present months else show full months */} + {selectedYear === DateTime.now().year + ? selectMonths + .slice(0, DateTime.now().month) + .map((month, key) => ( + <MenuItem + value={month.value} + key={key} + className="date-option" + > + {month.label} + </MenuItem> + )) + : selectMonths.map((month, key) => ( + <MenuItem + value={month.value} + key={key} + className="date-option" + > + {month.label} + </MenuItem> + ))} + </Select> + </div> + <div className="date-select"> + <Select + labelId="selectYearDate" + className="year" + defaultValue={selectedYear} + onChange={e => handleSelectYear(e)} + > + {selectYears.map((year, key) => ( + <MenuItem value={year} key={key} className="date-option"> + {year} + </MenuItem> + ))} + </Select> + </div> + </div> + ) : null} + </div> + <ProfileTypeNavigation + step={step} + handlePrevious={handlePrevious} + handleNext={handleNext} + disableNextButton={answer === ''} + /> + </> + ) +} + +export default ProfileTypeFormDateSelection diff --git a/src/components/ProfileType/ProfileTypeNavigation.tsx b/src/components/ProfileType/ProfileTypeNavigation.tsx index 20e467db5af6675d3353c2fc95a6ba1feece66de..d7011eedebcacdb47ffe2520a40583ed25766c7d 100644 --- a/src/components/ProfileType/ProfileTypeNavigation.tsx +++ b/src/components/ProfileType/ProfileTypeNavigation.tsx @@ -43,7 +43,7 @@ const ProfileTypeNavigation: React.FC<ProfileTypeNavigationProps> = ({ </Button> <Button aria-label={ - step === ProfileTypeStepForm.COOKING_FLUID + step === ProfileTypeStepForm.UPDATE_DATE ? t('profile_type.accessibility.button_end') : t('profile_type.accessibility.button_next') } @@ -57,7 +57,7 @@ const ProfileTypeNavigation: React.FC<ProfileTypeNavigationProps> = ({ label: 'text-16-normal', }} > - {step === ProfileTypeStepForm.COOKING_FLUID + {step === ProfileTypeStepForm.UPDATE_DATE ? t('profile_type.form.button_end') : `${t('profile_type.form.button_next')} >`} </Button> diff --git a/src/components/ProfileType/ProfileTypeView.tsx b/src/components/ProfileType/ProfileTypeView.tsx index 6ca59dd4ee963c5b6a049d80902d42d231dafd90..4b6d4c614fadea77f2c5e5feff0c40639061bfd6 100644 --- a/src/components/ProfileType/ProfileTypeView.tsx +++ b/src/components/ProfileType/ProfileTypeView.tsx @@ -21,14 +21,14 @@ import { } from 'enum/profileType.enum' import { FluidType } from 'enum/fluid.enum' import { ProfileTypeStepForm } from 'enum/profileType.enum' -import ProfileTypeService from 'services/profileType.service' import ProfileTypeFormMultiChoice from 'components/ProfileType/ProfileTypeFormMultiChoice' import ProfileTypeFormNumber from 'components/ProfileType/ProfileTypeFormNumber' import ProfileTypeFormNumberSelection from 'components/ProfileType/ProfileTypeFormNumberSelection' import { useSelector } from 'react-redux' import { AppStore } from 'store' import { DateTime } from 'luxon' -import ProfileTypeFormService from 'services/profileTypeForm' +import ProfileTypeFormService from 'services/profileTypeForm.service' +import ProfileTypeFormDateSelection from './ProfileTypeFormDateSelection' const ProfileTypeView = () => { const profile = useSelector((state: AppStore) => state.ecolyo.profile) @@ -37,9 +37,11 @@ const ProfileTypeView = () => { ) const [headerHeight, setHeaderHeight] = useState<number>(0) const [profileType, setProfileType] = useState<ProfileType>({ - updateDate: DateTime.local().setZone('utc', { - keepLocalTime: true, - }), + updateDate: DateTime.local() + .setZone('utc', { + keepLocalTime: true, + }) + .startOf('month'), housingType: HousingType.INDIVIDUAL_HOUSE, constructionYear: ConstructionYear.BETWEEN_1975_AND_1989, area: '0', @@ -65,6 +67,7 @@ const ProfileTypeView = () => { attribute: '', choices: [], }) + const [isLoading, setIsLoading] = useState<boolean>(true) const [viewedStep, setViewedStep] = useState<number>(-1) @@ -77,7 +80,8 @@ const ProfileTypeView = () => { setProfileType(_profileType) const profileTypeFormService = new ProfileTypeFormService(_profileType) const nextStep: ProfileTypeStepForm = profileTypeFormService.getNextFormStep( - step + step, + !profile.isProfileTypeCompleted ) setIsLoading(true) if (nextStep > viewedStep) { @@ -92,11 +96,11 @@ const ProfileTypeView = () => { (_profileType: ProfileType) => { setProfileType(_profileType) const profileTypeFormService = new ProfileTypeFormService(_profileType) - const nextStep: ProfileTypeStepForm = profileTypeFormService.getPreviousFormStep( + const previousStep: ProfileTypeStepForm = profileTypeFormService.getPreviousFormStep( step ) setIsLoading(true) - setStep(nextStep) + setStep(previousStep) }, [step] ) @@ -150,6 +154,18 @@ const ProfileTypeView = () => { setPrevioustStep={setPrevioustStep} /> ) + } else if (answerType.type === ProfileTypeFormType.DATE_SELECTION) { + return ( + <ProfileTypeFormDateSelection + step={step} + viewedStep={viewedStep} + profileType={profileType} + answerType={answerType} + setNextStep={setNextStep} + isProfileTypeComplete={profile.isProfileTypeCompleted} + setPrevioustStep={setPrevioustStep} + /> + ) } } @@ -157,7 +173,7 @@ const ProfileTypeView = () => { if (profile.isProfileTypeCompleted) { setProfileType(curProfileType) } - const _answerType: ProfileTypeAnswer = ProfileTypeService.getAnswerForStep( + const _answerType: ProfileTypeAnswer = ProfileTypeFormService.getAnswerForStep( step ) setAnswerType(_answerType) diff --git a/src/components/ProfileType/profileTypeForm.scss b/src/components/ProfileType/profileTypeForm.scss index 8870720e318a1f289e4c37f2287819c08bae1c1d..1e4f2bba05765309f76c2c6b679bb3f5d727a828 100644 --- a/src/components/ProfileType/profileTypeForm.scss +++ b/src/components/ProfileType/profileTypeForm.scss @@ -1,5 +1,6 @@ @import '../../styles/base/color'; @import '../../styles/base/breakpoint'; +@import '../../styles/base/typo-variables'; .profile-form-container { color: $white; @@ -152,3 +153,74 @@ button:disabled { opacity: 0.5; } +.date-select { + margin: 0.5em; + border: 1px solid $gold-shadow; + background: $dark-light-2; + font-weight: bold; + .MuiInput-underline:after { + display: none; + } + .year { + text-align: center; + display: inline-flex; + align-content: center; + font-size: 1.25rem; + font-family: $text-font; + color: $white; + width: 93px; + svg { + top: 0; + background-color: $gold-shadow; + height: 100%; + } + } + .month { + color: $white; + font-size: 1.25rem; + text-align: center; + display: inline-flex; + align-content: center; + font-family: $text-font; + min-width: 130px; + max-width: 150px; + svg { + top: 0; + right: 0; + background-color: $gold-shadow; + height: 100%; + } + } +} + +.select-container { + display: flex; +} + +.date-option { + color: $white; +} + +ul { + background: linear-gradient(180deg, #323339 0%, #25262b 100%); + color: $white; + font-weight: normal; + .MuiMenuItem-root { + font-family: $text-font; + text-align: center; + font-size: 1.25rem; + display: flex; + justify-content: space-evenly; + :hover { + background-color: $gold-shadow; + } + } + .MuiListItem-root.Mui-selected, + .MuiListItem-root.Mui-selected:hover { + background-color: $gold-shadow; + color: $dark-2; + font-weight: bold; + display: flex; + justify-content: space-evenly; + } +} diff --git a/src/enum/profileType.enum.ts b/src/enum/profileType.enum.ts index d20224f37b1524b64d1c5623975cf02a3df821eb..78e544a53f0393cb53cb121059dafbaddf76d422 100644 --- a/src/enum/profileType.enum.ts +++ b/src/enum/profileType.enum.ts @@ -73,7 +73,8 @@ export enum ProfileTypeStepForm { HOT_WATER_FLUID = 13, HOT_WATER_EQUIPMENT = 14, COOKING_FLUID = 15, - END = 16, + UPDATE_DATE = 16, + END = 17, } export enum ProfileTypeFormType { @@ -81,4 +82,5 @@ export enum ProfileTypeFormType { MULTI_CHOICE = 1, NUMBER_SELECTION = 2, NUMBER = 3, + DATE_SELECTION = 4, } diff --git a/src/locales/fr.json b/src/locales/fr.json index fc636f574f2d366384610710789a73c945976703..e85ef82d5fe4520b5a67933994a674dd30d57786 100644 --- a/src/locales/fr.json +++ b/src/locales/fr.json @@ -853,6 +853,10 @@ "0": "Électricité", "2": "Gaz" }, + "update_date": { + "title": "Date de prise d'effet", + "question": "A partir de quelle date souhaitez-vous que ce nouveau profil soit pris en compte dans l'analyse de vos données ?" + }, "fluidType": { "0": "Électricité", "1": "Eau", diff --git a/src/migrations/migration.data.ts b/src/migrations/migration.data.ts index 2a520874d8f68f88644ef054286621230e2e9794..08c343058f7c06f7e89c9c9ef104b70adaf24b8f 100644 --- a/src/migrations/migration.data.ts +++ b/src/migrations/migration.data.ts @@ -232,4 +232,36 @@ export const migrations: Migration[] = [ }) }, }, + { + baseSchemaVersion: 7, + targetSchemaVersion: 8, + appVersion: '1.5.0', + description: + 'ProfileTypes start now at the begining of the month, no duplications can exist over the same month.', + releaseNotes: null, + docTypes: PROFILETYPE_DOCTYPE, + run: async (_client: Client, docs: any[]): Promise<any[]> => { + function checkDate(d1, d2) { + const dtd1: DateTime = DateTime.fromISO(d1) + const dtd2: DateTime = DateTime.fromISO(d2) + return dtd1.year === dtd2.year && dtd1.month === dtd2.month + } + + for (let i = 0; i < docs.length; i++) { + const dtStartOfMonth: DateTime = DateTime.fromISO(docs[i].updateDate) + docs[i].updateDate = dtStartOfMonth + .setZone('utc', { + keepLocalTime: true, + }) + .startOf('month') + if ( + docs[i + 1] && + checkDate(docs[i].updateDate, docs[i + 1].updateDate) + ) { + docs[i].deleteAction = true + } + } + return docs + }, + }, ] diff --git a/src/models/profileType.model.ts b/src/models/profileType.model.ts index 4fbe2c974c1f2eddfb30fa8770dcaee30f3a63f5..33584fddb2c01e8fbb8174b9fc61dd8f7304d71c 100644 --- a/src/models/profileType.model.ts +++ b/src/models/profileType.model.ts @@ -31,7 +31,6 @@ interface ProfileTypeIndexableTypes { | null } export interface ProfileType extends ProfileTypeIndexableTypes { - updateDate: DateTime | null housingType: HousingType constructionYear: ConstructionYear area: string @@ -48,6 +47,7 @@ export interface ProfileType extends ProfileTypeIndexableTypes { warmingFluid: WarmingType | null hotWaterFluid: FluidType | null cookingFluid: FluidType + updateDate: DateTime } export interface MonthlyForecast { @@ -81,6 +81,7 @@ export type ProfileTypeAnswerChoices = | ThreeChoicesAnswer | HotWaterEquipment | FluidType + | DateTime | null export interface ProfileTypeAnswer { type: ProfileTypeFormType diff --git a/src/services/profileType.service.spec.ts b/src/services/profileType.service.spec.ts index 03a12f9ba4efbc181fe4fa9f42cf1da8282edd98..892f770474686124727ec81a9725a9078be2cf58 100644 --- a/src/services/profileType.service.spec.ts +++ b/src/services/profileType.service.spec.ts @@ -38,6 +38,7 @@ import { import ProfileTypeService from './profileType.service' import mockClient from '../../tests/__mocks__/client' import { DateTime } from 'luxon' +import ProfileTypeFormService from './profileTypeForm.service' const wrongNumber = 99999 describe('ProfileType service', () => { @@ -47,6 +48,8 @@ describe('ProfileType service', () => { DateTime.now().year ) + const profileTypeFormService = new ProfileTypeFormService(mockProfileType) + describe('calculateWarmingEstimatedConsumption', () => { it('should calculate the Warming Estimated Consumption', () => { const estimatedConsumption = profileTypeService.calculateWarmingEstimatedConsumption() @@ -253,189 +256,221 @@ describe('ProfileType service', () => { }) describe('getNextFormStep', () => { it('should get the next step in function of the current step', () => { - let nextStep = profileTypeService.getNextFormStep( - ProfileTypeStepForm.HOUSING_TYPE + let nextStep = profileTypeFormService.getNextFormStep( + ProfileTypeStepForm.HOUSING_TYPE, + false ) expect(nextStep).toEqual(ProfileTypeStepForm.AREA) - nextStep = profileTypeService.getNextFormStep(ProfileTypeStepForm.AREA) + nextStep = profileTypeFormService.getNextFormStep( + ProfileTypeStepForm.AREA, + false + ) expect(nextStep).toEqual(ProfileTypeStepForm.OCCUPANTS_NUMBER) - nextStep = profileTypeService.getNextFormStep( - ProfileTypeStepForm.OCCUPANTS_NUMBER + nextStep = profileTypeFormService.getNextFormStep( + ProfileTypeStepForm.OCCUPANTS_NUMBER, + false ) expect(nextStep).toEqual(ProfileTypeStepForm.CONSTRUCTION_YEAR) - nextStep = profileTypeService.getNextFormStep( - ProfileTypeStepForm.CONSTRUCTION_YEAR + nextStep = profileTypeFormService.getNextFormStep( + ProfileTypeStepForm.CONSTRUCTION_YEAR, + false ) expect(nextStep).toEqual(ProfileTypeStepForm.OUTSIDE_FACING_WALLS) - nextStep = profileTypeService.getNextFormStep( - ProfileTypeStepForm.OUTSIDE_FACING_WALLS + nextStep = profileTypeFormService.getNextFormStep( + ProfileTypeStepForm.OUTSIDE_FACING_WALLS, + false ) expect(nextStep).toEqual(ProfileTypeStepForm.FLOOR) - nextStep = profileTypeService.getNextFormStep(ProfileTypeStepForm.FLOOR) + nextStep = profileTypeFormService.getNextFormStep( + ProfileTypeStepForm.FLOOR, + false + ) expect(nextStep).toEqual(ProfileTypeStepForm.HEATING) - nextStep = profileTypeService.getNextFormStep(ProfileTypeStepForm.HEATING) + nextStep = profileTypeFormService.getNextFormStep( + ProfileTypeStepForm.HEATING, + false + ) expect(nextStep).toEqual(ProfileTypeStepForm.WARMING_FLUID) - nextStep = profileTypeService.getNextFormStep( - ProfileTypeStepForm.WARMING_FLUID + nextStep = profileTypeFormService.getNextFormStep( + ProfileTypeStepForm.WARMING_FLUID, + false ) expect(nextStep).toEqual(ProfileTypeStepForm.VENTILATION) - nextStep = profileTypeService.getNextFormStep( - ProfileTypeStepForm.VENTILATION + nextStep = profileTypeFormService.getNextFormStep( + ProfileTypeStepForm.VENTILATION, + false ) expect(nextStep).toEqual(ProfileTypeStepForm.HEATER_REPLACEMENT) - nextStep = profileTypeService.getNextFormStep( - ProfileTypeStepForm.HEATER_REPLACEMENT + nextStep = profileTypeFormService.getNextFormStep( + ProfileTypeStepForm.HEATER_REPLACEMENT, + false ) expect(nextStep).toEqual(ProfileTypeStepForm.COLD_WATER) - nextStep = profileTypeService.getNextFormStep( - ProfileTypeStepForm.COLD_WATER + nextStep = profileTypeFormService.getNextFormStep( + ProfileTypeStepForm.COLD_WATER, + false ) expect(nextStep).toEqual(ProfileTypeStepForm.HOT_WATER) - nextStep = profileTypeService.getNextFormStep( - ProfileTypeStepForm.HOT_WATER + nextStep = profileTypeFormService.getNextFormStep( + ProfileTypeStepForm.HOT_WATER, + false ) expect(nextStep).toEqual(ProfileTypeStepForm.HOT_WATER_FLUID) - nextStep = profileTypeService.getNextFormStep( - ProfileTypeStepForm.HOT_WATER_FLUID + nextStep = profileTypeFormService.getNextFormStep( + ProfileTypeStepForm.HOT_WATER_FLUID, + false ) expect(nextStep).toEqual(ProfileTypeStepForm.HOT_WATER_EQUIPMENT) - nextStep = profileTypeService.getNextFormStep( - ProfileTypeStepForm.HOT_WATER_EQUIPMENT + nextStep = profileTypeFormService.getNextFormStep( + ProfileTypeStepForm.HOT_WATER_EQUIPMENT, + false ) expect(nextStep).toEqual(ProfileTypeStepForm.COOKING_FLUID) - nextStep = profileTypeService.getNextFormStep( - ProfileTypeStepForm.COOKING_FLUID + nextStep = profileTypeFormService.getNextFormStep( + ProfileTypeStepForm.COOKING_FLUID, + false + ) + expect(nextStep).toEqual(ProfileTypeStepForm.UPDATE_DATE) + nextStep = profileTypeFormService.getNextFormStep( + ProfileTypeStepForm.UPDATE_DATE, + false ) expect(nextStep).toEqual(ProfileTypeStepForm.END) - nextStep = profileTypeService.getNextFormStep(wrongNumber) + nextStep = profileTypeFormService.getNextFormStep(wrongNumber, false) expect(nextStep).toEqual(ProfileTypeStepForm.HOUSING_TYPE) }) }) describe('getPreviousFormStep', () => { it('should get the previus step in function of the current step', () => { - let previousStep = profileTypeService.getPreviousFormStep( + let previousStep = profileTypeFormService.getPreviousFormStep( ProfileTypeStepForm.AREA ) expect(previousStep).toEqual(ProfileTypeStepForm.HOUSING_TYPE) - previousStep = profileTypeService.getPreviousFormStep( + previousStep = profileTypeFormService.getPreviousFormStep( ProfileTypeStepForm.OCCUPANTS_NUMBER ) expect(previousStep).toEqual(ProfileTypeStepForm.AREA) - previousStep = profileTypeService.getPreviousFormStep( + previousStep = profileTypeFormService.getPreviousFormStep( ProfileTypeStepForm.CONSTRUCTION_YEAR ) expect(previousStep).toEqual(ProfileTypeStepForm.OCCUPANTS_NUMBER) - previousStep = profileTypeService.getPreviousFormStep( + previousStep = profileTypeFormService.getPreviousFormStep( ProfileTypeStepForm.OUTSIDE_FACING_WALLS ) expect(previousStep).toEqual(ProfileTypeStepForm.CONSTRUCTION_YEAR) - previousStep = profileTypeService.getPreviousFormStep( + previousStep = profileTypeFormService.getPreviousFormStep( ProfileTypeStepForm.FLOOR ) expect(previousStep).toEqual(ProfileTypeStepForm.OUTSIDE_FACING_WALLS) - previousStep = profileTypeService.getPreviousFormStep( + previousStep = profileTypeFormService.getPreviousFormStep( ProfileTypeStepForm.HEATING ) expect(previousStep).toEqual(ProfileTypeStepForm.FLOOR) - previousStep = profileTypeService.getPreviousFormStep( + previousStep = profileTypeFormService.getPreviousFormStep( ProfileTypeStepForm.WARMING_FLUID ) expect(previousStep).toEqual(ProfileTypeStepForm.HEATING) - previousStep = profileTypeService.getPreviousFormStep( + previousStep = profileTypeFormService.getPreviousFormStep( ProfileTypeStepForm.INDIVIDUAL_INSULATION_WORK ) expect(previousStep).toEqual(ProfileTypeStepForm.WARMING_FLUID) - previousStep = profileTypeService.getPreviousFormStep( + previousStep = profileTypeFormService.getPreviousFormStep( ProfileTypeStepForm.VENTILATION ) expect(previousStep).toEqual(ProfileTypeStepForm.WARMING_FLUID) - previousStep = profileTypeService.getPreviousFormStep( + previousStep = profileTypeFormService.getPreviousFormStep( ProfileTypeStepForm.HEATER_REPLACEMENT ) expect(previousStep).toEqual(ProfileTypeStepForm.VENTILATION) - previousStep = profileTypeService.getPreviousFormStep( + previousStep = profileTypeFormService.getPreviousFormStep( ProfileTypeStepForm.HOT_WATER ) expect(previousStep).toEqual(ProfileTypeStepForm.COLD_WATER) - previousStep = profileTypeService.getPreviousFormStep( + previousStep = profileTypeFormService.getPreviousFormStep( ProfileTypeStepForm.HOT_WATER_FLUID ) expect(previousStep).toEqual(ProfileTypeStepForm.HOT_WATER) - previousStep = profileTypeService.getPreviousFormStep( + previousStep = profileTypeFormService.getPreviousFormStep( ProfileTypeStepForm.HOT_WATER_EQUIPMENT ) expect(previousStep).toEqual(ProfileTypeStepForm.HOT_WATER_FLUID) - previousStep = profileTypeService.getPreviousFormStep( + previousStep = profileTypeFormService.getPreviousFormStep( ProfileTypeStepForm.COOKING_FLUID ) expect(previousStep).toEqual(ProfileTypeStepForm.HOT_WATER_EQUIPMENT) - previousStep = profileTypeService.getPreviousFormStep(wrongNumber) + previousStep = profileTypeFormService.getPreviousFormStep(wrongNumber) expect(previousStep).toEqual(ProfileTypeStepForm.HOUSING_TYPE) }) }) describe('getAnswerForStep', () => { it('should get the good answers in function of the current step', () => { - let answers = ProfileTypeService.getAnswerForStep( + let answers = ProfileTypeFormService.getAnswerForStep( ProfileTypeStepForm.HOUSING_TYPE ) expect(answers).toEqual(mockProfileTypeAnswers[0]) - answers = ProfileTypeService.getAnswerForStep(ProfileTypeStepForm.AREA) + answers = ProfileTypeFormService.getAnswerForStep( + ProfileTypeStepForm.AREA + ) expect(answers).toEqual(mockProfileTypeAnswers[1]) - answers = ProfileTypeService.getAnswerForStep( + answers = ProfileTypeFormService.getAnswerForStep( ProfileTypeStepForm.OCCUPANTS_NUMBER ) expect(answers).toEqual(mockProfileTypeAnswers[2]) - answers = ProfileTypeService.getAnswerForStep( + answers = ProfileTypeFormService.getAnswerForStep( ProfileTypeStepForm.CONSTRUCTION_YEAR ) expect(answers).toEqual(mockProfileTypeAnswers[3]) - answers = ProfileTypeService.getAnswerForStep( + answers = ProfileTypeFormService.getAnswerForStep( ProfileTypeStepForm.OUTSIDE_FACING_WALLS ) expect(answers).toEqual(mockProfileTypeAnswers[4]) - answers = ProfileTypeService.getAnswerForStep(ProfileTypeStepForm.FLOOR) + answers = ProfileTypeFormService.getAnswerForStep( + ProfileTypeStepForm.FLOOR + ) expect(answers).toEqual(mockProfileTypeAnswers[5]) - answers = ProfileTypeService.getAnswerForStep(ProfileTypeStepForm.HEATING) + answers = ProfileTypeFormService.getAnswerForStep( + ProfileTypeStepForm.HEATING + ) expect(answers).toEqual(mockProfileTypeAnswers[6]) - answers = ProfileTypeService.getAnswerForStep( + answers = ProfileTypeFormService.getAnswerForStep( ProfileTypeStepForm.WARMING_FLUID ) expect(answers).toEqual(mockProfileTypeAnswers[7]) - answers = ProfileTypeService.getAnswerForStep( + answers = ProfileTypeFormService.getAnswerForStep( ProfileTypeStepForm.INDIVIDUAL_INSULATION_WORK ) expect(answers).toEqual(mockProfileTypeAnswers[8]) - answers = ProfileTypeService.getAnswerForStep( + answers = ProfileTypeFormService.getAnswerForStep( ProfileTypeStepForm.VENTILATION ) expect(answers).toEqual(mockProfileTypeAnswers[9]) - answers = ProfileTypeService.getAnswerForStep( + answers = ProfileTypeFormService.getAnswerForStep( ProfileTypeStepForm.HEATER_REPLACEMENT ) expect(answers).toEqual(mockProfileTypeAnswers[10]) - answers = ProfileTypeService.getAnswerForStep( + answers = ProfileTypeFormService.getAnswerForStep( ProfileTypeStepForm.COLD_WATER ) expect(answers).toEqual(mockProfileTypeAnswers[11]) - answers = ProfileTypeService.getAnswerForStep( + answers = ProfileTypeFormService.getAnswerForStep( ProfileTypeStepForm.HOT_WATER ) expect(answers).toEqual(mockProfileTypeAnswers[12]) - answers = ProfileTypeService.getAnswerForStep( + answers = ProfileTypeFormService.getAnswerForStep( ProfileTypeStepForm.HOT_WATER_FLUID ) expect(answers).toEqual(mockProfileTypeAnswers[13]) - answers = ProfileTypeService.getAnswerForStep( + answers = ProfileTypeFormService.getAnswerForStep( ProfileTypeStepForm.HOT_WATER_EQUIPMENT ) expect(answers).toEqual(mockProfileTypeAnswers[14]) - answers = ProfileTypeService.getAnswerForStep( + answers = ProfileTypeFormService.getAnswerForStep( ProfileTypeStepForm.COOKING_FLUID ) expect(answers).toEqual(mockProfileTypeAnswers[15]) - answers = ProfileTypeService.getAnswerForStep(wrongNumber) + answers = ProfileTypeFormService.getAnswerForStep(wrongNumber) expect(answers).toEqual(mockProfileTypeAnswers[16]) }) }) diff --git a/src/services/profileType.service.ts b/src/services/profileType.service.ts index 89c5c44fc0cb255c2f2953b92961c64234d17203..aaa2dc314117b2b6d213556a4a7dfec7289869d6 100644 --- a/src/services/profileType.service.ts +++ b/src/services/profileType.service.ts @@ -3,7 +3,6 @@ import { FluidForecast, MonthlyForecast, ProfileType, - ProfileTypeAnswer, } from 'models/profileType.model' import { DateTime } from 'luxon' import heatingData from 'constants/consumptionConstants/heating.json' @@ -19,10 +18,7 @@ import { HousingType, IndividualInsulationWork, OutsideFacingWalls, - ProfileTypeStepForm, - ProfileTypeFormType, ThreeChoicesAnswer, - WarmingType, } from 'enum/profileType.enum' import { FluidType } from 'enum/fluid.enum' import ConverterService from './converter.service' @@ -36,7 +32,7 @@ export default class ProfileTypeService { constructor(profileType: ProfileType, _client: Client, year: number) { log.info( - '[ProfileType] Analysis loaded profileType relative to: ', + '[ProfileType] Analysis loaded profileType related to: ', profileType.updateDate ? profileType.updateDate.toString() : 'no update date' @@ -526,229 +522,6 @@ export default class ProfileTypeService { } } - /** - * getNextFormStep - * @param {ProfileTypeStepForm} step - * @returns {ProfileTypeStepForm} next step - */ - public getNextFormStep(step: ProfileTypeStepForm): ProfileTypeStepForm { - switch (step) { - case ProfileTypeStepForm.HOUSING_TYPE: - return ProfileTypeStepForm.AREA - case ProfileTypeStepForm.AREA: - return ProfileTypeStepForm.OCCUPANTS_NUMBER - case ProfileTypeStepForm.OCCUPANTS_NUMBER: - return ProfileTypeStepForm.CONSTRUCTION_YEAR - case ProfileTypeStepForm.CONSTRUCTION_YEAR: - return ProfileTypeStepForm.OUTSIDE_FACING_WALLS - case ProfileTypeStepForm.OUTSIDE_FACING_WALLS: - return this.profileType.housingType === HousingType.INDIVIDUAL_HOUSE - ? ProfileTypeStepForm.WARMING_FLUID - : ProfileTypeStepForm.FLOOR - case ProfileTypeStepForm.FLOOR: - return ProfileTypeStepForm.HEATING - case ProfileTypeStepForm.HEATING: - return this.profileType.heating === IndividualOrCollective.INDIVIDUAL - ? ProfileTypeStepForm.WARMING_FLUID - : ProfileTypeStepForm.COLD_WATER - case ProfileTypeStepForm.WARMING_FLUID: - return this.profileType.constructionYear === ConstructionYear.AFTER_1998 - ? ProfileTypeStepForm.VENTILATION - : ProfileTypeStepForm.INDIVIDUAL_INSULATION_WORK - case ProfileTypeStepForm.INDIVIDUAL_INSULATION_WORK: - return ProfileTypeStepForm.VENTILATION - case ProfileTypeStepForm.VENTILATION: - return ProfileTypeStepForm.HEATER_REPLACEMENT - case ProfileTypeStepForm.HEATER_REPLACEMENT: - return this.profileType.housingType === HousingType.INDIVIDUAL_HOUSE - ? ProfileTypeStepForm.HOT_WATER_FLUID - : ProfileTypeStepForm.COLD_WATER - case ProfileTypeStepForm.COLD_WATER: - return ProfileTypeStepForm.HOT_WATER - case ProfileTypeStepForm.HOT_WATER: - return this.profileType.hotWater === IndividualOrCollective.INDIVIDUAL - ? ProfileTypeStepForm.HOT_WATER_FLUID - : ProfileTypeStepForm.COOKING_FLUID - case ProfileTypeStepForm.HOT_WATER_FLUID: - return ProfileTypeStepForm.HOT_WATER_EQUIPMENT - case ProfileTypeStepForm.HOT_WATER_EQUIPMENT: - return ProfileTypeStepForm.COOKING_FLUID - case ProfileTypeStepForm.COOKING_FLUID: - return ProfileTypeStepForm.END - default: - return ProfileTypeStepForm.HOUSING_TYPE - } - } - - /** - * getPreviousFormStep - * @param {ProfileTypeStepForm} step - * @returns {ProfileTypeStepForm} previous step - */ - public getPreviousFormStep(step: ProfileTypeStepForm): ProfileTypeStepForm { - switch (step) { - case ProfileTypeStepForm.AREA: - return ProfileTypeStepForm.HOUSING_TYPE - case ProfileTypeStepForm.OCCUPANTS_NUMBER: - return ProfileTypeStepForm.AREA - case ProfileTypeStepForm.CONSTRUCTION_YEAR: - return ProfileTypeStepForm.OCCUPANTS_NUMBER - case ProfileTypeStepForm.OUTSIDE_FACING_WALLS: - return ProfileTypeStepForm.CONSTRUCTION_YEAR - case ProfileTypeStepForm.FLOOR: - return ProfileTypeStepForm.OUTSIDE_FACING_WALLS - case ProfileTypeStepForm.HEATING: - return ProfileTypeStepForm.FLOOR - case ProfileTypeStepForm.WARMING_FLUID: - return this.profileType.housingType === HousingType.INDIVIDUAL_HOUSE - ? ProfileTypeStepForm.OUTSIDE_FACING_WALLS - : ProfileTypeStepForm.HEATING - case ProfileTypeStepForm.INDIVIDUAL_INSULATION_WORK: - return ProfileTypeStepForm.WARMING_FLUID - case ProfileTypeStepForm.VENTILATION: - return this.profileType.constructionYear === ConstructionYear.AFTER_1998 - ? ProfileTypeStepForm.WARMING_FLUID - : ProfileTypeStepForm.INDIVIDUAL_INSULATION_WORK - case ProfileTypeStepForm.HEATER_REPLACEMENT: - return ProfileTypeStepForm.VENTILATION - case ProfileTypeStepForm.COLD_WATER: - return this.profileType.heating === IndividualOrCollective.INDIVIDUAL - ? ProfileTypeStepForm.HEATER_REPLACEMENT - : ProfileTypeStepForm.HEATING - case ProfileTypeStepForm.HOT_WATER: - return ProfileTypeStepForm.COLD_WATER - case ProfileTypeStepForm.HOT_WATER_FLUID: - return this.profileType.housingType === HousingType.INDIVIDUAL_HOUSE - ? ProfileTypeStepForm.HEATER_REPLACEMENT - : ProfileTypeStepForm.HOT_WATER - case ProfileTypeStepForm.HOT_WATER_EQUIPMENT: - return ProfileTypeStepForm.HOT_WATER_FLUID - case ProfileTypeStepForm.COOKING_FLUID: - return this.profileType.hotWater === IndividualOrCollective.INDIVIDUAL - ? ProfileTypeStepForm.HOT_WATER_EQUIPMENT - : ProfileTypeStepForm.HOT_WATER - default: - return ProfileTypeStepForm.HOUSING_TYPE - } - } - - /** - * getAnswerForStep - * @param {ProfileTypeStepForm} step - * @returns {ProfileTypeAnswer} next step - */ - static getAnswerForStep(step: ProfileTypeStepForm): ProfileTypeAnswer { - switch (step) { - case ProfileTypeStepForm.HOUSING_TYPE: - return { - type: ProfileTypeFormType.SINGLE_CHOICE, - attribute: 'housingType', - choices: Object.values(HousingType), - } - case ProfileTypeStepForm.CONSTRUCTION_YEAR: - return { - type: ProfileTypeFormType.SINGLE_CHOICE, - attribute: 'constructionYear', - choices: Object.values(ConstructionYear), - } - case ProfileTypeStepForm.AREA: - return { - type: ProfileTypeFormType.NUMBER, - attribute: 'area', - choices: [], - } - case ProfileTypeStepForm.OCCUPANTS_NUMBER: - return { - type: ProfileTypeFormType.NUMBER_SELECTION, - attribute: 'occupantsNumber', - choices: [1, 2, 3, 4, 5, 6, 7, 8, 9], - } - case ProfileTypeStepForm.OUTSIDE_FACING_WALLS: - return { - type: ProfileTypeFormType.SINGLE_CHOICE, - attribute: 'outsideFacingWalls', - choices: Object.values(OutsideFacingWalls), - } - case ProfileTypeStepForm.FLOOR: - return { - type: ProfileTypeFormType.SINGLE_CHOICE, - attribute: 'floor', - choices: Object.values(Floor), - } - case ProfileTypeStepForm.HEATING: - return { - type: ProfileTypeFormType.SINGLE_CHOICE, - attribute: 'heating', - choices: Object.values(IndividualOrCollective), - } - case ProfileTypeStepForm.WARMING_FLUID: - return { - type: ProfileTypeFormType.SINGLE_CHOICE, - attribute: 'warmingFluid', - choices: [ - WarmingType.ELECTRICITY, - WarmingType.GAS, - WarmingType.WOOD, - WarmingType.FUEL, - ], - } - case ProfileTypeStepForm.INDIVIDUAL_INSULATION_WORK: - return { - type: ProfileTypeFormType.MULTI_CHOICE, - attribute: 'individualInsulationWork', - choices: Object.values(IndividualInsulationWork), - } - case ProfileTypeStepForm.VENTILATION: - return { - type: ProfileTypeFormType.SINGLE_CHOICE, - attribute: 'hasInstalledVentilation', - choices: Object.values(ThreeChoicesAnswer), - } - case ProfileTypeStepForm.HEATER_REPLACEMENT: - return { - type: ProfileTypeFormType.SINGLE_CHOICE, - attribute: 'hasReplacedHeater', - choices: Object.values(ThreeChoicesAnswer), - } - case ProfileTypeStepForm.COLD_WATER: - return { - type: ProfileTypeFormType.SINGLE_CHOICE, - attribute: 'coldWater', - choices: Object.values(IndividualOrCollective).reverse(), - } - case ProfileTypeStepForm.HOT_WATER: - return { - type: ProfileTypeFormType.SINGLE_CHOICE, - attribute: 'hotWater', - choices: Object.values(IndividualOrCollective), - } - case ProfileTypeStepForm.HOT_WATER_EQUIPMENT: - return { - type: ProfileTypeFormType.SINGLE_CHOICE, - attribute: 'hotWaterEquipment', - choices: Object.values(HotWaterEquipment), - } - case ProfileTypeStepForm.HOT_WATER_FLUID: - return { - type: ProfileTypeFormType.SINGLE_CHOICE, - attribute: 'hotWaterFluid', - choices: [FluidType.ELECTRICITY, FluidType.GAS], - } - case ProfileTypeStepForm.COOKING_FLUID: - return { - type: ProfileTypeFormType.SINGLE_CHOICE, - attribute: 'cookingFluid', - choices: [FluidType.ELECTRICITY, FluidType.GAS], - } - default: - return { - type: ProfileTypeFormType.SINGLE_CHOICE, - attribute: 'housingType', - choices: [], - } - } - } - /** * checkConsistency * @param {ProfileType} profileType @@ -780,7 +553,7 @@ export default class ProfileTypeService { delete profileType.cozyMetadata } if (profileType.updateDate) { - profileType.updateDate = DateTime.local().setZone('utc', { + profileType.updateDate = profileType.updateDate.setZone('utc', { keepLocalTime: true, }) } diff --git a/src/services/profileTypeEntity.service.ts b/src/services/profileTypeEntity.service.ts index 7e6947cc9b62eed69d133eaf7c0b17ee743f967f..a60765b6b6d8611c509510079af439b04535318c 100644 --- a/src/services/profileTypeEntity.service.ts +++ b/src/services/profileTypeEntity.service.ts @@ -1,5 +1,5 @@ import { Client, Q, QueryDefinition, QueryResult } from 'cozy-client' -import { ProfileType } from 'models' +import { ProfileType, TimePeriod } from 'models' import { PROFILETYPE_DOCTYPE } from 'doctypes' import { DateTime } from 'luxon' import profileTypeData from 'db/profileTypeData.json' @@ -26,7 +26,7 @@ export default class ProfileTypeEntityService { data: [profileType], }: QueryResult<ProfileType[]> = await this._client.query( query - .where({ updateDate: { $lt: date.toString() } }) + .where({ updateDate: { $lte: date.toString() } }) .sortBy([{ updateDate: 'desc' }]) .limitBy(1) ) @@ -74,6 +74,54 @@ export default class ProfileTypeEntityService { return null } + /** + * Retrieve all ProfileTypes from the PROFILETYPE_DOCTYPE + * When called with period parameter, return all profileTypes found for this period or null + * When called without parameters, return all profileTypes or null if none exist + * @param {DateTime} date + * @returns {ProfileType} + */ + public async getAllProfileTypes( + timePeriod?: TimePeriod + ): Promise<ProfileType[] | null> { + const query: QueryDefinition = Q(PROFILETYPE_DOCTYPE) + if (timePeriod) { + const data: QueryResult<ProfileType[]> = await this._client.query( + query + .where({ + updateDate: { $gte: timePeriod.startDate.toString() }, + }) + .sortBy([{ updateDate: 'asc' }]) + .limitBy(100) + ) + if (data.data.length) { + const profileTypesToReturn: ProfileType[] = [] + data.data.forEach((ele: ProfileType) => { + profileTypesToReturn.push( + this.parseProfileTypeEntityToProfileType(ele) + ) + }) + return profileTypesToReturn + } else { + return null + } + } else { + const data: QueryResult<ProfileType[]> = await this._client.query( + query.where({ _id: { $gt: null } }).sortBy([{ updateDate: 'desc' }]) + ) + if (data.data.length) { + const profileTypesToReturn: ProfileType[] = [] + data.data.forEach((ele: ProfileType) => { + profileTypesToReturn.push( + this.parseProfileTypeEntityToProfileType(ele) + ) + }) + return profileTypesToReturn + } + } + return null + } + public async updateProfileType( attributes: Partial<ProfileType> ): Promise<ProfileType | null> { @@ -93,6 +141,23 @@ export default class ProfileTypeEntityService { return null } + /** + * Delete Array of ProfileTypes + * @returns {boolean} + */ + public async deleteProfileTypes( + profileTypes: ProfileType[] + ): Promise<boolean> { + try { + for (let index = 0; index < profileTypes.length; index++) { + await this._client.destroy(profileTypes[index]) + } + return true + } catch (error) { + return false + } + } + /** * Retrieve Profile from the ProfileEntity * @param {ProfileType} profileTypeEntity diff --git a/src/services/profileTypeForm.ts b/src/services/profileTypeForm.service.ts similarity index 51% rename from src/services/profileTypeForm.ts rename to src/services/profileTypeForm.service.ts index 52cce991f384c83988796aad287d7b0328f0e673..1b8ca55ae0df5c8715c2bd2c05ca21725c678c8e 100644 --- a/src/services/profileTypeForm.ts +++ b/src/services/profileTypeForm.service.ts @@ -1,10 +1,18 @@ +import { FluidType } from 'enum/fluid.enum' import { ConstructionYear, + Floor, + HotWaterEquipment, HousingType, + IndividualInsulationWork, IndividualOrCollective, + OutsideFacingWalls, + ProfileTypeFormType, ProfileTypeStepForm, + ThreeChoicesAnswer, + WarmingType, } from 'enum/profileType.enum' -import { ProfileType } from 'models' +import { ProfileType, ProfileTypeAnswer } from 'models' export default class ProfileTypeFormService { private readonly profileType: ProfileType @@ -18,7 +26,10 @@ export default class ProfileTypeFormService { * @param {ProfileTypeStepForm} step * @returns {ProfileTypeStepForm} next step */ - public getNextFormStep(step: ProfileTypeStepForm): ProfileTypeStepForm { + public getNextFormStep( + step: ProfileTypeStepForm, + firstProfileType: boolean + ): ProfileTypeStepForm { switch (step) { case ProfileTypeStepForm.HOUSING_TYPE: return ProfileTypeStepForm.AREA @@ -61,6 +72,10 @@ export default class ProfileTypeFormService { case ProfileTypeStepForm.HOT_WATER_EQUIPMENT: return ProfileTypeStepForm.COOKING_FLUID case ProfileTypeStepForm.COOKING_FLUID: + return firstProfileType + ? ProfileTypeStepForm.END + : ProfileTypeStepForm.UPDATE_DATE + case ProfileTypeStepForm.UPDATE_DATE: return ProfileTypeStepForm.END default: return ProfileTypeStepForm.HOUSING_TYPE @@ -114,8 +129,133 @@ export default class ProfileTypeFormService { return this.profileType.hotWater === IndividualOrCollective.INDIVIDUAL ? ProfileTypeStepForm.HOT_WATER_EQUIPMENT : ProfileTypeStepForm.HOT_WATER + case ProfileTypeStepForm.UPDATE_DATE: + return ProfileTypeStepForm.COOKING_FLUID default: return ProfileTypeStepForm.HOUSING_TYPE } } + + /** + * getAnswerForStep + * @param {ProfileTypeStepForm} step + * @returns {ProfileTypeAnswer} + */ + static getAnswerForStep(step: ProfileTypeStepForm): ProfileTypeAnswer { + switch (step) { + case ProfileTypeStepForm.HOUSING_TYPE: + return { + type: ProfileTypeFormType.SINGLE_CHOICE, + attribute: 'housingType', + choices: Object.values(HousingType), + } + case ProfileTypeStepForm.CONSTRUCTION_YEAR: + return { + type: ProfileTypeFormType.SINGLE_CHOICE, + attribute: 'constructionYear', + choices: Object.values(ConstructionYear), + } + case ProfileTypeStepForm.AREA: + return { + type: ProfileTypeFormType.NUMBER, + attribute: 'area', + choices: [], + } + case ProfileTypeStepForm.OCCUPANTS_NUMBER: + return { + type: ProfileTypeFormType.NUMBER_SELECTION, + attribute: 'occupantsNumber', + choices: [1, 2, 3, 4, 5, 6, 7, 8, 9], + } + case ProfileTypeStepForm.OUTSIDE_FACING_WALLS: + return { + type: ProfileTypeFormType.SINGLE_CHOICE, + attribute: 'outsideFacingWalls', + choices: Object.values(OutsideFacingWalls), + } + case ProfileTypeStepForm.FLOOR: + return { + type: ProfileTypeFormType.SINGLE_CHOICE, + attribute: 'floor', + choices: Object.values(Floor), + } + case ProfileTypeStepForm.HEATING: + return { + type: ProfileTypeFormType.SINGLE_CHOICE, + attribute: 'heating', + choices: Object.values(IndividualOrCollective), + } + case ProfileTypeStepForm.WARMING_FLUID: + return { + type: ProfileTypeFormType.SINGLE_CHOICE, + attribute: 'warmingFluid', + choices: [ + WarmingType.ELECTRICITY, + WarmingType.GAS, + WarmingType.WOOD, + WarmingType.FUEL, + ], + } + case ProfileTypeStepForm.INDIVIDUAL_INSULATION_WORK: + return { + type: ProfileTypeFormType.MULTI_CHOICE, + attribute: 'individualInsulationWork', + choices: Object.values(IndividualInsulationWork), + } + case ProfileTypeStepForm.VENTILATION: + return { + type: ProfileTypeFormType.SINGLE_CHOICE, + attribute: 'hasInstalledVentilation', + choices: Object.values(ThreeChoicesAnswer), + } + case ProfileTypeStepForm.HEATER_REPLACEMENT: + return { + type: ProfileTypeFormType.SINGLE_CHOICE, + attribute: 'hasReplacedHeater', + choices: Object.values(ThreeChoicesAnswer), + } + case ProfileTypeStepForm.COLD_WATER: + return { + type: ProfileTypeFormType.SINGLE_CHOICE, + attribute: 'coldWater', + choices: Object.values(IndividualOrCollective).reverse(), + } + case ProfileTypeStepForm.HOT_WATER: + return { + type: ProfileTypeFormType.SINGLE_CHOICE, + attribute: 'hotWater', + choices: Object.values(IndividualOrCollective), + } + case ProfileTypeStepForm.HOT_WATER_EQUIPMENT: + return { + type: ProfileTypeFormType.SINGLE_CHOICE, + attribute: 'hotWaterEquipment', + choices: Object.values(HotWaterEquipment), + } + case ProfileTypeStepForm.HOT_WATER_FLUID: + return { + type: ProfileTypeFormType.SINGLE_CHOICE, + attribute: 'hotWaterFluid', + choices: [FluidType.ELECTRICITY, FluidType.GAS], + } + case ProfileTypeStepForm.COOKING_FLUID: + return { + type: ProfileTypeFormType.SINGLE_CHOICE, + attribute: 'cookingFluid', + choices: [FluidType.ELECTRICITY, FluidType.GAS], + } + case ProfileTypeStepForm.UPDATE_DATE: + return { + type: ProfileTypeFormType.DATE_SELECTION, + attribute: 'updateDate', + choices: [], + } + default: + return { + type: ProfileTypeFormType.SINGLE_CHOICE, + attribute: 'housingType', + choices: [], + } + } + } }