diff --git a/scripts/createConnections.js b/scripts/createConnections.js index 85fcbdaac31defc1a5cb1a5198cde10ebd823c5a..9d8b95a7255a8f1fe8ef74f37dacd9332b604475 100644 --- a/scripts/createConnections.js +++ b/scripts/createConnections.js @@ -22,6 +22,7 @@ const dataEnedisAccount = JSON.stringify({ }, data: { consentId: 43, + expirationDate: '2023-09-26', }, id: '70e68b8450cee09fe2f077610901094d', identifier: 'address', diff --git a/src/components/Connection/ExpiredConsentModal.tsx b/src/components/Connection/ExpiredConsentModal.tsx index 117fd283935295a3af8f30a516bf56d48d20d5b2..0f7bcab7da024c0693d5276c7c7b43a967524917 100644 --- a/src/components/Connection/ExpiredConsentModal.tsx +++ b/src/components/Connection/ExpiredConsentModal.tsx @@ -9,33 +9,61 @@ import { Button, IconButton } from '@material-ui/core' import { FluidType } from 'enum/fluid.enum' import './expiredConsentModal.scss' import { useHistory } from 'react-router-dom' -import { useDispatch } from 'react-redux' -import { setShouldRefreshConsent } from 'store/global/global.actions' +import { useDispatch, useSelector } from 'react-redux' +import { + setShouldRefreshConsent, + updateSgeStore, +} from 'store/global/global.actions' +import { AppStore } from 'store' +import { AccountSgeData } from 'models' interface ExpiredConsentModalProps { open: boolean - handleCloseClick: () => void + handleCloseClick: (fluidtype: FluidType) => void fluidType: FluidType + toggleModal: () => void } const ExpiredConsentModal: React.FC<ExpiredConsentModalProps> = ({ open, handleCloseClick, fluidType, + toggleModal, }: ExpiredConsentModalProps) => { const { t } = useI18n() const history = useHistory() const dispatch = useDispatch() + const { fluidStatus } = useSelector((state: AppStore) => state.ecolyo.global) const launchUpdateConsent = useCallback(() => { - dispatch(setShouldRefreshConsent(true)) + if (fluidType === FluidType.ELECTRICITY) { + const accountData = fluidStatus[FluidType.ELECTRICITY].connection.account + ?.auth as AccountSgeData + //store the previous account data since the onDelete will remove account from DB + dispatch( + updateSgeStore({ + currentStep: 0, + firstName: accountData.firstname, + lastName: accountData.lastname, + pdl: parseInt(accountData.pointId), + address: accountData.address, + zipCode: parseInt(accountData.postalCode), + city: accountData.city, + dataConsent: true, + pdlConfirm: true, + shouldLaunchAccount: true, + }) + ) + } + toggleModal() history.push(`/consumption/${FluidType[fluidType].toLocaleLowerCase()}`) - }, [dispatch, fluidType, history]) + dispatch(setShouldRefreshConsent(true)) + }, [dispatch, fluidStatus, fluidType, history, toggleModal]) return ( <Dialog open={open} - onClose={handleCloseClick} + onClose={toggleModal} aria-labelledby={'accessibility-title'} classes={{ root: 'modal-root', @@ -48,7 +76,7 @@ const ExpiredConsentModal: React.FC<ExpiredConsentModalProps> = ({ <IconButton aria-label={t('consumption_visualizer.modal.close')} className="modal-paper-close-button" - onClick={handleCloseClick} + onClick={toggleModal} > <Icon icon={CloseIcon} size={16} /> </IconButton> @@ -76,7 +104,7 @@ const ExpiredConsentModal: React.FC<ExpiredConsentModalProps> = ({ <div className="buttons"> <Button aria-label={t('consent_outdated.later')} - onClick={handleCloseClick} + onClick={() => handleCloseClick(fluidType)} classes={{ root: 'btn-secondary-positive', label: 'text-16-normal', diff --git a/src/components/Connection/SGEConnect/SgeInit.tsx b/src/components/Connection/SGEConnect/SgeInit.tsx index 594a42eb06f44fab9806203d1f9e2420bb4930dd..b2a55732edc70b0db64b6de7a96fa7502d4312b7 100644 --- a/src/components/Connection/SGEConnect/SgeInit.tsx +++ b/src/components/Connection/SGEConnect/SgeInit.tsx @@ -9,7 +9,10 @@ import { useHistory } from 'react-router-dom' import { useDispatch, useSelector } from 'react-redux' import { AppStore } from 'store' import useKonnectorAuth from 'components/Hooks/useKonnectorAuth' -import { updateSgeStore } from 'store/global/global.actions' +import { + setShouldRefreshConsent, + updateSgeStore, +} from 'store/global/global.actions' interface SgeInitProps { fluidStatus: FluidStatus } @@ -20,6 +23,7 @@ const SgeInit: React.FC<SgeInitProps> = ({ fluidStatus }: SgeInitProps) => { const konnectorSlug: string = fluidStatus.connection.konnectorConfig.slug const account: Account | null = fluidStatus.connection.account const { sgeConnect } = useSelector((state: AppStore) => state.ecolyo.global) + const dispatch = useDispatch() const [connect, update] = useKonnectorAuth(fluidStatus) @@ -27,6 +31,7 @@ const SgeInit: React.FC<SgeInitProps> = ({ fluidStatus }: SgeInitProps) => { async function launchConnect() { if (sgeConnect.shouldLaunchAccount) { dispatch(updateSgeStore({ ...sgeConnect, shouldLaunchAccount: false })) + dispatch(setShouldRefreshConsent(false)) if (!account) { await connect() } else { diff --git a/src/components/Home/ConsumptionView.tsx b/src/components/Home/ConsumptionView.tsx index f5a76f6c10db82b91a59591b26b3389dee8a5b8d..ff2eba5fb8ec46410b619c25952a82d18a7af642 100644 --- a/src/components/Home/ConsumptionView.tsx +++ b/src/components/Home/ConsumptionView.tsx @@ -89,6 +89,15 @@ const ConsumptionView: React.FC<ConsumptionViewProps> = ({ } }, [client, dispatch]) + const handleCloseExpirationModal = useCallback((fluidtype: FluidType) => { + if (fluidtype === FluidType.GAS) { + setopenExpiredConsentModal(false) + } + if (fluidtype === FluidType.ELECTRICITY) { + console.log('should delete enedis') + } + }, []) + useEffect(() => { setIsFluidKonnected(isKonnectorActive(fluidStatus, fluidType)) if ( @@ -106,16 +115,39 @@ const ConsumptionView: React.FC<ConsumptionViewProps> = ({ useEffect(() => { let subscribed = true //Check if some fluids have expired consent error - const expiredConsents = [] - for (const fluid of fluidStatus) { - if ( - fluid.connection.triggerState?.last_error && - getKonnectorUpdateError(fluid.connection.triggerState.last_error) === - 'error_update_oauth' - ) { - expiredConsents.push(fluid.fluidType) - } + const expiredConsents: FluidType[] = [] + //Check for GRDF consent expiration + const gasError = + fluidStatus[FluidType.GAS].connection.triggerState?.last_error + if ( + gasError && + getKonnectorUpdateError(gasError) === 'error_update_oauth' + ) { + expiredConsents.push(fluidStatus[FluidType.GAS].fluidType) } + //Check for Enedis SGE Consent Expiration + const today = DateTime.local() + .setZone('utc', { + keepLocalTime: true, + }) + .startOf('day') + .toISO() + + const sgeExpirationDate: string | undefined = + fluidStatus[FluidType.ELECTRICITY].connection.account?.data + ?.expirationDate + const isoExpirationDate: string = sgeExpirationDate + ? DateTime.fromISO(sgeExpirationDate) + .setZone('utc', { + keepLocalTime: true, + }) + .startOf('day') + .toISO() + : '' + // if (isoExpirationDate <= today) { + expiredConsents.push(fluidStatus[FluidType.ELECTRICITY].fluidType) + // } + if (subscribed) setconsentExpiredFluids(expiredConsents) return () => { subscribed = false @@ -199,8 +231,9 @@ const ConsumptionView: React.FC<ConsumptionViewProps> = ({ <ExpiredConsentModal key={fluid} open={openExpiredConsentModal} - handleCloseClick={() => setopenExpiredConsentModal(false)} + handleCloseClick={handleCloseExpirationModal} fluidType={fluid} + toggleModal={() => setopenExpiredConsentModal(prev => !prev)} /> ) })} diff --git a/src/models/account.model.ts b/src/models/account.model.ts index c3ae32e01a1938ee4deaaea0ae7c6b0fb712c70b..7db5f3b3acb00beb4316740c61732acc00970d17 100644 --- a/src/models/account.model.ts +++ b/src/models/account.model.ts @@ -6,6 +6,10 @@ export interface Account extends AccountAttributes { _type?: string cozyMetadata?: Record<string, any> } +export interface SgeConsentData { + consentId: number + expirationDate: string +} export interface AccountAttributes { account_type: string @@ -14,6 +18,7 @@ export interface AccountAttributes { identifier?: string state?: string | null name?: string + data?: SgeConsentData oauth_callback_results?: Record<string, any> }