Skip to content

Commit

Permalink
Remove ability to clear startedAt on updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Tornquist committed Aug 10, 2019
1 parent 271c56f commit 086685e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
10 changes: 7 additions & 3 deletions modules/Entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ async function insertRecord() {
this.props.type === undefined
) throw TimeError.Request.INVALID_STATE

if (this.props.started_at === undefined) this.start()
if (this.props.started_at === undefined || this.props.started_at === null) {
this.start()
}

let started_at_timezone_id = await getTimezoneID(this.props.started_at_timezone)
let ended_at_timezone_id = await getTimezoneID(this.props.ended_at_timezone)
Expand Down Expand Up @@ -201,13 +203,15 @@ module.exports = class Entry {
}
set startedAt(newStart) {
if (this.type === undefined) throw TimeError.Request.INVALID_STATE
if (this.props.ended_at && dateHelper.isAfter(newStart, this.props.ended_at)) {
if (
(newStart === null || newStart === undefined) ||
(this.props.ended_at && dateHelper.isAfter(newStart, this.props.ended_at))
) {
throw TimeError.Request.INVALID_VALUE
}

this.props.started_at = newStart
this._modifiedProps.push("started_at")
if (newStart === null) this.startedAtTimezone = null
}

get startedAtTimezone() {
Expand Down
24 changes: 24 additions & 0 deletions test/test-entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,16 @@ describe('Entry Module', () => {
moment.utc(freshEntry.props.started_at).isSame(startDate).should.eq(true)
})

it('rejects clearing startedAt', async () => {
try {
entry.startedAt = null
} catch (e) {
e.should.eq(Time.Error.Request.INVALID_VALUE)
return
}
throw new Error('Expected failure')
})

it('allows startedAtTimezone to be changed', async () => {
should.equal(entry.startedAtTimezone, null)
entry.startedAtTimezone = 'America/Chicago'
Expand Down Expand Up @@ -227,7 +237,21 @@ describe('Entry Module', () => {
entry.endedAtTimezone.should.eq(fakeTimezone)
})

it('allows clearing endedAt', async () => {
entry.endedAt = null
await entry.save()

should.equal(entry.endedAt, null)
should.equal(entry.endedAtTimezone, null)
})

it('clears endedAt and endedAtTimezone when changing type from range to event', async () => {
// Reset from previous test
entry.endedAt = new Date()
entry.endedAtTimezone = 'America/Chicago'
await entry.save()

// Test clearing on change
should.not.equal(entry.endedAt, null)
should.not.equal(entry.endedAtTimezone, null)

Expand Down

0 comments on commit 086685e

Please sign in to comment.