File size: 3,238 Bytes
ad67295
6f32ae8
ad67295
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6f32ae8
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
using Frontend.Models;
using LibraryManagement.Shared.Models;
using Frontend.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace Frontend.Controllers
{
    [Authorize]
    public class RewardsController : Controller
    {
        private readonly LibraryApiClient _apiClient;

        public RewardsController(LibraryApiClient apiClient)
        {
            _apiClient = apiClient;
        }

        // Member view: available rewards + pending redemptions + points history
        [HttpGet]
        public async Task<IActionResult> Index()
        {
            var rewards = await _apiClient.GetActiveRewardsAsync();
            var account = await _apiClient.GetMyLoyaltyAccountAsync();
            var pending = await _apiClient.GetMyPendingRedemptionsAsync();
            var redemptions = await _apiClient.GetMyRedemptionsAsync();
            if (account != null && string.IsNullOrWhiteSpace(account.Id) && !string.IsNullOrWhiteSpace(account.AccountId))
                account.Id = account.AccountId;

            var history = account != null && !string.IsNullOrWhiteSpace(account.Id)
                ? await _apiClient.GetPointsHistoryAsync(account.Id)
                : Enumerable.Empty<PointHistoryEntryDto>();

            var vm = new RewardsViewModel
            {
                Rewards = rewards,
                Account = account,
                PendingRedemptions = pending,
                RedemptionsHistory = redemptions,
                PointsHistory = history
            };

            return View(vm);
        }

        // Librarian view: all pending + all members' point history
        [HttpGet]
        [Authorize(Roles = "Librarian")]
        public async Task<IActionResult> Manage()
        {
            var pending = await _apiClient.GetPendingRedemptionsAsync();
            var allHistory = (await _apiClient.GetAllMembersPointsHistoryAsync())
                .Select(m =>
                {
                    m.History = m.History?.ToList() ?? new List<PointHistoryEntryDto>();
                    m.Redemptions = m.Redemptions?.ToList() ?? new List<LoyaltyRedemptionDto>();
                    return m;
                })
                .ToList();

            var vm = new LibrarianRewardsViewModel
            {
                PendingRedemptions = pending,
                AllMembersHistory = allHistory
            };

            return View(vm);
        }

        // POST: fulfill a redemption (librarian)
        [HttpPost]
        [Authorize(Roles = "Librarian")]
        public async Task<IActionResult> Fulfill(string id)
        {
            var (success, message) = await _apiClient.FulfillRedemptionAsync(id);
            if (success)
                TempData["Success"] = message;
            else
                TempData["Error"] = message;

            return RedirectToAction(nameof(Manage));
        }

        // POST: redeem a reward (member)
        [HttpPost]
        public async Task<IActionResult> Redeem(string rewardId)
        {
            var (success, message) = await _apiClient.ClaimRewardAsync(rewardId, "Redeemed via Library Rewards Page");
            return Json(new { success, message });
        }
    }
}