Skip to content
This repository has been archived by the owner on May 11, 2021. It is now read-only.

Commit

Permalink
feat(budget tip): add calculation of average recomended outcome
Browse files Browse the repository at this point in the history
  • Loading branch information
Uncleseneca committed Apr 9, 2019
1 parent 24d8e35 commit a5300b0
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions back/src/mind/application/adviser/BudgetAdviser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { IsAdviser } from '../../infrastructure/adviser/helpers/IsAdviser'
import { Statistician } from '@back/money/application/Statistician'
import { GroupBy } from '@shared/enum/GroupBy'
import { Currency } from '@shared/enum/Currency'
import { addMonths } from 'date-fns'
import { addMonths, lastDayOfMonth, differenceInDays } from 'date-fns'

@IsAdviser()
export class BudgetAdviser implements Adviser {
Expand All @@ -17,13 +17,22 @@ export class BudgetAdviser implements Adviser {
public async giveAdvice(userLogin: string): Promise<TipModel[]> {
const now = new Date()

const [_, ...result] = await this.statistician.showDateRangeStats(
const [_, ...stats] = await this.statistician.showDateRangeStats(
userLogin,
{ from: new Date(addMonths(new Date(), -2)), to: new Date() },
GroupBy.Month,
Currency.USD,
)

const lastMonthIncome = stats[0].income
const thisMonthOutcome = stats[1].outcome
const expectedProfit = lastMonthIncome - thisMonthOutcome
const daysRemainInMonth = differenceInDays(
lastDayOfMonth(new Date()),
new Date(),
)
const result = expectedProfit / daysRemainInMonth

return [
{
date: now,
Expand Down

0 comments on commit a5300b0

Please sign in to comment.