Skip to content

Commit

Permalink
[FEATURE] Initier un data repository pour enregistrer des passages ev…
Browse files Browse the repository at this point in the history
…ents (PIX-16728)

 #11557
  • Loading branch information
pix-service-auto-merge authored Mar 4, 2025
2 parents 4cd3a07 + 6fa4644 commit b945c4b
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 38 deletions.
2 changes: 0 additions & 2 deletions api/src/devcomp/domain/models/passage-events/PassageEvent.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@ class PassageEvent {
throw new PassageEventInstantiationError();
}

assertNotNullOrUndefined(id, 'The id is required for a PassageEvent');
assertNotNullOrUndefined(type, 'The type is required for a PassageEvent');
assertNotNullOrUndefined(occurredAt, 'The occurredAt is required for a PassageEvent');
assertNotNullOrUndefined(createdAt, 'The createdAt is required for a PassageEvent');
assertNotNullOrUndefined(passageId, 'The passageId is required for a PassageEvent');

this.id = id;
Expand Down
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 };
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'));
});
});
});
36 changes: 0 additions & 36 deletions api/tests/devcomp/unit/domain/models/module/PassageEvent_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,6 @@ describe('Unit | Devcomp | Domain | Models | PassageEvent', function () {
expect(error).to.be.instanceOf(PassageEventInstantiationError);
});

describe('if a passage event does not have an id', function () {
it('should throw an error', function () {
// given
class FakeEvent extends PassageEvent {
constructor() {
super({});
}
}

// when
const error = catchErrSync(() => new FakeEvent())();

// then
expect(error).to.be.instanceOf(DomainError);
expect(error.message).to.equal('The id is required for a PassageEvent');
});
});

describe('if a passage event does not have a type', function () {
it('should throw an error', function () {
// given
Expand Down Expand Up @@ -67,24 +49,6 @@ describe('Unit | Devcomp | Domain | Models | PassageEvent', function () {
});
});

describe('if a passage event does not have a createdAt', function () {
it('should throw an error', function () {
// given
class FakeEvent extends PassageEvent {
constructor() {
super({ id: 1, type: 'FAKE', occurredAt: Symbol('date') });
}
}

// when
const error = catchErrSync(() => new FakeEvent())();

// then
expect(error).to.be.instanceOf(DomainError);
expect(error.message).to.equal('The createdAt is required for a PassageEvent');
});
});

describe('if a passage event does not have a passageId', function () {
it('should throw an error', function () {
// given
Expand Down

0 comments on commit b945c4b

Please sign in to comment.