Skip to content

Commit

Permalink
Create endpoint to get messages
Browse files Browse the repository at this point in the history
  • Loading branch information
jotacemarin committed Sep 8, 2024
1 parent 37c137d commit c387dfd
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/functions/telegram_get_messages/function.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name: ${self:custom.secrets.service.name}-${self:custom.secrets.provider.stage}-telegram-get-messages
handler: index.telegramGetAll
memorySize: 128
timeout: 30
reservedConcurrency: 5
events:
- http:
path: /${self:custom.secrets.service.name}-${self:custom.secrets.provider.stage}/telegram/get-messages
method: POST
cors: true
authorizer: telegramAuthorizer
22 changes: 22 additions & 0 deletions src/functions/telegram_get_messages/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { APIGatewayEvent, Callback, Context } from "aws-lambda";
import { OK } from "http-status";
import { ChatMessageDao } from "../../lib/dao";

const execute = async (): Promise<any> => {
await ChatMessageDao.initInstance();
const messages = await ChatMessageDao.getAll();

return { statusCode: OK, body: JSON.stringify(messages) };
};

export const telegramGetAll = async (
event: APIGatewayEvent,
context: Context,
callback: Callback
): Promise<void> => {
context.callbackWaitsForEmptyEventLoop = false;

const response = await execute();

return callback(null, response);
};
9 changes: 9 additions & 0 deletions src/lib/dao/chatMessageDao.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,13 @@ export class ChatMessageDao {
): Promise<ChatMessage | null> {
return ChatMessageDao.chatMessageModel.create(chatMessage);
}

public static async getAll(): Promise<Array<ChatMessage>> {
const chatMessages = await ChatMessageDao.chatMessageModel.find({}).exec();
if (!chatMessages || !chatMessages?.length) {
return [];
}

return chatMessages;
}
}

0 comments on commit c387dfd

Please sign in to comment.