Skip to content

Instantly share code, notes, and snippets.

@gaboelnuevo
Last active November 27, 2019 04:32
Show Gist options
  • Save gaboelnuevo/261218f7a7f3552645fa0dbbf3d54619 to your computer and use it in GitHub Desktop.
Save gaboelnuevo/261218f7a7f3552645fa0dbbf3d54619 to your computer and use it in GitHub Desktop.
class Wallet {
constructor (patient, currency) {
this.patient = patient;
this.currency = currency;
}
async getBalanceAsync () {
// sum `amount` aggregation with patient and currency filter
return 0;
}
async addTransaction ({ type: any, amount: number, description: string: metadata?: any }) {
const currentBalance = await getBalanceAsync();
if ((currentBalance + amount) < 0) {
return Promise.reject(new Error("insufficient balance"));
}
let transaction = new WalletTransaction ({
patient: this.patient,
currency: this.currency,
// balance: currentBalance + amount,
amount,
description,
type,
metatada,
});
return transaction.save();
}
async execDeposit ({ amount: number, description: string, metadata?: any }) {
return this.addTransaction({
type: "deposit",
description: description,
metadata,
amount
});
}
async execRefund ({ amount: number, description: string, metadata?: any }) {
return this.addTransaction({
type: "refund",
description: description,
metadata,
amount: amount
});
}
async execPayment ({ amount: number, description: string, metadata?: any }) {
return this.addTransaction({
type: "payment",
description: description,
metadata,
amount: amount * -1
});
}
async addCashback ({ amount: number, description: string, metadata?: any }) {
return this.addTransaction({
type: "cashback",
description: description,
metadata,
amount: amount
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment