Last active
October 3, 2021 18:17
-
-
Save goldbergyoni/a68b94be5e845446d49f6255277aea01 to your computer and use it in GitHub Desktop.
Transfer Service
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class TransferService { | |
| async transfer({ id, sender, receiver, transferAmount, bankName }) { | |
| // Validation | |
| if (!sender || !receiver || !transferAmount || !bankName) { | |
| throw new Error("Some mandatory property was not provided"); | |
| } | |
| // Handle insufficient credit | |
| if (this.options.creditPolicy === "zero" && sender.credit < transferAmount) { | |
| this.numberOfDeclined++; //incrementing interal metric | |
| return {id, status: "declined",date}; | |
| } | |
| // All good, let's transfer using the bank 3rd party service + save in DB | |
| await this.bankProviderService.transfer(sender, receiver, transferAmount, bankName); | |
| await this.repository.save({id,sender,receiver,transferAmount,bankName}); | |
| return {id,status: "approved", date: new Date()}; | |
| } | |
| getTransfers(senderName){ | |
| // Query the DB | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I should you should move line 17 to the end of 16 to decrease confusion
maybe also await the transfer and save call