diff --git a/src/enum/updateUserSeason.enum.ts b/src/enum/updateUserSeason.enum.ts new file mode 100644 index 0000000000000000000000000000000000000000..c1b342adefab27d3578ead680c2b2bbddae908ba --- /dev/null +++ b/src/enum/updateUserSeason.enum.ts @@ -0,0 +1,6 @@ +export enum UpdateUserSeason { + QUIZ = 0, + MISSION = 1, + BOSS = 2, + END = 3, +} diff --git a/src/enum/userBoss.enum.ts b/src/enum/userBoss.enum.ts new file mode 100644 index 0000000000000000000000000000000000000000..d526b31fa2d48207ee0896b92267fbe57071faae --- /dev/null +++ b/src/enum/userBoss.enum.ts @@ -0,0 +1,6 @@ +export enum UserBossState { + LOCKED = 0, + UNLOCKED = 1, + ONGOING = 2, + DONE = 3, +} diff --git a/src/models/boss.model.ts b/src/models/boss.model.ts new file mode 100644 index 0000000000000000000000000000000000000000..b1265acc20488409bcb58e41bb2fdc7cec6012ce --- /dev/null +++ b/src/models/boss.model.ts @@ -0,0 +1,20 @@ +import { DateTime, Duration } from 'luxon' +import { UserBossState } from 'enum/userBoss.enum' + +export interface BossEntity { + id: string + title: string + description: string + duration: Duration + treshold: number +} +export interface Boss { + id: string + title: string + description: string + duration: Duration + treshold: number + state: UserBossState // Todo enum + startDate: DateTime | null + consumption: number +} diff --git a/src/models/index.ts b/src/models/index.ts index 15d286eab75b8692dbbdb406dd265647344717df..63a879235f5b2c0cfe760a362d6196e08719a318 100644 --- a/src/models/index.ts +++ b/src/models/index.ts @@ -12,6 +12,7 @@ export * from './modal.model' export * from './profile.model' export * from './report.model' export * from './season.model' +export * from './boss.model' export * from './timePeriod.model' export * from './trigger.model' export * from './userInstanceSettings.model' diff --git a/src/models/season.model.ts b/src/models/season.model.ts index 54129b263ac5f53bb2eb15ebee9387cbaee3e7e4..3b62f0c21d7270159e58556b5243dd9841096993 100644 --- a/src/models/season.model.ts +++ b/src/models/season.model.ts @@ -1,4 +1,5 @@ import { UserSeasonState, UserSeasonSuccess } from 'enum/userSeason.enum' +import { Boss, BossEntity } from 'models/boss.model' import { DateTime, Duration } from 'luxon' export interface SeasonState { @@ -11,39 +12,24 @@ export interface SeasonEntity { description: string duration: Duration target: number - boss: BossEntity[] + boss: BossEntity quizType: string } +// TODO Link icĂ´ne export interface UserSeason { id: string + title: string + description: string state: UserSeasonState duration: Duration target: number progress: number - boss: Boss[] + boss: Boss success: UserSeasonSuccess startDate: DateTime | null endingDate: DateTime | null quiz: null } -export interface BossEntity { - id: string - title: string - description: string - duration: Duration - treshold: number -} -export interface Boss { - id: string - title: string - description: string - duration: Duration - treshold: number - state: 'locked' | 'unlocked' | 'onGoing' | 'done' - start_date: DateTime - consumption: number -} - // export interface Quiz {} diff --git a/src/services/boss.service.ts b/src/services/boss.service.ts new file mode 100644 index 0000000000000000000000000000000000000000..98e55e718b48ef6d8d7b55d6d42f954b22f8bb00 --- /dev/null +++ b/src/services/boss.service.ts @@ -0,0 +1,66 @@ +import { Client, QueryDefinition, QueryResult, Q } from 'cozy-client' +import { BOSS_DOCTYPE } from 'doctypes/com-grandlyon-ecolyo-boss' +import { Boss, BossEntity } from 'models/boss.model' +import { UserBossState } from 'enum/userBoss.enum' + +export default class BossService { + private readonly _client: Client + + constructor(_client: Client) { + this._client = _client + } + + public async getAllBossEntities(): Promise<BossEntity[]> { + const query: QueryDefinition = Q(BOSS_DOCTYPE) + const { + data: bosses, + }: QueryResult<BossEntity[]> = await this._client.query(query) + return bosses + } + + public async deleteAllBossEntities(): Promise<boolean> { + try { + const bosses = await this.getAllBossEntities() + if (!bosses) return true + for (let index = 0; index < bosses.length; index++) { + await this._client.destroy(bosses[index]) + } + return true + } catch (err) { + console.log(err) + throw err + } + } + + public async startUserBoss(userBoss: Boss): Promise<Boss> { + /* + + */ + + return userBoss + } + + public formatToUserBoss(boss: BossEntity): Boss { + const userBoss: Boss = { + id: boss.id, + title: boss.title, + description: boss.description, + duration: boss.duration, + treshold: boss.treshold, + state: UserBossState.LOCKED, + startDate: null, + consumption: 0, + } + return userBoss + } + public uptadeUserBossConsumption( + boss: Boss, + newConsumptionValue: number + ): Boss { + const userBoss: Boss = { + ...boss, + consumption: newConsumptionValue, + } + return userBoss + } +} diff --git a/src/services/initialization.service.ts b/src/services/initialization.service.ts index 12aae6949ec85c3bfbe24d710a2fb9df6dd8336d..ce0a6c2e4f8b42b5b1312720c7f6c4ff748c4dd2 100644 --- a/src/services/initialization.service.ts +++ b/src/services/initialization.service.ts @@ -38,6 +38,9 @@ import { hashFile } from 'utils/hash' import { getActualReportDate } from 'utils/date' import { TimeStep } from 'enum/timeStep.enum' +import { UserSeason } from 'models/season.model' +import BossService from './boss.service' + export default class InitializationService { private readonly _client: Client @@ -407,10 +410,11 @@ export default class InitializationService { }> { const hashBossEntity = hashFile(bossEntityData) const seasonService = new SeasonService(this._client) + const bossService = new BossService(this._client) const profileService = new ProfileService(this._client) // Populate data if none BossEntity exists - const loadedBossTypes = await seasonService.getAllBossEntities() + const loadedBossTypes = await bossService.getAllBossEntities() if (!loadedBossTypes || (loadedBossTypes && loadedBossTypes.length === 0)) { // Populate the doctype with data try { @@ -418,7 +422,7 @@ export default class InitializationService { await this._client.create(BOSS_DOCTYPE, bossEntityData[i]) } // Check of created document - const checkCount = await seasonService.getAllBossEntities() + const checkCount = await bossService.getAllBossEntities() if ( !checkCount || (checkCount && checkCount.length !== bossEntityData.length) @@ -454,7 +458,7 @@ export default class InitializationService { // Update the doctype try { // Deletion of all documents - await seasonService.deleteAllBossEntities() + await bossService.getAllBossEntities() // Population with the data await Promise.all( bossEntityData.map(async bossEntity => { @@ -462,7 +466,7 @@ export default class InitializationService { }) ) // Check of created document - const checkCount = await seasonService.getAllBossEntities() + const checkCount = await bossService.getAllBossEntities() if ( !checkCount || (checkCount && checkCount.length !== bossEntityData.length) diff --git a/src/services/season.service.ts b/src/services/season.service.ts index b217d182f7e1c8c2f0870894bd7981a60cc52ea3..814691880cf3f389996170831f518dbbf9381775 100644 --- a/src/services/season.service.ts +++ b/src/services/season.service.ts @@ -1,7 +1,10 @@ import { Client, QueryDefinition, QueryResult, Q } from 'cozy-client' import { SEASON_DOCTYPE, USERSEASON_DOCTYPE } from 'doctypes' -import { SeasonEntity, BossEntity, UserSeason } from 'models/season.model' -import { BOSS_DOCTYPE } from 'doctypes/com-grandlyon-ecolyo-boss' +import { SeasonEntity, UserSeason } from 'models/season.model' +import { Boss, BossEntity } from 'models/boss.model' +import { UserSeasonState, UserSeasonSuccess } from 'enum/userSeason.enum' +import BossService from './boss.service' +import { UpdateUserSeason } from 'enum/updateUserSeason.enum' export default class SeasonService { private readonly _client: Client @@ -10,24 +13,29 @@ export default class SeasonService { this._client = _client } - private isAllSeasonLocked(userSeasons: UserSeason[]): UserSeason[] { + public isAllSeasonLocked(userSeasons: UserSeason[]): UserSeason[] { let isAllLocked = true userSeasons.forEach(season => { - if (season.state != 'locked') isAllLocked = false + if (season.state != UserSeasonState.LOCKED) isAllLocked = false }) - if (isAllLocked) userSeasons[0].state = 'unlocked' + if (isAllLocked) userSeasons[0].state = UserSeasonState.UNLOCKED return userSeasons } - private formatToUserSeason(season: SeasonEntity): UserSeason { + public formatToUserSeason(season: SeasonEntity): UserSeason { + const bossService = new BossService(this._client) + + const userBoss = bossService.formatToUserBoss(season.boss) const userSeason: UserSeason = { id: season.id, + title: season.title, + description: season.description, duration: season.duration, target: season.target, - boss: season.boss, - state: 'locked', + boss: userBoss, + state: UserSeasonState.LOCKED, progress: 0, - success: null, + success: UserSeasonSuccess.ONGOING, startDate: null, endingDate: null, quiz: null, @@ -35,7 +43,7 @@ export default class SeasonService { return userSeason } - public async buildSeasonList(): Promise<UserSeason[]> { + public async buildUserSeasonList(): Promise<UserSeason[]> { /** * Query UserSeason & SeasonEntities * for each match -> push UserSeason into list @@ -105,14 +113,6 @@ export default class SeasonService { } } - public async getAllBossEntities(): Promise<BossEntity[]> { - const query: QueryDefinition = Q(BOSS_DOCTYPE) - const { - data: bosses, - }: QueryResult<BossEntity[]> = await this._client.query(query) - return bosses - } - public async getAllUserSeasonsEntities(): Promise<UserSeason[]> { const query: QueryDefinition = Q(USERSEASON_DOCTYPE) const { @@ -121,39 +121,44 @@ export default class SeasonService { return userSeasons } - public async deleteAllBossEntities(): Promise<boolean> { - try { - const bosses = await this.getAllBossEntities() - if (!bosses) return true - for (let index = 0; index < bosses.length; index++) { - await this._client.destroy(bosses[index]) - } - return true - } catch (err) { - console.log(err) - throw err - } + public async startUserSeason(userSeason: UserSeason): Promise<boolean> { + /* + Passer une selected userSeason ONGOING + 0 Progress + Start Date / End Date from duration + Ongoing Success + Quizz . + create userSeason en base + */ + return true } - public async initUserSeason(userSeasonData: UserSeason[]): Promise<boolean> { - try { - for (let i = 0; i <= userSeasonData.length - 1; i++) { - await this._client.create(USERSEASON_DOCTYPE, userSeasonData[i]) - } - // Check of created document - const checkCount = await this.getAllUserSeasonsEntities() - if ( - !checkCount || - (checkCount && checkCount.length !== userSeasonData.length) - ) { - throw new Error( - 'initUserSeason: Created user season entities does not match' - ) - } - } catch (error) { - console.log('userSeason create error: ', error) - throw error - } + public async updateUserSeason( + userSeason: UserSeason, + flag: UpdateUserSeason + ): Promise<boolean> { + /* + Update Quiz + Update Mission + Update Boss + Update Season (END of season) + */ + return true } + + public async isUserSeasonOver(userSeason: UserSeason): Promise<boolean> { + /* + if progress done -> no boss -> bossAvailable + if date over -> no boss -> done -> set status + -> boss going -> isBossover() + + */ + + return true + } + + // public async getCurrentUserSeason(): Promise<UserSeason> { + + // } }