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

Commit

Permalink
feat(telegram): add login in telegram
Browse files Browse the repository at this point in the history
  • Loading branch information
igorkamyshev committed Feb 28, 2019
1 parent 5a95540 commit 0258751
Show file tree
Hide file tree
Showing 11 changed files with 104 additions and 11 deletions.
2 changes: 1 addition & 1 deletion back/.env.dist
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ DB_HOST=localhost
DB_PORT=5432
DB_USER=admin
DB_PASSWORD=admin
DB_NAME=checkmoney
DB_NAME=checkmoney2

PRODUCTION_READY=1

Expand Down
4 changes: 2 additions & 2 deletions back/.trona-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ const client = new Client({
})

client.connect().then(() => {
console.log('Connected!')
console.log(`Connected to ${process.env.DB_NAME}`)
})

module.exports = {
evolutionsFolderPath: ['evolutions'],
runQuery(query) {
return client.query(query)
return client.query(query).then(result => result.rows)
},
}
7 changes: 7 additions & 0 deletions back/evolutions/2.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
ALTER TABLE public."user"
ADD "telegramId" VARCHAR(255) DEFAULT NULL;

#DOWN

ALTER TABLE public."user"
DROP COLUMN "telegramId";
5 changes: 1 addition & 4 deletions back/src/telegram/telegram.module.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { MiddlewareConsumer, Module, NestModule } from '@nestjs/common'
import {
TelegramModule as OriginalTelegramModule,
TelegramBot,
} from 'nest-telegram'
import { TelegramModule as OriginalTelegramModule } from 'nest-telegram'

import { ConfigModule } from '@back/config/config.module'

Expand Down
16 changes: 16 additions & 0 deletions back/src/user/application/Registrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,20 @@ export class Registrator {

await this.entitySaver.save(user)
}

public async addTelegramAccount(
login: string,
telegramId: number,
): Promise<void> {
const attachedUser = await this.userRepo.findOneByTelegram(telegramId)
if (attachedUser.nonEmpty()) {
throw new LoginAlreadyTakenException(login)
}

const user = await this.userRepo.getOne(login)

user.attachTelegram(telegramId)

await this.entitySaver.save(user)
}
}
7 changes: 7 additions & 0 deletions back/src/user/domain/User.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ export class User {
@Column()
private password: string | undefined

@Column()
private telegramId: string | undefined

public constructor(login: string) {
this.login = login

Expand All @@ -33,4 +36,8 @@ export class User {
): Promise<boolean> {
return encoder.isPasswordValid(this.password, rawPassword)
}

public attachTelegram(telegramId: number): void {
this.telegramId = telegramId.toString()
}
}
11 changes: 11 additions & 0 deletions back/src/user/domain/UserRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@ class UserRepo {

return Option.of(user)
}

public async findOneByTelegram(telegramId: number): Promise<Option<User>> {
const user = await this.userRepo
.createQueryBuilder()
.where({
telegramId,
})
.getOne()

return Option.of(user)
}
}

export const UserRepository = UserRepo
Expand Down
32 changes: 29 additions & 3 deletions back/src/user/presentation/telegram/actions/AuthActions.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,36 @@
import { Injectable } from '@nestjs/common'
import { TelegramActionHandler, Context } from 'nest-telegram'
import { TelegramActionHandler, Context, PipeContext } from 'nest-telegram'

import { Authenticator } from '@back/user/application/Authenticator'
import { Registrator } from '@back/user/application/Registrator'

import { IsKnownUser } from '../transformer/IsKnownUser'

@Injectable()
export class AuthActions {
constructor(
private readonly authenticator: Authenticator,
private readonly registrator: Registrator,
) {}

@TelegramActionHandler({ onStart: true })
public async hello(ctx: Context) {
await ctx.reply('lol')
public async hello(
ctx: Context,
@PipeContext(IsKnownUser) loggedIn: boolean,
) {
await ctx.reply('Hello!')

await ctx.reply(
loggedIn ? 'Welcome back!' : 'To auth send me "/auth login password"',
)
}

@TelegramActionHandler({ command: 'auth' })
public async auth(ctx: Context) {
const [_, login, password] = ctx.message.text.split(' ')

await this.authenticator.signIn(login, password)
await this.registrator.addTelegramAccount(login, ctx.from.id)
await ctx.reply('Success')
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { TelegramErrorHandler, TelegramCatch, Context } from 'nest-telegram'

import { InvalidCredentialsException } from '@back/user/application/exception/InvalidCredentialsException'

@TelegramCatch(InvalidCredentialsException)
export class InvalidCredentialsCatcher
implements TelegramErrorHandler<InvalidCredentialsException> {
public async catch(ctx: Context) {
await ctx.reply('Invalid credentials')
}
}
14 changes: 14 additions & 0 deletions back/src/user/presentation/telegram/transformer/IsKnownUser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Injectable } from '@nestjs/common'
import { UserRepository } from '@back/user/domain/UserRepository'
import { ContextTransformer, Context } from 'nest-telegram'

@Injectable()
export class IsKnownUser implements ContextTransformer<boolean> {
public constructor(private readonly userRepo: UserRepository) {}

public async transform(ctx: Context) {
const user = await this.userRepo.findOneByTelegram(ctx.from.id)

return user.nonEmpty()
}
}
6 changes: 5 additions & 1 deletion back/src/user/user.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import { ProfileController } from './presentation/http/controller/ProfileControl
import { InvalidCredentialsFilter } from './presentation/http/filter/InvalidCredentialsFilter'
import { LoginAlreadyTakenFilter } from './presentation/http/filter/LoginAlreadyTakenFilter'
import { JwtGuard } from './presentation/http/security/JwtGuard'
import { AuthActions } from './presentation/telegram/actions/AuthActions'
import { InvalidCredentialsCatcher } from './presentation/telegram/catcher/InvalidCredentialsCatcher'
import { IsKnownUser } from './presentation/telegram/transformer/IsKnownUser'

import { User } from './domain/User.entity'
import { UserRepository } from './domain/UserRepository'
Expand All @@ -21,7 +24,6 @@ import { Registrator } from './application/Registrator'
import { JwtOptionsFactory } from './infrastructure/JwtOptionsFactory'
import { BcryptPasswordEncoder } from './infrastructure/PasswordEncoder/BcryptPasswordEncoder'
import { PasswordEncoder } from './infrastructure/PasswordEncoder/PasswordEncoder'
import { AuthActions } from './presentation/telegram/actions/AuthActions'

@Module({
imports: [
Expand All @@ -46,6 +48,8 @@ import { AuthActions } from './presentation/telegram/actions/AuthActions'
UserRepository,
JwtGuard,
AuthActions,
InvalidCredentialsCatcher,
IsKnownUser,
],
exports: [UserRepository, JwtGuard, Authenticator],
})
Expand Down

0 comments on commit 0258751

Please sign in to comment.