Skip to content

Commit

Permalink
Merge pull request #68 from gavrylenkoIvan/dev
Browse files Browse the repository at this point in the history
fix(i18n): change messages keys
  • Loading branch information
havrydotdev committed Dec 13, 2023
2 parents 5fe1c94 + d0cd913 commit b3e4b2c
Show file tree
Hide file tree
Showing 15 changed files with 132 additions and 112 deletions.
3 changes: 2 additions & 1 deletion src/controllers/updates/app.update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ export class AppUpdate {

await this.replyUseCases.sendMsgToChatI18n(
userId,
'messages.profile.deleted',
'messages.profile.delete.message',
);
}

Expand All @@ -168,6 +168,7 @@ export class AppUpdate {
@Registered()
@Action(UPDATE_PROFILE_CALLBACK)
async onUpdateProfile(@Ctx() ctx: MessageContext): Promise<HandlerResponse> {
console.log({ ctx });
await ctx.scene.enter(REGISTER_WIZARD_ID);
}
}
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 @@ -167,7 +167,7 @@ describe('AppUpdate', () => {
expect(deleteByUserSpy).toHaveBeenCalledWith(userId);
expect(replyUseCases.sendMsgToChatI18n).toHaveBeenCalledWith(
userId,
'messages.profile.deleted',
'messages.profile.delete.message',
);
});
});
Expand Down
4 changes: 2 additions & 2 deletions src/controllers/wizards/profiles.wizard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions src/controllers/wizards/register.wizard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export class RegisterWizard {
@Ctx() ctx: RegisterWizardContext,
@ReqUser() profile: Profile,
): Promise<HandlerResponse> {
console.log({ ctx });
ctx.wizard.next();

ctx.wizard.state.games = [];
Expand Down Expand Up @@ -148,6 +149,7 @@ export class RegisterWizard {
@Message() msg: PhotoMessage,
@ReqUser() user: User,
): Promise<HandlerResponse> {
console.log({ user });
const fileId = msg.photo.pop().file_id;

const profileDto: CreateProfileDto = {
Expand All @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/controllers/wizards/send-message.wizard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand All @@ -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,
},
Expand Down
4 changes: 3 additions & 1 deletion src/controllers/wizards/tests/register.wizard.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,9 @@ describe('RegisterWizard', () => {
}),
],
}),
undefined,
createMock<User>({
profile: undefined,
}),
);

expect(createSpy).toHaveBeenCalledWith(profileDto);
Expand Down
4 changes: 2 additions & 2 deletions src/controllers/wizards/tests/send-message.wizard.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand All @@ -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,
},
Expand Down
13 changes: 6 additions & 7 deletions src/core/guards/profile.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,18 @@ export class ProfileGuard implements CanActivate {

let cachedUser = await this.cache.get<User>(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;
Expand Down
2 changes: 1 addition & 1 deletion src/core/guards/telegraf-throttler.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class TelegrafThrottlerGuard extends ThrottlerGuard {
const tgCtx = tgExecutionContext.getContext<MessageContext>();
const key = this.generateKey(
context,
tgCtx.message.from.id.toString(),
tgCtx.from.id.toString(),
throttler.name,
);
const { totalHits } = await this.storageService.increment(key, ttl);
Expand Down
8 changes: 5 additions & 3 deletions src/frameworks/reply/telegraf/telegraf-reply.service.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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<I18nTranslations>,
@InjectBot() private bot: Telegraf<MessageContext>,
@Inject(CACHE_MANAGER) private readonly cache: Cache,
@InjectCache() private readonly cache: Cache,
) {
super(i18n);
}
Expand All @@ -32,6 +33,7 @@ class TelegrafReplyService extends IReplyService {
extra?: Extra,
): Promise<void> {
const user = await this.cache.get<User>(getProfileCacheKey(ctx.chat.id));
console.log({ user });

await this.sendMsgToChat(
ctx.chat.id,
Expand Down
166 changes: 86 additions & 80 deletions src/generated/i18n.generated.ts
Original file line number Diff line number Diff line change
@@ -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<I18nTranslations>;
3 changes: 2 additions & 1 deletion src/i18n/en/errors.json
Original file line number Diff line number Diff line change
Expand Up @@ -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!"
}
12 changes: 8 additions & 4 deletions src/i18n/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down
3 changes: 2 additions & 1 deletion src/i18n/ua/errors.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@
"about": {
"invalid": "Будь ласка, введіть коректний опис"
},
"forbidden": "Нажаль, ви не можете використовувати цю команду"
"forbidden": "Нажаль, ви не можете використовувати цю команду",
"throttler": "Гей, повільніше!"
}
Loading

1 comment on commit b3e4b2c

@vercel
Copy link

@vercel vercel bot commented on b3e4b2c Dec 13, 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.