Skip to content

Commit b945c4b

Browse files
[FEATURE] Initier un data repository pour enregistrer des passages events (PIX-16728)
#11557
2 parents 4cd3a07 + 6fa4644 commit b945c4b

File tree

4 files changed

+51
-38
lines changed

4 files changed

+51
-38
lines changed

api/src/devcomp/domain/models/passage-events/PassageEvent.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,8 @@ class PassageEvent {
1616
throw new PassageEventInstantiationError();
1717
}
1818

19-
assertNotNullOrUndefined(id, 'The id is required for a PassageEvent');
2019
assertNotNullOrUndefined(type, 'The type is required for a PassageEvent');
2120
assertNotNullOrUndefined(occurredAt, 'The occurredAt is required for a PassageEvent');
22-
assertNotNullOrUndefined(createdAt, 'The createdAt is required for a PassageEvent');
2321
assertNotNullOrUndefined(passageId, 'The passageId is required for a PassageEvent');
2422

2523
this.id = id;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { DomainTransaction } from '../../../shared/domain/DomainTransaction.js';
2+
3+
async function record(event) {
4+
const knexConn = DomainTransaction.getConnection();
5+
await knexConn('passage-events').insert({
6+
passageId: event.passageId,
7+
occurredAt: event.occurredAt,
8+
type: event.type,
9+
data: event.data,
10+
});
11+
}
12+
13+
export { record };
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { PassageStartedEvent } from '../../../../src/devcomp/domain/models/passage-events/passage-events.js';
2+
import * as passageEventRepository from '../../../../src/devcomp/infrastructure/repositories/passage-event-repository.js';
3+
import { databaseBuilder, expect, knex, sinon } from '../../../test-helper.js';
4+
5+
describe('Integration | DevComp | Repositories | PassageEventRepository', function () {
6+
describe('#record', function () {
7+
let clock;
8+
9+
beforeEach(function () {
10+
clock = sinon.useFakeTimers(new Date('2023-12-31'), 'Date');
11+
});
12+
13+
afterEach(function () {
14+
clock.restore();
15+
});
16+
17+
it('should record a passage event', async function () {
18+
// given
19+
const passage = databaseBuilder.factory.buildPassage();
20+
await databaseBuilder.commit();
21+
const event = new PassageStartedEvent({
22+
occurredAt: new Date('2019-04-28'),
23+
passageId: passage.id,
24+
contentHash: 'abcd1234',
25+
});
26+
27+
// when
28+
await passageEventRepository.record(event);
29+
30+
// then
31+
const recordedEvent = await knex('passage-events')
32+
.where({ type: 'PASSAGE_STARTED', passageId: passage.id })
33+
.first();
34+
expect(recordedEvent.data.contentHash).to.equal('abcd1234');
35+
expect(recordedEvent.occurredAt).to.deep.equal(new Date('2019-04-28'));
36+
});
37+
});
38+
});

api/tests/devcomp/unit/domain/models/module/PassageEvent_test.js

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,6 @@ describe('Unit | Devcomp | Domain | Models | PassageEvent', function () {
1313
expect(error).to.be.instanceOf(PassageEventInstantiationError);
1414
});
1515

16-
describe('if a passage event does not have an id', function () {
17-
it('should throw an error', function () {
18-
// given
19-
class FakeEvent extends PassageEvent {
20-
constructor() {
21-
super({});
22-
}
23-
}
24-
25-
// when
26-
const error = catchErrSync(() => new FakeEvent())();
27-
28-
// then
29-
expect(error).to.be.instanceOf(DomainError);
30-
expect(error.message).to.equal('The id is required for a PassageEvent');
31-
});
32-
});
33-
3416
describe('if a passage event does not have a type', function () {
3517
it('should throw an error', function () {
3618
// given
@@ -67,24 +49,6 @@ describe('Unit | Devcomp | Domain | Models | PassageEvent', function () {
6749
});
6850
});
6951

70-
describe('if a passage event does not have a createdAt', function () {
71-
it('should throw an error', function () {
72-
// given
73-
class FakeEvent extends PassageEvent {
74-
constructor() {
75-
super({ id: 1, type: 'FAKE', occurredAt: Symbol('date') });
76-
}
77-
}
78-
79-
// when
80-
const error = catchErrSync(() => new FakeEvent())();
81-
82-
// then
83-
expect(error).to.be.instanceOf(DomainError);
84-
expect(error.message).to.equal('The createdAt is required for a PassageEvent');
85-
});
86-
});
87-
8852
describe('if a passage event does not have a passageId', function () {
8953
it('should throw an error', function () {
9054
// given

0 commit comments

Comments
 (0)