Skip to content

Commit

Permalink
feat(profile service): close all todos
Browse files Browse the repository at this point in the history
  • Loading branch information
havrydotdev committed Nov 10, 2023
1 parent 960a3ca commit e4ad594
Show file tree
Hide file tree
Showing 11 changed files with 95 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import {
ProfilesWizard,
RegisterWizard,
} from './controllers/wizards';
import { ClearLastProfilesWizard } from './controllers/wizards/clear-last.wizard';
import { ClearLastProfilesWizard } from './controllers/wizards';
import { SendMessageWizard } from './controllers/wizards';
import { GlobalFilter } from './core/filters';
import { RoleGuard } from './core/guards';
import { CacheInterceptor, I18nInterceptor } from './core/interceptors';
Expand Down Expand Up @@ -78,6 +79,7 @@ import { UserUseCasesModule } from './use-cases/user';
NextActionWizard,
ProfilesWizard,
ClearLastProfilesWizard,
SendMessageWizard,
{
provide: APP_INTERCEPTOR,
useClass: I18nInterceptor,
Expand Down
12 changes: 11 additions & 1 deletion src/controllers/updates/app.update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
PROFILE_CALLBACK,
PROFILES_WIZARD_ID,
REGISTER_WIZARD_ID,
SEND_MESSAGE_WIZARD_ID,
UPDATE_PROFILE_CALLBACK,
} from 'src/core/constants';
import { Roles } from 'src/core/decorators';
Expand All @@ -32,7 +33,10 @@ import { ProfileUseCases } from 'src/use-cases/profile';
import { ReplyUseCases } from 'src/use-cases/reply';
import { ReportUseCases } from 'src/use-cases/reports';
import { deunionize, Markup } from 'telegraf';
import { InlineQueryResult } from 'telegraf/typings/core/types/typegram';
import {
InlineQueryResult,
Message,
} from 'telegraf/typings/core/types/typegram';

