fromGoal static method

GoalCardData fromGoal(
  1. GoalGetResponse response
)

Implementation

static GoalCardData fromGoal(GoalGetResponse response) {
  final deposits = response.deposits ?? [];
  final unmissedDeposits = deposits
      .where((element) => element.depositStatus != DepositStatus.skipped)
      .toList();
  final successDeposits = deposits
      .where((element) => element.depositStatus == DepositStatus.success)
      .toList();

  final bool isGoalActive = response.status == GoalStatus.active ||
      response.status == GoalStatus.firstDepositFailed;
  final isGoalStoppedMidway =
      !isGoalActive && unmissedDeposits.length != successDeposits.length ||
          response.status == GoalStatus.cancelled;

  final id = response.id ?? '';
  final status = response.status ?? GoalStatus.draft;
  final name = response.name ?? '';
  final tenure = response.tenure?.toTenure() ?? Tenure.dummy();
  final completedTenure = Tenure(
      value: response.deposits
              ?.map((e) => e.depositStatus == DepositStatus.success)
              .where((element) => element)
              .length ??
          0,
      unit: TenureUnit.month);

  final redeemedAmount = response.redeemedAmount ?? 0;
  final withdrawnAmount = response.withdrawnAmount ?? 0;
  final availableAmount = response.availableAmount ?? 0;
  final totalAccumulatedAmount =
      availableAmount + withdrawnAmount + redeemedAmount;

  final double accumulatedRewardValue = response.deposits
          ?.where((e) => e.isRewardActive == true)
          .map((e) => e.rewardAmount ?? 0)
          .fold(0, (value, element) => value! + element) ??
      0;

  final totalAmountUpTillNow =
      buildMoneyNumberString(response.availableAmount);

  final accumulatedReward = buildMoneyNumberString(accumulatedRewardValue);
  double takeOutPercent;
  final isGoalExited = response.status == GoalStatus.partiallyRedeemed ||
      response.status == GoalStatus.redeemed ||
      response.status == GoalStatus.partiallyWithdrawn;

  takeOutPercent = 0;

  if (isGoalExited) {
    takeOutPercent = numOrZero(
        ((response.redeemedAmount ?? 0) + (response.withdrawnAmount ?? 0)) /
            ((response.availableAmount ?? 0) +
                (response.redeemedAmount ?? 0) +
                (response.withdrawnAmount ?? 0)));
  }

  final double requiredDeposit = response.deposits
          ?.where((e) => e.depositStatus == DepositStatus.success)
          .map((e) => e.dueAmount ?? 0)
          .fold(0, (value, element) => element + value!) ??
      0;

  final double rewardPercent =
      accumulatedRewardValue > 0 && requiredDeposit > 0
          ? accumulatedRewardValue / requiredDeposit
          : 0;
  final rewardEMI = buildMoneyNumberString(rewardPercent);
  bool isPaymentDue = false;

  final goalAmount = buildMoneyNumberString(response.goalAmount ?? 0);

  final paidAmount = buildMoneyNumberString(response.deposits
          ?.where((e) => e.depositStatus == DepositStatus.success)
          .map((e) => (e.depositAmount ?? 0))
          .fold(0, (value, element) => value! + element) ??
      0);

  final dueAmount = buildMoneyNumberString(response.deposits
          ?.firstWhere(
            (element) => element.depositNo == response.currentDepositNo,
            orElse: () => GoalDeposit.fromJson({}),
          )
          .dueAmount ??
      0);

  final dueDate = response.dates?.nextDue ?? DateTime.now();
  final imageUrl = response.imageUrl ?? '';
  final alertingPayments = deposits.where((element) =>
      element.depositStatus == DepositStatus.due ||
      element.depositStatus == DepositStatus.processing ||
      element.depositStatus == DepositStatus.failed);
  final showRibbon = alertingPayments.length == 1 &&
      (response.status == GoalStatus.active ||
          response.status == GoalStatus.firstDepositFailed);

  var ribbonText = '';

  if (alertingPayments.isNotEmpty) {
    final duePayment = alertingPayments.first;
    if (duePayment.depositStatus == DepositStatus.processing) {
      final isPaymentThroughMandate =
          response.autoDebitDetails?.isAutoDebitChargeProcessing ?? false;
      ribbonText = isPaymentThroughMandate
          ? 'We will auto-process the upcoming payment'
          : "We're processing your payment";
    } else {
      ribbonText =
          "Payment of ${buildMoneyNumberString(duePayment.dueAmount)} is due${toDateText(response.dates?.nextDue, defaultValue: "", prefix: " on ")}";
    }
  }

  final showPaymentDueDate = response.status == GoalStatus.active;
  final autoMandateDetails = response.autoDebitDetails;

  return GoalCardData(
    isSubscriptionPending: autoMandateDetails?.isSubscriptionPending ?? false,
    autoDebitSubscriptionExternalId:
        autoMandateDetails?.autoDebitSubscriptionExternalId ?? '',
    isActiveSubscriptionPresent:
        autoMandateDetails?.isActiveSubscriptionPresent ?? false,
    isAutoDebitSubscriptionPossible:
        autoMandateDetails?.isAutoDebitSubscriptionPossible ?? false,
    tnc: autoMandateDetails?.tnc ?? [],
    showSwitchBrand: response.isGoalSwitchable == true,
    isAnyVouchersGenerated: response.isAnyVouchersGenerated(),
    discontinueDate: response.discontinuedOn,
    ribbonText: ribbonText,
    unmissedDepositsLength: unmissedDeposits.length,
    isGoalActive: isGoalActive,
    isGoalStoppedMidway: isGoalStoppedMidway,
    totalAmountUpTillNow: totalAmountUpTillNow,
    availableAmount: availableAmount,
    status: status,
    imageUrl: imageUrl,
    name: name,
    showRibbon: showRibbon,
    goalAmount: goalAmount,
    dueAmount: dueAmount,
    tenure: tenure,
    dueDate: dueDate,
    id: id,
    showPaymentDueDate: showPaymentDueDate,
    rewardPercent: rewardPercent,
    rewardEMI: rewardEMI,
    takeOutPercent: takeOutPercent,
    accumulatedReward: accumulatedReward,
    paidAmount: paidAmount,
    completedTenure: completedTenure,
    isPaymentDue: isPaymentDue,
    totalAccumulatedAmount: totalAccumulatedAmount,
    isRedeemed: response.status == GoalStatus.redeemed,
    endDate: response.dates?.end ?? DateTime.now(),
    isGoalRevisable: response.isGoalRevisable ?? false,
  );
}