using AcaMate.Common.Models; using AcaMate.V1.Models; using Microsoft.AspNetCore.Mvc; using AcaMate.V1.Services; namespace AcaMate.V1.Controllers; [ApiController] [Route("/api/v1/in/push")] [ApiExplorerSettings(GroupName = "공통")] public class PushController : ControllerBase { private readonly IWebHostEnvironment _env; private readonly Logger _logger; public PushController(IWebHostEnvironment env, Logger logger) { _env = env; _logger = logger; } [HttpGet()] [CustomOperation("푸시 확인", "저장된 양식을 확인 할 수 있다..", "푸시")] public IActionResult GetPushData() { return Ok("SEND"); } /// /// Sends a push notification to the specified device token with the provided payload. /// /// The device token to send the notification to. /// The payload of the push notification. /// An IActionResult indicating the result of the operation. /// Push notification sent successfully. /// Invalid input parameters. /// Internal server error occurred. /// Service unavailable. [HttpPost("send")] [CustomOperation("푸시전송", "저장된 양식으로, 사용자에게 푸시를 전송한다.(로컬 테스트 불가)", "푸시")] [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(APIResponseStatus))] public async Task SendPush(string deviceToken, [FromBody] Payload? payload) { if (string.IsNullOrWhiteSpace(deviceToken) || payload == null) return BadRequest(APIResponse.InvalidInputError()); var isDev = _env.IsDevelopment(); var pushFile = new PushFile() { uri = isDev ? "https://api.sandbox.push.apple.com/" : "https://api.push.apple.com/", p12Path = isDev ? "/src/private/AM_Push_Sandbox.p12" : "/src/private/AM_Push.p12", p12PWPath = "/src/private/appleKeys.json", apnsTopic = "me.myds.ipstein.acamate.AcaMate" }; try { if (await new ApnsPushService().SendPushNotificationAsync(deviceToken, pushFile, payload)) { return Ok(APIResponse.Send("000", "정상", Empty)); } else { return StatusCode(500, APIResponse.UnknownError()); } } catch (ServiceConnectionFailedException failEx) { _logger.LogError($"[푸시][에러] : {failEx}"); return StatusCode(300, APIResponse.InternalSeverError()); } catch (HttpRequestException httpEx) { _logger.LogError($"[푸시][에러] : {httpEx}"); return StatusCode(300, APIResponse.InternalSeverError()); } catch (Exception ex) { _logger.LogError($"[푸시][에러] : {ex}"); return StatusCode(500, APIResponse.UnknownError()); } } }