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

[T-7346] feat: 예약 발송을 위한 eventBridgeHandler 추가 #20

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all 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
55 changes: 53 additions & 2 deletions src/lambda.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { APIGatewayProxyEvent, SNSEvent } from 'aws-lambda';
import { APIGatewayProxyEvent, EventBridgeEvent, SNSEvent } from 'aws-lambda';
import { v4 as uuid } from 'uuid';

import tokenFactory from './modules/tokenFactory';
Expand Down Expand Up @@ -187,6 +187,7 @@ const sendPush = async (dto: RequestSendPushMessageDTO) => {
throw new Error(`send Push error: ${e}`);
}
};

const sendPushAll = async (dto: RequestSendAllPushMessageDTO) => {
try {
const { transactionId, title, content, category, webLink, deepLink, service } = dto;
Expand Down Expand Up @@ -242,6 +243,54 @@ const sendPushAll = async (dto: RequestSendAllPushMessageDTO) => {
}
};

const eventBridgeHandler = async (event: EventBridgeEvent<string, any>) => {
const { header, body } = event.detail;

if (!header || !body) {
return response(400, status.success(statusCode.BAD_REQUEST, responseMessage.INVALID_REQUEST));
}

const { action, transactionId, service } = header;

try {
switch (action) {
case Actions.SEND: {
const dto: RequestSendPushMessageDTO = {
...body,
transactionId,
service,
};

if (!dtoValidator.toRequestSendPushMessageDto(dto)) {
return response(400, status.success(statusCode.BAD_REQUEST, responseMessage.INVALID_REQUEST));
}

await sendPush(dto);
return response(200, status.success(statusCode.OK, responseMessage.SEND_SUCCESS));
}
case Actions.SEND_ALL: {
const dto: RequestSendAllPushMessageDTO = {
...body,
transactionId,
service,
};

if (!dtoValidator.toRequestSendAllPushMessageDTO(dto)) {
return response(400, status.success(statusCode.BAD_REQUEST, responseMessage.INVALID_REQUEST));
}

await sendPushAll(dto);
return response(200, status.success(statusCode.OK, responseMessage.SEND_SUCCESS));
}
default: {
return response(400, status.success(statusCode.BAD_REQUEST, responseMessage.INVALID_REQUEST));
}
}
} catch (error) {
return response(500, status.fail(statusCode.INTERNAL_ERROR, responseMessage.INTERNAL_SERVER_ERROR));
}
};

const apiGateWayHandler = async (event: APIGatewayProxyEvent) => {
if (event.body === null || event.headers.action === undefined) {
return response(400, status.success(statusCode.BAD_REQUEST, responseMessage.INVALID_REQUEST));
Expand Down Expand Up @@ -340,9 +389,11 @@ const snsHandler = async (event: SNSEvent) => {
return response(204, status.success(statusCode.NO_CONTENT, responseMessage.NO_CONTENT));
};

export const service = async (event: APIGatewayProxyEvent | SNSEvent): Promise<any> => {
export const service = async (event: APIGatewayProxyEvent | EventBridgeEvent<string, any> | SNSEvent): Promise<any> => {
if ('Records' in event) {
return await snsHandler(event);
} else if ('detail' in event) {
return await eventBridgeHandler(event);
} else {
return await apiGateWayHandler(event);
}
Expand Down