Skip to content

Commit

Permalink
Merge pull request #265 from PinguApps/207-feat-implement-users-updat…
Browse files Browse the repository at this point in the history
…e-phone

Implemented users update phone
  • Loading branch information
pingu2k4 authored Oct 9, 2024
2 parents 3410320 + 6f732d8 commit 23d5402
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 10 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,9 @@ string emailAddressOrErrorMessage = userResponse.Result.Match(
```

## ⌛ Progress
![Server & Client - 81 / 291](https://img.shields.io/badge/Server_&_Client-81%20%2F%20291-red?style=for-the-badge)
![Server & Client - 82 / 291](https://img.shields.io/badge/Server_&_Client-82%20%2F%20291-red?style=for-the-badge)

![Server - 37 / 201](https://img.shields.io/badge/Server-37%20%2F%20201-red?style=for-the-badge)
![Server - 38 / 201](https://img.shields.io/badge/Server-38%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 Down Expand Up @@ -207,7 +207,7 @@ string emailAddressOrErrorMessage = userResponse.Result.Match(
| [Create Phone Verification (Confirmation)](https://appwrite.io/docs/references/1.6.x/client-rest/account#updatePhoneVerification) ||| |

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

| Endpoint | Client | Server |
|:-:|:-:|:-:|
Expand Down Expand Up @@ -237,7 +237,7 @@ string emailAddressOrErrorMessage = userResponse.Result.Match(
| [Create MFA Recovery Codes](https://appwrite.io/docs/references/1.6.x/server-rest/users#createMfaRecoveryCodes) |||
| [Update Name](https://appwrite.io/docs/references/1.6.x/server-rest/users#updateName) |||
| [Update Password](https://appwrite.io/docs/references/1.6.x/server-rest/users#updatePassword) |||
| [Update Phone](https://appwrite.io/docs/references/1.6.x/server-rest/users#updatePhone) || |
| [Update Phone](https://appwrite.io/docs/references/1.6.x/server-rest/users#updatePhone) || |
| [Get User Preferences](https://appwrite.io/docs/references/1.6.x/server-rest/users#getPrefs) |||
| [Update User Preferences](https://appwrite.io/docs/references/1.6.x/server-rest/users#updatePrefs) |||
| [List User Sessions](https://appwrite.io/docs/references/1.6.x/server-rest/users#listSessions) |||
Expand Down
6 changes: 3 additions & 3 deletions src/PinguApps.Appwrite.Playground/App.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ public App(Client.IAppwriteClient client, Server.Clients.IAppwriteClient server,

public async Task Run(string[] args)
{
var request = new UpdatePasswordRequest()
var request = new UpdatePhoneRequest()
{
UserId = "664aac1a00113f82e620",
Password = "MySuperPassword"
PhoneNumber = "+447501234567"
};

var response = await _server.Users.UpdatePassword(request);
var response = await _server.Users.UpdatePhone(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 @@ -221,7 +221,13 @@ public interface IUsersClient
/// <param name="request">The request content</param>
/// <returns>The user</returns>
Task<AppwriteResult<User>> UpdatePassword(UpdatePasswordRequest request);
[Obsolete("This method hasn't yet been implemented.", true)]

/// <summary>
/// Update the user phone by its unique ID
/// <para><see href="https://appwrite.io/docs/references/1.6.x/server-rest/users#updatePhone">Appwrite Docs</see></para>
/// </summary>
/// <param name="request">The request content</param>
/// <returns>The user</returns>
Task<AppwriteResult<User>> UpdatePhone(UpdatePhoneRequest request);
[Obsolete("This method hasn't yet been implemented.", true)]
Task<AppwriteResult<IReadOnlyDictionary<string, string>>> GetUserPreferences(GetUserPreferencesRequest 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 @@ -463,9 +463,22 @@ public async Task<AppwriteResult<User>> UpdatePassword(UpdatePasswordRequest req
}
}

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

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

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

[ExcludeFromCodeCoverage]
/// <inheritdoc/>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
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 UpdatePhone_ShouldReturnSuccess_WhenApiCallSucceeds()
{
// Arrange
var request = new UpdatePhoneRequest
{
UserId = IdUtils.GenerateUniqueId(),
PhoneNumber = "+1234567890"
};

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

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

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

[Fact]
public async Task UpdatePhone_ShouldHandleException_WhenApiCallFails()
{
// Arrange
var request = new UpdatePhoneRequest
{
UserId = IdUtils.GenerateUniqueId(),
PhoneNumber = "+1234567890"
};

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

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

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

[Fact]
public async Task UpdatePhone_ShouldReturnErrorResponse_WhenExceptionOccurs()
{
// Arrange
var request = new UpdatePhoneRequest
{
UserId = IdUtils.GenerateUniqueId(),
PhoneNumber = "+1234567890"
};

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

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

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

0 comments on commit 23d5402

Please sign in to comment.