cleanup, patterns
This commit is contained in:
+4
-10
@@ -1,17 +1,11 @@
|
||||
import { randomUUID } from 'crypto';
|
||||
import { catchResult, bind, tap, tapError, mapError, pipe } from './library/result.js';
|
||||
import { output, outputError } from './library/logging.js';
|
||||
import { catchResult, bind, mapError, pipe } from './library/result.js';
|
||||
import { log, logWith, formatMoney } from './library/logging.js';
|
||||
import { createInMemoryRepository, innerAccountError } from './infrastructure/repository.js';
|
||||
import { openAccount } from './domain/account.js';
|
||||
import { createMoney } from './domain/money.js';
|
||||
import { createWithdrawMoney } from './application/accountApp.js';
|
||||
|
||||
const log = (src, msg) => (result) =>
|
||||
tap((x) => output(src, msg)(x))(tapError((e) => outputError(src, e))(result));
|
||||
|
||||
const logWith = (src, renderText) => (result) =>
|
||||
tap((x) => output(src, renderText(x))(x))(tapError((e) => outputError(src, e))(result));
|
||||
|
||||
console.log('[js-fp3] Starting withdraw money demo...');
|
||||
|
||||
const accountId = randomUUID();
|
||||
@@ -30,11 +24,11 @@ pipe(
|
||||
bind((withdrawMoney) =>
|
||||
pipe(
|
||||
openAccount(accountId, createMoney(200)),
|
||||
logWith('js-fp3 seed', (account) => `Seeding account ${account.id} with opening balance ${account.balance.amount.toFixed(2)}`),
|
||||
logWith('js-fp3 seed', (account) => `Seeding account ${account.id} with opening balance ${formatMoney(account.balance)}`),
|
||||
bind(repo.saveAccount),
|
||||
log('js-fp3 exec', `Executing withdrawal ${withdrawalAmount.toFixed(2)} from account ${accountId}`),
|
||||
bind(() => withdrawMoney(accountId, withdrawalAmount)),
|
||||
logWith('js-fp3 new balance', (account) => `New balance is ${account.balance.amount.toFixed(2)}`),
|
||||
logWith('js-fp3 new balance', (account) => `New balance is ${formatMoney(account.balance)}`),
|
||||
mapError((ae) => innerAccountError(ae))
|
||||
)
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user