Skip to content

Commit

Permalink
Merge pull request #120 from FromDoppler/add-get-message-status
Browse files Browse the repository at this point in the history
Add get message status
  • Loading branch information
mmosquera authored Oct 4, 2024
2 parents f54f1fc + 605ee93 commit 26382ce
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 0 deletions.
66 changes: 66 additions & 0 deletions DopplerBeplic.Tests/GetTemplateMessageStatusTests.cs
Original file line number Diff line number Diff line change
@@ -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<TemplateMessageStatusResponse>()
{
new TemplateMessageStatusResponse()
{
Status = "READ",
StatusDate = DateTime.UtcNow
}
}
};

var beplicServiceMock = new Mock<IBeplicService>();
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<TemplateMessageResponse>(content);

Assert.Equal(messageResponse?.MessageId, expectedResponse.MessageId);
Assert.Equal("READ", messageResponse?.Status?[0].Status);
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace DopplerBeplic.Models.Responses.BeplicResponses
{
public class BeplicTemplateMessageStatusResponse
{
public string? Status { get; set; }
public DateTime StatusDate { get; set; }
}
}
1 change: 1 addition & 0 deletions doppler-beplic/Models/Responses/TemplateMessageResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ namespace DopplerBeplic.Models.Responses
public class TemplateMessageResponse
{
public string? MessageId { get; set; }
public List<TemplateMessageStatusResponse>? Status { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace DopplerBeplic.Models.Responses
{
public class TemplateMessageStatusResponse
{
public string? Status { get; set; }
public DateTime StatusDate { get; set; }
}
}
17 changes: 17 additions & 0 deletions doppler-beplic/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,23 @@
.RequireAuthorization(Policies.OnlySuperuser);


app.MapGet("/customer/messages/{messageId}", async Task<Results<Ok<TemplateMessageResponse>, BadRequest<string>>> (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<Results<Ok<IEnumerable<TemplateResponse>>, BadRequest<string>>> (int roomId, IBeplicService beplicService) =>
{
try
Expand Down
35 changes: 35 additions & 0 deletions doppler-beplic/Services/Classes/BeplicService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,41 @@ public async Task<PlanAssignResponse> PlanAssign(PlanAssignmentDTO planAssignDat
return result;
}

public async Task<TemplateMessageResponse> 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<BeplicServiceResponse<List<BeplicTemplateMessageStatusResponse>>>(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<BeplicServiceResponse<dynamic>>(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<TemplateMessageResponse> SendTemplateMessage(int roomId, int templateId, TemplateMessageDTO messageDTO)
{
var bodyParams = new
Expand Down
1 change: 1 addition & 0 deletions doppler-beplic/Services/Interfaces/IBeplicService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@ public interface IBeplicService
Task<PlanCancellationResponse> CancelPlan(string idExternal);

Task<UserPlanResponse> GetUserPlan(int idExternal);
Task<TemplateMessageResponse> GetMessageStatus(string messageId);
}
}

0 comments on commit 26382ce

Please sign in to comment.