diff --git a/src/models/ecogesture.model.ts b/src/models/ecogesture.model.ts index 4eb05ed763f96513b9499e752a3885845441ecda..843c748b85a86d5e727c7e449db3ee69f4e01a2f 100644 --- a/src/models/ecogesture.model.ts +++ b/src/models/ecogesture.model.ts @@ -11,4 +11,7 @@ export interface Ecogesture { nwh: number pack: number unlocked?: boolean + _id: string + _rev?: string + _type?: string } diff --git a/src/models/userProfile.model.ts b/src/models/userProfile.model.ts index a009f04f0672ec93ea953f9f0cb7fe68109ae5db..237cc7541c3c5263d40e74f01ffc79b6b5ed4b7a 100644 --- a/src/models/userProfile.model.ts +++ b/src/models/userProfile.model.ts @@ -8,7 +8,7 @@ export interface UserProfile { ecogestureHash: string haveSeenFavoriteModal: boolean isFirstConnection: boolean - haveSeenOldFluidModal: DateTime | false + haveSeenOldFluidModal: DateTime | boolean notificationEcogesture: string[] report: ReportAttributes } diff --git a/src/services/__mocks__/userProfile.json b/src/services/__mocks__/userProfile.json new file mode 100644 index 0000000000000000000000000000000000000000..930134512b606f08e3d6557ad3f076b0af657728 --- /dev/null +++ b/src/services/__mocks__/userProfile.json @@ -0,0 +1,17 @@ +{ + "_id": "4d9403218ef13e65b2e3a8ad1700bc41", + "_rev": "16-57473da4fc26315247c217083175dfa0", + "challengeTypeHash": "1136feb6185c7643e071d14180c0e95782aa4ba3", + "ecogestureHash": "9798a0aaccb47cff906fc4931a2eff5f9371dd8b", + "haveSeenFavoriteModal": false, + "haveSeenOldFluidModal": false, + "id": "4d9403218ef13e65b2e3a8ad1700bc41", + "isFirstConnection": true, + "level": 1, + "notificationEcogesture": ["0085", "0092"], + "report": { + "haveSeenLastReport": true, + "monthlyReportDate": "2020-11-03T00:00:00.000+01:00", + "sendReportNotification": false + } +} diff --git a/src/services/initialization.service.spec.ts b/src/services/initialization.service.spec.ts new file mode 100644 index 0000000000000000000000000000000000000000..bde3373d30f4cc6fee67635480b9d4e97f2b221f --- /dev/null +++ b/src/services/initialization.service.spec.ts @@ -0,0 +1,154 @@ +import { QueryResult } from 'cozy-client' +import { DateTime } from 'luxon' +import { UserProfile } from 'models' +import InitializationService from './initialization.service' +import mockClient from './__mocks__/client' +import userProfileEntityData from './__mocks__/userProfile.json' + +const mockCreateIndexKonnector = jest.fn() +jest.mock('./konnector.service', () => { + return jest.fn(() => { + return { + createIndexKonnector: mockCreateIndexKonnector, + } + }) +}) + +const mockCreateIndexAccount = jest.fn() +jest.mock('./account.service', () => { + return jest.fn(() => { + return { + createIndexAccount: mockCreateIndexAccount, + } + }) +}) + +const mockGetUserProfile = jest.fn() +jest.mock('./userProfile.service', () => { + return jest.fn(() => { + return { + getUserProfile: mockGetUserProfile, + } + }) +}) + +const userProfileData = { + ...userProfileEntityData, + report: { + ...userProfileEntityData.report, + monthlyReportDate: DateTime.fromISO( + userProfileEntityData.report.monthlyReportDate + ), + }, +} + +describe('Initialization service', () => { + const initializationService = new InitializationService(mockClient) + + describe('initIndex method', () => { + it('shoud return true when all indexes created', async () => { + const mockQueryResult: QueryResult<boolean> = { + data: true, + bookmark: '', + next: false, + skip: 0, + } + mockClient.query.mockResolvedValue(mockQueryResult) + mockCreateIndexKonnector.mockResolvedValue(mockQueryResult) + mockCreateIndexAccount.mockResolvedValue(mockQueryResult) + const result: boolean = await initializationService.initIndex() + expect(result).toBe(true) + }) + + it('shoud throw error when an index is not created', async () => { + let error + const mockQueryResult: QueryResult<boolean> = { + data: true, + bookmark: '', + next: false, + skip: 0, + } + mockClient.query.mockResolvedValue(mockQueryResult) + mockCreateIndexKonnector.mockRejectedValue(new Error()) + mockCreateIndexAccount.mockResolvedValue(mockQueryResult) + try { + await initializationService.initIndex() + } catch (err) { + error = err + } + expect(error).toEqual(new Error()) + }) + }) + + describe('initUserProfile method', () => { + it('shoud return the userProfil when existing', async () => { + mockGetUserProfile.mockResolvedValue(userProfileData) + const result: UserProfile | null = await initializationService.initUserProfile() + expect(result).toEqual(userProfileData) + }) + + it('shoud create and return the userProfil when no existing', async () => { + const mockQueryResult: QueryResult<boolean> = { + data: true, + bookmark: '', + next: false, + skip: 0, + } + mockGetUserProfile + .mockResolvedValueOnce(null) + .mockResolvedValueOnce(userProfileData) + mockClient.create.mockResolvedValue(mockQueryResult) + const result: UserProfile | null = await initializationService.initUserProfile() + expect(result).toEqual(userProfileData) + }) + + it('shoud throw error when the userProfile is not created', async () => { + const mockQueryResult: QueryResult<boolean> = { + data: true, + bookmark: '', + next: false, + skip: 0, + } + mockGetUserProfile.mockResolvedValueOnce(null).mockResolvedValueOnce(null) + mockClient.create.mockResolvedValue(mockQueryResult) + let error + try { + await initializationService.initUserProfile() + } catch (err) { + error = err + } + expect(error).toEqual( + new Error('initUserProfile: UserProfile not created') + ) + }) + + it('shoud throw error when the userProfile could not be fetched', async () => { + mockGetUserProfile.mockRejectedValueOnce(new Error()) + let error + try { + await initializationService.initUserProfile() + } catch (err) { + error = err + } + expect(error).toEqual(new Error()) + }) + + it('shoud throw error when the userProfile failed to be created', async () => { + const mockQueryResult: QueryResult<UserProfile> = { + data: userProfileData, + bookmark: '', + next: false, + skip: 0, + } + mockGetUserProfile.mockResolvedValueOnce(null) + mockClient.create.mockRejectedValueOnce(new Error()) + let error + try { + await initializationService.initUserProfile() + } catch (err) { + error = err + } + expect(error).toEqual(new Error()) + }) + }) +}) diff --git a/src/services/initialization.service.ts b/src/services/initialization.service.ts index 177def8bb835449ee05ff7401f1612eb9d305d33..bc1289a92bfb8e56c57081e4f05b11e33938dba1 100644 --- a/src/services/initialization.service.ts +++ b/src/services/initialization.service.ts @@ -1,4 +1,4 @@ -import { Client } from 'cozy-client' +import { Client, Q, QueryDefinition } from 'cozy-client' import { CHALLENGETYPE_DOCTYPE, ECOGESTURE_DOCTYPE, @@ -50,23 +50,21 @@ export default class InitializationService { /* * Call a query with where clause to create the index if not exist */ - public async createIndex(doctype: string): Promise<object> { - return await this._client.query( - this._client - .find(doctype) - .where({ - year: { - $lte: 9999, - }, - month: { - $lte: 12, - }, - day: { - $lte: 31, - }, - }) - .limitBy(1) - ) + private async createIndex(doctype: string): Promise<object> { + const query: QueryDefinition = Q(doctype) + .where({ + year: { + $lte: 9999, + }, + month: { + $lte: 12, + }, + day: { + $lte: 31, + }, + }) + .limitBy(1) + return await this._client.query(query) } /* diff --git a/src/utils/hash.ts b/src/utils/hash.ts index 89b4eadadd5cdf4f58e44b9c1225bd3e1bd30908..5ce23aa0cfeac129e942bd629e3470a8529d3a79 100644 --- a/src/utils/hash.ts +++ b/src/utils/hash.ts @@ -1,8 +1,9 @@ +import { ChallengeType, Ecogesture } from 'models' import hash from 'object-hash' /*** * sha1 hex encoding (default) */ -export function hashFile(file: File) { +export function hashFile(file: any) { return hash(file) }