diff --git a/src/components/Splash/SplashRoot.tsx b/src/components/Splash/SplashRoot.tsx index 415a185eb5bbcb24536390ea62bd655d0f3777f6..ee5641d8887bf7af25556cff9831c141d2841d72 100644 --- a/src/components/Splash/SplashRoot.tsx +++ b/src/components/Splash/SplashRoot.tsx @@ -85,6 +85,16 @@ const SplashRoot = ({ if (subscribed && resultQuiz.result && resultQuiz.profile) { profile = resultQuiz.profile } + const resultExploration = await initializationService.initExplorationEntity( + profile.explorationHash + ) + if ( + subscribed && + resultExploration.result && + resultExploration.profile + ) { + profile = resultExploration.profile + } const resultChallengeEntity = await initializationService.initChallengeEntity( profile.challengeHash diff --git a/src/db/explorationEntity.json b/src/db/explorationEntity.json index f947a49f405933e64aca687cb378e07e88e512df..f1c5c039e88511cd66cb87085f6b4420c4694bf2 100644 --- a/src/db/explorationEntity.json +++ b/src/db/explorationEntity.json @@ -2,78 +2,78 @@ { "_id": "EXPLORATION001", "state": 0, - "description": "Se connecter 3 fois à Ecolyo", + "description": "Consultez 3 fois Ecolyo", "target": 3, "type": 0, "date": null, "ecogesture_id": "", - "fluid_config": [], + "fluid_condition": [], "priority_by_condition": 1 }, { "_id": "EXPLORATION002", "state": 0, - "description": "Contrôle du nuage", + "description": "Consultez l'écogeste \"Chat échaudé\"", "target": 1, "type": 3, "date": null, "ecogesture_id": "0032", - "fluid_config": [], + "fluid_condition": [], "priority_by_condition": 1 }, { "_id": "EXPLORATION003", "state": 0, - "description": "Parler en bien d’Ecolyo à un ami (quitte à mentir)", + "description": "Parlez en bien d’Ecolyo à un ami (quitte à mentir)", "target": 1, "type": 0, "date": null, "ecogesture_id": "", - "fluid_config": [], + "fluid_condition": [], "priority_by_condition": 1 }, { "_id": "EXPLORATION004", "state": 0, - "description": "Allez voir combien vous avez consommez le 24 décembre 2020", + "description": "Allez voir vos consommations par an", "target": 1, - "type": 2, + "type": 1, "date": null, "ecogesture_id": "", - "fluid_config": [], + "fluid_condition": [], "priority_by_condition": 1 }, { "_id": "EXPLORATION005", "state": 0, - "description": "Nous envoyer un feedback", + "description": "Envoyez un feedback", "target": 1, "type": 1, "date": null, "ecogesture_id": "", - "fluid_config": [], + "fluid_condition": [], "priority_by_condition": 1 }, { "_id": "EXPLORATION006", "state": 0, - "description": "Dévérrouiller les données électricité à la demi-heure", + "description": "Dévérrouillez les données électricité à la demi-heure", "target": 1, "type": 1, "date": null, "ecogesture_id": "", - "fluid_config": [0], + "fluid_condition": [0], "priority_by_condition": 1 }, { "_id": "EXPLORATION007", "state": 0, - "description": "Mettre l'appli Ecolyo en favoris (rappel des raccourcis)", + "description": "Placez l'appli Ecolyo en favoris (rappel des raccourcis)", "target": 1, "type": 0, "date": null, "ecogesture_id": "", - "fluid_config": [], + "fluid_condition": [], "priority_by_condition": 1 } ] diff --git a/src/models/profile.model.ts b/src/models/profile.model.ts index f0081bfe79fe1a5e9db305003a4a619f1056cea3..7569b6bb43be2f83324b4f6f898363bfab4f55d3 100644 --- a/src/models/profile.model.ts +++ b/src/models/profile.model.ts @@ -6,6 +6,7 @@ export interface Profile { challengeHash: string duelHash: string quizHash: string + explorationHash: string isFirstConnection: boolean lastConnectionDate: DateTime haveSeenFavoriteModal: boolean diff --git a/src/services/initialization.service.ts b/src/services/initialization.service.ts index 4113417ad473a8e5d93ab53456efef36a4d8aa6c..6e66ba50e9d2caecf172b0ea06a376e0ac5e2002 100644 --- a/src/services/initialization.service.ts +++ b/src/services/initialization.service.ts @@ -17,6 +17,7 @@ import { CHALLENGE_DOCTYPE, DUEL_DOCTYPE, QUIZ_DOCTYPE, + EXPLORATION_DOCTYPE, } from 'doctypes' import { FluidType } from 'enum/fluid.enum' @@ -28,6 +29,7 @@ import ecogestureData from 'db/ecogestureData.json' import challengeEntityData from 'db/challengeEntity.json' import duelEntityData from 'db/duelEntity.json' import quizEntityData from 'db/quizEntity.json' +import explorationEntityData from 'db/explorationEntity.json' import ProfileService from 'services/profile.service' import profileData from 'db/profileData.json' @@ -37,6 +39,7 @@ import AccountService from 'services/account.service' import FluidService from 'services/fluid.service' import DuelService from 'services/duel.service' import QuizService from 'services/quiz.service' +import ExplorationService from 'services/exploration.service' import { hashFile } from 'utils/hash' import { getActualReportDate } from 'utils/date' @@ -617,6 +620,114 @@ export default class InitializationService { } } + public async initExplorationEntity( + hash: string + ): Promise<{ + result: boolean + profile: Profile | null + }> { + const explorationHash = hashFile(explorationEntityData) + const explorationService = new ExplorationService(this._client) + const profileService = new ProfileService(this._client) + + // Populate data if none explorationEntity exists + const loadedExplorationEntity = await explorationService.getAllExplorationEntities() + if ( + !loadedExplorationEntity || + (loadedExplorationEntity && loadedExplorationEntity.length === 0) + ) { + // Populate the doctype with data + try { + for (let i = 0; i <= explorationEntityData.length - 1; i++) { + await this._client.create( + EXPLORATION_DOCTYPE, + explorationEntityData[i] + ) + } + // Check of created document + const checkCount = await explorationService.getAllExplorationEntities() + if ( + !checkCount || + (checkCount && checkCount.length !== explorationEntityData.length) + ) { + throw new Error( + 'initExplorationEntity: Created exploration type entities does not match' + ) + } + // Update profil with the hash + const updatedProfile = await profileService.updateProfile({ + explorationHash: explorationHash, + }) + if (updatedProfile) { + console.log( + '%c Initialization: Exploration entities created', + 'background: #222; color: white' + ) + return { + result: true, + profile: updatedProfile, + } + } else { + throw new Error('initExplorationEntity: Profile not updated') + } + } catch (error) { + console.log('Initialization error: ', error) + throw error + } + } + + // Update if the hash is not the same as the one from profile + if (hash !== explorationHash) { + // Update the doctype + try { + // Deletion of all documents + await explorationService.deleteAllExplorationEntities() + // Population with the data + await Promise.all( + explorationEntityData.map(async explorationEntity => { + await this._client.create(EXPLORATION_DOCTYPE, explorationEntity) + }) + ) + // Check of created document + const checkCount = await explorationService.getAllExplorationEntities() + if ( + !checkCount || + (checkCount && checkCount.length !== explorationEntityData.length) + ) { + throw new Error( + 'initExplorationEntity: Created exploration entities does not match' + ) + } + // Update profil with the hash + const updatedProfile = await profileService.updateProfile({ + explorationHash: explorationHash, + }) + if (updatedProfile) { + console.log( + '%c Initialization: Exploration entities updated', + 'background: #222; color: white' + ) + return { + result: true, + profile: updatedProfile, + } + } else { + throw new Error('initExplorationEntity: Profile not updated') + } + } catch (error) { + console.log('Initialization error: ', error) + throw error + } + } else { + // Doctype already up to date + console.log( + '%c Initialization: Exploration Entity loaded', + 'background: #222; color: white' + ) + return { result: true, profile: null } + } + } + public async initReport( profile: Profile ): Promise<{