Skip to content
Snippets Groups Projects
migration.data.ts 16.9 KiB
Newer Older
  • Learn to ignore specific revisions
  •   ECOGESTURE_DOCTYPE,
    
      ENEDIS_DAY_DOCTYPE,
    
      ENEDIS_MONTH_DOCTYPE,
      ENEDIS_YEAR_DOCTYPE,
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
      FLUIDSPRICES_DOCTYPE,
    
      GRDF_MONTH_DOCTYPE,
      GRDF_YEAR_DOCTYPE,
    
      PROFILETYPE_DOCTYPE,
      PROFILE_DOCTYPE,
      USERCHALLENGE_DOCTYPE,
    
    } from 'doctypes'
    
    import { UserQuizState } from 'enums'
    
    import {
      DataloadEntity,
      Ecogesture,
      Profile,
      ProfileType,
      UserChallenge,
    } from 'models'
    import ecogestureData from '../db/ecogestureData.json'
    
    import { Migration } from './migration.type'
    
    
    export const SCHEMA_INITIAL_VERSION = 0
    
    export const MIGRATION_RESULT_NOOP = 'MigrationNoop'
    export const MIGRATION_RESULT_COMPLETE = 'MigrationComplete'
    export const MIGRATION_RESULT_FAILED = 'MigrationFailed'
    
    export const migrations: Migration[] = [
      {
        baseSchemaVersion: SCHEMA_INITIAL_VERSION,
        targetSchemaVersion: 1,
    
        appVersion: '1.3.0',
        description:
    
          'Removes old profileType artifacts from users database : \n - Oldest profileType is deleted \n - Removes insulation work form fields that were prone to errors \n - Changes area and outsideFacingWalls form field to strings \n - Changes updateDate values of all existing profileType to match "created_at" entry (former updateDate values got corrupted and hold no meanings).',
        releaseNotes: null,
    
        docTypes: PROFILETYPE_DOCTYPE,
    
        run: (_client: Client, docs: any[]): ProfileType[] => {
    
          docs.sort(function (a, b) {
    
            const c = DateTime.fromISO(a.cozyMetadata.createdAt, {
              zone: 'utc',
            })
            const d = DateTime.fromISO(b.cozyMetadata.createdAt, {
              zone: 'utc',
            })
            return d.millisecond - c.millisecond
          })
          if (docs[0].area === 100) {
            docs[0].deleteAction = true
          }
          return docs.map(doc => {
            if (
              doc.individualInsulationWork.includes(
                'window_replacement_and_wall_insulation'
              )
            ) {
    
              doc.individualInsulationWork = [
                'window_replacement',
                'wall_insulation',
              ]
    
            if (
              doc.individualInsulationWork.includes(
                'window_replacement_and_roof_insulation'
              )
            ) {
              doc.individualInsulationWork = [
                'window_replacement',
                'roof_insulation',
              ]
            }
            if (
              doc.individualInsulationWork.includes(
                'window_replacement_and_roof_and_wall_insulation'
              )
            ) {
              doc.individualInsulationWork = [
                'window_replacement',
                'roof_insulation',
                'wall_insulation',
              ]
            }
    
    
            doc.outsideFacingWalls = doc.outsideFacingWalls.toString()
            doc.area = doc.area.toString()
            doc.updateDate = doc.cozyMetadata.createdAt
            return doc
          })
        },
      },
      {
        baseSchemaVersion: 1,
        targetSchemaVersion: 2,
        appVersion: '1.3.0',
        description: 'Removes old profileType and GCUApprovalDate from profile.',
    
        docTypes: PROFILE_DOCTYPE,
    
        run: (_client: Client, docs: any[]): Profile[] => {
    
          return docs.map(doc => {
            if (doc.GCUApprovalDate) {
              delete doc.GCUApprovalDate
            }
    
            if (doc.profileType) {
              delete doc.profileType
            }
            return doc
          })
        },
      },
      {
        baseSchemaVersion: 2,
        targetSchemaVersion: 3,
        appVersion: '1.3.0',
        description:
          'Updates userChallenges to make sure no quiz results are overflowing.',
    
        docTypes: USERCHALLENGE_DOCTYPE,
    
        run: (_client: Client, docs: any[]): UserChallenge[] => {
    
          return docs.map(doc => {
            if (doc.quiz.result > 5) {
              doc.quiz.result = 5
              doc.progress = {
                actionProgress: 5,
                explorationProgress: 5,
                quizProgress: 5,
              }
              doc.quiz.state = UserQuizState.DONE
            }
    
            return doc
          })
        },
      },
    
      {
        baseSchemaVersion: 3,
        targetSchemaVersion: 4,
        appVersion: '1.4.3',
    
        description: 'Corrects daily EGL data.',
    
        releaseNotes: {
          title:
            "Des corrections sur la connexion aux données de consommation d'eau ont été apportées.",
          description:
            'Merci de mettre à jour votre connecteur après avoir fermé cette fenêtre. Pour mettre à jour votre connecteur, rendez-vous maintenant du côté de la page Conso, dans la page du fluide concerné.',
        },
        docTypes: EGL_DAY_DOCTYPE,
        queryOptions: {
          scope: 'conso',
          tag: 'day',
          limit: 120,
        },
    
        run: (_client: Client, docs: any[]): any[] => {
    
          return docs.map(doc => {
            doc.deleteAction = true
            return doc
          })
        },
      },
      {
        baseSchemaVersion: 4,
        targetSchemaVersion: 5,
        appVersion: '1.4.3',
    
        description: 'Corrects monthly EGL data.',
    
        releaseNotes: null,
        docTypes: EGL_MONTH_DOCTYPE,
        queryOptions: {
          scope: 'conso',
          tag: 'month',
          limit: 4,
        },
    
        run: (_client: Client, docs: any[]): any[] => {
    
          return docs.map(doc => {
            doc.deleteAction = true
            return doc
          })
        },
      },
      {
        baseSchemaVersion: 5,
        targetSchemaVersion: 6,
        appVersion: '1.4.3',
    
        description: 'Corrects yearly EGL data.',
    
        releaseNotes: null,
        docTypes: EGL_YEAR_DOCTYPE,
        queryOptions: {
          scope: 'conso',
          tag: 'year',
          limit: 1,
        },
    
        run: (_client: Client, docs: any[]): any[] => {
    
      {
        baseSchemaVersion: 6,
        targetSchemaVersion: 7,
        appVersion: '1.4.4',
        description: 'Corrects individual insulation work field on profileType.',
        releaseNotes: null,
        docTypes: PROFILETYPE_DOCTYPE,
    
        run: (_client: Client, docs: any[]): ProfileType[] => {
    
          return docs.map(doc => {
            if (!Array.isArray(doc.individualInsulationWork)) {
              doc.individualInsulationWork = [doc.individualInsulationWork]
            }
            if (
              doc.individualInsulationWork.includes(
                'window_replacement_and_wall_insulation'
              )
            ) {
              doc.individualInsulationWork = [
                'window_replacement',
                'wall_insulation',
              ]
            }
            if (
              doc.individualInsulationWork.includes(
                'window_replacement_and_roof_insulation'
              )
            ) {
              doc.individualInsulationWork = [
                'window_replacement',
                'roof_insulation',
              ]
            }
            if (
              doc.individualInsulationWork.includes(
                'window_replacement_and_roof_and_wall_insulation'
              )
            ) {
              doc.individualInsulationWork = [
                'window_replacement',
                'roof_insulation',
                'wall_insulation',
              ]
            }
    
            return doc
          })
        },
      },
    
      {
        baseSchemaVersion: 7,
        targetSchemaVersion: 8,
        appVersion: '1.5.0',
        description:
          'ProfileTypes start now at the begining of the month, no duplications can exist over the same month.',
        releaseNotes: null,
        docTypes: PROFILETYPE_DOCTYPE,
    
        run: (_client: Client, docs: any[]): any[] => {
    
          function checkDate(d1: string, d2: string) {
    
            const dtd1 = DateTime.fromISO(d1)
            const dtd2 = DateTime.fromISO(d2)
    
            return dtd1.year === dtd2.year && dtd1.month === dtd2.month
          }
    
          for (let i = 0; i < docs.length; i++) {
    
            const dtStartOfMonth = DateTime.fromISO(docs[i].updateDate)
    
            docs[i].updateDate = dtStartOfMonth
              .setZone('utc', {
                keepLocalTime: true,
              })
              .startOf('month')
            if (
              docs[i + 1] &&
              checkDate(docs[i].updateDate, docs[i + 1].updateDate)
            ) {
              docs[i].deleteAction = true
            }
          }
          return docs
        },
      },
    
      {
        baseSchemaVersion: 8,
        targetSchemaVersion: 9,
        appVersion: '1.6.0',
    
        description: 'Init new doctype fluidPrices --deprecated--',
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
        docTypes: FLUIDSPRICES_DOCTYPE,
    
        run: (): any => {
    
          return []
    
      {
        baseSchemaVersion: 9,
        targetSchemaVersion: 10,
    
        appVersion: '1.6.0',
        description:
          "Profil now contains partnersIssueDate in order to handle partners' issue display",
        releaseNotes: null,
        docTypes: PROFILE_DOCTYPE,
    
        run: (_client: Client, docs: any[]): Profile[] => {
    
          return docs.map(doc => {
    
            doc.partnersIssueDate = DateTime.local()
              .minus({ day: 1 })
              .startOf('day')
            return doc
          })
        },
      },
      {
        baseSchemaVersion: 10,
        targetSchemaVersion: 11,
        appVersion: '1.6.0',
    
        description:
          'Rename tutorial to onboaring in ecolyo profile, remove isLastTermAccepted',
        releaseNotes: null,
        docTypes: PROFILE_DOCTYPE,
    
        run: (_client: Client, docs: any[]): ProfileType[] => {
    
          return docs.map(doc => {
            if (doc.tutorial) {
              doc.onboarding = { ...doc.tutorial }
              delete doc.tutorial
            }
            if (typeof doc.isLastTermAccepted != 'undefined') {
              delete doc.isLastTermAccepted
            }
            return doc
          })
        },
      },
    
      {
        baseSchemaVersion: 11,
        targetSchemaVersion: 12,
        appVersion: '1.6.1',
        description: 'Corrects daily Enedis data.',
        releaseNotes: null,
        docTypes: ENEDIS_DAY_DOCTYPE,
        queryOptions: {
          scope: 'conso',
          tag: 'day',
    
        run: (_client: Client, docs: any[]): DataloadEntity[] => {
    
          let prevData: DataloadEntity = {
            id: '',
            day: 0,
            hour: 0,
            load: 0,
            minute: 0,
            month: 0,
            year: 0,
          }
          return docs.map(doc => {
            if (
              prevData.day === doc.day &&
              prevData.month === doc.month &&
              prevData.year === doc.year
            ) {
              doc.deleteAction = true
            }
            if (doc.price) {
              delete doc.price
            }
            prevData = doc
            return doc
          })
        },
      },
      {
        baseSchemaVersion: 12,
        targetSchemaVersion: 13,
        appVersion: '1.6.1',
        description: 'Corrects daily GRDF data.',
        releaseNotes: null,
        docTypes: GRDF_DAY_DOCTYPE,
        queryOptions: {
          scope: 'conso',
          tag: 'day',
    
        run: (_client: Client, docs: any[]): DataloadEntity[] => {
    
          let prevData: DataloadEntity = {
            id: '',
            day: 0,
            hour: 0,
            load: 0,
            minute: 0,
            month: 0,
            year: 0,
          }
          return docs.map(doc => {
            if (
              prevData.day === doc.day &&
              prevData.month === doc.month &&
              prevData.year === doc.year
            ) {
              doc.deleteAction = true
            }
            if (doc.price) {
              delete doc.price
            }
            prevData = doc
            return doc
          })
        },
      },
    
      {
        baseSchemaVersion: 13,
        targetSchemaVersion: 14,
        appVersion: '1.6.1',
        description: 'Remove wrong price for Enedis month',
        releaseNotes: null,
        docTypes: ENEDIS_MONTH_DOCTYPE,
        queryOptions: {
          scope: 'conso',
          tag: 'month',
    
        run: (_client: Client, docs: any[]): DataloadEntity[] => {
    
          return docs.map(doc => {
            if (doc.price) {
              delete doc.price
            }
            return doc
          })
        },
      },
      {
    
        baseSchemaVersion: 14,
        targetSchemaVersion: 15,
    
        appVersion: '1.6.1',
        description: 'Remove wrong price for Enedis year',
        releaseNotes: null,
        docTypes: ENEDIS_YEAR_DOCTYPE,
        queryOptions: {
          scope: 'conso',
          tag: 'year',
    
        run: (_client: Client, docs: any[]): DataloadEntity[] => {
    
          return docs.map(doc => {
            if (doc.price) {
              delete doc.price
            }
            return doc
          })
        },
      },
      {
    
        baseSchemaVersion: 15,
        targetSchemaVersion: 16,
    
        appVersion: '1.6.1',
        description: 'Remove wrong price for GRDF month',
        releaseNotes: null,
        docTypes: GRDF_MONTH_DOCTYPE,
        queryOptions: {
          scope: 'conso',
          tag: 'month',
    
        run: (_client: Client, docs: any[]): DataloadEntity[] => {
    
          return docs.map(doc => {
            if (doc.price) {
              delete doc.price
            }
            return doc
          })
        },
      },
      {
    
        baseSchemaVersion: 16,
        targetSchemaVersion: 17,
    
        appVersion: '1.6.1',
        description: 'Remove wrong price for GRDF year',
        releaseNotes: null,
        docTypes: GRDF_YEAR_DOCTYPE,
        queryOptions: {
          scope: 'conso',
          tag: 'year',
    
        run: (_client: Client, docs: any[]): DataloadEntity[] => {
    
          return docs.map(doc => {
            if (doc.price) {
              delete doc.price
            }
            return doc
          })
        },
      },
    
      {
        baseSchemaVersion: 17,
        targetSchemaVersion: 18,
        appVersion: '1.7.0',
    
        description: 'Init new fluidPrices for water -- deprecated --',
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
        docTypes: FLUIDSPRICES_DOCTYPE,
    
        run: (): any => {
    
          return []
    
      {
        baseSchemaVersion: 18,
        targetSchemaVersion: 19,
        appVersion: '1.7.0',
        description: 'Replace old minCons with the new calculation',
        releaseNotes: null,
        docTypes: ENEDIS_MONTHLY_ANALYSIS_DATA_DOCTYPE,
    
        run: (_client: Client, docs: any[]): any => {
    
            if (doc.minLoad) {
              const numberofDaysInMonth = DateTime.fromObject({
                month: doc.month,
                year: doc.year,
              }).daysInMonth
              doc.minimumLoad = doc.minLoad * 48 * numberofDaysInMonth
              delete doc.minLoad
            }
    
      {
        baseSchemaVersion: 19,
        targetSchemaVersion: 20,
        appVersion: '1.8.0',
        description:
          'Empty fluidPrices db so it can be fetched with right format from remote doctype',
        releaseNotes: null,
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
        docTypes: FLUIDSPRICES_DOCTYPE,
    
        run: (_client: Client, docs: any[]): any => {
    
      {
        baseSchemaVersion: 20,
        targetSchemaVersion: 21,
    
        appVersion: '2.0.0',
    
        description: 'Inform user of the new SGE konnector',
        releaseNotes: {
    
          title:
            "Le parcours d'accès aux données de consommation électrique évolue\u00a0!",
    
            "Pour continuer à accéder à vos données d'électricité, merci de vous reconnecter via ce nouveau parcours. Aucune donnée ne sera perdue, et vos données seront à nouveau mises à jour quotidiennement. <p>Pourquoi ce changement ?</p> Pour faciliter l'accès aux données de consommation au plus grand nombre. Plus besoin de se créer un compte Enedis, l'accès aux données en est facilité. N'hésitez pas à en parler autour de vous ! :)",
    
        redirectLink: '/consumption/electricity',
    
        run: (): any => undefined,
    
      {
        baseSchemaVersion: 21,
        targetSchemaVersion: 22,
        appVersion: '2.0.3',
        description:
          'Profil now contains partnersIssueSeenDates in order to handle each partners issue date. Also removes previous partnersIssueDate',
        releaseNotes: null,
        docTypes: PROFILE_DOCTYPE,
    
        run: (_client: Client, docs: any[]): Profile[] => {
    
          return docs.map(doc => {
            doc.partnersIssueSeenDate = {
              enedis: DateTime.local().minus({ day: 1 }).startOf('day'),
              egl: DateTime.local().minus({ day: 1 }).startOf('day'),
              grdf: DateTime.local().minus({ day: 1 }).startOf('day'),
            }
            if (doc.partnersIssueDate) {
              delete doc.partnersIssueDate
            }
    
            return doc
          })
        },
      },
      {
        baseSchemaVersion: 22,
        targetSchemaVersion: 23,
        appVersion: '2.3.0',
        description: 'Fix apartment typo',
        releaseNotes: null,
        docTypes: PROFILETYPE_DOCTYPE,
    
        run: (_client: Client, docs: any[]) => {
    
          return docs.map(doc => {
            if (doc.housingType === 'appartment') {
              doc.housingType = 'apartment'
            }
    
      {
        baseSchemaVersion: 23,
        targetSchemaVersion: 24,
        appVersion: '3.0.0',
        description: 'Add garden room & equipment type',
        releaseNotes: null,
        docTypes: ECOGESTURE_DOCTYPE,
    
        run: (_client: Client, ecogestures: Ecogesture[]) => {
    
          return ecogestures.map(ecogesture => {
            const ecData = ecogestureData.find(
              ec => ec._id === ecogesture.id
            ) as Ecogesture
    
            if (!ecData) return ecogesture
    
            ecogesture.room = ecData.room
            ecogesture.equipmentType = ecData.equipmentType
    
            return ecogesture
          })
        },
      },
    
      {
        baseSchemaVersion: 24,
        targetSchemaVersion: 25,
        appVersion: '3.1.0',
        description: 'Initialize isAnalysisReminderEnabled in profile',
        releaseNotes: null,
        docTypes: PROFILE_DOCTYPE,
    
        run: (_client: Client, docs: Profile[]) => {
    
          return docs.map(doc => {
            doc.isAnalysisReminderEnabled = true
            return doc
          })
        },
      },