diff --git a/src/controllers/updates/app.update.ts b/src/controllers/updates/app.update.ts index c739d0d..58eabf6 100644 --- a/src/controllers/updates/app.update.ts +++ b/src/controllers/updates/app.update.ts @@ -142,7 +142,7 @@ export class AppUpdate { await this.replyUseCases.sendMsgToChatI18n( userId, - 'messages.profile.deleted', + 'messages.profile.delete.message', ); } @@ -168,6 +168,7 @@ export class AppUpdate { @Registered() @Action(UPDATE_PROFILE_CALLBACK) async onUpdateProfile(@Ctx() ctx: MessageContext): Promise { + console.log({ ctx }); await ctx.scene.enter(REGISTER_WIZARD_ID); } } diff --git a/src/controllers/updates/tests/app.update.spec.ts b/src/controllers/updates/tests/app.update.spec.ts index eff31f6..50e4a27 100644 --- a/src/controllers/updates/tests/app.update.spec.ts +++ b/src/controllers/updates/tests/app.update.spec.ts @@ -167,7 +167,7 @@ describe('AppUpdate', () => { expect(deleteByUserSpy).toHaveBeenCalledWith(userId); expect(replyUseCases.sendMsgToChatI18n).toHaveBeenCalledWith( userId, - 'messages.profile.deleted', + 'messages.profile.delete.message', ); }); }); diff --git a/src/controllers/wizards/profiles.wizard.ts b/src/controllers/wizards/profiles.wizard.ts index 80b8d51..9509631 100644 --- a/src/controllers/wizards/profiles.wizard.ts +++ b/src/controllers/wizards/profiles.wizard.ts @@ -106,12 +106,12 @@ export class ProfilesWizard { await this.replyUseCases.sendMsgToChatI18n( profile.user.id, - 'messages.profile.deleted', + 'messages.profile.delete.message', ); await this.replyUseCases.replyI18n( ctx, - 'messages.profile.delete_success', + 'messages.profile.delete.success', ); await ctx.scene.reenter(); diff --git a/src/controllers/wizards/register.wizard.ts b/src/controllers/wizards/register.wizard.ts index 0c64239..d3da670 100644 --- a/src/controllers/wizards/register.wizard.ts +++ b/src/controllers/wizards/register.wizard.ts @@ -36,6 +36,7 @@ export class RegisterWizard { @Ctx() ctx: RegisterWizardContext, @ReqUser() profile: Profile, ): Promise { + console.log({ ctx }); ctx.wizard.next(); ctx.wizard.state.games = []; @@ -148,6 +149,7 @@ export class RegisterWizard { @Message() msg: PhotoMessage, @ReqUser() user: User, ): Promise { + console.log({ user }); const fileId = msg.photo.pop().file_id; const profileDto: CreateProfileDto = { @@ -162,8 +164,6 @@ export class RegisterWizard { if (user.profile) { await this.profileUseCases.update(user.profile.id, profileDto); - await this.replyUseCases.replyI18n(ctx, 'messages.register.completed'); - await ctx.scene.enter(NEXT_WIZARD_ID); return; diff --git a/src/controllers/wizards/send-message.wizard.ts b/src/controllers/wizards/send-message.wizard.ts index d734dcb..0f8d311 100644 --- a/src/controllers/wizards/send-message.wizard.ts +++ b/src/controllers/wizards/send-message.wizard.ts @@ -29,7 +29,7 @@ export class SendMessageWizard { ctx.wizard.next(); return [ - 'messages.send_message.enter_ua', + 'messages.send_message.enter.ua', { reply_markup: Keyboards.remove, }, @@ -47,7 +47,7 @@ export class SendMessageWizard { ctx.wizard.next(); return [ - 'messages.send_message.enter_en', + 'messages.send_message.enter.en', { reply_markup: Keyboards.skipStep, }, diff --git a/src/controllers/wizards/tests/register.wizard.spec.ts b/src/controllers/wizards/tests/register.wizard.spec.ts index 03770f9..957dc29 100644 --- a/src/controllers/wizards/tests/register.wizard.spec.ts +++ b/src/controllers/wizards/tests/register.wizard.spec.ts @@ -237,7 +237,9 @@ describe('RegisterWizard', () => { }), ], }), - undefined, + createMock({ + profile: undefined, + }), ); expect(createSpy).toHaveBeenCalledWith(profileDto); diff --git a/src/controllers/wizards/tests/send-message.wizard.spec.ts b/src/controllers/wizards/tests/send-message.wizard.spec.ts index 34bfe64..e98cdef 100644 --- a/src/controllers/wizards/tests/send-message.wizard.spec.ts +++ b/src/controllers/wizards/tests/send-message.wizard.spec.ts @@ -34,7 +34,7 @@ describe('SendMessageWizard', () => { expect(ctx.wizard.next).toHaveBeenCalled(); expect(result).toEqual([ - 'messages.send_message.enter_ua', + 'messages.send_message.enter.ua', { reply_markup: Keyboards.remove, }, @@ -58,7 +58,7 @@ describe('SendMessageWizard', () => { expect(ctx.wizard.next).toHaveBeenCalled(); expect(result).toEqual([ - 'messages.send_message.enter_en', + 'messages.send_message.enter.en', { reply_markup: Keyboards.skipStep, }, diff --git a/src/core/guards/profile.guard.ts b/src/core/guards/profile.guard.ts index 1c7c02b..5184f14 100644 --- a/src/core/guards/profile.guard.ts +++ b/src/core/guards/profile.guard.ts @@ -31,19 +31,18 @@ export class ProfileGuard implements CanActivate { let cachedUser = await this.cache.get(profileKey); if (!cachedUser) { - const user = await this.userUseCases.findById(tgCtx.from.id); + let user = await this.userUseCases.findById(tgCtx.from.id); if (!user) { - await this.userUseCases.create({ + const createdUser = await this.userUseCases.create({ id: tgCtx.from.id, }); - } - const userToCache = - user ?? ({ id: tgCtx.from.id, profile: null } as User); + user ??= createdUser; + } - await this.cache.set(profileKey, userToCache); + await this.cache.set(profileKey, user); - cachedUser = userToCache; + cachedUser = user; } req.user = cachedUser; diff --git a/src/core/guards/telegraf-throttler.guard.ts b/src/core/guards/telegraf-throttler.guard.ts index fe7cb81..d869ef9 100644 --- a/src/core/guards/telegraf-throttler.guard.ts +++ b/src/core/guards/telegraf-throttler.guard.ts @@ -17,7 +17,7 @@ export class TelegrafThrottlerGuard extends ThrottlerGuard { const tgCtx = tgExecutionContext.getContext(); const key = this.generateKey( context, - tgCtx.message.from.id.toString(), + tgCtx.from.id.toString(), throttler.name, ); const { totalHits } = await this.storageService.increment(key, ttl); diff --git a/src/frameworks/reply/telegraf/telegraf-reply.service.ts b/src/frameworks/reply/telegraf/telegraf-reply.service.ts index 1a6ed1d..9bd990b 100644 --- a/src/frameworks/reply/telegraf/telegraf-reply.service.ts +++ b/src/frameworks/reply/telegraf/telegraf-reply.service.ts @@ -1,9 +1,9 @@ -import { CACHE_MANAGER } from '@nestjs/cache-manager'; -import { Inject, Injectable } from '@nestjs/common'; +import { Injectable } from '@nestjs/common'; import { Cache } from 'cache-manager'; import { I18nService } from 'nestjs-i18n'; import { InjectBot } from 'nestjs-telegraf'; import { IReplyService } from 'src/core/abstracts'; +import { InjectCache } from 'src/core/decorators'; import { User } from 'src/core/entities'; import { getProfileCacheKey } from 'src/core/utils'; import { I18nTranslations } from 'src/generated/i18n.generated'; @@ -16,12 +16,13 @@ import { } from 'src/types/telegraf'; import { Telegraf } from 'telegraf'; +// TODO: don't use telegraf context but user entity @Injectable() class TelegrafReplyService extends IReplyService { constructor( protected readonly i18n: I18nService, @InjectBot() private bot: Telegraf, - @Inject(CACHE_MANAGER) private readonly cache: Cache, + @InjectCache() private readonly cache: Cache, ) { super(i18n); } @@ -32,6 +33,7 @@ class TelegrafReplyService extends IReplyService { extra?: Extra, ): Promise { const user = await this.cache.get(getProfileCacheKey(ctx.chat.id)); + console.log({ user }); await this.sendMsgToChat( ctx.chat.id, diff --git a/src/generated/i18n.generated.ts b/src/generated/i18n.generated.ts index 776d2b0..a495d6d 100644 --- a/src/generated/i18n.generated.ts +++ b/src/generated/i18n.generated.ts @@ -1,89 +1,95 @@ /* DO NOT EDIT, file generated by nestjs-i18n */ -import { Path } from 'nestjs-i18n'; +import { Path } from "nestjs-i18n"; export type I18nTranslations = { - buttons: { - profile: string; - change_lang: string; - help: string; - partner: string; - }; - commands: { - start: string; - help: string; - coop: string; - profiles: string; - }; - errors: { - unknown: string; - only_private: string; - limit_exceeded: string; - age: { - invalid: string; + "buttons": { + "profile": string; + "change_lang": string; + "help": string; + "partner": string; }; - about: { - invalid: string; + "commands": { + "start": string; + "help": string; + "coop": string; + "profiles": string; }; - forbidden: string; - }; - messages: { - lang: { - update: string; - select: string; - changed: string; - invalid: string; + "errors": { + "unknown": string; + "only_private": string; + "limit_exceeded": string; + "age": { + "invalid": string; + }; + "about": { + "invalid": string; + }; + "forbidden": string; + "throttler": string; }; - user: { - new: string; + "messages": { + "lang": { + "update": string; + "select": string; + "changed": string; + "invalid": string; + }; + "user": { + "new": string; + }; + "name": { + "send": string; + }; + "age": { + "send": string; + "invalid": string; + }; + "picture": { + "send": string; + }; + "about": { + "send": string; + }; + "game": { + "invalid": string; + "ok": string; + "already_added": string; + "send": string; + }; + "register": { + "completed": string; + }; + "next_action": string; + "searching_teammates": string; + "report": { + "ad": string; + "send": string; + "ok": string; + "channel": { + "ok": string; + }; + }; + "profile": { + "last": { + "clear": string; + "cleared": string; + "no_more": string; + }; + "delete": { + "message": string; + "success": string; + }; + "update": string; + "deleted": string; + }; + "send_message": { + "enter": { + "ua": string; + "en": string; + }; + "photo": string; + "sent": string; + }; }; - name: { - send: string; - }; - age: { - send: string; - invalid: string; - }; - picture: { - send: string; - }; - about: { - send: string; - }; - game: { - invalid: string; - ok: string; - already_added: string; - send: string; - }; - register: { - completed: string; - }; - next_action: string; - searching_teammates: string; - report: { - ad: string; - send: string; - ok: string; - channel: { - ok: string; - }; - }; - profile: { - last: { - clear: string; - cleared: string; - no_more: string; - }; - delete_success: string; - deleted: string; - update: string; - }; - send_message: { - enter_ua: string; - enter_en: string; - photo: string; - sent: string; - }; - }; }; export type I18nPath = Path; diff --git a/src/i18n/en/errors.json b/src/i18n/en/errors.json index fa50d5b..5f4a795 100644 --- a/src/i18n/en/errors.json +++ b/src/i18n/en/errors.json @@ -8,5 +8,6 @@ "about": { "invalid": "Please, enter valid about" }, - "forbidden": "Unfortunately, you can't use this command" + "forbidden": "Unfortunately, you can't use this command", + "throttler": "Hey, slow down!" } diff --git a/src/i18n/en/messages.json b/src/i18n/en/messages.json index 0f89e0b..effaade 100644 --- a/src/i18n/en/messages.json +++ b/src/i18n/en/messages.json @@ -45,13 +45,17 @@ "cleared": "Viewed profiles list wiped", "no_more": "It seems that you have already viewed all profiles." }, - "delete_success": "Profile was deleteed successfully", - "deleted": "Hi! I'm sorry, but your profile has been deleted by admin. You can create a new one. Have a nice day!", + "delete": { + "message": "Hi! I'm sorry, but your profile has been deleted by admin. You can create a new one. Have a nice day!", + "success": "Profile was deleteed successfully" + }, "update": "Update profile" }, "send_message": { - "enter_ua": "Enter message text in ukrainian", - "enter_en": "Enter message text in english", + "enter": { + "ua": "Enter message text in ukrainian", + "en": "Enter message text in english" + }, "photo": "Send message photo", "sent": "Message sent to all users" } diff --git a/src/i18n/ua/errors.json b/src/i18n/ua/errors.json index a0da9c4..1862478 100644 --- a/src/i18n/ua/errors.json +++ b/src/i18n/ua/errors.json @@ -8,5 +8,6 @@ "about": { "invalid": "Будь ласка, введіть коректний опис" }, - "forbidden": "Нажаль, ви не можете використовувати цю команду" + "forbidden": "Нажаль, ви не можете використовувати цю команду", + "throttler": "Гей, повільніше!" } diff --git a/src/i18n/ua/messages.json b/src/i18n/ua/messages.json index 730f9d7..f1cceb9 100644 --- a/src/i18n/ua/messages.json +++ b/src/i18n/ua/messages.json @@ -45,13 +45,17 @@ "no_more": "Схоже, ви бачили всі доступні профілі.", "cleared": "Список побачених профілів очищено" }, - "delete_success": "Профіль видалено", - "deleted": "Привіт! Твій профіль було видалено модерацією. Щоб далі користуватись ботом, заповни йогo знову. Гарного дня!", + "deleted": { + "success": "Профіль видалено", + "message": "Привіт! Твій профіль було видалено модерацією. Щоб далі користуватись ботом, заповни йогo знову. Гарного дня!" + }, "update": "Змінити профіль" }, "send_message": { - "enter_ua": "Введіть текст повідомлення українською", - "enter_en": "Введіть текст повідомлення англійською", + "enter": { + "ua": "Введіть текст повідомлення українською", + "en": "Введіть текст повідомлення англійською" + }, "photo": "Надішліть фото для повідомлення", "sent": "Повідомлення надіслано до всіх користувачів" }