Skip to content

Commit

Permalink
fix: get available times to acknowledge restrictions
Browse files Browse the repository at this point in the history
  • Loading branch information
meenahoda committed Oct 10, 2018
1 parent eb251cb commit 2ce6d58
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
19 changes: 17 additions & 2 deletions lib/components/state-resources/get-available-diary-slots/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,36 @@ module.exports = class GetAvailableDiarySlots {
const namespace = context.stateMachineMeta.namespace
const diaryService = this.services.diaries
const diary = diaryService.diaries[namespace + '_' + this.diaryId]

let entries = await this.entryModel.find({ where: { 'diaryId': { equals: this.diaryId } } })
entries = entries.filter(booking => moment(event.date).format('YYYY-MM-DD') === moment(booking.startDateTime).format('YYYY-MM-DD'))

const availableTimes = getAvailableDiarySlots(diary, event.date)

const remove = []

Object.values(availableTimes).forEach((timeSlot, index) => {
Object.values(entries).forEach(booking => {
if (timeSlot[0] === moment(booking.startDateTime).format('HH:mm:ss')) {
timeSlot[1]++
if (timeSlot[1] >= diary.slots.maxConcurrency) availableTimes.splice(index, 1)
if (timeSlot[1] >= diary.slots.maxConcurrency) remove.push(index)
}
})

Object.values(diary.slots.restrictions).forEach(restriction => {
if (
timeSlot[0] >= restriction.timesAffected[0] &&
timeSlot[0] < restriction.timesAffected[1] &&
timeSlot[1] >= restriction.changes.maxConcurrency
) {
remove.push(index)
}
})
})

const times = Object.values(availableTimes).map((timeSlot) => {
const final = availableTimes.filter((e, idx) => !remove.includes(idx))

const times = Object.values(final).map((timeSlot) => {
const t = moment(event.date.split('T')[0] + 'T' + timeSlot[0])
return {
value: t.format(),
Expand Down
4 changes: 2 additions & 2 deletions test/get-available-times-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,10 @@ describe('Test the get available times state resource', function () {
expect(executionDescription.status).to.eql('SUCCEEDED')

const filtered = executionDescription.ctx.availableTimes.filter(e => ['10:30 - 11:30', '12:30 - 13:30', '18:30 - 19:30'].includes(e.label))
// expect(filtered.length).to.eql(0) UNCOMMENT WHEN FIXED
expect(filtered.length).to.eql(0)

expect(executionDescription.ctx.availableTimes[0].label).to.eql('08:30 - 09:30')
expect(executionDescription.ctx.availableTimes[12].label).to.eql('21:30 - 22:30')
expect(executionDescription.ctx.availableTimes[10].label).to.eql('21:30 - 22:30')
})

it('should shutdown Tymly', async () => {
Expand Down

0 comments on commit 2ce6d58

Please sign in to comment.