thenativeweb / node-cqrs-domain

Node-cqrs-domain is a node.js module based on nodeEventStore that. It can be very useful as domain component if you work with (d)ddd, cqrs, eventdenormalizer, host, etc.
http://cqrs.js.org/pages/domain.html
MIT License
269 stars 57 forks source link

[Bug] Precondition aggregate is empty #137

Closed ssipos90 closed 5 years ago

ssipos90 commented 5 years ago

Hi,

So I was adding a precondition to prevent changing a field of an aggregate to the same value

cqrs
  .definePreCondition({
    name: ['renameNode'],
    description: 'Cannot rename to same.',
    priority: 1
  }, (cmd, agg, cb) => {
    if (agg.get('label') === cmd.payload.label) {
      return cb(new Error())
    }
    cb(null);
  })

and guess what? The pre-condition aggregate doesn't have the attributes loaded. The command works fine and if I log something in the callback, it shows.

I've debugged for 3 hours already and I think the issue is in defaultCommandHandler.js somewhere within loadAggregate: function.

What am I missing?

ssipos90 commented 5 years ago

My aggregate looks like this.

cqrs.defineAggregate(
    {
      name: 'node',
      defaultEventPayload: 'payload'
    },
    {} // default starting data for the aggregate
  )

Command is:

require('cqrs-domain').defineCommand(
  {
    existing: true,
    meta: 'meta'
  },
  ({ payload, meta }, aggregate) => aggregate.apply('nodeRenamed', {
    ...payload,
    _meta: meta
  })
);

Domain thingy is


require('cqrs-domain')({...})
  .defineCommand({
    aggregateId: 'aggregate.id',
    aggregate: 'aggregate.name',
    payload: 'payload',
    context: 'context.name',
  })
  .defineEvent({
    name: 'event',
    aggregateId: 'aggregate.id',
    aggregate: 'aggregate.name',
    context: 'context.name',
  });
ssipos90 commented 5 years ago

Never mind, I'm stupid and haven't seen that the event is messed up:

require('cqrs-domain').defineEvent(
  {
    version: 0,
    meta: 'meta'
  },
  (cmd, aggregate) => aggregate.set(cmd.payload)
);

and it should be (note the cb function):

require('cqrs-domain').defineEvent(
  {
    version: 0,
    meta: 'meta'
  },
  (payload, aggregate) => aggregate.set(payload)
);

I hope I didn't waste anyone's time. Thanks! :)