Skip to content

Commit

Permalink
[MERGE] feat/scheduled-push-notification_T-7346 -> develop
Browse files Browse the repository at this point in the history
[T-7346] feat: 예약 발송을 위한 eventBridgeHandler 추가
  • Loading branch information
yummygyudon authored Dec 31, 2024
2 parents 412fe27 + f678b55 commit 2506dcd
Showing 1 changed file with 53 additions and 2 deletions.
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

0 comments on commit 2506dcd

Please sign in to comment.