diff --git a/DopplerBeplic.Tests/GetTemplateMessageStatusTests.cs b/DopplerBeplic.Tests/GetTemplateMessageStatusTests.cs new file mode 100644 index 0000000..55fc456 --- /dev/null +++ b/DopplerBeplic.Tests/GetTemplateMessageStatusTests.cs @@ -0,0 +1,66 @@ +using DopplerBeplic.Models.Responses; +using DopplerBeplic.Services.Interfaces; +using Newtonsoft.Json; + +namespace DopplerBeplic.Tests +{ + public class GetTemplateMessageStatusTests + { + [Fact] + public async Task GET_get_message_status_should_not_authorize_without_token() + { + var application = new PlaygroundApplication(); + var client = application.CreateClient(); + + var response = await client.GetAsync("/customer/messages/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"); + + Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode); + Assert.Equal("Bearer", response.Headers.WwwAuthenticate.ToString()); + } + + [Fact] + public async Task GET_get_message_status_should_authorize_super_user_and_returns_status() + { + var bodyParams = new { }; + + var application = new PlaygroundApplication(); + + var messageId = Guid.NewGuid().ToString(); + + var expectedResponse = new TemplateMessageResponse() + { + MessageId = messageId, + Status = new List() + { + new TemplateMessageStatusResponse() + { + Status = "READ", + StatusDate = DateTime.UtcNow + } + } + }; + + var beplicServiceMock = new Mock(); + beplicServiceMock.Setup(x => x.GetMessageStatus(messageId)).ReturnsAsync(expectedResponse); + + application.ConfigureServices(services => services.AddSingleton(beplicServiceMock.Object)); + + var client = application.CreateClient(); + client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", TestUsersData.Token_Superuser_Expire2033_05_18); + + var response = await client.GetAsync($"/customer/messages/{messageId}"); + + Assert.Equal(HttpStatusCode.OK, response.StatusCode); + + beplicServiceMock.Verify(x => x.GetMessageStatus(messageId), Times.Once); + + var content = await response.Content.ReadAsStringAsync(); + + var messageResponse = JsonConvert.DeserializeObject(content); + + Assert.Equal(messageResponse?.MessageId, expectedResponse.MessageId); + Assert.Equal("READ", messageResponse?.Status?[0].Status); + } + + } +} diff --git a/doppler-beplic/Models/Responses/BeplicResponses/BeplicTemplateMessageStatusResponse.cs b/doppler-beplic/Models/Responses/BeplicResponses/BeplicTemplateMessageStatusResponse.cs new file mode 100644 index 0000000..a8fe432 --- /dev/null +++ b/doppler-beplic/Models/Responses/BeplicResponses/BeplicTemplateMessageStatusResponse.cs @@ -0,0 +1,8 @@ +namespace DopplerBeplic.Models.Responses.BeplicResponses +{ + public class BeplicTemplateMessageStatusResponse + { + public string? Status { get; set; } + public DateTime StatusDate { get; set; } + } +} diff --git a/doppler-beplic/Models/Responses/TemplateMessageResponse.cs b/doppler-beplic/Models/Responses/TemplateMessageResponse.cs index bfcc65a..5ad4407 100644 --- a/doppler-beplic/Models/Responses/TemplateMessageResponse.cs +++ b/doppler-beplic/Models/Responses/TemplateMessageResponse.cs @@ -3,5 +3,6 @@ namespace DopplerBeplic.Models.Responses public class TemplateMessageResponse { public string? MessageId { get; set; } + public List? Status { get; set; } } } diff --git a/doppler-beplic/Models/Responses/TemplateMessageStatusResponse.cs b/doppler-beplic/Models/Responses/TemplateMessageStatusResponse.cs new file mode 100644 index 0000000..b676ac2 --- /dev/null +++ b/doppler-beplic/Models/Responses/TemplateMessageStatusResponse.cs @@ -0,0 +1,8 @@ +namespace DopplerBeplic.Models.Responses +{ + public class TemplateMessageStatusResponse + { + public string? Status { get; set; } + public DateTime StatusDate { get; set; } + } +} diff --git a/doppler-beplic/Program.cs b/doppler-beplic/Program.cs index c5e20e3..1d3ac8f 100644 --- a/doppler-beplic/Program.cs +++ b/doppler-beplic/Program.cs @@ -267,6 +267,23 @@ .RequireAuthorization(Policies.OnlySuperuser); +app.MapGet("/customer/messages/{messageId}", async Task, BadRequest>> (string messageId, IBeplicService beplicService) => +{ + try + { + var response = await beplicService.GetMessageStatus(messageId); + + return TypedResults.Ok(response); + } + catch (Exception e) + { + return TypedResults.BadRequest(e.Message); + } +}) +.WithName("MessageStatus") +.WithOpenApi() +.RequireAuthorization(Policies.OnlySuperuser); + app.MapGet("/customer/rooms/{roomId}/templates", async Task>, BadRequest>> (int roomId, IBeplicService beplicService) => { try diff --git a/doppler-beplic/Services/Classes/BeplicService.cs b/doppler-beplic/Services/Classes/BeplicService.cs index 4d6720d..474e529 100644 --- a/doppler-beplic/Services/Classes/BeplicService.cs +++ b/doppler-beplic/Services/Classes/BeplicService.cs @@ -330,6 +330,41 @@ public async Task PlanAssign(PlanAssignmentDTO planAssignDat return result; } + public async Task GetMessageStatus(string messageId) + { + var parameters = new Parameter[] + { + Parameter.CreateParameter("messageId",messageId,ParameterType.UrlSegment) + }; + var response = await _sdk.ExecuteServiceResource("/services/partner/messages/{messageId}", parameters, Method.Get); + + if (response.IsSuccessStatusCode) + { + var result = JsonConvert.DeserializeObject>>(response.Content ?? string.Empty); + + return new TemplateMessageResponse() + { + MessageId = messageId, + Status = result?.Data?.Select(x => new TemplateMessageStatusResponse() + { + Status = x.Status, + StatusDate = x.StatusDate, + }).ToList(), + }; + } + else + { + var errorResponse = JsonConvert.DeserializeObject>(response.Content ?? string.Empty); + + var message = errorResponse?.Message ?? string.Empty; + var statusCode = errorResponse?.HttpStatusCode ?? (int)HttpStatusCode.InternalServerError; + + LogErrorMethod("GetMessageStatus", response.Content ?? message); + + throw new BadHttpRequestException(message, statusCode); + } + } + public async Task SendTemplateMessage(int roomId, int templateId, TemplateMessageDTO messageDTO) { var bodyParams = new diff --git a/doppler-beplic/Services/Interfaces/IBeplicService.cs b/doppler-beplic/Services/Interfaces/IBeplicService.cs index 5126945..8c75d4e 100644 --- a/doppler-beplic/Services/Interfaces/IBeplicService.cs +++ b/doppler-beplic/Services/Interfaces/IBeplicService.cs @@ -28,5 +28,6 @@ public interface IBeplicService Task CancelPlan(string idExternal); Task GetUserPlan(int idExternal); + Task GetMessageStatus(string messageId); } }