Skip to content
Snippets Groups Projects
index.js 3.33 KiB
Newer Older
import Realm from 'realm'
import { LocalDate } from 'js-joda'
import {
  cycleWithTempAndNoMucusShift,
  cycleWithFhm,
  longAndComplicatedCycle
} from './fixtures'

const TemperatureSchema = {
  name: 'Temperature',
  properties: {
    value: 'double',
    exclude: 'bool',
    time: {
      type: 'string',
      optional: true
    },
const BleedingSchema = {
  name: 'Bleeding',
  properties: {
    value: 'int',
    exclude: 'bool'
  }
}

const MucusSchema = {
  name: 'Mucus',
  properties: {
    feeling: 'int',
    texture: 'int',
Julia Friesel's avatar
Julia Friesel committed
    value: 'int',
    exclude: 'bool'
  }
}

emelko's avatar
emelko committed
const CervixSchema = {
  name: 'Cervix',
  properties: {
    opening: 'int',
    firmness: 'int',
    position: {type: 'int', optional: true },
    exclude: 'bool'
  }
}

Julia Friesel's avatar
Julia Friesel committed
const NoteSchema = {
  name: 'Note',
  properties: {
    value: 'string'
  }
}

const CycleDaySchema = {
  name: 'CycleDay',
  properties: {
    temperature: {
      type: 'Temperature',
      optional: true
    },
    bleeding: {
      type: 'Bleeding',
      optional: true
    },
    mucus: {
      type: 'Mucus',
      optional: true
emelko's avatar
emelko committed
    },
    cervix: {
      type: 'Cervix',
      optional: true
Julia Friesel's avatar
Julia Friesel committed
    },
    note: {
      type: 'Note',
      optional: true
const realmConfig = {
  schema: [
    CycleDaySchema,
    TemperatureSchema,
    BleedingSchema,
emelko's avatar
emelko committed
    MucusSchema,
Julia Friesel's avatar
Julia Friesel committed
    CervixSchema,
    NoteSchema
  ],
  // we only want this in dev mode
  deleteRealmIfMigrationNeeded: true
}

const db = new Realm(realmConfig)

const bleedingDaysSortedByDate = db.objects('CycleDay').filtered('bleeding != null').sorted('date', true)
const temperatureDaysSortedByDate = db.objects('CycleDay').filtered('temperature != null').sorted('date', true)
function saveSymptom(symptom, cycleDay, val) {
  db.write(() => {
    cycleDay[symptom] = val
const cycleDaysSortedByDate = db.objects('CycleDay').sorted('date', true)
function getOrCreateCycleDay(localDate) {
  let result = db.objectForPrimaryKey('CycleDay', localDate)
  if (!result) {
    db.write(() => {
      result = db.create('CycleDay', {
function getCycleDay(localDate) {
  return db.objectForPrimaryKey('CycleDay', localDate)
}

function fillWithDummyData() {
  const dummyCycles = [
    cycleWithFhm,
    longAndComplicatedCycle,
    cycleWithTempAndNoMucusShift
  ]

  db.write(() => {
    db.deleteAll()
    dummyCycles.forEach(cycle => {
      cycle.forEach(day => {
        const existing = getCycleDay(day.date)
        if (existing) {
          Object.keys(day).forEach(key => {
            if (key === 'date') return
            existing[key] = day[key]
          })
        } else {
          db.create('CycleDay', day)
        }
      })
    })
Julia Friesel's avatar
Julia Friesel committed
function deleteAll() {
  db.write(() => {
    db.deleteAll()
  })
}

function getPreviousTemperature(cycleDay) {
  cycleDay.wrappedDate = LocalDate.parse(cycleDay.date)
  const winner = temperatureDaysSortedByDate.find(day => {
    const wrappedDate = LocalDate.parse(day.date)
    return wrappedDate.isBefore(cycleDay.wrappedDate)
  })
  if (!winner) return null
  return winner.temperature.value
}

  saveSymptom,
  getOrCreateCycleDay,
  bleedingDaysSortedByDate,
  cycleDaysSortedByDate,
  fillWithDummyData,
Julia Friesel's avatar
Julia Friesel committed
  deleteAll,
  getPreviousTemperature,
  getCycleDay