-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api): add passage-event-repository with record method
Co-authored-by: Diane Cordier <[email protected]> Co-authored-by: Eric Lim <[email protected]>
- Loading branch information
1 parent
7ac58f7
commit 6fa4644
Showing
2 changed files
with
51 additions
and
0 deletions.
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
api/src/devcomp/infrastructure/repositories/passage-event-repository.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,13 @@ | ||
import { DomainTransaction } from '../../../shared/domain/DomainTransaction.js'; | ||
|
||
async function record(event) { | ||
const knexConn = DomainTransaction.getConnection(); | ||
await knexConn('passage-events').insert({ | ||
passageId: event.passageId, | ||
occurredAt: event.occurredAt, | ||
type: event.type, | ||
data: event.data, | ||
}); | ||
} | ||
|
||
export { record }; |
38 changes: 38 additions & 0 deletions
38
api/tests/devcomp/integration/repositories/passage-event-repository_test.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,38 @@ | ||
import { PassageStartedEvent } from '../../../../src/devcomp/domain/models/passage-events/passage-events.js'; | ||
import * as passageEventRepository from '../../../../src/devcomp/infrastructure/repositories/passage-event-repository.js'; | ||
import { databaseBuilder, expect, knex, sinon } from '../../../test-helper.js'; | ||
|
||
describe('Integration | DevComp | Repositories | PassageEventRepository', function () { | ||
describe('#record', function () { | ||
let clock; | ||
|
||
beforeEach(function () { | ||
clock = sinon.useFakeTimers(new Date('2023-12-31'), 'Date'); | ||
}); | ||
|
||
afterEach(function () { | ||
clock.restore(); | ||
}); | ||
|
||
it('should record a passage event', async function () { | ||
// given | ||
const passage = databaseBuilder.factory.buildPassage(); | ||
await databaseBuilder.commit(); | ||
const event = new PassageStartedEvent({ | ||
occurredAt: new Date('2019-04-28'), | ||
passageId: passage.id, | ||
contentHash: 'abcd1234', | ||
}); | ||
|
||
// when | ||
await passageEventRepository.record(event); | ||
|
||
// then | ||
const recordedEvent = await knex('passage-events') | ||
.where({ type: 'PASSAGE_STARTED', passageId: passage.id }) | ||
.first(); | ||
expect(recordedEvent.data.contentHash).to.equal('abcd1234'); | ||
expect(recordedEvent.occurredAt).to.deep.equal(new Date('2019-04-28')); | ||
}); | ||
}); | ||
}); |