Skip to content

Commit

Permalink
save commit
Browse files Browse the repository at this point in the history
  • Loading branch information
praslnx8 committed Jan 20, 2024
1 parent 6b9ef32 commit e112f49
Show file tree
Hide file tree
Showing 16 changed files with 106 additions and 33 deletions.
2 changes: 1 addition & 1 deletion lib/api/apis/sip_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class SipApi {
executedTill: const Value.absent()));
}

Future<int> delete({final int? id, final int? investmentId}) async {
Future<int> deleteBy({final int? id, final int? investmentId}) async {
if (id != null) {
return (_db.delete(_db.sipTable)..where((t) => t.id.equals(id))).go();
} else if (investmentId != null) {
Expand Down
8 changes: 4 additions & 4 deletions lib/domain/models/goal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class Goal {
.getById(id: goalInvestment.investmentId)
.then((investmentDO) =>
Investment.from(investmentDO: investmentDO))
.then((investment) => investment.getTotalInvestedAmount())
.then((investment) => investment.getTotalInvestedAmount(till: maturityDate))
.then((amount) => calculatePercentageOfValue(
value: amount,
percentage: goalInvestment.splitPercentage)))))
Expand All @@ -69,9 +69,9 @@ class Goal {
Future<double> getMaturityAmount() {
return Future(() => _irrCalculator.calculateValueOnIRR(
irr: inflation,
date: maturityDate,
value: amount,
valueUpdatedOn: amountUpdatedOn));
futureDate: maturityDate,
currentValue: amount,
currentValueUpdatedOn: amountUpdatedOn));
}

