Nodejs

 

 

  • gRPC 기반의 Notification Proto 파일 작성

  • NotificationService 정의 및 SendPaymentNotification RPC 구현

  • NestJS에서 gRPC와 호환되도록 Controller + DTO + Service 구조 정리

  • 알림 기능의 확장 가능성 확인 (이메일, SMS, 푸시 알림 등)

 

 Notification Proto 정의

syntax = "proto3";

package notification;

service NotificationService {
  rpc SendPaymentNotification(SendPaymentNotificationRequest) returns (SendPaymentNotificationResponse);
}

message SendPaymentNotificationRequest {
  string to = 1;
  string orderId = 2;
}

message SendPaymentNotificationResponse {
  string from = 1;
  string to = 2;
  string subject = 3;
  string content = 4;
  string notificationStatus = 5;
}

설명

  • 서비스 이름: NotificationService

  • RPC 메서드: SendPaymentNotification

    • 요청(Request)에는 to(수신자), orderId(주문 ID)가 포함됨

    • 응답(Response)에는 발신자, 수신자, 제목, 본문, 상태값을 반환

이렇게 정의된 .proto 파일은 gRPC 통신 시 **자동 코드 생성기(protoc)**에 의해 NestJS에서 사용 가능한 TypeScript 인터페이스로 변환됩니다.

 

 

 Controller

import {
  Controller,
  UseInterceptors,
  UsePipes,
  ValidationPipe,
} from '@nestjs/common';
import { NotificationService } from './notification.service';
import { MessagePattern, Payload } from '@nestjs/microservices';
import { RpcInterceptor } from '@app/common';
import { SendPaymentNotificationDto } from './dto/send-payment-notification.dto';

@Controller()
export class NotificationController {
  constructor(private readonly notificationService: NotificationService) {}

  @MessagePattern({ cmd: 'send_payment_notification' })
  @UsePipes(ValidationPipe)
  @UseInterceptors(RpcInterceptor)
  async sendPaymentNotification(
    @Payload() payload: SendPaymentNotificationDto,
  ) {
    console.log('✔️ NotificationController sendPaymentNotification', payload);
    return this.notificationService.sendPaymentNotification(payload);
  }
}

포인트

  • @MessagePattern으로 gRPC 요청을 수신

  • ValidationPipe로 들어온 데이터 검증

  • RpcInterceptor로 gRPC 응답을 NestJS 표준 형식에 맞게 가공

  • 실제 비즈니스 로직은 NotificationService로 위임

 

DTO 정의

import { IsNotEmpty, IsString } from 'class-validator';

export class SendPaymentNotificationDto {
  @IsString()
  @IsNotEmpty()
  to: string;

  @IsString()
  @IsNotEmpty()
  orderId: string;
}

포인트

  • to: 알림을 받을 사용자 ID 또는 이메일

  • orderId: 결제된 주문 ID

  • class-validator를 활용해 유효성 검증 수행

 

NotificationService (구현 예시)

import { Injectable } from '@nestjs/common';
import { SendPaymentNotificationDto } from './dto/send-payment-notification.dto';

@Injectable()
export class NotificationService {
  async sendPaymentNotification(dto: SendPaymentNotificationDto) {
    const { to, orderId } = dto;

    return {
      from: 'no-reply@ourapp.com',
      to,
      subject: `주문 ${orderId} 결제 완료 알림`,
      content: `고객님의 주문 ${orderId}이 성공적으로 결제되었습니다.`,
      notificationStatus: 'SENT',
    };
  }
}

 

설명

  • 실무에서는 이메일 서버(SMTP) 또는 Push 서버와 연동하여 전송

  • 현재는 단순히 응답 객체를 반환하는 구조

  • 이후 확장 시 Kafka, RabbitMQ, Firebase Cloud Messaging 등과 쉽게 연결 가능

 

 

 

 

about author

PHRASE

Level 60  라이트

우주에서 무엇이건 보배롭고 아름다운 것은 그렇게 되는 데 시간이 걸린다. -앤드류 매튜스

댓글 ( 0)

댓글 남기기

작성