Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented update user target #282

Merged
merged 4 commits into from
Oct 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,10 @@ string emailAddressOrErrorMessage = userResponse.Result.Match(
```

## ⌛ Progress
<!-- `red` for first third, `gold` for second third, `forestgreen` for final third -->
![Server & Client - 93 / 291](https://img.shields.io/badge/Server_&_Client-93%20%2F%20291-red?style=for-the-badge)
<!-- `red` for first third, `gold` for second third, `forestgreen` for final third, `blue` for 100% -->
![Server & Client - 94 / 291](https://img.shields.io/badge/Server_&_Client-94%20%2F%20291-red?style=for-the-badge)

![Server - 49 / 201](https://img.shields.io/badge/Server-49%20%2F%20201-red?style=for-the-badge)
![Server - 50 / 201](https://img.shields.io/badge/Server-50%20%2F%20201-red?style=for-the-badge)

![Client - 44 / 90](https://img.shields.io/badge/Client-44%20%2F%2090-gold?style=for-the-badge)

Expand All @@ -155,7 +155,7 @@ string emailAddressOrErrorMessage = userResponse.Result.Match(
| ❌ | There is currently no intention to implement the endpoint for the given SDK type (client or server) |

### Account
![Account - 55 / 55](https://img.shields.io/badge/Account-55%20%2F%2055-forestgreen?style=for-the-badge)
![Account - 55 / 55](https://img.shields.io/badge/Account-55%20%2F%2055-blue?style=for-the-badge)

| Endpoint | Client | Server | Notes |
|:-:|:-:|:-:|:-:|
Expand Down Expand Up @@ -208,7 +208,7 @@ string emailAddressOrErrorMessage = userResponse.Result.Match(
| [Create Phone Verification (Confirmation)](https://appwrite.io/docs/references/1.6.x/client-rest/account#updatePhoneVerification) | ✅ | ❌ | |

### Users
![Account - 38 / 41](https://img.shields.io/badge/Users-38%20%2F%2041-forestgreen?style=for-the-badge)
![Account - 39 / 41](https://img.shields.io/badge/Users-39%20%2F%2041-forestgreen?style=for-the-badge)

| Endpoint | Client | Server |
|:-:|:-:|:-:|
Expand Down Expand Up @@ -249,7 +249,7 @@ string emailAddressOrErrorMessage = userResponse.Result.Match(
| [List User Targets](https://appwrite.io/docs/references/1.6.x/server-rest/users#listTargets) | ❌ | ✅ |
| [Create User Target](https://appwrite.io/docs/references/1.6.x/server-rest/users#createTarget) | ❌ | ✅ |
| [Get User Target](https://appwrite.io/docs/references/1.6.x/server-rest/users#getTarget) | ❌ | ✅ |
| [Update User Target](https://appwrite.io/docs/references/1.6.x/server-rest/users#updateTarget) | ❌ | |
| [Update User Target](https://appwrite.io/docs/references/1.6.x/server-rest/users#updateTarget) | ❌ | |
| [Delete User Target](https://appwrite.io/docs/references/1.6.x/server-rest/users#deleteTarget) | ❌ | ✅ |
| [Create Token](https://appwrite.io/docs/references/1.6.x/server-rest/users#createToken) | ❌ | ⬛ |
| [Update Email Verification](https://appwrite.io/docs/references/1.6.x/server-rest/users#updateEmailVerification) | ❌ | ⬛ |
Expand Down
7 changes: 4 additions & 3 deletions src/PinguApps.Appwrite.Playground/App.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ public App(Client.IAppwriteClient client, Server.Clients.IAppwriteClient server,

public async Task Run(string[] args)
{
var request = new GetUserTargetRequest()
var request = new UpdateUserTargertRequest()
{
UserId = "664aac1a00113f82e620",
TargetId = "def"
TargetId = "def",
Name = "New Name"
};

var response = await _server.Users.GetUserTarget(request);
var response = await _server.Users.UpdateUserTarget(request);

Console.WriteLine(response.Result.Match(
result => result.ToString(),
Expand Down
8 changes: 7 additions & 1 deletion src/PinguApps.Appwrite.Server/Clients/IUsersClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,13 @@ public interface IUsersClient
/// <param name="request">The request content</param>
/// <returns>The target</returns>
Task<AppwriteResult<Target>> GetUserTarget(GetUserTargetRequest request);
[Obsolete("This method hasn't yet been implemented.", true)]

/// <summary>
/// Update a messaging target
/// <para><see href="https://appwrite.io/docs/references/1.6.x/server-rest/users#updateTarget">Appwrite Docs</see></para>
/// </summary>
/// <param name="request">The request content</param>
/// <returns>The target</returns>
Task<AppwriteResult<Target>> UpdateUserTarget(UpdateUserTargertRequest request);
[Obsolete("This method hasn't yet been implemented.", true)]
Task<AppwriteResult<Token>> CreateToken(CreateTokenRequest request);
Expand Down
17 changes: 15 additions & 2 deletions src/PinguApps.Appwrite.Server/Clients/UsersClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -667,9 +667,22 @@ public async Task<AppwriteResult<Target>> GetUserTarget(GetUserTargetRequest req
}
}

[ExcludeFromCodeCoverage]
/// <inheritdoc/>
public Task<AppwriteResult<Target>> UpdateUserTarget(UpdateUserTargertRequest request) => throw new NotImplementedException();
public async Task<AppwriteResult<Target>> UpdateUserTarget(UpdateUserTargertRequest request)
{
try
{
request.Validate(true);

var result = await _usersApi.UpdateUserTarget(request.UserId, request.TargetId, request);

return result.GetApiResponse();
}
catch (Exception e)
{
return e.GetExceptionResponse<Target>();
}
}

[ExcludeFromCodeCoverage]
/// <inheritdoc/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,20 @@ public class UpdateUserTargertRequest : UserIdBaseRequest<UpdateUserTargertReque
/// The target identifier (token, email, phone etc.)
/// </summary>
[JsonPropertyName("identifier")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string? Identifier { get; set; }

/// <summary>
/// Provider ID. Message will be sent to this target from the specified provider ID. If no provider ID is set the first setup provider will be used
/// </summary>
[JsonPropertyName("providerId")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string? ProviderId { get; set; }

/// <summary>
/// Target name. Max length: 128 chars. For example: My Awesome App Galaxy S23.
/// </summary>
[JsonPropertyName("name")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string? Name { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using System.Net;
using PinguApps.Appwrite.Shared.Requests.Users;
using PinguApps.Appwrite.Shared.Tests;
using PinguApps.Appwrite.Shared.Utils;
using RichardSzalay.MockHttp;

namespace PinguApps.Appwrite.Server.Tests.Clients.Users;
public partial class UsersClientTests
{
[Fact]
public async Task UpdateUserTarget_ShouldReturnSuccess_WhenApiCallSucceeds()
{
// Arrange
var request = new UpdateUserTargertRequest
{
UserId = IdUtils.GenerateUniqueId(),
TargetId = IdUtils.GenerateUniqueId(),
// Add other properties as needed
};

_mockHttp.Expect(HttpMethod.Patch, $"{Constants.Endpoint}/users/{request.UserId}/targets/{request.TargetId}")
.ExpectedHeaders()
.Respond(Constants.AppJson, Constants.TargetResponse);

// Act
var result = await _appwriteClient.Users.UpdateUserTarget(request);

// Assert
Assert.True(result.Success);
}

[Fact]
public async Task UpdateUserTarget_ShouldHandleException_WhenApiCallFails()
{
// Arrange
var request = new UpdateUserTargertRequest
{
UserId = IdUtils.GenerateUniqueId(),
TargetId = IdUtils.GenerateUniqueId(),
// Add other properties as needed
};

_mockHttp.Expect(HttpMethod.Patch, $"{Constants.Endpoint}/users/{request.UserId}/targets/{request.TargetId}")
.ExpectedHeaders()
.Respond(HttpStatusCode.BadRequest, Constants.AppJson, Constants.AppwriteError);

// Act
var result = await _appwriteClient.Users.UpdateUserTarget(request);

// Assert
Assert.True(result.IsError);
Assert.True(result.IsAppwriteError);
}

[Fact]
public async Task UpdateUserTarget_ShouldReturnErrorResponse_WhenExceptionOccurs()
{
// Arrange
var request = new UpdateUserTargertRequest
{
UserId = IdUtils.GenerateUniqueId(),
TargetId = IdUtils.GenerateUniqueId(),
// Add other properties as needed
};

_mockHttp.Expect(HttpMethod.Patch, $"{Constants.Endpoint}/users/{request.UserId}/targets/{request.TargetId}")
.ExpectedHeaders()
.Throw(new HttpRequestException("An error occurred"));

// Act
var result = await _appwriteClient.Users.UpdateUserTarget(request);

// Assert
Assert.False(result.Success);
Assert.True(result.IsInternalError);
Assert.Equal("An error occurred", result.Result.AsT2.Message);
}
}