File size: 1,363 Bytes
ad67295
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
using System.Security.Claims;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace Backend.Features.Loyalty
{
    [ApiController]
    [Route("api/v1/accounts")]
    [Authorize]
    public class LoyaltyAccountsController : ControllerBase
    {
        private readonly ILoyaltyService _loyaltyService;

        public LoyaltyAccountsController(ILoyaltyService loyaltyService)
        {
            _loyaltyService = loyaltyService;
        }

        [HttpGet("{accountId}/history")]
        public async Task<IActionResult> GetAccountHistory(string accountId)
        {
            // Librarians may view any account history; members may only view their own.
            if (!User.IsInRole("Librarian"))
            {
                var userIdStr = User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
                if (string.IsNullOrEmpty(userIdStr)) return Unauthorized();

                var myAccount = await _loyaltyService.GetUserAccountAsync(userIdStr);
                if (myAccount == null) return NotFound("Loyalty account not found.");
                if (!string.Equals(myAccount.Id, accountId, StringComparison.OrdinalIgnoreCase))
                    return Forbid();
            }

            var history = await _loyaltyService.GetPointsHistoryAsync(accountId);
            return Ok(history);
        }
    }
}