Skip to content

Instantly share code, notes, and snippets.

@goldbergyoni
Last active October 3, 2021 18:17
Show Gist options
  • Select an option

  • Save goldbergyoni/a68b94be5e845446d49f6255277aea01 to your computer and use it in GitHub Desktop.

Select an option

Save goldbergyoni/a68b94be5e845446d49f6255277aea01 to your computer and use it in GitHub Desktop.
Transfer Service
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
}
}
@rluvaton
Copy link

rluvaton commented Oct 3, 2021

I should you should move line 17 to the end of 16 to decrease confusion

maybe also await the transfer and save call

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment