Skip to content

Commit

Permalink
Merge pull request #40 from gavrylenkoIvan/dev
Browse files Browse the repository at this point in the history
Increase test coverage, fix global filter
  • Loading branch information
havrydotdev committed Dec 2, 2023
2 parents 981ffd5 + d76d9b5 commit 4ddb8b7
Show file tree
Hide file tree
Showing 26 changed files with 448 additions and 55 deletions.
48 changes: 46 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ua-teammates",
"version": "0.0.1",
"version": "0.0.3",
"description": "Bot for searching teammates for games from Ukraine",
"author": "Havrylenko Ivan <havrylenko.inbox@gmail.com>",
"private": true,
Expand Down Expand Up @@ -63,6 +63,7 @@
"cache-manager": "^5.3.1",
"cache-manager-redis-yet": "^4.1.2",
"dotenv": "^16.3.1",
"joi": "^17.11.0",
"kysely": "^0.23.5",
"nestjs-i18n": "^10.3.7",
"nestjs-telegraf": "^2.7.0",
Expand Down
23 changes: 5 additions & 18 deletions src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import { CacheModule, CacheStore } from '@nestjs/cache-manager';
import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { APP_FILTER, APP_GUARD, APP_INTERCEPTOR } from '@nestjs/core';
import { redisStore } from 'cache-manager-redis-yet';
import { RedisClientOptions } from 'redis';

import { AppUpdate } from './controllers/updates';
import {
Expand All @@ -28,6 +24,8 @@ import {
TelegramModule,
UserModule,
} from './services';
import { ApiCacheModule } from './services/cache/cache.module';
import { ApiConfigModule } from './services/config/config.module';
import { GameModule } from './services/game/game.module';
import { I18nModule } from './services/i18n/i18n.module';
import { ProfileModule } from './services/profile/profile.module';
Expand All @@ -36,24 +34,13 @@ import { SubscribersModule } from './subscribers/subscribers.module';
import { GameUseCasesModule } from './use-cases/game';
import { ProfileUseCasesModule } from './use-cases/profile';
import { ReplyUseCasesModule } from './use-cases/reply';
import { ReportUseCasesModule } from './use-cases/reports/report.use-case.module';
import { ReportUseCasesModule } from './use-cases/report/report.use-case.module';
import { UserUseCasesModule } from './use-cases/user';

@Module({
imports: [
CacheModule.registerAsync<RedisClientOptions>({
imports: [ConfigModule],
inject: [ConfigService],
isGlobal: true,
useFactory: async (configService: ConfigService) => ({
store: redisStore as unknown as CacheStore,
ttl: 24 * 60 * 60, // 1 day
url: configService.get<string>('REDIS_URL'),
}),
}),
ConfigModule.forRoot({
isGlobal: true,
}),
ApiCacheModule,
ApiConfigModule,
TelegramModule,
DatabaseModule,
SubscribersModule,
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/updates/app.update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { HandlerResponse, MessageContext } from 'src/types';
import { GameUseCases } from 'src/use-cases/game';
import { ProfileUseCases } from 'src/use-cases/profile';
import { ReplyUseCases } from 'src/use-cases/reply';
import { ReportUseCases } from 'src/use-cases/reports';
import { ReportUseCases } from 'src/use-cases/report';
import { deunionize } from 'telegraf';
import { InlineQueryResult } from 'telegraf/typings/core/types/typegram';

Expand Down
2 changes: 1 addition & 1 deletion src/controllers/updates/tests/app.update.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { MessageContext } from 'src/types/telegraf';
import { GameUseCases } from 'src/use-cases/game';
import { ProfileUseCases } from 'src/use-cases/profile';
import { ReplyUseCases } from 'src/use-cases/reply';
import { ReportUseCases } from 'src/use-cases/reports';
import { ReportUseCases } from 'src/use-cases/report';
import { UserUseCases } from 'src/use-cases/user/user.use-case';
import { InlineQueryResult } from 'telegraf/typings/core/types/typegram';

Expand Down
2 changes: 1 addition & 1 deletion src/controllers/wizards/profiles.wizard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { getCaption, getProfileMarkup } from 'src/core/utils';
import { HandlerResponse, ProfilesWizardContext } from 'src/types';
import { ProfileUseCases } from 'src/use-cases/profile';
import { ReplyUseCases } from 'src/use-cases/reply';
import { ReportUseCases } from 'src/use-cases/reports';
import { ReportUseCases } from 'src/use-cases/report';
import { deunionize } from 'telegraf';

@Wizard(PROFILES_WIZARD_ID)
Expand Down
10 changes: 6 additions & 4 deletions src/controllers/wizards/register.wizard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
} from 'src/core/constants';
import { ReqUser } from 'src/core/decorators';
import { CreateProfileDto } from 'src/core/dtos';
import { Profile } from 'src/core/entities';
import { Profile, User } from 'src/core/entities';
import { BotException } from 'src/core/errors';
import { AboutPipe, AgePipe, GamePipe } from 'src/core/pipes';
import { getNameMarkup } from 'src/core/utils';
Expand Down Expand Up @@ -142,7 +142,7 @@ export class RegisterWizard {
@Ctx()
ctx: RegisterWizardContext,
@Message() msg: PhotoMessage,
@ReqUser() profile: Profile,
@ReqUser() user: User,
): Promise<HandlerResponse> {
const fileId = msg.photo.pop().file_id;

Expand All @@ -155,8 +155,10 @@ export class RegisterWizard {
userId: ctx.from.id,
};

if (profile) {
await this.profileUseCases.update(profile.id, profileDto);
if (user) {
await this.profileUseCases.update(user.profile.id, profileDto);

await this.replyUseCases.replyI18n(ctx, 'messages.register.completed');

await ctx.scene.enter(NEXT_WIZARD_ID);

Expand Down
3 changes: 1 addition & 2 deletions src/controllers/wizards/send-message.wizard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,14 @@ import { ProfileUseCases } from 'src/use-cases/profile';
export class SendMessageWizard {
constructor(private readonly profileUseCases: ProfileUseCases) {}

// TODO: add error description
@WizardStep(1)
@Registered()
async onEnter(
@Ctx() ctx: WizardContext,
@ReqUser() profile: Profile,
): Promise<HandlerResponse> {
if (profile.user.role !== 'admin') {
throw new BotException('errors.unknown');
throw new BotException('errors.forbidden');
}

ctx.wizard.next();
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/wizards/tests/profiles.wizard.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { getCaption, getProfileMarkup } from 'src/core/utils';
import { ProfilesWizardContext } from 'src/types';
import { ProfileUseCases } from 'src/use-cases/profile';
import { ReplyUseCases } from 'src/use-cases/reply';
import { ReportUseCases } from 'src/use-cases/reports';
import { ReportUseCases } from 'src/use-cases/report';
import { deunionize } from 'telegraf';
import { ChatFromGetChat } from 'telegraf/typings/core/types/typegram';

Expand Down
8 changes: 5 additions & 3 deletions src/controllers/wizards/tests/register.wizard.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Test, TestingModule } from '@nestjs/testing';
import { RegisterWizard } from 'src/controllers/wizards/register.wizard';
import { Keyboards, NEXT_WIZARD_ID } from 'src/core/constants';
import { CreateProfileDto } from 'src/core/dtos';
import { Profile } from 'src/core/entities';
import { Profile, User } from 'src/core/entities';
import { BotException } from 'src/core/errors';
import { getNameMarkup } from 'src/core/utils';
import { PhotoMessage, RegisterWizardContext } from 'src/types/telegraf';
Expand Down Expand Up @@ -315,8 +315,10 @@ describe('RegisterWizard', () => {
}),
],
}),
createMock<Profile>({
id: 1,
createMock<User>({
profile: createMock<Profile>({
id: 1,
}),
}),
);

Expand Down
2 changes: 1 addition & 1 deletion src/controllers/wizards/tests/send-message.wizard.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ describe('SendMessageWizard', () => {
});

expect(wizard.onEnter(ctx, profile)).rejects.toThrowError(
new BotException('errors.unknown'),
new BotException('errors.forbidden'),
);
});
});
Expand Down
39 changes: 39 additions & 0 deletions src/core/decorators/tests/profile.decorator.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { createMock } from '@golevelup/ts-jest';
import { ExecutionContext } from '@nestjs/common';
import { ROUTE_ARGS_METADATA } from '@nestjs/common/constants';
import { TelegrafExecutionContext } from 'nestjs-telegraf';
import { User } from 'src/core/entities';

import { ReqUser } from '../profile.decorator';

// eslint-disable-next-line @typescript-eslint/ban-types
function getParamDecoratorFactory(decorator: Function) {
class Test {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
public test(@decorator() value) {}
}

const args = Reflect.getMetadata(ROUTE_ARGS_METADATA, Test, 'test');
return args[Object.keys(args)[0]].factory;
}

describe('ReqUser', () => {
it('should return the user from the request', () => {
const user = createMock<User>({
id: 1,
});

jest.spyOn(TelegrafExecutionContext, 'create').mockReturnValue(
createMock<TelegrafExecutionContext>({
switchToHttp: () => ({
getRequest: () => ({ user }),
}),
}),
);

const factory = getParamDecoratorFactory(ReqUser);
const result = factory(null, createMock<ExecutionContext>());

expect(result).toEqual(user);
});
});
8 changes: 8 additions & 0 deletions src/core/filters/global.filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { TelegrafArgumentsHost } from 'nestjs-telegraf';
import { MessageContext, MsgKey } from 'src/types';
import { ReplyUseCases } from 'src/use-cases/reply';

import { NEXT_WIZARD_ID } from '../constants';

@Catch()
export class UnexpectedExceptionFilter implements ExceptionFilter {
constructor(private readonly replyUseCases: ReplyUseCases) {}
Expand All @@ -11,6 +13,12 @@ export class UnexpectedExceptionFilter implements ExceptionFilter {
const telegrafHost = TelegrafArgumentsHost.create(host);
const ctx = telegrafHost.getContext<MessageContext>();

if (ctx.scene.current) {
ctx.scene.leave();

ctx.scene.enter(NEXT_WIZARD_ID);
}

const message: MsgKey = 'errors.unknown';

await this.replyUseCases.replyI18n(ctx, message);
Expand Down
Loading

1 comment on commit 4ddb8b7

@vercel
Copy link

@vercel vercel bot commented on 4ddb8b7 Dec 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.