-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccount.js
93 lines (82 loc) · 2.41 KB
/
account.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
/**
* @fileoverview Aggregate for an account
* @author Joey Whelan <joey.whelan@gmail.com>
*/
/*jshint esversion: 6 */
'use strict';
'use esversion 6';
const logger = require('./accountLogger');
/** @desc Account aggregate */
module.exports = class Account {
constructor(id, version=0, timestamp=0) {
logger.debug(`Account constructor - id:${id}, version:${version}, timestamp:${timestamp}`);
this.id = id;
this.version = version;
this.timestamp = timestamp;
this.funds = 0;
}
/**
* Performs a funds deposit to a player
* Will generate an exception for a non-positive deposit amount.
* @param {int} amount - amount to be deposited
* @return void
*/
deposit(amount) {
logger.debug(`Account.deposit - amount:${amount}`);
if (amount <= 0) {
throw new Error('Attempting a transaction with a 0 or negative value');
}
this.funds += amount;
return;
}
/**
* Updates the aggregate from a list of events.
* @param {array} events - list of event objects
* @return none
*/
rehydrate(events) {
logger.debug(`Account.rehydrate - events.length:${events.length}`);
for (let event of events) {
if (event.id === this.id && event.timestamp !== this.timestamp) { //only apply events that have happened since last timestamp on agg
this.version = event.version;
this.timestamp = event.timestamp;
if (event.hasOwnProperty('amount') && event.hasOwnProperty('type')) {
switch (event.type) {
case 'deposit':
this.funds += event.amount;
break;
case 'withdraw':
this.funds -= event.amount;
break;
default:
break;
}
}
}
}
}
/**
* Helper function to generate an object from the account class
* @return {Object} object representing the current state of aggregate.
*/
toObject() {
return {'id':this.id, 'version':this.version, 'timestamp':this.timestamp, 'funds':this.funds};
}
/**
* Performs a funds withdrawal from an account
* Will generate an exception for a non-positive withdrawal amount or an attempt to overdraft
* @param {int} amount - amount to be withdrawn
* @return void
*/
withdraw(amount) {
logger.debug(`Account.withdraw - amount:${amount}`);
if (amount <= 0) {
throw new Error('Attempting a transaction with a 0 or negative value');
}
if (this.funds - amount < 0) {
throw new Error('Attempting to deduct more funds than available');
}
this.funds -= amount;
return;
}
};