Skip to content

Commit

Permalink
feat: se desinscrire du service linky (#653)
Browse files Browse the repository at this point in the history
Co-authored-by: dlamande <[email protected]>
  • Loading branch information
OCTO-GUIC and dlamande authored Oct 9, 2024
1 parent 9447087 commit 1e8efdd
Show file tree
Hide file tree
Showing 13 changed files with 151 additions and 11 deletions.
44 changes: 42 additions & 2 deletions src/components/custom/Linky/LinkyFormConfiguration.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<template>
<form @submit.prevent="parametrerLeService()">
<InputText name="nom" v-model="nomDeFamille" label="Votre nom de famille" />
<InputText
name="prm"
v-model="parametreDuService"
Expand All @@ -24,24 +25,51 @@
</template>

<script setup lang="ts">
import { ref } from 'vue';
import { onMounted, ref } from 'vue';
import Alert from '@/components/custom/Alert.vue';
import InputCheckboxUnitaire from '@/components/dsfr/InputCheckboxUnitaire.vue';
import InputText from '@/components/dsfr/InputText.vue';
import { useAlerte } from '@/composables/useAlerte';
import { SessionRepositoryStore } from '@/domaines/authentification/adapters/session.repository.store';
import {
ProfileUtilisateurPresenterImpl,
ProfileUtilisateurViewModel,
} from '@/domaines/profileUtilisateur/adapters/profileUtilisateur.presenter.impl';
import {
ChargerProfileUtilisateurUsecase,
ProfileUtilisateurRepositoryAxiosImpl,
} from '@/domaines/profileUtilisateur/chargerProfileUtilisateur.usecase';
import { MettreAJourProfileUtilisateurUsecase } from '@/domaines/profileUtilisateur/mettreAJourProfileUtilisateurUsecase';
import { ServiceRepositoryAxios } from '@/domaines/services/adapters/service.repository.axios';
import { LinkyEventBusImpl } from '@/domaines/services/linkyEventBusImpl';
import { ParametrerServiceUsecase } from '@/domaines/services/parametrerService.usecase';
import { utilisateurStore } from '@/store/utilisateur';
defineProps<{ prm: string }>();
const { id: utilisateurId } = utilisateurStore().utilisateur;
const nomDeFamille = ref<string>('');
const acceptationCGU = ref<boolean>(false);
const parametreDuService = defineModel<string>('prm', { default: '' });
const profileUtilisateurViewModel = ref<ProfileUtilisateurViewModel>();
const { id: utilisateurId, nom: utilisateurNom } = utilisateurStore().utilisateur;
const { alerte, afficherAlerte } = useAlerte();
const parametrerLeService = async () => {
if (nomDeFamille.value !== utilisateurNom) {
const usecase = new MettreAJourProfileUtilisateurUsecase(
new ProfileUtilisateurRepositoryAxiosImpl(),
new SessionRepositoryStore(),
);
const donneesAMettreAjour: ProfileUtilisateurViewModel = {
...profileUtilisateurViewModel.value!,
nom: nomDeFamille.value,
};
await usecase.execute(donneesAMettreAjour);
}
const parametrerService = new ParametrerServiceUsecase(
new ServiceRepositoryAxios(),
LinkyEventBusImpl.getInstance(),
Expand All @@ -52,4 +80,16 @@
.then()
.catch(error => afficherAlerte('error', 'Erreur', error.data.message || 'Erreur inattendue'));
};
onMounted(async () => {
const usecase = new ChargerProfileUtilisateurUsecase(new ProfileUtilisateurRepositoryAxiosImpl());
await usecase.execute(
utilisateurId,
new ProfileUtilisateurPresenterImpl(viewModel => {
profileUtilisateurViewModel.value = viewModel;
nomDeFamille.value = viewModel.nom;
}),
);
});
</script>
16 changes: 15 additions & 1 deletion src/components/custom/Linky/LinkyGraphique.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
<CarteInfo>
<LinkyExplicationAleatoire />
</CarteInfo>
<button @click="desactiverServiceLinky" class="fr-btn fr-btn--icon-left fr-btn--tertiary fr-icon-close-circle-fill">
Désactiver le service
</button>
</template>

<script setup lang="ts">
Expand All @@ -29,17 +32,28 @@
} from '@/domaines/serviceRecherche/linky/adapters/serviceRechercheConsommationLinky.presenter.impl';
import { ServiceRechercheLinkyRepositoryAxios } from '@/domaines/serviceRecherche/linky/adapters/serviceRechercheLinky.repository.axios';
import { RecupererConsommationElectriqueUsecase } from '@/domaines/serviceRecherche/linky/recupererConsommationElectrique.usecase';
import { SeDesinscrireDuServiceUsecase } from '@/domaines/serviceRecherche/linky/seDesinscrireDuService.usecase';
import { LinkyEventBusImpl } from '@/domaines/services/linkyEventBusImpl';
import { utilisateurStore } from '@/store/utilisateur';
const serviceConsommationLinkyViewModel = ref<ServiceConsommationLinkyViewModel>();
const { id: utilisateurId } = utilisateurStore().utilisateur;
onMounted(async () => {
const recupererConsommationElectriqueUsecase = new RecupererConsommationElectriqueUsecase(
new ServiceRechercheLinkyRepositoryAxios(),
);
await recupererConsommationElectriqueUsecase.execute(
utilisateurStore().utilisateur.id,
utilisateurId,
new ServiceRechercheConsommationLinkyPresenterImpl(vm => (serviceConsommationLinkyViewModel.value = vm)),
);
});
const desactiverServiceLinky = async () => {
const seDesinscrireDuServiceUsecase = new SeDesinscrireDuServiceUsecase(
new ServiceRechercheLinkyRepositoryAxios(),
LinkyEventBusImpl.getInstance(),
);
await seDesinscrireDuServiceUsecase.execute(utilisateurId);
};
</script>
21 changes: 14 additions & 7 deletions src/components/pages/PagesService/PageServiceLinky.vue
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,26 @@
const usecase = new ObtenirInformationCompteurUsecase(new ServiceRechercheLinkyRepositoryAxios());
onMounted(async () => {
await usecase.execute(
utilisateurId,
new ServiceRechercheLinkyPresenterImpl(vm => (serviceLinkyViewModel.value = vm)),
);
LinkyEventBusImpl.getInstance().subscribe('linky', LinkyEvent.PRM_A_ETE_ENVOYE, async () => {
await usecase.execute(
utilisateurId,
new ServiceRechercheLinkyPresenterImpl(vm => (serviceLinkyViewModel.value = vm)),
);
});
isLoading.value = false;
});
LinkyEventBusImpl.getInstance().subscribe('linky', LinkyEvent.DESABONNEMENT, async () => {
await usecase.execute(
utilisateurId,
new ServiceRechercheLinkyPresenterImpl(vm => (serviceLinkyViewModel.value = vm)),
);
});
LinkyEventBusImpl.getInstance().subscribe('linky', LinkyEvent.PRM_A_ETE_ENVOYE, async () => {
await usecase.execute(
utilisateurId,
new ServiceRechercheLinkyPresenterImpl(vm => (serviceLinkyViewModel.value = vm)),
);
isLoading.value = false;
});
onUnmounted(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ enum ServiceIdInterne {
FRUITS_ET_LEGUMES = 'fruits_legumes',
PRES_DE_CHEZ_VOUS = 'proximite',
RECETTES = 'recettes',
LINKY = 'linky',
}

export class ServiceRecherchePresenterImpl implements ServiceRecherchePresenter {
Expand All @@ -28,6 +29,7 @@ export class ServiceRecherchePresenterImpl implements ServiceRecherchePresenter
[ServiceIdInterne.FRUITS_ET_LEGUMES]: RouteServiceName.FRUITS_ET_LEGUMES,
[ServiceIdInterne.PRES_DE_CHEZ_VOUS]: RouteServiceName.PROXIMITE,
[ServiceIdInterne.RECETTES]: RouteServiceName.RECETTES,
[ServiceIdInterne.LINKY]: RouteServiceName.LINKY,
};

this.serviceViewModel({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ export class ServiceRechercheLinkyRepositoryAxios implements ServiceRechercheLin
};
}

@intercept401()
async seDesabonner(idUtilsateur: string): Promise<void> {
const axiosInstance = AxiosFactory.getAxios();
await axiosInstance.delete(`/utilisateurs/${idUtilsateur}/services/linky`);
}

private mapConsommationElectrique(data: ConsommationElectriqueApiModel['data']): ConsommationElectrique['data'] {
return data.map(donneeConsommation => ({
mois: donneeConsommation.mois,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ import { ConsommationElectriqueGlobal } from '@/domaines/serviceRecherche/linky/
export interface ServiceRechercheLinkyRepository {
recupererConsommationElectrique(idUtilsateur: string): Promise<ConsommationElectriqueGlobal>;
recupererInformationCompteur(idUtilsateur: string): Promise<InformationCompteur>;
seDesabonner(idUtilsateur: string): Promise<void>;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { ServiceRechercheLinkyRepository } from '@/domaines/serviceRecherche/linky/ports/serviceRechercheLinky.repository';
import { LinkyEvent } from '@/domaines/services/linkyEventBusImpl';
import { EventBus } from '@/shell/eventBus';

export class SeDesinscrireDuServiceUsecase {
constructor(
private serviceRechercheLinkyRepository: ServiceRechercheLinkyRepository,
private readonly eventBus: EventBus<LinkyEvent>,
) {}

async execute(utilisateurId: string) {
await this.serviceRechercheLinkyRepository.seDesabonner(utilisateurId);
this.eventBus.publish(LinkyEvent.DESABONNEMENT);
}
}
2 changes: 2 additions & 0 deletions src/domaines/services/linkyEventBusImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ import { EventBus } from '@/shell/eventBus';

export enum LinkyEvent {
PRM_A_ETE_ENVOYE,
DESABONNEMENT,
}

export class LinkyEventBusImpl extends EventBus<LinkyEvent> {
private static instance: LinkyEventBusImpl | null = null;

protected eventSubscribers: Record<LinkyEvent, { subscriberName: string; callback: () => void }[]> = {
[LinkyEvent.PRM_A_ETE_ENVOYE]: [],
[LinkyEvent.DESABONNEMENT]: [],
};
private constructor() {
super();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import { ConsommationElectriqueGlobal } from '@/domaines/serviceRecherche/linky/
export class ServiceRechercheConsommationLinkyRepositoryMock implements ServiceRechercheLinkyRepository {
constructor(private consommationElectriqueGlobalARetourner: ConsommationElectriqueGlobal) {}

seDesabonner(_idUtilsateur: string): Promise<void> {
throw new Error('Method not implemented.');
}

recupererInformationCompteur(_idUtilisateur: string): Promise<InformationCompteur> {
throw new Error('Method not implemented.');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@ import { ConsommationElectriqueGlobal } from '@/domaines/serviceRecherche/linky/
export class ServiceRechercheLinkyRepositoryMock implements ServiceRechercheLinkyRepository {
constructor(private informationCompteurARetourner: InformationCompteur) {}

seDesabonner(_idUtilsateur: string): Promise<void> {
throw new Error('Method not implemented.');
}

recupererInformationCompteur(_idUtilisateur: string): Promise<InformationCompteur> {
return Promise.resolve(this.informationCompteurARetourner);
}

recupererConsommationElectrique(idUtilsateur: string): Promise<ConsommationElectriqueGlobal> {
recupererConsommationElectrique(_idUtilsateur: string): Promise<ConsommationElectriqueGlobal> {
throw new Error('Method not implemented.');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { InformationCompteur } from '@/domaines/serviceRecherche/linky/obtenirInformationCompteur.usecase';
import { ServiceRechercheLinkyRepository } from '@/domaines/serviceRecherche/linky/ports/serviceRechercheLinky.repository';
import { ConsommationElectriqueGlobal } from '@/domaines/serviceRecherche/linky/recupererConsommationElectrique.usecase';

export class SpyServiceRechercheLinky implements ServiceRechercheLinkyRepository {
private _seDesabonnerDuServiceAEteAppele: boolean = false;

get seDesabonnerDuServiceAEteAppele(): boolean {
return this._seDesabonnerDuServiceAEteAppele;
}

recupererConsommationElectrique(_idUtilsateur: string): Promise<ConsommationElectriqueGlobal> {
throw new Error('Method not implemented.');
}

recupererInformationCompteur(_idUtilsateur: string): Promise<InformationCompteur> {
throw new Error('Method not implemented.');
}

seDesabonner(_idUtilsateur: string): Promise<void> {
this._seDesabonnerDuServiceAEteAppele = true;
return Promise.resolve();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { SeDesinscrireDuServiceUsecase } from '@/domaines/serviceRecherche/linky/seDesinscrireDuService.usecase';
import { SpyLinkyEventBus } from '../../services/adapters/spyLinkyEventBus';
import { LinkyEvent } from '@/domaines/services/linkyEventBusImpl';
import { SpyServiceRechercheLinky } from './adapters/serviceRechercheLinky.repository.spy';

describe('Fichier de tests concernant la désinscirption au service linky', () => {
it("En donnant un id d'utilisateur, doit appeler le repo et publier un evenement", async () => {
// GIVEN
const serviceRepositorySpy = new SpyServiceRechercheLinky();
const linkyEventBusSpy = new SpyLinkyEventBus();
const seDesinscrireDuServiceUsecase = new SeDesinscrireDuServiceUsecase(serviceRepositorySpy, linkyEventBusSpy);

// WHEN
await seDesinscrireDuServiceUsecase.execute('utilisateurId');

// THEN
expect(serviceRepositorySpy.seDesabonner).toBeTruthy();
expect(linkyEventBusSpy.eventName).toBe(LinkyEvent.DESABONNEMENT);
});
});
1 change: 1 addition & 0 deletions tests/services/adapters/spyLinkyEventBus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ export class SpyLinkyEventBus extends EventBus<LinkyEvent> {

eventSubscribers: Record<LinkyEvent, { subscriberName: string; callback: () => void }[]> = {
[LinkyEvent.PRM_A_ETE_ENVOYE]: [],
[LinkyEvent.DESABONNEMENT]: [],
};
}

0 comments on commit 1e8efdd

Please sign in to comment.