-
Notifications
You must be signed in to change notification settings - Fork 1
/
Command.js
127 lines (114 loc) · 3.23 KB
/
Command.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
'use strict';
const formats = require('formats'),
uuid = require('uuidv4');
const Command = function (options) {
if (!formats.isObject(options)) {
throw new Error('Options are missing.');
}
if (!formats.isObject(options.context)) {
throw new Error('Context is missing.');
}
if (!formats.isAlphanumeric(options.context.name, { minLength: 1 })) {
throw new Error('Context name is missing.');
}
if (!formats.isObject(options.aggregate)) {
throw new Error('Aggregate is missing.');
}
if (!formats.isAlphanumeric(options.aggregate.name, { minLength: 1 })) {
throw new Error('Aggregate name is missing.');
}
if (!formats.isUuid(options.aggregate.id)) {
throw new Error('Aggregate id is missing.');
}
if (!formats.isAlphanumeric(options.name, { minLength: 1 })) {
throw new Error('Command name is missing.');
}
if (options.data && !formats.isObject(options.data)) {
throw new Error('Data must be an object.');
}
if (options.custom && !formats.isObject(options.custom)) {
throw new Error('Custom must be an object.');
}
this.context = { name: options.context.name };
this.aggregate = { name: options.aggregate.name, id: options.aggregate.id };
this.name = options.name;
this.id = uuid();
this.data = options.data || {};
this.custom = options.custom || {};
this.user = null;
this.metadata = {
timestamp: Date.now(),
correlationId: this.id,
causationId: this.id
};
};
Command.wrap = function (options) {
const command = new Command(options);
command.id = options.id;
command.metadata.timestamp = options.metadata.timestamp;
command.metadata.correlationId = options.metadata.correlationId;
command.metadata.causationId = options.metadata.causationId;
if (options.user && options.user.token) {
command.addToken(options.user.token);
}
if (!Command.isWellformed(command)) {
throw new Error('Command is malformed.');
}
return command;
};
Command.prototype.addToken = function (token) {
if (!token) {
throw new Error('Token is missing.');
}
this.user = {
id: token.sub,
token
};
};
Command.isWellformed = function (command) {
return formats.isObject(command, {
schema: {
context: formats.object({
schema: {
name: formats.alphanumeric({ minLength: 1 })
}
}),
aggregate: formats.object({
schema: {
name: formats.alphanumeric({ minLength: 1 }),
id: formats.uuid()
}
}),
name: formats.alphanumeric({ minLength: 1 }),
id: formats.uuid(),
data: formats.object({
schema: {},
isSchemaRelaxed: true
}),
custom: formats.object({
schema: {},
isSchemaRelaxed: true
}),
user: formats.object({
schema: {
id: formats.string({ minLength: 1 }),
token: formats.object({
schema: {
sub: formats.string({ minLength: 1 })
},
isSchemaRelaxed: true
})
},
isOptional: true
}),
metadata: formats.object({
schema: {
timestamp: formats.number(),
correlationId: formats.uuid(),
causationId: formats.uuid()
}
})
}
});
};
module.exports = Command;