Skip to content
Snippets Groups Projects
Commit 151d7a77 authored by rpapin's avatar rpapin
Browse files

Finish init + update exploration descriptions

parent 3774a9c9
No related branches found
No related tags found
1 merge request!188Features/init json
......@@ -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
......
......@@ -2,7 +2,7 @@
{
"_id": "EXPLORATION001",
"state": 0,
"description": "Se connecter 3 fois à Ecolyo",
"description": "Consultez 3 fois Ecolyo",
"target": 3,
"type": 0,
"date": null,
......@@ -13,7 +13,7 @@
{
"_id": "EXPLORATION002",
"state": 0,
"description": "Contrôle du nuage",
"description": "Consultez l'écogeste \"Chat échaudé\"",
"target": 1,
"type": 3,
"date": null,
......@@ -24,7 +24,7 @@
{
"_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,
......@@ -35,9 +35,9 @@
{
"_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_condition": [],
......@@ -46,7 +46,7 @@
{
"_id": "EXPLORATION005",
"state": 0,
"description": "Nous envoyer un feedback",
"description": "Envoyez un feedback",
"target": 1,
"type": 1,
"date": null,
......@@ -57,7 +57,7 @@
{
"_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,
......@@ -68,7 +68,7 @@
{
"_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,
......
......@@ -6,6 +6,7 @@ export interface Profile {
challengeHash: string
duelHash: string
quizHash: string
explorationHash: string
isFirstConnection: boolean
lastConnectionDate: DateTime
haveSeenFavoriteModal: boolean
......
......@@ -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<{
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment