From c1342f9de7a4b914bb8e3e356721c224fe66c136 Mon Sep 17 00:00:00 2001
From: Yoan VALLET <ext.sopra.yvallet@grandlyon.com>
Date: Mon, 5 Oct 2020 17:14:18 +0200
Subject: [PATCH] feat: create report attributes

---
 src/atoms/userProfile.state.ts                |  7 +++++-
 src/components/Options/ReportOptions.tsx      | 25 +++++++++++++------
 src/db/userProfileData.json                   |  6 ++++-
 src/models/index.ts                           |  1 +
 src/models/report.model.ts                    |  7 ++++++
 src/models/userProfile.model.ts               |  3 ++-
 src/services/userProfile.service.ts           |  9 ++++++-
 .../services/monthlyReportNotification.ts     |  2 +-
 8 files changed, 48 insertions(+), 12 deletions(-)
 create mode 100644 src/models/report.model.ts

diff --git a/src/atoms/userProfile.state.ts b/src/atoms/userProfile.state.ts
index ef16b1834..029a9cc27 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 493bb50c1..bcd3b5236 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 d1699a220..6e9c6ab3f 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 88c73990f..a3b55612c 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 000000000..c9c4caaf7
--- /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 69660d77d..f5752934a 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 033903ac1..6538ce71b 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 97e72e5f2..19c26a6b2 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
   }
-- 
GitLab