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 retrieving income and outome for 2 months
Browse files Browse the repository at this point in the history
  • Loading branch information
Uncleseneca committed Apr 9, 2019
1 parent c25bb1c commit 24d8e35
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 1 deletion.
7 changes: 7 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"workbench.colorCustomizations": {
"activityBar.background": "#412720",
"titleBar.activeBackground": "#5B372D",
"titleBar.activeForeground": "#FCFAF9"
}
}
45 changes: 45 additions & 0 deletions back/src/mind/application/adviser/BudgetAdviser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import * as md5 from 'md5'

import { TipModel } from '@shared/models/mind/TipModel'
import { TipAction } from '@shared/enum/TipAction'

import { Adviser } from '../../infrastructure/adviser/helpers/Adviser'
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'

@IsAdviser()
export class BudgetAdviser implements Adviser {
public constructor(private readonly statistician: Statistician) {}

public async giveAdvice(userLogin: string): Promise<TipModel[]> {
const now = new Date()

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

return [
{
date: now,
action: TipAction.MergeSources,
meta: result,
token: this.createToken([''], TipAction.MergeSources),
},
]
}

private createToken(variants: string[], action: TipAction): string {
const payload = {
variants: variants.sort(),
action,
}

return md5(JSON.stringify(payload))
}
}
2 changes: 2 additions & 0 deletions back/src/mind/mind.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { DisabledTipRepository } from './domain/DisabledTipRepository'
import { TipsDisabler } from './application/TipsDisabler'
import { TypoController } from './presentation/http/controller/TypoController'
import { TypoMerger } from './application/TypoMerger'
import { BudgetAdviser } from './application/adviser/BudgetAdviser'

@Module({
imports: [
Expand All @@ -29,6 +30,7 @@ import { TypoMerger } from './application/TypoMerger'
TypoFinder,
TypoMerger,
TypoAdviser,
BudgetAdviser,
AdviserUnity,
TipsFilter,
TipsDisabler,
Expand Down
2 changes: 1 addition & 1 deletion back/src/money/money.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ import { ApiClientUnity } from './insfrastructure/ExchangeRateApi/ApiClientUnity
TransactionActions,
UnexpectedParameterCatcher,
],
exports: [IncomeRepository, OutcomeRepository],
exports: [IncomeRepository, OutcomeRepository, Statistician],
})
export class MoneyModule implements NestModule {
public configure(consumer: MiddlewareConsumer) {
Expand Down

0 comments on commit 24d8e35

Please sign in to comment.