This repository has been archived by the owner on May 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(telegram): add income and outcome commands in bot
- Loading branch information
1 parent
0258751
commit 2cb8966
Showing
28 changed files
with
215 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
back/src/money/presentation/telegram/actions/TransactionActions.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { Injectable } from '@nestjs/common' | ||
import { Context, PipeContext, TelegramActionHandler } from 'nest-telegram' | ||
|
||
import { CurrentSender } from '@back/user/presentation/telegram/transformer/CurrentSender' | ||
import { TokenPayload } from '@back/user/application/dto/TokenPayload' | ||
import { Accountant } from '@back/money/application/Accountant' | ||
import { Templating } from '@back/utils/infrastructure/Templating/Templating' | ||
import { parseCurrency } from '../helpers/parseCurrency' | ||
import { parseAmount } from '../helpers/parseAmount' | ||
|
||
@Injectable() | ||
export class TransactionActions { | ||
public constructor( | ||
private readonly accountant: Accountant, | ||
private readonly templating: Templating, | ||
) {} | ||
|
||
@TelegramActionHandler({ command: '/income' }) | ||
public async income( | ||
ctx: Context, | ||
@PipeContext(CurrentSender) { login }: TokenPayload, | ||
) { | ||
const [_, rawAmount, rawCurrency, ...source] = ctx.message.text.split(' ') | ||
|
||
// TODO: Add message parser to nest-telegram lib as decorator | ||
const incomeFields = { | ||
amount: parseAmount(rawAmount), | ||
currency: parseCurrency(rawCurrency), | ||
date: new Date(), | ||
source: source.join(' '), | ||
} | ||
|
||
await this.accountant.income(login, incomeFields) | ||
|
||
const responseText = await this.templating.render( | ||
'telegram/income-created', | ||
incomeFields, | ||
) | ||
|
||
await ctx.reply(responseText) | ||
} | ||
|
||
@TelegramActionHandler({ command: '/outcome' }) | ||
public async outcome( | ||
ctx: Context, | ||
@PipeContext(CurrentSender) { login }: TokenPayload, | ||
) { | ||
const [_, rawAmount, rawCurrency, ...category] = ctx.message.text.split(' ') | ||
|
||
// TODO: Add message parser to nest-telegram lib as decorator | ||
const outcomeFields = { | ||
amount: parseAmount(rawAmount), | ||
currency: parseCurrency(rawCurrency), | ||
date: new Date(), | ||
category: category.join(' '), | ||
} | ||
|
||
await this.accountant.outcome(login, outcomeFields) | ||
|
||
const responseText = await this.templating.render( | ||
'telegram/outcome-created', | ||
outcomeFields, | ||
) | ||
|
||
await ctx.reply(responseText) | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
back/src/money/presentation/telegram/catcher/UnexpectedParameterCatcher.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { TelegramErrorHandler, TelegramCatch, Context } from 'nest-telegram' | ||
|
||
import { UnexpectedParameterException } from '@back/utils/infrastructure/exception/UnexpectedParameterException' | ||
|
||
@TelegramCatch(UnexpectedParameterException) | ||
export class UnexpectedParameterCatcher | ||
implements TelegramErrorHandler<UnexpectedParameterException> { | ||
public async catch(ctx: Context, exception: UnexpectedParameterException) { | ||
await ctx.reply(exception.message) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export const parseAmount = (rawAmount: string): number => | ||
parseFloat(rawAmount.replace(/,/g, '.')) * 100 |
17 changes: 17 additions & 0 deletions
17
back/src/money/presentation/telegram/helpers/parseCurrency.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { Currency } from '@shared/enum/Currency' | ||
import { UnexpectedParameterException } from '@back/utils/infrastructure/exception/UnexpectedParameterException' | ||
|
||
export const parseCurrency = (rawCurrency: string): Currency => { | ||
const transformedCurrency = rawCurrency.toUpperCase() | ||
|
||
if (!Object.values(Currency).includes(transformedCurrency)) { | ||
throw new UnexpectedParameterException( | ||
'currency', | ||
`"${rawCurrency}" is invalid currency, please use one of follow insted: ${Object.values( | ||
Currency, | ||
).join(', ')}`, | ||
) | ||
} | ||
|
||
return transformedCurrency as Currency | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
back/src/user/presentation/telegram/transformer/CurrentSender.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Injectable } from '@nestjs/common' | ||
import { ContextTransformer, Context } from 'nest-telegram' | ||
|
||
import { UserRepository } from '@back/user/domain/UserRepository' | ||
import { TokenPayload } from '@back/user/application/dto/TokenPayload' | ||
|
||
@Injectable() | ||
export class CurrentSender implements ContextTransformer<TokenPayload> { | ||
public constructor(private readonly userRepo: UserRepository) {} | ||
|
||
public async transform(ctx: Context) { | ||
const user = await this.userRepo.getOneByTeleram(ctx.from.id) | ||
|
||
return { | ||
login: user.login, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
back/src/utils/infrastructure/Templating/HandlebarsTemplating.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { Injectable } from '@nestjs/common' | ||
import * as Handlebars from 'handlebars' | ||
import { readFile } from 'fs' | ||
import { promisify } from 'util' | ||
import { resolve } from 'path' | ||
|
||
import { Templating } from './Templating' | ||
import { money } from './handlebarsHelpers/money' | ||
|
||
type PrecompiledTempltes = { | ||
[name: string]: Handlebars.TemplateDelegate | ||
} | ||
|
||
@Injectable() | ||
export class HandlebarsTemplating implements Templating { | ||
private readonly precompiledTemplates = {} | ||
|
||
public constructor() { | ||
Handlebars.registerHelper('money', money) | ||
} | ||
|
||
public async render(templateName: string, context: object) { | ||
const compiled = await this.compile(templateName) | ||
|
||
return compiled(context) | ||
} | ||
|
||
private async compile( | ||
templateName: string, | ||
): Promise<Handlebars.TemplateDelegate> { | ||
if (this.precompiledTemplates[templateName]) { | ||
return this.precompiledTemplates[templateName] | ||
} | ||
|
||
const templateContent = (await promisify(readFile)( | ||
resolve(__dirname, '../../../../templates', `${templateName}.twig`), | ||
)).toString() | ||
|
||
const compiled = Handlebars.compile(templateContent) | ||
|
||
this.precompiledTemplates[templateName] = compiled | ||
|
||
return compiled | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export abstract class Templating { | ||
public abstract render(templateName: string, context: object): Promise<string> | ||
} |
7 changes: 7 additions & 0 deletions
7
back/src/utils/infrastructure/Templating/handlebarsHelpers/money.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import * as Handlebars from 'handlebars' | ||
|
||
import { Currency } from '@shared/enum/Currency' | ||
import { displayMoney } from '@shared/helpers/displayMoney' | ||
|
||
export const money = (amount: number, currency: Currency) => | ||
new Handlebars.SafeString(displayMoney(currency)(amount)) |
5 changes: 5 additions & 0 deletions
5
back/src/utils/infrastructure/exception/UnexpectedParameterException.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export class UnexpectedParameterException extends Error { | ||
public constructor(public readonly parameter: string, message: string) { | ||
super(message) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Income transaction created. | ||
|
||
{{money amount currency}} ({{source}}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Outcome transaction created. | ||
|
||
{{money amount currency}} ({{category}}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
front/src/features/app/features/history/components/Outcomes.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
front/src/features/app/features/stats/components/currency-switch.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.