@@ -192,40 +192,49 @@ interface Props {
192192
193193export 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
255264money .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
278299export 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
357379Check lib documentation on link [ Here] ( https://github.com/4lessandrodev/types-ddd/tree/main/docs )
0 commit comments