diff --git a/src/components/ContentComponents/Report/Report.tsx b/src/components/ContentComponents/Report/Report.tsx index ed65ba4410f01c4d0e0159d6f6bbda5f8104869b..30f8649ab0dd2338aa5f7748d261b3c2168b61f6 100644 --- a/src/components/ContentComponents/Report/Report.tsx +++ b/src/components/ContentComponents/Report/Report.tsx @@ -1,7 +1,7 @@ import React, { useContext, useEffect } from 'react' import { translate } from 'cozy-ui/react/I18n' import { AppContext } from 'components/Contexts/AppContextProvider' -import UserProfileManager from 'services/userProfileDataManagerService' +import UserProfileService from 'services/userProfile.service' import { Client, withClient } from 'cozy-client' interface ReportProps { @@ -12,9 +12,11 @@ interface ReportProps { const Report: React.FC<ReportProps> = ({ t, client }: ReportProps) => { const { userProfile, setUserProfile } = useContext(AppContext) - const upm = new UserProfileManager(client) + const userProfileService = new UserProfileService(client) const updateUserProfileReport = async (value: boolean) => { - const updatedProfile = await upm.updateUserProfile({ report: value }) + const updatedProfile = await userProfileService.updateUserProfile({ + report: value, + }) if (updatedProfile) { setUserProfile(updatedProfile) } diff --git a/src/components/Contexts/AppContextProvider.tsx b/src/components/Contexts/AppContextProvider.tsx index d46c3198a395eb6782550842b5377db9d2435410..41d434c039097999b1820dc92e9bd5e2b0ef848f 100644 --- a/src/components/Contexts/AppContextProvider.tsx +++ b/src/components/Contexts/AppContextProvider.tsx @@ -4,7 +4,7 @@ import { FluidType } from 'enum/fluid.enum' import { ScreenType } from 'enum/screen.enum' import InitDataManager from 'services/initDataManagerService' -import UserProfileDataManager from 'services/userProfileDataManagerService' +import UserProfileService from 'services/userProfile.service' import { UserChallenge, UserProfile } from 'services/dataChallengeContracts' import { TimeStep } from 'services/dataConsumptionContracts' import { IFluidStatus } from 'services/fluidService' @@ -135,8 +135,8 @@ const AppContextProvider: React.FC<AppContextProviderProps> = ({ } const setWelcomeModalViewed = async () => { - const upm = new UserProfileDataManager(client) - const updatedUserProfile = await upm.updateUserProfile({ + const userProfileService = new UserProfileService(client) + const updatedUserProfile = await userProfileService.updateUserProfile({ haveSeenWelcomeModal: true, }) if (updatedUserProfile) { @@ -186,8 +186,8 @@ const AppContextProvider: React.FC<AppContextProviderProps> = ({ } const setNotificationEcogesture = async (ecogestureList: string[]) => { - const updm = new UserProfileDataManager(client) - const updatedUserProfile = await updm.updateUserProfile({ + const userProfileService = new UserProfileService(client) + const updatedUserProfile = await userProfileService.updateUserProfile({ notificationEcogesture: ecogestureList, }) if (updatedUserProfile) { @@ -203,7 +203,7 @@ const AppContextProvider: React.FC<AppContextProviderProps> = ({ handleResize() window.addEventListener('resize', handleResize) const im = new InitDataManager(client) - const updm = new UserProfileDataManager(client) + const userProfileService = new UserProfileService(client) async function loadData() { // Create missing indexes const resultIndexConsoData = await im.initIndex() @@ -278,7 +278,7 @@ const AppContextProvider: React.FC<AppContextProviderProps> = ({ } //Retrieve the UserProfile - const loadedUserProfile = await updm.getUserProfile() + const loadedUserProfile = await userProfileService.getUserProfile() if (!loadedUserProfile) { setError(true) } diff --git a/src/models/index.ts b/src/models/index.ts index 5b8c1a7e102c7c0aeacd0bb4fc7d457fc3dc5b3b..e5120970cb241c4e51c54e3c24c0b0a9dffb5f53 100644 --- a/src/models/index.ts +++ b/src/models/index.ts @@ -1,3 +1,4 @@ export * from './account.model' export * from './konnector.model' export * from './trigger.model' +export * from './userProfile.model' diff --git a/src/models/userProfile.model.ts b/src/models/userProfile.model.ts new file mode 100644 index 0000000000000000000000000000000000000000..3010f1f2b6a2f19d8de5191093673afaceed3996 --- /dev/null +++ b/src/models/userProfile.model.ts @@ -0,0 +1,11 @@ +import { UserProfile } from 'services/dataChallengeContracts' + +export interface UserProfile { + id: string + level: number + challengeTypeHash: string + ecogestureHash: string + haveSeenWelcomeModal: boolean + notificationEcogesture: string[] + report: boolean +} diff --git a/src/services/challengeDataManagerService.ts b/src/services/challengeDataManagerService.ts index 10740c590f1bbb2fdf04f6b014e960d6946f3555..34220e0be41de2004c3a3198f1789e42ae7e9e07 100644 --- a/src/services/challengeDataManagerService.ts +++ b/src/services/challengeDataManagerService.ts @@ -5,12 +5,12 @@ import { UserChallenge, ChallengeType, EcogestureType, - UserProfile, ChallengeState, TypeChallenge, BadgeState, } from './dataChallengeContracts' -import UserProfileDataManager from 'services/userProfileDataManagerService' +import { UserProfile } from 'models' +import UserProfileService from 'services/userProfile.service' import ChallengeDataMapper, { UserChallengeEntity, ChallengeTypeEntity, @@ -405,8 +405,8 @@ export default class ChallengeManager implements IChallengeManager { public async getAvailableChallenges( fluidTypes?: FluidType[] ): Promise<ChallengeType[] | null> { - const userProfileDataManager = new UserProfileDataManager(this._client) - const userProfile: UserProfile | null = await userProfileDataManager.getUserProfile() + const userProfileService = new UserProfileService(this._client) + const userProfile: UserProfile | null = await userProfileService.getUserProfile() if (fluidTypes && fluidTypes.length === 0) { fluidTypes = [0, 1, 2] diff --git a/src/services/dataChallengeContracts.ts b/src/services/dataChallengeContracts.ts index 11446ff3c52f6cc2aa56722a0d0a4fefee022001..f9fbec23f8480219969a7137efa3674f697652cb 100644 --- a/src/services/dataChallengeContracts.ts +++ b/src/services/dataChallengeContracts.ts @@ -134,66 +134,3 @@ export class UserChallenge { return false } } - -export class UserProfile { - id: string - level: number - challengeTypeHash: string - ecogestureHash: string - haveSeenWelcomeModal: boolean - notificationEcogesture: string[] - report: boolean - - constructor( - id: string, - level: number, - challengeTypeHash: string, - ecogestureHash: string, - haveSeenWelcomeModal: boolean, - notificationEcogesture: string[], - report: boolean - ) { - this.id = id - this.level = level - this.challengeTypeHash = challengeTypeHash - this.ecogestureHash = ecogestureHash - this.haveSeenWelcomeModal = haveSeenWelcomeModal - this.notificationEcogesture = notificationEcogesture - this.report = report - } -} - -// eslint-disable-next-line @typescript-eslint/interface-name-prefix -export interface IChallengeManager { - getCurrentChallenge(withEcogestures?: boolean): Promise<UserChallenge | null> - - updateChallengeState( - id: string, - newState: number - ): Promise<UserChallenge | null> - cancelChallenge(id: string): Promise<UserChallenge | null> - - // fulfillChallenge(challenge: UserChallenge): Promise<UserChallenge | null> - - startChallenge( - challenge: ChallengeType, - fluidTypes: FluidType[], - selectedEcogestes: EcogestureType[] - ): Promise<UserChallenge | null> - - getAvailableChallenges( - fluidTypes?: FluidType[] - ): Promise<ChallengeType[] | null> - - // getUnlockedBadges(): Promise<ChallengeType[] | null> - - getUnlockedEcogestures(): Promise<EcogestureType[] | null> -} - -// eslint-disable-next-line @typescript-eslint/interface-name-prefix -export interface IUserProfileManager { - getUserProfile(): Promise<UserProfile | null> - updateUserProfile(attributes: { - [key: string]: string - }): Promise<UserProfile | null> -} diff --git a/src/services/initDataManagerService.ts b/src/services/initDataManagerService.ts index ac20ee4cf5dc40e5c884dd6d7eef7f93a67198bb..110a694f658cc8110538f8ddc3ea6b6c4184f632 100644 --- a/src/services/initDataManagerService.ts +++ b/src/services/initDataManagerService.ts @@ -17,23 +17,21 @@ import { GRDF_YEAR_DOCTYPE, } from 'doctypes' +import { UserProfile } from 'models' + import ChallengeDataManager from 'services/challengeDataManagerService' import challengeTypeData from 'db/challengeTypeData.json' import userChallengeData from 'db/userChallengeData.json' import EcogestureDataManager from 'services/ecogestureDataManagerService' import ecogestureData from 'db/ecogestureData.json' -import UserProfileDataManager from 'services/userProfileDataManagerService' +import UserProfileService from 'services/userProfile.service' import userProfileData from 'db/userProfileData.json' import KonnectorStatusService from 'services/konnectorStatusService' import KonnectorService from 'services/konnectorService' import AccountService from 'services/account.service' import { hashFile } from 'utils/hash' -import { - UserProfile, - TypeChallenge, - UserChallenge, -} from 'services/dataChallengeContracts' +import { TypeChallenge, UserChallenge } from 'services/dataChallengeContracts' import { FluidType } from 'enum/fluid.enum' import { DateTime } from 'luxon' import { FluidService, IFluidStatus } from 'services/fluidService' @@ -48,7 +46,7 @@ export default class InitDataManager { public async checkChallengeType(hash: string): Promise<boolean> { const hashChanllengeType = hashFile(challengeTypeData) const cdm = new ChallengeDataManager(this._client) - const updm = new UserProfileDataManager(this._client) + const userProfileService = new UserProfileService(this._client) // Populate data if none challengeType exists const loadedChallengeTypes = await cdm.getAllChallengeTypeEntities() @@ -76,7 +74,7 @@ export default class InitDataManager { return false } // Update userProfil with the hash - const updatedUserProfil = await updm.updateUserProfile({ + const updatedUserProfil = await userProfileService.updateUserProfile({ challengeTypeHash: hashChanllengeType, }) if (!updatedUserProfil) { @@ -116,7 +114,7 @@ export default class InitDataManager { return false } // Update userProfil with the hash - const updatedUserProfil = await updm.updateUserProfile({ + const updatedUserProfil = await userProfileService.updateUserProfile({ challengeTypeHash: hashChanllengeType, }) if (!updatedUserProfil) { @@ -136,7 +134,7 @@ export default class InitDataManager { public async checkEcogesture(hash: string): Promise<boolean> { const hashEcogestureType = hashFile(ecogestureData) const edm = new EcogestureDataManager(this._client) - const updm = new UserProfileDataManager(this._client) + const userProfileService = new UserProfileService(this._client) // Populate data if none ecogesture exists const loadedEcogestures = await edm.getAllEcogestures() @@ -165,7 +163,7 @@ export default class InitDataManager { return false } // Update userProfil with the hash - const updatedUserProfil = await updm.updateUserProfile({ + const updatedUserProfil = await userProfileService.updateUserProfile({ ecogestureHash: hashEcogestureType, }) if (!updatedUserProfil) { @@ -205,7 +203,7 @@ export default class InitDataManager { return false } // Update userProfil with the hash - const updatedUserProfil = await updm.updateUserProfile({ + const updatedUserProfil = await userProfileService.updateUserProfile({ ecogestureHash: hashEcogestureType, }) if (!updatedUserProfil) { @@ -260,14 +258,14 @@ export default class InitDataManager { * failure return: null */ public async checkUserProfile(): Promise<UserProfile | null> { - const updm = new UserProfileDataManager(this._client) - const loadedUserProfile = await updm.getUserProfile() + const userProfileService = new UserProfileService(this._client) + const loadedUserProfile = await userProfileService.getUserProfile() if (!loadedUserProfile) { try { // Population with the data await this._client.create(USERPROFILE_DOCTYPE, userProfileData[0]) // Check of created document - const checkUserProfile = await updm.getUserProfile() + const checkUserProfile = await userProfileService.getUserProfile() return checkUserProfile } catch (error) { console.log('Context error: ', error) diff --git a/src/services/userProfileDataManagerService.ts b/src/services/userProfile.service.ts similarity index 77% rename from src/services/userProfileDataManagerService.ts rename to src/services/userProfile.service.ts index 36ae18ad1f5260e5347256d06fab81665ce3166b..f356597974fcb1d19a101a4a38b78863dadb15a6 100644 --- a/src/services/userProfileDataManagerService.ts +++ b/src/services/userProfile.service.ts @@ -1,22 +1,18 @@ import { Client } from 'cozy-client' -import { IUserProfileManager, UserProfile } from './dataChallengeContracts' +import { UserProfile } from 'models' import { USERPROFILE_DOCTYPE } from 'doctypes' -export default class UserProfileManager implements IUserProfileManager { +export default class UserProfileService { private readonly _client: Client - //private readonly _queryRunner: QueryRunner - constructor(_client: Client) { this._client = _client - //this._queryRunner = new QueryRunner(this._client) } public async getUserProfile(): Promise<UserProfile | null> { const { data: userProfiles } = await this._client.query( this._client.find(USERPROFILE_DOCTYPE).limitBy(1) ) - return userProfiles[0] ? userProfiles[0] : null } diff --git a/src/targets/services/monthlyReport.ts b/src/targets/services/monthlyReport.ts index 70da2644af930a21c880b21886e2409c18dec11b..4deee8c4c220924de3f0119979ffa82038f7b564 100644 --- a/src/targets/services/monthlyReport.ts +++ b/src/targets/services/monthlyReport.ts @@ -5,7 +5,7 @@ import { DateTime } from 'luxon' import { runService } from './service' import { createEmail } from './createEmail' import { TimePeriod } from 'services/dataConsumptionContracts' -import UserProfileManager from 'services/userProfileDataManagerService' +import UserProfileService from 'services/userProfile.service' import ConsumptionDataManager from 'services/consumptionDataManagerService' import PerformanceIndicatorAggregateCalculator from 'services/performanceIndicatorAggregateCalculatorService' @@ -17,7 +17,7 @@ interface MonthlyReportProps { const monthlyReport = async ({ client }: MonthlyReportProps) => { log('info', 'Fetching user profile...') - const upm = new UserProfileManager(client) + const upm = new UserProfileService(client) const userProfil = await upm.getUserProfile() if (!userProfil || !userProfil.report) { log('info', 'End of process - Report disabled in user profile')