fix(web-client-tpos): fix roles API response parsing and property mapping
This commit is contained in:
@@ -38,7 +38,13 @@ public class IamApiService
|
||||
|
||||
// ─── ROLES ───
|
||||
|
||||
public record RoleDto(Guid Id, string Name, string? Description, bool IsSystem, DateTime CreatedAt, int? UserCount);
|
||||
public record RoleDto(
|
||||
Guid Id,
|
||||
string Name,
|
||||
string? Description,
|
||||
[property: JsonPropertyName("isSystemRole")] bool IsSystem,
|
||||
DateTime CreatedAt,
|
||||
int? UserCount);
|
||||
|
||||
public async Task<List<RoleDto>> GetRolesAsync()
|
||||
{
|
||||
@@ -49,8 +55,17 @@ public class IamApiService
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
var json = await response.Content.ReadFromJsonAsync<JsonElement>(_jsonOptions);
|
||||
if (json.TryGetProperty("data", out var data) && data.TryGetProperty("items", out var items))
|
||||
return items.Deserialize<List<RoleDto>>(_jsonOptions) ?? new();
|
||||
// EN: API returns { success, data: [...], pagination: {...} }
|
||||
// VI: API trả về { success, data: [...], pagination: {...} }
|
||||
if (json.TryGetProperty("data", out var data))
|
||||
{
|
||||
// data is a direct array of roles
|
||||
if (data.ValueKind == JsonValueKind.Array)
|
||||
return data.Deserialize<List<RoleDto>>(_jsonOptions) ?? new();
|
||||
// data might have items sub-property
|
||||
if (data.TryGetProperty("items", out var items))
|
||||
return items.Deserialize<List<RoleDto>>(_jsonOptions) ?? new();
|
||||
}
|
||||
if (json.TryGetProperty("items", out var items2))
|
||||
return items2.Deserialize<List<RoleDto>>(_jsonOptions) ?? new();
|
||||
return json.Deserialize<List<RoleDto>>(_jsonOptions) ?? new();
|
||||
|
||||
Reference in New Issue
Block a user