Permalink
Browse files

emit missingEvent if commandRejected and revision not in sync

  • Loading branch information...
1 parent 05e0224 commit 0f53520aa2ecd54760d1efd013603c797976a734 @adrai committed Feb 7, 2014
Showing with 72 additions and 1 deletion.
  1. +4 −0 README.md
  2. +16 −0 lib/eventDenormalizer.js
  3. +1 −1 package.json
  4. +51 −0 test/integration/eventDenormalizerTest.js
View
@@ -132,6 +132,10 @@ See [tests](https://github.com/adrai/node-cqrs-eventdenormalizer/tree/master/tes
# Release Notes
+## v0.3.5
+
+- emit missingEvent if commandRejected and revision not in sync
+
## v0.3.4
- handle versioned events
View
@@ -117,6 +117,22 @@ module.exports = evtDen = _.extend(new EventEmitter2({
},
denormalize: function(evt, callback) {
+
+ if (evt.event === 'commandRejected' &&
+ evt.payload && evt.payload.reason &&
+ evt.payload.reason.name === 'AggregateDestroyed') {
+
+ if (revisionGuard && revisionGuard.store) {
+ revisionGuard.store.getRevision(evt.payload.reason.aggregateId, function(err, entry) {
+ if (entry.revision - 1 < evt.payload.reason.aggregateRevision) {
+ evtDen.emit('eventMissing', evt.payload.reason.aggregateId, entry.revision, evt.payload.reason.aggregateRevision, evt);
+ }
+ });
+ } else {
+ evtDen.emit('eventMissing', evt.payload.reason.aggregateId, 0, evt.payload.reason.aggregateRevision, evt);
+ }
+ }
+
var entry = {
// workers: eventEmitter.listeners('denormalize:' + evt.event).length,
workers: eventEmitter.registerCount('denormalize:' + evt.event),
View
@@ -1,7 +1,7 @@
{
"author": "adrai",
"name": "cqrs-eventdenormalizer",
- "version": "0.3.4",
+ "version": "0.3.5",
"private": false,
"main": "index.js",
"engines": {
@@ -722,6 +722,57 @@ describe('EventDenormalizer', function() {
});
+ describe('noting a commandRejected Event because of destroyed aggregate', function() {
+
+ var evtRej,
+ existingEvt;
+
+ beforeEach(function() {
+
+ existingEvt = {
+ id: '165475112323',
+ event: 'dummyCreated',
+ head: {
+ revision: 1
+ },
+ payload: {
+ id: '50123123'
+ }
+ };
+
+ evtRej = {
+ id: '13131316547',
+ event: 'commandRejected',
+ payload: {
+ reason: {
+ name: 'AggregateDestroyed',
+ message: 'Aggregate has already been destroyed!',
+ aggregateRevision: 5,
+ aggregateId: '50123123'
+ }
+ }
+ };
+
+ eventDenormalizer.denormalize(existingEvt, function(err) {});
+
+ });
+
+ it('it should emit eventMissing', function(done) {
+
+ eventDenormalizer.once('eventMissing', function(id, aggregateRevision, eventRevision, evt) {
+ expect(evt).to.eql(evtRej);
+ expect(id).to.eql(existingEvt.payload.id);
+ expect(aggregateRevision).to.eql(2);
+ expect(eventRevision).to.eql(5);
+ done();
+ });
+
+ eventDenormalizer.denormalize(evtRej, function(err) {});
+
+ });
+
+ });
+
describe('having a custom action', function() {
var evt;

0 comments on commit 0f53520

Please sign in to comment.