@Update()
export class AppUpdate {
Expand Down Expand Up @@ -137,6 +141,12 @@ export class AppUpdate {
@Action(/reporter-info-*/)
async onReporterInfo(@Ctx() ctx: MessageContext): Promise<HandlerResponse> {}

@Roles(['admin'])
@Command('send_message')
async onSendMessage(@Ctx() ctx: MessageContext): Promise<HandlerResponse> {
await ctx.scene.enter(SEND_MESSAGE_WIZARD_ID);
}

@Action(/sen-*/)
async onSentence(@Ctx() ctx: MessageContext): Promise<HandlerResponse> {
const userId = parseInt(
Expand Down
2 changes: 2 additions & 0 deletions src/controllers/wizards/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ export * from './change-lang.wizard';
export * from './next.wizard';
export * from './profiles.wizard';
export * from './register.wizard';
export * from './clear-last.wizard';
export * from './send-message.wizard';
48 changes: 48 additions & 0 deletions src/controllers/wizards/send-message.wizard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/* eslint-disable perfectionist/sort-classes */
import { CACHE_MANAGER } from '@nestjs/cache-manager';
import { Inject } from '@nestjs/common';
import { Cache } from 'cache-manager';
import { Ctx, Message, On, Wizard, WizardStep } from 'nestjs-telegraf';
import { SEND_MESSAGE_WIZARD_ID } from 'src/core/constants';
import { Profile } from 'src/core/entities';
import { getProfileCacheKey } from 'src/core/utils';
import { HandlerResponse, WizardContext } from 'src/types';
import { ProfileUseCases } from 'src/use-cases/profile';

// TODO: add multi-language messages
@Wizard(SEND_MESSAGE_WIZARD_ID)
export class SendMessageWizard {
constructor(
@Inject(CACHE_MANAGER) private readonly cache: Cache,
private readonly profileUseCases: ProfileUseCases,
) {}

@WizardStep(1)
async onEnter(@Ctx() ctx: WizardContext): Promise<HandlerResponse> {
const profile = await this.cache.get<Profile>(
getProfileCacheKey(ctx.from.id),
);
if (!profile || profile.user.role !== 'admin') {
return;
}

ctx.wizard.next();

return 'messages.send_message.enter';
}

@On('text')
@WizardStep(2)
async onLang(
@Ctx() ctx: WizardContext,
@Message() msg: { text: string },
): Promise<HandlerResponse> {
const profiles = await this.profileUseCases.findAll();

for (const profile of profiles) {
await ctx.telegram.sendMessage(profile.user.id, msg.text);
}

await ctx.scene.leave();
}
}
2 changes: 2 additions & 0 deletions src/core/abstracts/profile.abstract.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ abstract class IProfileService {

abstract deleteByUser(userId: number): Promise<void>;

abstract findAll(): Promise<Profile[]>;

abstract findById(profileId: number): Promise<Profile | undefined>;

abstract findByUser(userId: number): Promise<Profile>;
Expand Down
3 changes: 3 additions & 0 deletions src/core/constants/wizards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ const PROFILES_WIZARD_ID = 'profiles_scene';

const CLEAR_LAST_WIZARD_ID = 'clear_last_profiles_wizard';

const SEND_MESSAGE_WIZARD_ID = 'send_message_wizard';

export {
CHANGE_LANG_WIZARD_ID,
CLEAR_LAST_WIZARD_ID,
NEXT_WIZARD_ID,
PROFILES_WIZARD_ID,
REGISTER_WIZARD_ID,
SEND_MESSAGE_WIZARD_ID,
};
9 changes: 9 additions & 0 deletions src/frameworks/profile/typeorm/typeorm-profile.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export class TypeOrmProfileService implements IProfileService {
return this.profileRepo.findOne({
relations: {
games: true,
user: true,
},
where: {
id: res.id,
Expand Down Expand Up @@ -47,6 +48,14 @@ export class TypeOrmProfileService implements IProfileService {
return result;
}

async findAll(): Promise<Profile[]> {
return this.profileRepo.find({
relations: {
user: true,
},
});
}

async findById(profileId: number): Promise<Profile | undefined> {
return this.profileRepo.findOne({
relations: {
Expand Down
4 changes: 4 additions & 0 deletions src/generated/i18n.generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ export type I18nTranslations = {
"deleted": string;
"update": string;
};
"send_message": {
"enter": string;
"sent": string;
};
};
};
export type I18nPath = Path<I18nTranslations>;
4 changes: 4 additions & 0 deletions src/i18n/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,9 @@
},
"deleted": "Hi! I'm sorry, but your profile has been deleted by admin. You can create a new one. Have a nice day!",
"update": "Update profile"
},
"send_message": {
"enter": "Enter message text",
"sent": "Message sent to all users"
}
}
6 changes: 5 additions & 1 deletion src/i18n/ua/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"invalid": "Ця гра не існує або не підтримується",
"ok": "Гра додана",
"already_added": "Ця гра вже додана",
"send": "Майже готово! Тепер, обери ігри, в які ти граєш. (Використовуй @{username} для пошуку доступних ігор)"
"send": "Майже готово! Тепер, обери ігри, в які ти граєш. (Напиши у полі вводу @{username} для пошуку доступних ігор)"
},
"register": {
"completed": "Реєстрація завершена! Тепер, ти можеш шукати тімейтів"
Expand All @@ -47,5 +47,9 @@
},
"deleted": "Привіт! Твій профіль було видалено модерацією. Щоб далі користуватись ботом, заповни йогo знову. Гарного дня!",
"update": "Змінити профіль"
},
"send_message": {
"enter": "Enter message text",
"sent": "Message sent to all users"
}
}
4 changes: 4 additions & 0 deletions src/use-cases/profile/profile.use-case.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ export class ProfileUseCases {
return this.profileService.deleteByUser(userId);
}

async findAll(): Promise<Profile[]> {
return this.profileService.findAll();
}

async findRecommended(
profile: Profile,
seenProfiles: number[],
Expand Down

0 comments on commit e4ad594

Please sign in to comment.