Merge branch '413-bug-cycle-start-not-detected-if-day-before-period-excluded' into 'master'
Resolve "Bug: cycle start not detected if day before period excluded" Closes #413 See merge request bloodyhealth/drip!259
This commit is contained in:
+9
-39
@@ -5,9 +5,10 @@ import fs from 'react-native-fs'
|
|||||||
import restart from 'react-native-restart'
|
import restart from 'react-native-restart'
|
||||||
import schemas from './schemas'
|
import schemas from './schemas'
|
||||||
import cycleModule from '../lib/cycle'
|
import cycleModule from '../lib/cycle'
|
||||||
|
import maybeSetNewCycleStart from '../lib/set-new-cycle-start'
|
||||||
|
|
||||||
let db
|
let db
|
||||||
let isMensesStart
|
let checkIsMensesStart
|
||||||
let getMensesDaysRightAfter
|
let getMensesDaysRightAfter
|
||||||
|
|
||||||
export async function openDb (hash) {
|
export async function openDb (hash) {
|
||||||
@@ -50,7 +51,7 @@ export async function openDb (hash) {
|
|||||||
|
|
||||||
db = connection
|
db = connection
|
||||||
const cycle = cycleModule()
|
const cycle = cycleModule()
|
||||||
isMensesStart = cycle.isMensesStart
|
checkIsMensesStart = cycle.isMensesStart
|
||||||
getMensesDaysRightAfter = cycle.getMensesDaysRightAfter
|
getMensesDaysRightAfter = cycle.getMensesDaysRightAfter
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
@@ -77,52 +78,21 @@ export function saveSymptom(symptom, date, val) {
|
|||||||
if (!cycleDay) cycleDay = createCycleDay(date)
|
if (!cycleDay) cycleDay = createCycleDay(date)
|
||||||
|
|
||||||
db.write(() => {
|
db.write(() => {
|
||||||
if (bleedingValueDeleted(symptom, val)) {
|
if (symptom === 'bleeding') {
|
||||||
cycleDay.bleeding = val
|
const mensesDaysAfter = getMensesDaysRightAfter(cycleDay)
|
||||||
cycleDay.isCycleStart = false
|
maybeSetNewCycleStart({
|
||||||
maybeSetNewCycleStart(cycleDay, val)
|
val, cycleDay, mensesDaysAfter, checkIsMensesStart
|
||||||
} else if (bleedingValueAddedOrChanged(symptom, val)) {
|
})
|
||||||
cycleDay.bleeding = val
|
|
||||||
cycleDay.isCycleStart = isMensesStart(cycleDay)
|
|
||||||
maybeClearOldCycleStarts(cycleDay)
|
|
||||||
} else {
|
} else {
|
||||||
cycleDay[symptom] = val
|
cycleDay[symptom] = val
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
function bleedingValueDeleted(symptom, val) {
|
|
||||||
return symptom === 'bleeding' && !val
|
|
||||||
}
|
|
||||||
|
|
||||||
function bleedingValueAddedOrChanged(symptom, val) {
|
|
||||||
return symptom === 'bleeding' && val
|
|
||||||
}
|
|
||||||
|
|
||||||
function maybeSetNewCycleStart(dayWithDeletedBleeding) {
|
|
||||||
// if a bleeding value is deleted, we need to check if
|
|
||||||
// there are any following bleeding days and if the
|
|
||||||
// next one of them is now a cycle start
|
|
||||||
const mensesDaysAfter = getMensesDaysRightAfter(dayWithDeletedBleeding)
|
|
||||||
if (!mensesDaysAfter.length) return
|
|
||||||
const nextOne = mensesDaysAfter[mensesDaysAfter.length - 1]
|
|
||||||
if (isMensesStart(nextOne)) {
|
|
||||||
nextOne.isCycleStart = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function maybeClearOldCycleStarts(cycleDay) {
|
|
||||||
// if we have a new bleeding day, we need to clear the
|
|
||||||
// menses start marker from all following days of this
|
|
||||||
// menses that may have been marked as start before
|
|
||||||
const mensesDaysAfter = getMensesDaysRightAfter(cycleDay)
|
|
||||||
mensesDaysAfter.forEach(day => day.isCycleStart = false)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function updateCycleStartsForAllCycleDays() {
|
export function updateCycleStartsForAllCycleDays() {
|
||||||
db.write(() => {
|
db.write(() => {
|
||||||
getBleedingDaysSortedByDate().forEach(day => {
|
getBleedingDaysSortedByDate().forEach(day => {
|
||||||
if (isMensesStart(day)) {
|
if (checkIsMensesStart(day)) {
|
||||||
day.isCycleStart = true
|
day.isCycleStart = true
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -0,0 +1,34 @@
|
|||||||
|
export default function ({
|
||||||
|
val, cycleDay, mensesDaysAfter, checkIsMensesStart
|
||||||
|
}) {
|
||||||
|
|
||||||
|
cycleDay.bleeding = val
|
||||||
|
|
||||||
|
// if a bleeding value is deleted or excluded, we need to check if there are
|
||||||
|
// any following bleeding days and if the next one of them is now a cycle
|
||||||
|
// start
|
||||||
|
if (bleedingValueDeletedOrExluded(val)) {
|
||||||
|
cycleDay.isCycleStart = false
|
||||||
|
if (!mensesDaysAfter.length) return
|
||||||
|
const nextOne = mensesDaysAfter[mensesDaysAfter.length - 1]
|
||||||
|
if (checkIsMensesStart(nextOne)) {
|
||||||
|
nextOne.isCycleStart = true
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
cycleDay.isCycleStart = checkIsMensesStart(cycleDay)
|
||||||
|
maybeClearOldCycleStarts(cycleDay)
|
||||||
|
}
|
||||||
|
|
||||||
|
function bleedingValueDeletedOrExluded(val) {
|
||||||
|
const bleedingDeleted = !val
|
||||||
|
const bleedingExcluded = val && val.exclude
|
||||||
|
return bleedingDeleted || bleedingExcluded
|
||||||
|
}
|
||||||
|
|
||||||
|
function maybeClearOldCycleStarts() {
|
||||||
|
// if we have a new bleeding day, we need to clear the
|
||||||
|
// menses start marker from all following days of this
|
||||||
|
// menses that may have been marked as start before
|
||||||
|
mensesDaysAfter.forEach(day => day.isCycleStart = false)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,151 @@
|
|||||||
|
import chai from 'chai'
|
||||||
|
import dirtyChai from 'dirty-chai'
|
||||||
|
import maybeSetNewCycleStart from '../lib/set-new-cycle-start'
|
||||||
|
|
||||||
|
const expect = chai.expect
|
||||||
|
chai.use(dirtyChai)
|
||||||
|
|
||||||
|
describe('maybeSetNewCycleStart', () => {
|
||||||
|
const deletedBleedingValue = null
|
||||||
|
|
||||||
|
const excludedBleedingValue = {
|
||||||
|
value: 2,
|
||||||
|
exclude: true
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function getFixtures() {
|
||||||
|
const cycleStartDay = {
|
||||||
|
date: '2020-01-01',
|
||||||
|
isCycleStart: true,
|
||||||
|
bleeding: {
|
||||||
|
value: 2,
|
||||||
|
exclude: false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const mensesDaysAfter = [
|
||||||
|
{
|
||||||
|
date: '2020-01-04',
|
||||||
|
isCycleStart: false,
|
||||||
|
value: {
|
||||||
|
bleeding: {
|
||||||
|
value: 2,
|
||||||
|
exclude: false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
date: '2020-01-03',
|
||||||
|
isCycleStart: false,
|
||||||
|
value: {
|
||||||
|
bleeding: {
|
||||||
|
value: 2,
|
||||||
|
exclude: false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
date: '2020-01-02',
|
||||||
|
isCycleStart: false,
|
||||||
|
value: {
|
||||||
|
bleeding: {
|
||||||
|
value: 2,
|
||||||
|
exclude: false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
|
const notCycleStartDay = {
|
||||||
|
date: '2020-01-02',
|
||||||
|
isCycleStart: false,
|
||||||
|
bleeding: {
|
||||||
|
value: 2,
|
||||||
|
exclude: false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return [cycleStartDay, mensesDaysAfter, notCycleStartDay]
|
||||||
|
}
|
||||||
|
|
||||||
|
const checkIsMensesStart = cycleDay => {
|
||||||
|
if (cycleDay.date === '2020-01-02') return true
|
||||||
|
}
|
||||||
|
|
||||||
|
it('sets new cycle start when first day of period deleted', () => {
|
||||||
|
const [cycleStartDay, mensesDaysAfter] = getFixtures()
|
||||||
|
|
||||||
|
maybeSetNewCycleStart({
|
||||||
|
val: deletedBleedingValue,
|
||||||
|
cycleDay: cycleStartDay,
|
||||||
|
mensesDaysAfter,
|
||||||
|
checkIsMensesStart
|
||||||
|
})
|
||||||
|
expect(cycleStartDay.isCycleStart).to.be.false()
|
||||||
|
expect(cycleStartDay.bleeding).to.be.null()
|
||||||
|
expect(mensesDaysAfter[2].isCycleStart).to.be.true()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('sets new cycle start when first day of period excluded', () => {
|
||||||
|
const [cycleStartDay, mensesDaysAfter] = getFixtures()
|
||||||
|
|
||||||
|
maybeSetNewCycleStart({
|
||||||
|
val: excludedBleedingValue,
|
||||||
|
cycleDay: cycleStartDay,
|
||||||
|
mensesDaysAfter,
|
||||||
|
checkIsMensesStart
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(cycleStartDay.isCycleStart).to.be.false()
|
||||||
|
expect(cycleStartDay.bleeding).to.equal(excludedBleedingValue)
|
||||||
|
expect(mensesDaysAfter[2].isCycleStart).to.be.true()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('does not set new cycle start when other day of period deleted', () => {
|
||||||
|
const [cycleStartDay, mensesDaysAfter, notCycleStartDay] = getFixtures()
|
||||||
|
|
||||||
|
maybeSetNewCycleStart({
|
||||||
|
val: deletedBleedingValue,
|
||||||
|
cycleDay: notCycleStartDay,
|
||||||
|
mensesDaysAfter,
|
||||||
|
checkIsMensesStart
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(cycleStartDay.isCycleStart).to.be.true()
|
||||||
|
expect(notCycleStartDay.isCycleStart).to.be.false()
|
||||||
|
expect(notCycleStartDay.bleeding).to.equal(deletedBleedingValue)
|
||||||
|
})
|
||||||
|
it('does not set new cycle start when other day of period excluded', () => {
|
||||||
|
const excludedBleedingValue = {
|
||||||
|
value: 2,
|
||||||
|
exclude: true
|
||||||
|
}
|
||||||
|
|
||||||
|
const [cycleStartDay, mensesDaysAfter, notCycleStartDay] = getFixtures()
|
||||||
|
|
||||||
|
maybeSetNewCycleStart({
|
||||||
|
val: excludedBleedingValue,
|
||||||
|
cycleDay: notCycleStartDay,
|
||||||
|
mensesDaysAfter,
|
||||||
|
checkIsMensesStart
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(cycleStartDay.isCycleStart).to.be.true()
|
||||||
|
expect(notCycleStartDay.isCycleStart).to.be.false()
|
||||||
|
expect(notCycleStartDay.bleeding).to.equal(excludedBleedingValue)
|
||||||
|
})
|
||||||
|
it('works when there are no following bleeding days', () => {
|
||||||
|
const [cycleStartDay] = getFixtures()
|
||||||
|
|
||||||
|
maybeSetNewCycleStart({
|
||||||
|
val: deletedBleedingValue,
|
||||||
|
cycleDay: cycleStartDay,
|
||||||
|
mensesDaysAfter: [],
|
||||||
|
checkIsMensesStart
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(cycleStartDay.isCycleStart).to.be.false()
|
||||||
|
expect(cycleStartDay.bleeding).to.equal(deletedBleedingValue)
|
||||||
|
})
|
||||||
|
})
|
||||||
Reference in New Issue
Block a user