-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
merged tests; added tests for stopping an application
- Loading branch information
Showing
18 changed files
with
631 additions
and
142 deletions.
There are no files selected for viewing
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
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,63 @@ | ||
const { | ||
USER_CREATED, | ||
USER_UPDATED, | ||
USER_REJECTED, | ||
USER_MAIL_CHANGE_REVERTED | ||
} = require('../events/users'); | ||
|
||
const { | ||
ConflictError, | ||
BadRequestError, | ||
ForbiddenError | ||
} = require('../errors'); | ||
|
||
module.exports = { | ||
create: async (command, state/* , context */) => { | ||
if(state.registered) | ||
throw new ConflictError('User already registered'); | ||
if(!command.payload.email) | ||
throw new BadRequestError('Please provide an email address'); | ||
if(!command.payload.name) | ||
throw new BadRequestError('Please provide a name'); | ||
|
||
return { | ||
type: USER_CREATED, | ||
payload: { | ||
email: command.payload.email, | ||
name: command.payload.name | ||
} | ||
}; | ||
}, | ||
update: async (command, state/* , context */) => { | ||
if(!state.registered || state.removed) | ||
throw new ForbiddenError(); | ||
if(!command.payload.name && !command.payload.email) | ||
throw new BadRequestError('Please specify a new name and/or email address'); | ||
if(command.payload.name === state.name) | ||
throw new BadRequestError(`Name is already set to '${state.name}'`); | ||
if(command.payload.email === state.email) | ||
throw new BadRequestError(`Email address is already set to '${state.email}'`); | ||
|
||
return { | ||
type: USER_UPDATED, | ||
payload: { | ||
name: command.payload.name ?? state.name, | ||
email: command.payload.email ?? state.email | ||
} | ||
}; | ||
}, | ||
reject: async (command/* , state, context */) => { | ||
return { | ||
type: USER_REJECTED, | ||
payload: command.payload | ||
}; | ||
}, | ||
restoreEmailAddress: async (command, state/*, context */) => { | ||
if(!state.registered || state.removed) | ||
throw new ForbiddenError(); | ||
return { | ||
type: USER_MAIL_CHANGE_REVERTED, | ||
payload: {}, | ||
}; | ||
} | ||
}; |
11 changes: 9 additions & 2 deletions
11
...testExample/aggregates/user.projection.js → ...ces/testApp/aggregates/user.projection.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 |
---|---|---|
@@ -1,15 +1,22 @@ | ||
const { | ||
USER_CREATED, | ||
USER_UPDATED, | ||
USER_REJECTED | ||
} = require('../events/users'); | ||
|
||
module.exports = { | ||
init: () => ({}), | ||
[USER_CREATED]: (state, event) => ({ | ||
...state | ||
[USER_CREATED]: (state, {payload}) => ({ | ||
...state, | ||
...payload, | ||
registered: true | ||
}), | ||
[USER_UPDATED]: (state, {payload}) => ({ | ||
...state, | ||
...payload | ||
}), | ||
[USER_REJECTED]: state => ({ | ||
...state, | ||
removed: true | ||
}) | ||
}; |
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,88 @@ | ||
const Blackrik = require('blackrik'); | ||
|
||
module.exports = { | ||
aggregates: [ | ||
{ | ||
name: 'user', | ||
commands: require('./aggregates/user.commands'), | ||
projection: require('./aggregates/user.projection') | ||
} | ||
], | ||
readModels: [ | ||
{ | ||
name: 'user', | ||
projection: require('./readModels/user.projection'), | ||
resolvers: require('./readModels/user.resolvers'), | ||
adapter: 'default' | ||
} | ||
], | ||
sagas: [ | ||
{ | ||
name: 'user', | ||
source: require('./sagas/user'), | ||
adapter: 'default' | ||
} | ||
], | ||
adapter: 'default', | ||
readModelStoreAdapters: { | ||
default: { | ||
module: Blackrik.ADAPTERS.READMODELSTORE.MySQL, | ||
args: { | ||
host: 'localhost', | ||
database: 'readmodelstore', | ||
user: 'root', | ||
password: '1234' | ||
} | ||
} | ||
}, | ||
eventStoreAdapter: { | ||
module: Blackrik.ADAPTERS.EVENTSTORE.MySQL, | ||
args: { | ||
host: 'localhost', | ||
database: 'eventstore', | ||
user: 'root', | ||
password: '1234' | ||
} | ||
}, | ||
eventBusAdapter: { | ||
module: Blackrik.ADAPTERS.EVENTBUS.Kafka, | ||
args: { | ||
brokers: ['localhost:9092'] | ||
} | ||
// module: Blackrik.ADAPTERS.EVENTBUS.Local, | ||
// args: {} | ||
}, | ||
server: { | ||
config: { | ||
port: 3000, | ||
skipDefaultMiddlewares: false | ||
}, | ||
middlewares: [ | ||
// a middleware for all routes | ||
(req, res, next) => (req.test = 21) && next(), | ||
// a middleware for /test only | ||
[ | ||
'/test', | ||
(req, res, next) => (req.test = 42) && next() | ||
] | ||
], | ||
routes: [ | ||
{ | ||
method: 'GET', | ||
path: '/test', | ||
callback: (req, res) => { | ||
console.log('CALLED /test; req.test is', req.test); | ||
res.json({middlewareValue: req.test}); | ||
} | ||
}, | ||
{ | ||
method: 'GET', | ||
path: '/test2', | ||
callback: (req, res) => { | ||
console.log('CALLED /test2; req.test is', req.test); | ||
res.json({middlewareValue: req.test}); | ||
} | ||
} | ||
] | ||
} | ||
}; |
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,59 @@ | ||
class BaseError extends Error | ||
{ | ||
constructor(message = 'Internal Error', status = 500) | ||
{ | ||
super(message); | ||
this.status = status; | ||
} | ||
} | ||
module.exports.BaseError = BaseError; | ||
|
||
class BadRequestError extends BaseError | ||
{ | ||
constructor(message = 'Bad Request') | ||
{ | ||
super(message); | ||
this.status = 400; | ||
} | ||
} | ||
module.exports.BadRequestError = BadRequestError; | ||
|
||
class UnauthorizedError extends BaseError | ||
{ | ||
constructor(message = 'Unauthorized') | ||
{ | ||
super(message); | ||
this.status = 401; | ||
} | ||
} | ||
module.exports.UnauthorizedError = UnauthorizedError; | ||
|
||
class ForbiddenError extends BaseError | ||
{ | ||
constructor(message = 'Forbidden') | ||
{ | ||
super(message); | ||
this.status = 403; | ||
} | ||
} | ||
module.exports.ForbiddenError = ForbiddenError; | ||
|
||
class NotFoundError extends BaseError | ||
{ | ||
constructor(message = 'Not Found') | ||
{ | ||
super(message); | ||
this.status = 404; | ||
} | ||
} | ||
module.exports.NotFoundError = NotFoundError; | ||
|
||
class ConflictError extends BaseError | ||
{ | ||
constructor(message = 'Conflict') | ||
{ | ||
super(message); | ||
this.status = 409; | ||
} | ||
} | ||
module.exports.ConflictError = ConflictError; |
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,6 @@ | ||
module.exports = { | ||
USER_CREATED: 'USER_CREATED', | ||
USER_UPDATED: 'USER_UPDATED', | ||
USER_REJECTED: 'USER_REJECTED', | ||
USER_MAIL_CHANGE_REVERTED: 'USER_MAIL_CHANGE_REVERTED' | ||
}; |
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,43 @@ | ||
const { | ||
USER_CREATED, | ||
USER_UPDATED, | ||
USER_REJECTED | ||
} = require('../events/users'); | ||
|
||
const tableName = 'Users'; | ||
|
||
module.exports = { | ||
init: async store => { | ||
await store.defineTable(tableName, { | ||
id: { | ||
type: 'uuid', | ||
primaryKey: true, | ||
}, | ||
email: 'String', | ||
name: 'String', | ||
createdAt: 'Date', | ||
updatedAt: 'Date' | ||
}); | ||
}, | ||
[USER_CREATED]: async (store, event) => { | ||
const createdAt = new Date(event.timestamp); | ||
await store.insert(tableName, { | ||
id: event.aggregateId, | ||
email: event.payload.email, | ||
name: event.payload.name, | ||
createdAt, | ||
updatedAt: createdAt | ||
}); | ||
}, | ||
[USER_UPDATED]: async (store, event) => { | ||
await store.update(tableName, { | ||
id: event.aggregateId | ||
}, { | ||
name: event.payload.name, | ||
updatedAt: new Date(event.timestamp) | ||
}); | ||
}, | ||
[USER_REJECTED]: async (store, {aggregateId: id}) => { | ||
await store.delete(tableName, {id}); | ||
} | ||
}; |
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,9 @@ | ||
const tableName = 'Users'; | ||
|
||
module.exports = { | ||
get: async (store, {id, position}/* , context */) => { | ||
if(!id) | ||
return null; | ||
return await store.findOne(tableName, {id}, {position}); | ||
} | ||
}; |
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,37 @@ | ||
const {USER_CREATED} = require('../events/users'); | ||
|
||
const tableName = 'RegisteredUsers'; | ||
|
||
module.exports = { | ||
handlers: { | ||
init: async store => { | ||
await store.defineTable(tableName, { | ||
email: { | ||
type: 'String', | ||
primaryKey: true, | ||
} | ||
}); | ||
return { | ||
noopSideEffectsOnReplay: true | ||
}; | ||
}, | ||
[USER_CREATED]: async (store, {aggregateId, payload: {email, name}}, sideEffects) => { | ||
if(await store.findOne(tableName, {email})) | ||
return await sideEffects.executeCommand({ | ||
aggregateName: 'user', | ||
aggregateId, | ||
type: 'reject', | ||
payload: { | ||
reason: 'email address already taken' | ||
} | ||
}); | ||
await store.insert(tableName, {email}); | ||
await sideEffects.sendRegistrationMail(email, name); | ||
} | ||
}, | ||
sideEffects: { | ||
sendRegistrationMail: async (email, name) => { | ||
console.log(`Sending an email to "${name} <${email}>"...`); | ||
} | ||
} | ||
}; |
Oops, something went wrong.