From 59916fec140d19f341b04509ccff21eec35954bd Mon Sep 17 00:00:00 2001
From: Bastien DUMONT <bdumont@grandlyon.com>
Date: Thu, 24 Oct 2024 12:33:44 +0000
Subject: [PATCH] chore(init): convert files to typescript

---
 package.json                                  |   7 +-
 ...ateConnections.js => createConnections.ts} | 149 +++++++++---------
 ...eDayDataFiles.js => createDayDataFiles.ts} |  51 ++++--
 tsconfig.json                                 |   2 +-
 4 files changed, 120 insertions(+), 89 deletions(-)
 rename scripts/{createConnections.js => createConnections.ts} (62%)
 rename scripts/{createDayDataFiles.js => createDayDataFiles.ts} (80%)

diff --git a/package.json b/package.json
index a655cf9c1..adff49a25 100644
--- a/package.json
+++ b/package.json
@@ -25,7 +25,12 @@
     "test": "cs test --verbose --coverage",
     "tx": "tx pull --all || true",
     "watch": "yarn watch:browser",
-    "watch:browser": "cs watch --browser"
+    "watch:browser": "cs watch --browser",
+    "clean": "rm -rf ./data",
+    "data:create": "npx tsx scripts/createDayDataFiles.ts",
+    "data:import": "./scripts/importData.sh",
+    "data:seed": "yarn data:create && yarn data:import",
+    "data:connections": "npx tsx scripts/createConnections.ts"
   },
   "repository": {
     "type": "git",
diff --git a/scripts/createConnections.js b/scripts/createConnections.ts
similarity index 62%
rename from scripts/createConnections.js
rename to scripts/createConnections.ts
index 80a42cca2..e8b11a1ba 100644
--- a/scripts/createConnections.js
+++ b/scripts/createConnections.ts
@@ -1,6 +1,7 @@
 /* eslint-disable camelcase */
-const axios = require('axios')
-const config = require('./config')
+import axios from 'axios'
+import { Account, Trigger } from 'models'
+import config from './config'
 
 const headers = {
   Accept: 'application/json',
@@ -12,7 +13,7 @@ const headers = {
 const COZY_PREFIX = 'cozy35ba44d2d1749e6f21646edce51e7190'
 
 const ENEDIS_ACCOUNT_ID = '70e68b8450cee09fe2f077610901094d'
-const dataEnedisAccount = JSON.stringify({
+const ENEDIS_ACCOUNT: Partial<Account> = {
   id: ENEDIS_ACCOUNT_ID,
   account_type: 'enedissgegrandlyon',
   name: '',
@@ -31,10 +32,11 @@ const dataEnedisAccount = JSON.stringify({
   },
   identifier: 'address',
   state: null,
-})
+}
+const dataEnedisAccount = JSON.stringify(ENEDIS_ACCOUNT)
 
 const GRDF_ACCOUNT_ID = '89e68b8450cee09fe2f077610901094d'
-const dataGrdfAccount = JSON.stringify({
+const GRDF_ACCOUNT: Partial<Account> = {
   id: GRDF_ACCOUNT_ID,
   account_type: 'grdfgrandlyon',
   auth: {
@@ -44,10 +46,11 @@ const dataGrdfAccount = JSON.stringify({
   },
   identifier: 'login',
   state: null,
-})
+}
+const dataGrdfAccount = JSON.stringify(GRDF_ACCOUNT)
 
 const EGL_ACCOUNT_ID = '90e68b8450cee09fe2f077610901094d'
-const dataEglAccount = JSON.stringify({
+const EGL_ACCOUNT: Partial<Account> = {
   id: EGL_ACCOUNT_ID,
   account_type: 'eglgrandlyon',
   auth: {
@@ -57,83 +60,87 @@ const dataEglAccount = JSON.stringify({
   },
   identifier: 'login',
   state: null,
-})
+}
+const dataEglAccount = JSON.stringify(EGL_ACCOUNT)
 
+const ENEDIS_TRIGGER: Trigger = {
+  _id: '3ed832cec67e6e0b2c6382edd30df11c',
+  domain: 'cozy.tools:8080',
+  prefix: COZY_PREFIX,
+  type: '@cron',
+  worker: 'konnector',
+  arguments: '0 47 8 * * *',
+  debounce: '',
+  options: null,
+  message: {
+    account: ENEDIS_ACCOUNT_ID,
+    konnector: 'enedissgegrandlyon',
+  },
+  cozyMetadata: {
+    doctypeVersion: '1',
+    metadataVersion: 1,
+    createdAt: '2020-10-09T08:00:00.6092798Z',
+    createdByApp: 'ecolyo',
+    updatedAt: '2020-10-09T08:00:00.6092798Z',
+  },
+}
 const dataEnedisTrigger = JSON.stringify({
   data: {
-    attributes: {
-      _id: '3ed832cec67e6e0b2c6382edd30df11c',
-      domain: 'cozy.tools:8080',
-      prefix: COZY_PREFIX,
-      type: '@cron',
-      worker: 'konnector',
-      arguments: '0 47 8 * * *',
-      debounce: '',
-      options: null,
-      message: {
-        account: ENEDIS_ACCOUNT_ID,
-        konnector: 'enedissgegrandlyon',
-      },
-      cozyMetadata: {
-        doctypeVersion: '1',
-        metadataVersion: 1,
-        createdAt: '2020-10-09T08:00:00.6092798Z',
-        createdByApp: 'ecolyo',
-        updatedAt: '2020-10-09T08:00:00.6092798Z',
-      },
-    },
+    attributes: ENEDIS_TRIGGER,
   },
 })
 
+const GRDF_TRIGGER: Trigger = {
+  _id: '4ed832cec67e6e0b2c6382edd30df11c',
+  domain: 'cozy.tools:8080',
+  prefix: COZY_PREFIX,
+  type: '@cron',
+  worker: 'konnector',
+  arguments: '0 47 8 * * *',
+  debounce: '',
+  options: null,
+  message: {
+    account: GRDF_ACCOUNT_ID,
+    konnector: 'grdfgrandlyon',
+  },
+  cozyMetadata: {
+    doctypeVersion: '1',
+    metadataVersion: 1,
+    createdAt: '2020-10-09T08:00:00.6092798Z',
+    createdByApp: 'ecolyo',
+    updatedAt: '2020-10-09T08:00:00.6092798Z',
+  },
+}
 const dataGrdfTrigger = JSON.stringify({
   data: {
-    attributes: {
-      _id: '4ed832cec67e6e0b2c6382edd30df11c',
-      domain: 'cozy.tools:8080',
-      prefix: COZY_PREFIX,
-      type: '@cron',
-      worker: 'konnector',
-      arguments: '0 47 8 * * *',
-      debounce: '',
-      options: null,
-      message: {
-        account: GRDF_ACCOUNT_ID,
-        konnector: 'grdfgrandlyon',
-      },
-      cozyMetadata: {
-        doctypeVersion: '1',
-        metadataVersion: 1,
-        createdAt: '2020-10-09T08:00:00.6092798Z',
-        createdByApp: 'ecolyo',
-        updatedAt: '2020-10-09T08:00:00.6092798Z',
-      },
-    },
+    attributes: GRDF_TRIGGER,
   },
 })
 
+const EGL_TRIGGER: Trigger = {
+  _id: '5ed832cec67e6e0b2c6382edd30df11c',
+  domain: 'cozy.tools:8080',
+  prefix: COZY_PREFIX,
+  type: '@cron',
+  worker: 'konnector',
+  arguments: '0 47 8 * * *',
+  debounce: '',
+  options: null,
+  message: {
+    account: EGL_ACCOUNT_ID,
+    konnector: 'eglgrandlyon',
+  },
+  cozyMetadata: {
+    doctypeVersion: '1',
+    metadataVersion: 1,
+    createdAt: '2020-10-09T08:00:00.6092798Z',
+    createdByApp: 'ecolyo',
+    updatedAt: '2020-10-09T08:00:00.6092798Z',
+  },
+}
 const dataEglTrigger = JSON.stringify({
   data: {
-    attributes: {
-      _id: '5ed832cec67e6e0b2c6382edd30df11c',
-      domain: 'cozy.tools:8080',
-      prefix: COZY_PREFIX,
-      type: '@cron',
-      worker: 'konnector',
-      arguments: '0 47 8 * * *',
-      debounce: '',
-      options: null,
-      message: {
-        account: EGL_ACCOUNT_ID,
-        konnector: 'eglgrandlyon',
-      },
-      cozyMetadata: {
-        doctypeVersion: '1',
-        metadataVersion: 1,
-        createdAt: '2020-10-09T08:00:00.6092798Z',
-        createdByApp: 'ecolyo',
-        updatedAt: '2020-10-09T08:00:00.6092798Z',
-      },
-    },
+    attributes: EGL_TRIGGER,
   },
 })
 
diff --git a/scripts/createDayDataFiles.js b/scripts/createDayDataFiles.ts
similarity index 80%
rename from scripts/createDayDataFiles.js
rename to scripts/createDayDataFiles.ts
index 94a78e4a6..5e3870b73 100644
--- a/scripts/createDayDataFiles.js
+++ b/scripts/createDayDataFiles.ts
@@ -1,21 +1,31 @@
+import { FluidType } from 'enums'
+import fs from 'fs'
+import { DateTime } from 'luxon'
+import { DataloadEntity } from 'models'
+import config from './config'
+const fluidConfig = require('../src/constants/config.json').fluidConfig
+
 /*
  * Create dummy data files for each fluid.
  * Hourly data is not generated
  */
-const fs = require('fs')
-const { DateTime } = require('luxon')
-const config = require('./config')
-const fluidConfig = require('../src/constants/config.json').fluidConfig
 
-function getRandomNumber(min, max) {
-  // Generate a random float between min and max
+type IMockData = Omit<DataloadEntity, 'id'>
+
+/** Generate a random float between min and max */
+function getRandomNumber(min: number, max: number) {
   let randomFloat = Math.random() * (max - min) + min
   // Round to two decimal places
   randomFloat = Math.round(randomFloat * 100) / 100
   return randomFloat
 }
 
-const generateHalfAnHourData = (startingDate, endingDate, min, max) => {
+const generateHalfAnHourData = (
+  startingDate: DateTime,
+  endingDate: DateTime,
+  min: number,
+  max: number
+) => {
   let parsingDate = DateTime.local(
     startingDate.year,
     startingDate.month,
@@ -23,7 +33,7 @@ const generateHalfAnHourData = (startingDate, endingDate, min, max) => {
     0,
     0
   )
-  const halfAnHourDumpArray = []
+  const halfAnHourDumpArray: IMockData[] = []
   while (parsingDate <= endingDate) {
     const load = getRandomNumber(min, max)
     halfAnHourDumpArray.push({
@@ -40,13 +50,19 @@ const generateHalfAnHourData = (startingDate, endingDate, min, max) => {
   return halfAnHourDumpArray
 }
 
-const generateData = (startingDate, endingDate, min, max, fluidType) => {
+const generateData = (
+  startingDate: DateTime,
+  endingDate: DateTime,
+  min: number,
+  max: number,
+  fluidType: FluidType
+) => {
   let parsingDate = DateTime.local(
     startingDate.year,
     startingDate.month,
     startingDate.day
   )
-  const dayDumpArray = []
+  const dayDumpArray: IMockData[] = []
   while (parsingDate <= endingDate) {
     const load = getRandomNumber(min, max)
     dayDumpArray.push({
@@ -63,11 +79,11 @@ const generateData = (startingDate, endingDate, min, max, fluidType) => {
   return dayDumpArray
 }
 
-// Function to aggregate load data for a specific period
-function aggregateLoadData(data, period) {
-  const aggregatedData = {}
+/** Function to aggregate load data for a specific period */
+function aggregateLoadData(data: IMockData[], period: string) {
+  const aggregatedData: Record<string, IMockData> = {}
   data.forEach(entry => {
-    let key
+    let key = ''
     const entryCopy = { ...entry }
     switch (period) {
       case 'day':
@@ -101,9 +117,12 @@ function aggregateLoadData(data, period) {
       }
     } else {
       aggregatedData[key].load += entryCopy.load
-      aggregatedData[key].price += entryCopy.price
+      if (aggregatedData[key].price !== undefined) {
+        aggregatedData[key].price = 0
+      }
     }
   })
+
   return Object.values(aggregatedData)
 }
 
@@ -162,6 +181,6 @@ const dump = {
 
 const dumpString = JSON.stringify(dump)
 
-fs.writeFile('data/loads.json', dumpString, function (err) {
+fs.writeFile('data/loads.json', dumpString, function (err: unknown) {
   if (err) console.log('error', err)
 })
diff --git a/tsconfig.json b/tsconfig.json
index 918223a92..3da6f149f 100644
--- a/tsconfig.json
+++ b/tsconfig.json
@@ -20,5 +20,5 @@
       "*": ["src/*", "../node_modules/*", "types/*"]
     }
   },
-  "include": ["src/**/*", "tests/**/*"]
+  "include": ["src/**/*", "tests/**/*", "scripts/**/*"]
 }
-- 
GitLab