diff --git a/src/components/Season/SeasonView.tsx b/src/components/Season/SeasonView.tsx index 4a3944b2fc8e2f889bf5355d399d3f82366df12b..2ba6bc821cb152b99a1fc73cdf99bb41ffcd7b24 100644 --- a/src/components/Season/SeasonView.tsx +++ b/src/components/Season/SeasonView.tsx @@ -9,7 +9,7 @@ import RightArrowIcon from 'assets/icons/ico/right-arrow.svg' import './season.scss' import { UserSeason } from 'models' -import { userSeasonMock } from '../../../test/__mocks__/userSeasonData.mock' +import { userSeasonData } from '../../../test/__mocks__/userSeasonData.mock' import { useSelector } from 'react-redux' import { EcolyoState } from 'store' @@ -29,7 +29,7 @@ const SeasonView: React.FC = () => { } const [userSeasonLists, setUserSeasonList] = useState<UserSeason[]>( - userSeasonMock + userSeasonData ) const resetValues = () => { //Method used to cancel a swipe on a simple click diff --git a/src/db/seasons/bossEntity.json b/src/db/seasons/bossEntity.json index f56009d0681baeeed1858f3fda24da3f748d709c..ca3c79918b6197c5543463ddd97cdabeaa5c2c5a 100644 --- a/src/db/seasons/bossEntity.json +++ b/src/db/seasons/bossEntity.json @@ -6,4 +6,4 @@ "duration": { "days": 7 }, "treshold": 40 } -] \ No newline at end of file +] diff --git a/src/db/seasons/seasonEntity.json b/src/db/seasons/seasonEntity.json index e4be2647ad958d9d4f721a6c5b9a7c9ca3242743..c8cc651684947e0a692c9ad1c260521d9fd816d6 100644 --- a/src/db/seasons/seasonEntity.json +++ b/src/db/seasons/seasonEntity.json @@ -6,9 +6,9 @@ "duration": { "days": 3 }, "target": 40, "relationships": { - "boss": [ - { "_id": "BO001", "_type": "com.grandlyon.ecolyo.boss" } - ] + "boss": { + "data": { "_id": "BO001", "_type": "com.grandlyon.ecolyo.boss" } + } }, "quizType": "Culture Générale" }, @@ -19,9 +19,9 @@ "duration": { "days": 3 }, "target": 40, "relationships": { - "boss": [ - { "_id": "BO002", "_type": "com.grandlyon.ecolyo.boss" } - ] + "boss": { + "data": { "_id": "BO002", "_type": "com.grandlyon.ecolyo.boss" } + } }, "quizType": "Culture Générale" }, @@ -32,9 +32,9 @@ "duration": { "days": 3 }, "target": 40, "relationships": { - "boss": [ - { "_id": "BO003", "_type": "com.grandlyon.ecolyo.boss" } - ] + "boss": { + "data": { "_id": "BO003", "_type": "com.grandlyon.ecolyo.boss" } + } }, "quizType": "Culture Générale" } diff --git a/src/doctypes/index.ts b/src/doctypes/index.ts index b5ad51b8d6bbeeda13d27835d034ed75bcbbbf9c..f5c3fd47e1841215bed2021ba301f4f1a1394940 100644 --- a/src/doctypes/index.ts +++ b/src/doctypes/index.ts @@ -58,7 +58,12 @@ const doctypes = { season: { doctype: SEASON_DOCTYPE, attributes: {}, - relationships: {}, + relationships: { + boss: { + doctype: BOSS_DOCTYPE, + type: 'has-one', + }, + }, }, userseason: { doctype: USERSEASON_DOCTYPE, diff --git a/src/services/boss.service.ts b/src/services/boss.service.ts index 98e55e718b48ef6d8d7b55d6d42f954b22f8bb00..1d31b4b1e516744c05e4bfea719cc78380e52c0c 100644 --- a/src/services/boss.service.ts +++ b/src/services/boss.service.ts @@ -17,6 +17,15 @@ export default class BossService { }: QueryResult<BossEntity[]> = await this._client.query(query) return bosses } + public async getBossEntityById(bossId: string): Promise<BossEntity> { + const query: QueryDefinition = Q(BOSS_DOCTYPE) + const { + data: [boss], + }: QueryResult<BossEntity> = await this._client.query( + query.where({ _id: bossId }) + ) + return boss + } public async deleteAllBossEntities(): Promise<boolean> { try { @@ -41,6 +50,8 @@ export default class BossService { } public formatToUserBoss(boss: BossEntity): Boss { + console.log('boss', boss) + const userBoss: Boss = { id: boss.id, title: boss.title, diff --git a/src/services/season.service.ts b/src/services/season.service.ts index 64289d4d0bbf097ddd95e65a224d3d7c4f135cf2..9b4969ff46c51b84ac8b95cff3a924eb0e810fb9 100644 --- a/src/services/season.service.ts +++ b/src/services/season.service.ts @@ -2,10 +2,17 @@ import { Client, QueryDefinition, QueryResult, Q } from 'cozy-client' import { SEASON_DOCTYPE, USERSEASON_DOCTYPE } from 'doctypes' import { SeasonEntity, UserSeason } from 'models/season.model' import { Boss, BossEntity } from 'models/boss.model' +import get from 'lodash/get' + import { UserSeasonState, UserSeasonSuccess } from 'enum/userSeason.enum' import BossService from './boss.service' import { UpdateUserSeason } from 'enum/updateUserSeason.enum' +export interface Relation { + _id: string + _type: string +} + export default class SeasonService { private readonly _client: Client @@ -22,17 +29,28 @@ export default class SeasonService { return userSeasons } - public formatToUserSeason(season: SeasonEntity): UserSeason { - const bossService = new BossService(this._client) + /** + * Get one relation in doc + * + * @param {object} doc - DocumentEntity + * @param {string} relName - Name of the relation + */ + public getRelationship<D>(doc: D, relName: string): Relation { + return get(doc, `relationships.${relName}[0]`, []) + } + + public formatToUserSeason(season: SeasonEntity, boss: Boss): UserSeason { + // const bossService = new BossService(this._client) + // const seasonBoss: Relation = this.getRelationship(season, 'boss') + // const bossEntity = await bossService.getBossEntityById(seasonBoss._id) - const userBoss: Boss = bossService.formatToUserBoss(season.boss) const userSeason: UserSeason = { id: season.id, title: season.title, description: season.description, duration: season.duration, target: season.target, - boss: userBoss, + boss: boss, state: UserSeasonState.LOCKED, progress: 0, success: UserSeasonSuccess.ONGOING, @@ -40,6 +58,8 @@ export default class SeasonService { endingDate: null, quiz: null, } + console.log('userSeason', userSeason) + return userSeason } @@ -50,22 +70,40 @@ export default class SeasonService { * else Push new UserSeason into list * return UserSeason List */ + const bossService = new BossService(this._client) + console.log('include') const querySeasonEntity: QueryDefinition = Q(SEASON_DOCTYPE) - const { - data: seasonEntity, - }: QueryResult<SeasonEntity[]> = await this._client.query(querySeasonEntity) - + .where({}) + .include(['boss']) + console.log('include2') + // const { + // data: seasonEntity, + // }: QueryResult<SeasonEntity[]> = await this._client.query(querySeasonEntity) + const result = await this._client.query(querySeasonEntity) + console.log('result', result) + console.log('include3') const queryUserSeason: QueryDefinition = Q(USERSEASON_DOCTYPE) const { data: userSeason, }: QueryResult<UserSeason[]> = await this._client.query(queryUserSeason) + console.log('seasonEntity', seasonEntity) + console.log('relationships', relationships) let buildList: UserSeason[] = [] + if (seasonEntity.length > 0 && userSeason.length === 0) { - seasonEntity.forEach(season => { - buildList.push(this.formatToUserSeason(season)) + seasonEntity.forEach(async season => { + console.log('season', season) + + // const seasonBoss: Relation = this.getRelationship(season, 'boss') + // const bossEntity = await bossService.getBossEntityById(seasonBoss._id) + + // console.log('bossEntity', bossEntity) + // const userBoss: Boss = bossService.formatToUserBoss(bossEntity) + // buildList.push(this.formatToUserSeason(season, userBoss)) }) buildList = this.isAllSeasonLocked(buildList) + console.log('buildList', buildList) return buildList } if (userSeason.length !== 0 && seasonEntity.length !== 0) { @@ -77,10 +115,11 @@ export default class SeasonService { } } else { // format seasonEntity to fit userSeason array - buildList.push(this.formatToUserSeason(season)) + // buildList.push(this.formatToUserSeason(season)) } }) buildList = this.isAllSeasonLocked(buildList) + return buildList } return buildList