-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: update device when next event change
- Loading branch information
1 parent
e900d92
commit f6934b9
Showing
6 changed files
with
138 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,32 @@ | ||
import { Reservation } from '../Reservation.js'; | ||
|
||
export class HandleNextReservationUseCase { | ||
constructor({ reservationRepository, passRepository }) { | ||
constructor({ reservationRepository, passRepository, deviceRepository, notificationAdapter }) { | ||
this.reservationRepository = reservationRepository; | ||
this.passRepository = passRepository; | ||
this.deviceRepository = deviceRepository; | ||
this.notificationAdapter = notificationAdapter; | ||
} | ||
|
||
async execute() { | ||
const reservations = await this.reservationRepository.findByStatus(Reservation.STATUSES.RESERVED); | ||
const now = new Date(); | ||
const nextReservations = reservations.filter(({ start }) => now < start); | ||
const nextReservation = nextReservations.sort((reservationA, reservationB) => reservationA.start - reservationB.start)[0]; | ||
await this.passRepository.updateAll({ nextEvent: nextReservation.code }); | ||
const passes = await this.passRepository.findAll(); | ||
const updatedPasses = []; | ||
for (const pass of passes) { | ||
if (pass.nextEvent !== nextReservation.code) { | ||
continue; | ||
} | ||
|
||
await this.passRepository.update({ ...pass, nextEvent: nextReservation.code, updated_at: now }); | ||
updatedPasses.push(pass); | ||
} | ||
|
||
const devicesToNotify = await this.deviceRepository.findByPasses(updatedPasses); | ||
for (const device of devicesToNotify) { | ||
await this.notificationAdapter.notify(device.pushToken); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
tests/integration/infrastructure/DeviceRepository_tests.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import { knex } from '../../../db/knex-database-connection.js'; | ||
import { deviceRepository } from '../../../src/infrastructure/repositories/DeviceRepository.js'; | ||
import { expect, sinon } from '../../test-helpers.js'; | ||
|
||
describe('Integration | Infrastructure | DeviceRepository', function () { | ||
describe('#save', function () { | ||
let clock; | ||
const now = new Date('2024-01-01'); | ||
|
||
beforeEach(async function () { | ||
await knex('devices').delete(); | ||
clock = sinon.useFakeTimers({ now, toFake: ['Date'] }); | ||
}); | ||
|
||
afterEach(async function () { | ||
await knex('devices').delete(); | ||
clock.restore(); | ||
}); | ||
|
||
it('should save device', async function () { | ||
const device = { | ||
deviceLibraryIdentifier: 'device-type-id', | ||
pushToken: 'push-token', | ||
}; | ||
|
||
await deviceRepository.save(device); | ||
|
||
const { created_at, ...savedDevice } = await knex('devices').where(device).first(); | ||
expect(savedDevice).to.deep.equal(device); | ||
}); | ||
}); | ||
|
||
describe('#findByPasses', function () { | ||
beforeEach(async function () { | ||
await knex('registrations').delete(); | ||
await knex('passes').delete(); | ||
await knex('devices').delete(); | ||
}); | ||
|
||
afterEach(async function () { | ||
await knex('registrations').delete(); | ||
await knex('passes').delete(); | ||
await knex('devices').delete(); | ||
}); | ||
|
||
it('should return devices register to given passes', async function () { | ||
const device1 = { | ||
deviceLibraryIdentifier: 'device-type-id-1', | ||
pushToken: 'push-token-2', | ||
}; | ||
|
||
const device2 = { | ||
deviceLibraryIdentifier: 'device-type-id-2', | ||
pushToken: 'push-token-2', | ||
}; | ||
await knex('devices').insert([device1, device2]); | ||
|
||
const pass1 = { | ||
passTypeIdentifier: 'pass.type.identifier1', | ||
serialNumber: 'serial1', | ||
}; | ||
const pass2 = { | ||
passTypeIdentifier: 'pass.type.identifier2', | ||
serialNumber: 'serial2', | ||
}; | ||
const pass3 = { | ||
passTypeIdentifier: 'pass.type.identifier2', | ||
serialNumber: 'serial3', | ||
}; | ||
await knex('passes').insert([pass1, pass2, pass3]); | ||
|
||
await knex('registrations').insert({ | ||
deviceLibraryIdentifier: device1.deviceLibraryIdentifier, | ||
passTypeIdentifier: pass1.passTypeIdentifier, | ||
serialNumber: pass1.serialNumber, | ||
}); | ||
await knex('registrations').insert({ | ||
deviceLibraryIdentifier: device1.deviceLibraryIdentifier, | ||
passTypeIdentifier: pass2.passTypeIdentifier, | ||
serialNumber: pass2.serialNumber, | ||
}); | ||
await knex('registrations').insert({ | ||
deviceLibraryIdentifier: device2.deviceLibraryIdentifier, | ||
passTypeIdentifier: pass2.passTypeIdentifier, | ||
serialNumber: pass2.serialNumber, | ||
}); | ||
|
||
const devices = await deviceRepository.findByPasses([pass1, pass2]); | ||
|
||
expect(devices).to.deep.equal([ | ||
{ | ||
deviceLibraryIdentifier: 'device-type-id-1', | ||
pushToken: 'push-token-2', | ||
}, | ||
{ | ||
deviceLibraryIdentifier: 'device-type-id-2', | ||
pushToken: 'push-token-2', | ||
}, | ||
]); | ||
}); | ||
}); | ||
}); |