Future<double> getValueOnMaturity() async {
Expand Down
20 changes: 14 additions & 6 deletions lib/domain/models/investment.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,17 @@ class Investment {
_goalApi = goalApi ?? GoalApi(),
_sipApi = sipApi ?? SipApi();

Future<double> getTotalInvestedAmount() async {
Future<double> getTotalInvestedAmount({final DateTime? till}) async {
return _transactionApi
.getBy(investmentId: id)
.then((transactions) => transactions
.map((transactionDO) =>
Transaction.from(transactionDO: transactionDO))
.toList())
.then((transactions) =>
transactions.map((transaction) => transaction.amount))
.then((transactions) => transactions
.where((transaction) =>
till == null || till.isAfter(transaction.createdOn))
.map((transaction) => transaction.amount))
.then((amounts) => amounts.isNotEmpty
? amounts.reduce((value, element) => value + element)
: 0);
Expand All @@ -87,7 +89,10 @@ class Investment {
final irr = _irrCalculator.calculateIRR(
payments: payments, value: value, valueUpdatedOn: valueUpdatedOn);
return _irrCalculator.calculateValueOnIRR(
irr: irr, date: date, value: value, valueUpdatedOn: valueUpdatedOn);
irr: irr,
futureDate: date,
currentValue: value,
currentValueUpdatedOn: valueUpdatedOn);
} else if (irr != null) {
return _irrCalculator.calculateFutureValueOnIRR(
payments: payments, irr: irr, date: date);
Expand Down Expand Up @@ -172,8 +177,11 @@ class Investment {
.then((_) => sip.performSipTransactions().then((_) => sip)));
}

Future<void> deleteSIP({required final int sipId}) async {
return _sipApi.delete(id: sipId).then((count) => {});
Future<void> deleteSIP({required final int id}) async {
return _transactionApi
.deleteBy(sipId: id)
.then((_) => _sipApi.deleteBy(id: id))
.then((_) => {});
}

Future<Transaction> createTransaction(
Expand Down
10 changes: 5 additions & 5 deletions lib/domain/models/irr_calculator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ class IRRCalculator {

double calculateValueOnIRR({
required final double irr,
required final DateTime date,
required final double value,
required final DateTime valueUpdatedOn,
required final DateTime futureDate,
required final double currentValue,
required final DateTime currentValueUpdatedOn,
}) {
double years = valueUpdatedOn.difference(date).inDays / 365;
return value / pow(1 + irr / 100, years);
double years = currentValueUpdatedOn.difference(futureDate).inDays / 365;
return currentValue / pow(1 + irr / 100, years);
}
}

Expand Down
5 changes: 3 additions & 2 deletions lib/domain/models/sip.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ class SIP {

Future<List<Payment>> getFuturePayment({required final DateTime till}) {
return Future(() {
List<Payment> payments = [];
final List<Payment> payments = [];
final DateTime? endDate = this.endDate;
for (var i = executedTill ?? startDate;
i.isBefore(endDate ?? till);
i.isBefore(till) && (endDate == null || i.isBefore(endDate));
i = i.add(getDuration(frequency))) {
payments.add(Payment(amount: amount, createdOn: i));
}
Expand Down
8 changes: 7 additions & 1 deletion lib/domain/services/backup_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ import 'package:wealth_wave/api/apis/backup_api.dart';
class BackupService {
final BackupApi _backupApi;

BackupService({final BackupApi? backupApi})
factory BackupService() {
return _instance;
}

static final BackupService _instance = BackupService._();

BackupService._({final BackupApi? backupApi})
: _backupApi = backupApi ?? BackupApi();

Future<void> import() async {
Expand Down
9 changes: 8 additions & 1 deletion lib/domain/services/basket_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@ import 'package:wealth_wave/domain/models/basket.dart';
class BasketService {
final BasketApi _basketApi;

BasketService({BasketApi? basketApi}) : _basketApi = basketApi ?? BasketApi();
factory BasketService() {
return _instance;
}

static final BasketService _instance = BasketService._();

BasketService._({BasketApi? basketApi})
: _basketApi = basketApi ?? BasketApi();

Future<Basket> create(
{required final String name, required final String description}) {
Expand Down
8 changes: 7 additions & 1 deletion lib/domain/services/boot_strap_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ import 'package:wealth_wave/domain/services/sip_service.dart';
class BootStrapService {
final SipService _sipService;

BootStrapService({final SipService? sipService})
factory BootStrapService() {
return _instance;
}

static final BootStrapService _instance = BootStrapService._();

BootStrapService._({final SipService? sipService})
: _sipService = sipService ?? SipService();

Future<void> performBootStrapOperations() {
Expand Down
8 changes: 7 additions & 1 deletion lib/domain/services/goal_investment_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ import 'package:wealth_wave/domain/models/goal_investment_tag.dart';
class GoalInvestmentService {
final GoalInvestmentApi _goalInvestmentApi;

GoalInvestmentService({final GoalInvestmentApi? goalInvestmentApi})
factory GoalInvestmentService() {
return _instance;
}

static final GoalInvestmentService _instance = GoalInvestmentService._();

GoalInvestmentService._({final GoalInvestmentApi? goalInvestmentApi})
: _goalInvestmentApi = goalInvestmentApi ?? GoalInvestmentApi();

Future<GoalInvestmentTag> getBy({required final int id}) async {
Expand Down
8 changes: 7 additions & 1 deletion lib/domain/services/goal_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ import 'package:wealth_wave/domain/models/goal.dart';
class GoalService {
final GoalApi _goalApi;

GoalService({GoalApi? goalApi}) : _goalApi = goalApi ?? GoalApi();
factory GoalService() {
return _instance;
}

static final GoalService _instance = GoalService._();

GoalService._({GoalApi? goalApi}) : _goalApi = goalApi ?? GoalApi();

Future<Goal> create({
required final String name,
Expand Down
24 changes: 21 additions & 3 deletions lib/domain/services/investment_service.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
import 'package:wealth_wave/api/apis/investment_api.dart';
import 'package:wealth_wave/api/apis/sip_api.dart';
import 'package:wealth_wave/api/apis/transaction_api.dart';
import 'package:wealth_wave/contract/risk_level.dart';
import 'package:wealth_wave/domain/models/investment.dart';

class InvestmentService {
final InvestmentApi _investmentApi;
final TransactionApi _transactionApi;
final SipApi _sipApi;

InvestmentService({final InvestmentApi? investmentApi})
: _investmentApi = investmentApi ?? InvestmentApi();
factory InvestmentService() {
return _instance;
}

static final InvestmentService _instance = InvestmentService._();

InvestmentService._(
{final InvestmentApi? investmentApi,
final TransactionApi? transactionApi,
final SipApi? sipApi})
: _investmentApi = investmentApi ?? InvestmentApi(),
_transactionApi = transactionApi ?? TransactionApi(),
_sipApi = sipApi ?? SipApi();

Future<Investment> create(
{required final String name,
Expand Down Expand Up @@ -75,6 +90,9 @@ class InvestmentService {
}

Future<void> deleteBy({required final int id}) {
return _investmentApi.deleteBy(id: id);
return _transactionApi
.deleteBy(investmentId: id)
.then((_) => _sipApi.deleteBy(investmentId: id))
.then((_) => _investmentApi.deleteBy(id: id));
}
}
8 changes: 7 additions & 1 deletion lib/domain/services/sip_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ import 'package:wealth_wave/domain/models/sip.dart';
class SipService {
final SipApi _sipApi;

SipService({final SipApi? sipApi}) : _sipApi = sipApi ?? SipApi();
factory SipService() {
return _instance;
}

static final SipService _instance = SipService._();

SipService._({final SipApi? sipApi}) : _sipApi = sipApi ?? SipApi();

Future<SIP> getBy({required final int id}) async {
return _sipApi.getById(id: id).then((sipDO) {
Expand Down
8 changes: 7 additions & 1 deletion lib/domain/services/transaction_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ import 'package:wealth_wave/domain/models/transaction.dart';
class TransactionService {
final TransactionApi _transactionApi;

TransactionService({final TransactionApi? transactionApi})
factory TransactionService() {
return _instance;
}

static final TransactionService _instance = TransactionService._();

TransactionService._({final TransactionApi? transactionApi})
: _transactionApi = transactionApi ?? TransactionApi();

Future<Transaction> getBy({required final int id}) async {
Expand Down
5 changes: 4 additions & 1 deletion lib/presentation/create_goal_presenter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,10 @@ class CreateGoalPresenter extends Presenter<CreateGoalViewState> {
final double inflation = viewState.inflation;

final double targetAmount = _irrCalculator.calculateValueOnIRR(
irr: inflation, date: targetDate, value: amount, valueUpdatedOn: date);
irr: inflation,
futureDate: targetDate,
currentValue: amount,
currentValueUpdatedOn: date);

viewState.targetAmount = targetAmount;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/presentation/sips_presenter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class SipsPresenter extends Presenter<SipsViewState> {
void deleteSip({required final int id}) {
_investmentService
.getBy(id: _investmentId)
.then((investment) => investment.deleteSIP(sipId: id))
.then((investment) => investment.deleteSIP(id: id))
.then((_) => getSips());
}
}
Expand Down
6 changes: 3 additions & 3 deletions test/domain/irr_calculator_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ void main() {
test('calculateIRR value', () {
final calculator = IRRCalculator();
final value = calculator.calculateValueOnIRR(
value: 1000,
valueUpdatedOn: DateTime(2020, 1, 1),
currentValue: 1000,
currentValueUpdatedOn: DateTime(2020, 1, 1),
irr: 10,
date: DateTime(2022, 1, 1),
futureDate: DateTime(2022, 1, 1),
);

expect(value, closeTo(1210, 1)); // Expected IRR is 20%, tolerance is 1%
Expand Down

0 comments on commit e112f49

Please sign in to comment.