Skip to content

Commit af852cc

Browse files
Merge pull request #231 from 4lessandrodev/chore/deps
Chore/deps
2 parents 3aef7ce + 67e81c1 commit af852cc

3 files changed

Lines changed: 52 additions & 25 deletions

File tree

README.md

Lines changed: 45 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -192,40 +192,49 @@ interface Props {
192192

193193
export default class Money extends ValueObject<Props> {
194194

195-
// private constructor
195+
// private constructor. Avoid public new.
196196
private constructor(props: Props) {
197197
super(props);
198198
}
199199

200-
// any business rule behavior
201-
sum(x: Money): Money {
200+
// any business rule behavior. Check.
201+
public isGt(x: Money): boolean {
202+
const { number: Check } = this.validator;
203+
const xValue = x.get('amount');
204+
const currentValue = this.get('amount');
205+
return Check(x).isGreatThan(currentValue);
206+
}
207+
208+
// any business rule behavior. Calc.
209+
public sum(x: Money): Money {
202210
const { number: Calc } = this.util;
203211
const value = fee.get('amount');
204-
const current = this.props.total.get('amount');
212+
const current = this.get('amount');
205213
const amount = Calc(current).sum(value);
206214
return new Money({ amount });
207215
}
208216

209-
// any business rule behavior
210-
subtract(x: Money): Money {
217+
// any business rule behavior. Calc.
218+
public subtract(x: Money): Money {
211219
const { number: Calc } = this.util;
212220
const value = fee.get('amount');
213-
const current = this.props.total.get('amount');
221+
const current = this.get('amount');
214222
const amount = Calc(current).subtract(value);
215223
return new Money({ amount });
216224
}
217225

218-
// any business rule
226+
// any business rule to validate state.
219227
public static isValidProps({ amount }: Props): boolean {
220-
return this.validator.number(amount).isPositive();
228+
const { number: Check } = this.validator;
229+
return Check(amount).isPositive();
221230
}
222231

223232
// shortcut to create a zero value
224233
public static zero(): Money {
225234
return new Money({ amount: 0 });
226235
}
227236

228-
// factory method to create a instance
237+
// factory method to create an instance and validate value.
229238
public static create(amount: number): Result<Money> {
230239

231240
const isValid = this.isValidProps({ amount });
@@ -254,6 +263,17 @@ const money = result.value();
254263

255264
money.get("amount"); // 500
256265

266+
// using methods
267+
money.isGt(Money.zero());
268+
269+
> true
270+
271+
const other = Money.create(100).value();
272+
273+
const result = money.sum(other);
274+
275+
result.get('amount'); // 600
276+
257277
```
258278

259279
---
@@ -275,41 +295,41 @@ interface Props {
275295
fees: Money;
276296
}
277297

298+
// simple example as monetary value object business behavior
278299
export default class Payment extends Entity<Props> {
279300

280301
// private constructor
281302
private constructor(props: Props){
282303
super(props);
283304
}
284305

285-
// any business rule behavior
286-
checkDiscount(value: Money): Money {
287-
const { number: Check } = this.validator;
288-
const total = this.props.total.get('amount');
289-
const discount = value.get('amount');
290-
const isGt = Check(discount).isGreaterThan(total);
291-
if(isGt) return this.props.total;
306+
// any business rule behavior. Discount must be less or equal total.
307+
public checkDiscount(value: Money): Money {
308+
const total = this.props.total;
309+
const isGt = value.isGt(total);
310+
if(isGt) return total;
292311
return value;
293312
}
294313

295-
// any business rule behavior
296-
applyFees(value: Money): Payment {
314+
// any business rule behavior. Update total.
315+
public applyFees(value: Money): Payment {
297316
const props = this.props;
298-
const fees = props.fees.sum(value);
317+
const currentFee = this.props.fee;
318+
const fees = currentFee.sum(value);
299319
const total = props.total.sum(fees);
300320
return new Payment({ ...props, total, fees });
301321
}
302322

303-
// any business rule behavior
304-
applyDiscount(value: Money): Payment {
323+
// any business rule behavior. Discount must be less or equal total.
324+
public applyDiscount(value: Money): Payment {
305325
const props = this.props;
306326
const result = this.checkDiscount(value);
307327
const discount = props.discount.subtract(result);
308328
const total = props.total.subtract(discount);
309329
return new Payment({ ...props, total, discount });
310330
}
311331

312-
// factory method to create a instance
332+
// factory method to create a instance. Value must be positive.
313333
public static create(props: Props): Result<Payment> {
314334
return Ok(new Payment(props));
315335
}
@@ -352,6 +372,8 @@ console.log(result.toObject());
352372

353373
---
354374

375+
### See also how to use Aggregate.
376+
355377
## Lib Full Documentation
356378

357379
Check lib documentation on link [Here](https://github.com/4lessandrodev/types-ddd/tree/main/docs)

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@
7777
"rimraf": "^4.1.2",
7878
"ts-jest": "^27.1.4",
7979
"ts-node": "^10.7.0",
80-
"typescript": "^4.9.4"
80+
"typescript": "^4.9.5"
8181
},
8282
"peerDependencies": {
8383
"bcrypt": "^5.0.1",

yarn.lock

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4074,11 +4074,16 @@ typescript@^3.9.10, typescript@^3.9.5, typescript@^3.9.7:
40744074
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.9.10.tgz#70f3910ac7a51ed6bef79da7800690b19bf778b8"
40754075
integrity sha512-w6fIxVE/H1PkLKcCPsFqKE7Kv7QUwhU8qQY2MueZXWx5cPZdwFupLgKK3vntcK98BtNHZtAF4LA/yl2a7k8R6Q==
40764076

4077-
typescript@^4.0.0, typescript@^4.5.5, typescript@^4.9.4:
4077+
typescript@^4.0.0, typescript@^4.5.5:
40784078
version "4.9.4"
40794079
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.9.4.tgz#a2a3d2756c079abda241d75f149df9d561091e78"
40804080
integrity sha512-Uz+dTXYzxXXbsFpM86Wh3dKCxrQqUcVMxwU54orwlJjOpO3ao8L7j5lH+dWfTwgCwIuM9GQ2kvVotzYJMXTBZg==
40814081

4082+
typescript@^4.9.5:
4083+
version "4.9.5"
4084+
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.9.5.tgz#095979f9bcc0d09da324d58d03ce8f8374cbe65a"
4085+
integrity sha512-1FXk9E2Hm+QzZQ7z+McJiHL4NW1F2EzMu9Nq9i3zAaGqibafqYwCVU6WyWAuyQRRzOlxou8xZSyXLEN8oKj24g==
4086+
40824087
uniq@^1.0.1:
40834088
version "1.0.1"
40844089
resolved "https://registry.yarnpkg.com/uniq/-/uniq-1.0.1.tgz#b31c5ae8254844a3a8281541ce2b04b865a734ff"

0 commit comments

Comments
 (0)