Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question service #28

Merged
merged 11 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ COPY . .
RUN npm run build bot
RUN npm run build event-store
RUN npm run build graph-store
RUN npm run build question-service

FROM node:alpine AS production

Expand Down
25 changes: 25 additions & 0 deletions apps/bot/src/bot.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Controller, Logger, UseInterceptors } from '@nestjs/common';
import { MessagePattern, Payload } from '@nestjs/microservices';
import { BotService } from './bot.service';
import { TelegramAction } from '@app/common';
import { EventsInterceptor } from '@app/common/rmq/interceptors/events.interceptor';

interface MessagePayload {
chat_id: number | string;
text: string;
other: any;
}

@Controller()
@UseInterceptors(EventsInterceptor)
export class BotController {
private readonly logger = new Logger(BotController.name);
constructor(private readonly botService: BotService) { }

@MessagePattern(TelegramAction.SEND_MESSAGE)
async sendMessage(@Payload() payload: MessagePayload): Promise<void> {
const { chat_id, text, other } = payload;
await this.botService.sendMessage(chat_id, text, other);
return;
}
}
5 changes: 4 additions & 1 deletion apps/bot/src/bot.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
RmqModule,
} from '@app/common';
import { Services } from '@app/common';
import { BotController } from './bot.controller';

@Module({
imports: [
Expand All @@ -19,7 +20,9 @@ import { Services } from '@app/common';
}),
RmqModule.register(Services.EventStore),
RmqModule.register(Services.GraphStore),
RmqModule.register(Services.TGQuestionService),
],
controllers: [BotController],
providers: [BotService],
})
export class BotModule {}
export class BotModule { }
6 changes: 6 additions & 0 deletions apps/bot/src/bot.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ describe('BotService', () => {
emit: jest.fn(),
},
},
{
provide: Services.TGQuestionService.name,
useValue: {
emit: jest.fn(),
},
},
{
provide: ConfigService,
useValue: {
Expand Down
20 changes: 19 additions & 1 deletion apps/bot/src/bot.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import { IgnoreEvent, Services, UpdateEvent } from '@app/common';
import { Inject, Injectable, Logger, OnModuleInit } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { ClientProxy } from '@nestjs/microservices';
import { API_CONSTANTS, Bot } from 'grammy';
import { API_CONSTANTS, Bot, RawApi } from 'grammy';
import { Other } from 'grammy/out/core/api';
import { Message } from 'grammy/types';

@Injectable()
export class BotService implements OnModuleInit {
Expand All @@ -12,6 +14,8 @@ export class BotService implements OnModuleInit {
constructor(
@Inject(Services.EventStore.name) private readonly eventClient: ClientProxy,
@Inject(Services.GraphStore.name) private readonly graphClient: ClientProxy,
@Inject(Services.TGQuestionService.name)
private readonly tgQuestionClient: ClientProxy,
private readonly configService: ConfigService,
) {
this.bot = new Bot(configService.get<string>('telegram.token'));
Expand All @@ -27,6 +31,10 @@ export class BotService implements OnModuleInit {
this.logger.log(`Received ${event} from ${ctx.chat.id}`);
this.eventClient.emit(event, ctx);
this.graphClient.emit(event, ctx);
if (!ctx.update.message.from.is_bot) {
this.tgQuestionClient.emit(event, ctx);
}
return;
});
});
}
Expand All @@ -36,4 +44,14 @@ export class BotService implements OnModuleInit {
allowed_updates: API_CONSTANTS.ALL_UPDATE_TYPES,
});
}

async sendMessage(
chat_id: number | string,
text: string,
other?: Other<RawApi, 'sendMessage', 'text' | 'chat_id'>,
): Promise<Message.TextMessage | null> {
const receipt = await this.bot.api.sendMessage(chat_id, text, other);
this.logger.log(`Message ${receipt.message_id} sent to ${receipt.chat.id}`);
return receipt;
}
}
1 change: 0 additions & 1 deletion apps/bot/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { BotModule } from './bot.module';
import { RmqService, Services } from '@app/common';

async function bootstrap() {
// const app = await NestFactory.createMicroservice(BotModule);
const app = await NestFactory.create(BotModule);
const rmqService = app.get<RmqService>(RmqService);
app.connectMicroservice(rmqService.getOptions(Services.TelegramBot.queue));
Expand Down
4 changes: 2 additions & 2 deletions apps/event-store/src/event-store.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import { Controller, Logger, UseInterceptors } from '@nestjs/common';
import { EventStoreService } from './event-store.service';
import { MessagePattern, Payload } from '@nestjs/microservices';
import { UpdateEvent } from '@app/common';
import { EventsInterceptor } from './interceptors/events.interceptor';
import { EventsInterceptor } from '@app/common/rmq/interceptors/events.interceptor';
import { API_CONSTANTS } from 'grammy';

@Controller()
@UseInterceptors(EventsInterceptor)
export class EventStoreController {
private readonly logger = new Logger(EventStoreController.name);

constructor(private readonly eventsService: EventStoreService) {}
constructor(private readonly eventsService: EventStoreService) { }

a = typeof API_CONSTANTS.ALL_UPDATE_TYPES;

Expand Down
6 changes: 3 additions & 3 deletions apps/graph-store/src/chat_member/chat_member.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { UpdateEvent } from '@app/common';
import { Controller, Logger, UseInterceptors } from '@nestjs/common';
import { MessagePattern } from '@nestjs/microservices';
import { DChatMemberUpdated } from '../decorators/chatMemberUpdated.decorator';
import { RmqInterceptor } from '../interceptors/rmq.interceptor';
import { EventsInterceptor } from '@app/common/rmq/interceptors/events.interceptor';
import { Chat, ChatMemberUpdated } from 'grammy/types';
import { ChatMemberService } from './chat_member.service';
import { DChat } from '../decorators/chat.decorator';
Expand All @@ -25,11 +25,11 @@ type Action =
| 'DEMOTED';

@Controller('chat-member')
@UseInterceptors(RmqInterceptor)
@UseInterceptors(EventsInterceptor)
export class ChatMemberController {
private readonly logger = new Logger(ChatMemberController.name);

constructor(private readonly chatMemberService: ChatMemberService) {}
constructor(private readonly chatMemberService: ChatMemberService) { }

@MessagePattern(UpdateEvent.CHAT_MEMBER)
async chat_member(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import { MentionedService } from '../mentioned/mentioned.service';
import { MessageService } from '../message/message.service';
import { RepliedService } from '../replied/replied.service';
import { UserService } from '../user/user.service';
import { RmqInterceptor } from '../interceptors/rmq.interceptor';
import { EventsInterceptor } from '@app/common/rmq/interceptors/events.interceptor';

@Controller('edited-message')
@UseInterceptors(RmqInterceptor)
@UseInterceptors(EventsInterceptor)
export class EditedMessageController {
private readonly logger = new Logger(EditedMessageController.name);

Expand All @@ -28,7 +28,7 @@ export class EditedMessageController {
private readonly joinedService: JoinedService,
private readonly repliedService: RepliedService,
private readonly mentionedService: MentionedService,
) {}
) { }

@MessagePattern(UpdateEvent.EDITED_MESSAGE)
async chat_member(
Expand Down
2 changes: 1 addition & 1 deletion apps/graph-store/src/graph-store.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ import { MessageReactionModule } from './message_reaction/message_reaction.modul
MessageReactionModule,
],
})
export class GraphStoreModule {}
export class GraphStoreModule { }
65 changes: 0 additions & 65 deletions apps/graph-store/src/interceptors/rmq.interceptor.spec.ts

This file was deleted.

24 changes: 0 additions & 24 deletions apps/graph-store/src/interceptors/rmq.interceptor.ts

This file was deleted.

6 changes: 3 additions & 3 deletions apps/graph-store/src/message/message.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { UpdateEvent } from '@app/common';
import { Controller, Logger, UseInterceptors } from '@nestjs/common';
import { MessagePattern } from '@nestjs/microservices';
import { DChat } from '../decorators/chat.decorator';
import { RmqInterceptor } from '../interceptors/rmq.interceptor';
import { MessageService } from './message.service';
import { DMessage } from '../decorators/message.decorator';
import { Chat, Message, MessageEntity, User } from 'grammy/types';
Expand All @@ -14,9 +13,10 @@ import { MentionedService } from '../mentioned/mentioned.service';
import { RepliedService } from '../replied/replied.service';
import { UserService } from '../user/user.service';
import { Neo4jService } from 'nest-neo4j/dist';
import { EventsInterceptor } from '@app/common/rmq/interceptors/events.interceptor';

@Controller('message')
@UseInterceptors(RmqInterceptor)
@UseInterceptors(EventsInterceptor)
export class MessageController {
private readonly logger = new Logger(MessageController.name);

Expand All @@ -28,7 +28,7 @@ export class MessageController {
private readonly joinedService: JoinedService,
private readonly repliedService: RepliedService,
private readonly mentionedService: MentionedService,
) {}
) { }

@MessagePattern(UpdateEvent.MESSAGE)
async message(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import { DUser } from '../decorators/user.decorator';
import { JoinedService } from '../joined/joined.service';
import { UserService } from '../user/user.service';
import { MessageReactionService } from './message_reaction.service';
import { RmqInterceptor } from '../interceptors/rmq.interceptor';
import { EventsInterceptor } from '@app/common/rmq/interceptors/events.interceptor';

@Controller('message-reaction')
@UseInterceptors(RmqInterceptor)
@UseInterceptors(EventsInterceptor)
export class MessageReactionController {
private readonly logger = new Logger(MessageReactionController.name);

Expand All @@ -23,7 +23,7 @@ export class MessageReactionController {
private readonly userService: UserService,
private readonly joinedService: JoinedService,
private readonly messageReactionService: MessageReactionService,
) {}
) { }

@MessagePattern(UpdateEvent.MESSAGE_REACTION)
async message(
Expand Down
4 changes: 4 additions & 0 deletions apps/question/src/dto/QuestionResult.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export class QuestionResult {
label: 'QUESTION' | 'STATEMENT';
score: number;
}
3 changes: 3 additions & 0 deletions apps/question/src/dto/QuestionText.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export class QuestionText {
text: string;
}
13 changes: 13 additions & 0 deletions apps/question/src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { NestFactory } from '@nestjs/core';
import { QuestionModule } from './question.module';
import { RmqService, Services } from '@app/common';

async function bootstrap() {
const app = await NestFactory.create(QuestionModule);
const rmqService = app.get<RmqService>(RmqService);
app.connectMicroservice(
rmqService.getOptions(Services.TGQuestionService.queue),
);
await app.startAllMicroservices();
}
bootstrap();
25 changes: 25 additions & 0 deletions apps/question/src/question.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Test, TestingModule } from '@nestjs/testing';
import { QuestionController } from './question.controller';
import { QuestionService } from './question.service';

describe('QuestionController', () => {
let questionController: QuestionController;

beforeEach(async () => {
const app: TestingModule = await Test.createTestingModule({
controllers: [QuestionController],
providers: [QuestionService],
}).compile();

questionController = app.get<QuestionServiceController>(
QuestionServiceController,
);
Copy link

Choose a reason for hiding this comment

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

Fix the typo in the class name.

There is a typo in the class name used to retrieve the questionController instance. It should be QuestionController instead of QuestionServiceController.

Apply this diff to fix the typo:

-questionController = app.get<QuestionServiceController>(
-  QuestionServiceController,
-);
+questionController = app.get<QuestionController>(
+  QuestionController,
+);
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
questionController = app.get<QuestionServiceController>(
QuestionServiceController,
);
questionController = app.get<QuestionController>(
QuestionController,
);

});

describe('root', () => {
it('should return "Hello World!"', () => {
expect(questionController).toBeTruthy;
// expect(questionServiceController.message ()).toBe('Hello World!');
});
});
});
Loading
Loading