diff --git a/src/atoms/userProfile.state.ts b/src/atoms/userProfile.state.ts index ef16b1834eb0b1df0188e9c5e94beff87f02f597..029a9cc279685625551fbbe7c2b899c2e2c8f024 100644 --- a/src/atoms/userProfile.state.ts +++ b/src/atoms/userProfile.state.ts @@ -1,3 +1,4 @@ +import { DateTime } from 'luxon' import { UserProfile } from 'models' import { atom } from 'recoil' @@ -11,6 +12,10 @@ export const userProfileState = atom<UserProfile>({ haveSeenWelcomeModal: false, haveSeenOldFluidModal: false, notificationEcogesture: [], - sendReportNotification: false, + report: { + sendReportNotification: false, + haveSeenLastReport: true, + monthlyReportDate: DateTime.fromISO('0000-00-00T00:00:00.000Z'), + }, }, }) diff --git a/src/components/Options/ReportOptions.tsx b/src/components/Options/ReportOptions.tsx index 493bb50c1fe0dbb8acf0b302f2eba6fe6890e55b..bcd3b5236a5071b07ce9ef295fe8c1703d7f3be2 100644 --- a/src/components/Options/ReportOptions.tsx +++ b/src/components/Options/ReportOptions.tsx @@ -16,11 +16,20 @@ const ReportOptions: React.FC = () => { const updateUserProfileReport = useCallback( async (value: boolean) => { const userProfileService = new UserProfileService(client) - await userProfileService - .updateUserProfile({ sendReportNotification: value }) - .then(updatedUserProfile => { - updatedUserProfile && setUserProfile(updatedUserProfile) - }) + const reportAttributes = { + sendReportNotification: value, + haveSeenLastReport: userProfile.report.haveSeenLastReport, + monthlyReportDate: userProfile.report.monthlyReportDate, + } + try { + await userProfileService + .updateUserProfile({ report: reportAttributes }) + .then(updatedUserProfile => { + updatedUserProfile && setUserProfile(updatedUserProfile) + }) + } catch (err) { + console.log(err) + } }, [setUserProfile] ) @@ -46,7 +55,8 @@ const ReportOptions: React.FC = () => { value="true" onChange={handleChange} checked={ - userProfile && userProfile.sendReportNotification === true + userProfile && + userProfile.report.sendReportNotification === true ? true : false } @@ -61,7 +71,8 @@ const ReportOptions: React.FC = () => { value="false" onChange={handleChange} checked={ - userProfile && userProfile.sendReportNotification === false + userProfile && + userProfile.report.sendReportNotification === false ? true : false } diff --git a/src/db/userProfileData.json b/src/db/userProfileData.json index d1699a2202ac55b2804deaabba6b6b24a7d26568..6e9c6ab3fa68ee7c790cfd7658aa53329aea9ae7 100644 --- a/src/db/userProfileData.json +++ b/src/db/userProfileData.json @@ -6,6 +6,10 @@ "haveSeenWelcomeModal": false, "haveSeenOldFluidModal": false, "notificationEcogesture": ["0085", "0092"], - "sendReportNotification": false + "report": { + "sendReportNotification": false, + "haveSeenLastReport": true, + "monthlyReportDate": "0000-00-00T00:00:00.000Z" + } } ] diff --git a/src/models/index.ts b/src/models/index.ts index 88c73990f4f8351e79fc372dbd9774464e174413..a3b55612c5d1a66d798cf120c9c677e87481439e 100644 --- a/src/models/index.ts +++ b/src/models/index.ts @@ -8,6 +8,7 @@ export * from './fluid.model' export * from './indicator.model' export * from './konnector.model' export * from './modal.model' +export * from './report.model' export * from './timePeriod.model' export * from './trigger.model' export * from './userProfile.model' diff --git a/src/models/report.model.ts b/src/models/report.model.ts new file mode 100644 index 0000000000000000000000000000000000000000..c9c4caaf7a2645d370bc52320f4ab3939f54d720 --- /dev/null +++ b/src/models/report.model.ts @@ -0,0 +1,7 @@ +import { DateTime } from 'luxon' + +export interface ReportAttributes { + sendReportNotification: boolean + haveSeenLastReport: boolean + monthlyReportDate: DateTime +} diff --git a/src/models/userProfile.model.ts b/src/models/userProfile.model.ts index 69660d77d0e5185df72ea707c0c7407e9c918ecc..f5752934a2c4b2681616b7069d88c2d38f8d344c 100644 --- a/src/models/userProfile.model.ts +++ b/src/models/userProfile.model.ts @@ -1,4 +1,5 @@ import { DateTime } from 'luxon' +import { ReportAttributes } from './report.model' export interface UserProfile { id: string @@ -8,5 +9,5 @@ export interface UserProfile { haveSeenWelcomeModal: boolean haveSeenOldFluidModal: DateTime | false notificationEcogesture: string[] - sendReportNotification: boolean + report: ReportAttributes } diff --git a/src/services/userProfile.service.ts b/src/services/userProfile.service.ts index 033903ac1f62d0fb7859742a45d69b3e561783c3..6538ce71b4515e870b5248e0f2732a573150c6b3 100644 --- a/src/services/userProfile.service.ts +++ b/src/services/userProfile.service.ts @@ -2,6 +2,7 @@ import { Client } from 'cozy-client' import { UserProfile } from 'models' import { USERPROFILE_DOCTYPE } from 'doctypes' import { DateTime } from 'luxon' +import { ReportAttributes } from 'models' export default class UserProfileService { private readonly _client: Client @@ -24,7 +25,13 @@ export default class UserProfileService { } public async updateUserProfile(attributes: { - [key: string]: string | string[] | boolean | number | DateTime + [key: string]: + | string + | string[] + | boolean + | number + | DateTime + | ReportAttributes }): Promise<UserProfile | null> { const { data: userProfile } = await this._client .query(this._client.find(USERPROFILE_DOCTYPE).limitBy(1)) diff --git a/src/targets/services/monthlyReportNotification.ts b/src/targets/services/monthlyReportNotification.ts index 97e72e5f2a6359d6d70662ae2c57e5529ffb6a04..19c26a6b28d11e55c0db22c51e1b65f96af5d3de 100644 --- a/src/targets/services/monthlyReportNotification.ts +++ b/src/targets/services/monthlyReportNotification.ts @@ -21,7 +21,7 @@ const monthlyReportNotification = async ({ log('info', 'Fetching user profile...') const upm = new UserProfileService(client) const userProfil = await upm.getUserProfile() - if (!userProfil || !userProfil.sendReportNotification) { + if (!userProfil || !userProfil.report.sendReportNotification) { log('info', 'End of process - Report Notification disabled in user profile') return }