Skip to content

Commit

Permalink
🐛 Mobile | Weekly Leaderboard displays stale data (#937)
Browse files Browse the repository at this point in the history
* refactor GetTimeElapsed and UpdateRecentActivity methods

* replace User to LeaderboardUserDto.cs mapping with a constructor
  • Loading branch information
AntPolkanov committed Jun 5, 2024
1 parent 3e9e4b8 commit fcb4241
Show file tree
Hide file tree
Showing 18 changed files with 155 additions and 188 deletions.
39 changes: 0 additions & 39 deletions src/Application/Leaderboard/Queries/Common/Mapping.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using AutoMapper.QueryableExtensions;
using MoreLinq;
using SSW.Rewards.Shared.DTOs.Leaderboard;
using SSW.Rewards.Application.Common.Extensions;

Expand All @@ -16,16 +16,13 @@ public GetFilteredLeaderboardList(LeaderboardFilter filter)
public class GetFilteredLeaderboardListHandler : IRequestHandler<GetFilteredLeaderboardList, LeaderboardViewModel>
{
private readonly IApplicationDbContext _context;
private readonly IMapper _mapper;
private readonly IDateTime _dateTime;

public GetFilteredLeaderboardListHandler(
IApplicationDbContext context,
IMapper mapper,
IDateTime dateTime)
{
_context = context;
_mapper = mapper;
_dateTime = dateTime;
}

Expand Down Expand Up @@ -54,23 +51,18 @@ public async Task<LeaderboardViewModel> Handle(GetFilteredLeaderboardList reques
query = query.Where(u => u.UserAchievements.Any(a => start <= a.AwardedAt && a.AwardedAt <= end));
}

var users = await query.Include(u => u.UserAchievements)
var users = await query
.Include(u => u.UserAchievements)
.ThenInclude(ua => ua.Achievement)
.ProjectTo<LeaderboardUserDto>(_mapper.ConfigurationProvider)
.Where(u => !string.IsNullOrWhiteSpace(u.FullName))
.Select(u => new LeaderboardUserDto(u, DateTime.Now.FirstDayOfWeek()))
.ToListAsync(cancellationToken);

var model = new LeaderboardViewModel
{
// need to set rank outside of AutoMapper
Users = users
.Where(u => !string.IsNullOrWhiteSpace(u.Name))
.OrderByDescending(u => u.TotalPoints)
.Select((u, i) =>
{
u.Rank = i + 1;
return u;
}).ToList()
};

users
.OrderByDescending(lud => lud.TotalPoints)
.ForEach((u, i) => u.Rank = i + 1);

var model = new LeaderboardViewModel { Users = users };

return model;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using AutoMapper.QueryableExtensions;
using MoreLinq;
using SSW.Rewards.Shared.DTOs.Leaderboard;
using SSW.Rewards.Application.Common.Extensions;

Expand All @@ -12,16 +12,13 @@ public class GetFilteredLeaderboardListQuery : IRequest<LeaderboardViewModel>
public class GetFilteredLeaderboardListQueryHandler : IRequestHandler<GetFilteredLeaderboardListQuery, LeaderboardViewModel>
{
private readonly IApplicationDbContext _context;
private readonly IMapper _mapper;
private readonly IDateTime _dateTime;

public GetFilteredLeaderboardListQueryHandler(
IApplicationDbContext context,
IMapper mapper,
IDateTime dateTime)
{
_context = context;
_mapper = mapper;
_dateTime = dateTime;
}

Expand Down Expand Up @@ -50,23 +47,18 @@ public async Task<LeaderboardViewModel> Handle(GetFilteredLeaderboardListQuery r
query = query.Where(u => u.UserAchievements.Any(a => start <= a.AwardedAt && a.AwardedAt <= end));
}

var users = await query.Include(u => u.UserAchievements)
var users = await query
.Include(u => u.UserAchievements)
.ThenInclude(ua => ua.Achievement)
.ProjectTo<LeaderboardUserDto>(_mapper.ConfigurationProvider)
.Where(u => !string.IsNullOrWhiteSpace(u.FullName))
.Select(u => new LeaderboardUserDto(u, DateTime.Now.FirstDayOfWeek()))
.ToListAsync(cancellationToken);

users
.OrderByDescending(lud => lud.TotalPoints)
.ForEach((u, i) => u.Rank = i + 1);

var model = new LeaderboardViewModel
{
// need to set rank outside of AutoMapper
Users = users
.Where(u => !string.IsNullOrWhiteSpace(u.Name))
.OrderByDescending(u => u.TotalPoints)
.Select((u, i) =>
{
u.Rank = i + 1;
return u;
}).ToList()
};
var model = new LeaderboardViewModel { Users = users };

return model;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using AutoMapper.QueryableExtensions;
using MoreLinq;
using SSW.Rewards.Application.Common.Extensions;
using SSW.Rewards.Shared.DTOs.Leaderboard;

namespace SSW.Rewards.Application.Leaderboard.Queries.GetLeaderboardList;
Expand All @@ -10,14 +11,10 @@ public class GetLeaderboardListQuery : IRequest<LeaderboardViewModel>

public class Handler : IRequestHandler<GetLeaderboardListQuery, LeaderboardViewModel>
{
private readonly IMapper _mapper;
private readonly IApplicationDbContext _context;

public Handler(
IMapper mapper,
IApplicationDbContext context)
public Handler(IApplicationDbContext context)
{
_mapper = mapper;
_context = context;
}

Expand All @@ -27,21 +24,15 @@ public async Task<LeaderboardViewModel> Handle(GetLeaderboardListQuery request,
.Where(u => u.Activated)
.Include(u => u.UserAchievements)
.ThenInclude(ua => ua.Achievement)
.ProjectTo<LeaderboardUserDto>(_mapper.ConfigurationProvider)
.Where(u => !string.IsNullOrWhiteSpace(u.FullName))
.Select(u => new LeaderboardUserDto(u, DateTime.Now.FirstDayOfWeek()))
.ToListAsync(cancellationToken);

var model = new LeaderboardViewModel
{
// need to set rank outside of AutoMapper
Users = users
.Where(u => !string.IsNullOrWhiteSpace(u.Name))
.OrderByDescending(u => u.TotalPoints)
.Select((u, i) =>
{
u.Rank = i + 1;
return u;
}).ToList()
};
users
.OrderByDescending(lud => lud.TotalPoints)
.ForEach((u, i) => u.Rank = i + 1);

var model = new LeaderboardViewModel { Users = users };

return model;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using AutoMapper.QueryableExtensions;
using MoreLinq;
using SSW.Rewards.Application.Common.Extensions;
using SSW.Rewards.Shared.DTOs.Leaderboard;
using SSW.Rewards.Shared.DTOs.Network;
using SSW.Rewards.Shared.DTOs.Users;
Expand All @@ -11,16 +12,13 @@ public class GetNetworkProfileListHandler : IRequestHandler<GetNetworkProfileLis
{
private readonly IApplicationDbContext _dbContext;
private readonly IUserService _userService;
private readonly IMapper _mapper;

public GetNetworkProfileListHandler(
IApplicationDbContext dbContext,
IUserService userService,
IMapper mapper)
IUserService userService)
{
_dbContext = dbContext;
_userService = userService;
_mapper = mapper;
}

public async Task<NetworkProfileListViewModel> Handle(GetNetworkProfileListQuery request, CancellationToken cancellationToken)
Expand Down Expand Up @@ -54,22 +52,18 @@ public async Task<NetworkProfileListViewModel> Handle(GetNetworkProfileListQuery
.Where(x => (!x.IsDeleted && x.Activated) && x.UserId != user.Id)
.ToListAsync(cancellationToken);

var users = await _dbContext.Users
var leaderboardUserDtos = await _dbContext.Users
.Where(u => u.Activated)
.Include(u => u.UserAchievements)
.ThenInclude(ua => ua.Achievement)
.ProjectTo<LeaderboardUserDto>(_mapper.ConfigurationProvider)
.Where(u => !string.IsNullOrWhiteSpace(u.FullName))
.Select(u => new LeaderboardUserDto(u, DateTime.Now.FirstDayOfWeek()))
.ToListAsync(cancellationToken);


// need to set rank outside of AutoMapper
var leaderboardUserDtos = users
.Where(u => !string.IsNullOrWhiteSpace(u.Name))
.OrderByDescending(u => u.TotalPoints)
.Select((u, i) =>
{
u.Rank = i + 1;
return u;
}).ToList();
leaderboardUserDtos
.OrderByDescending(lud => lud.TotalPoints)
.ForEach((u, i) => u.Rank = i + 1);

profiles.AddRange(staffDtos.Select(profile =>
{
Expand Down
34 changes: 32 additions & 2 deletions src/Common/DTOs/Leaderboard/LeaderboardUserDto.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace SSW.Rewards.Shared.DTOs.Leaderboard;
using SSW.Rewards.Domain.Entities;

namespace SSW.Rewards.Shared.DTOs.Leaderboard;

public class LeaderboardUserDto
{
Expand All @@ -13,5 +15,33 @@ public class LeaderboardUserDto
public int PointsThisWeek { get; set; }
public int PointsThisMonth { get; set; }
public int PointsThisYear { get; set; }
public int Balance { get { return TotalPoints - PointsClaimed; } set { _ = value; } }
public int Balance => TotalPoints - PointsClaimed;

public LeaderboardUserDto(User user, DateTime firstDayOfWeek)
{
var start = firstDayOfWeek;
var end = start.AddDays(7);
Rank = 0; // Ignored
UserId = user.Id;
Name = user.FullName;
Email = user.Email;
ProfilePic = user.Avatar;
TotalPoints = user.UserAchievements.Sum(ua => ua.Achievement.Value);
PointsClaimed = user.UserRewards.Sum(ur => ur.Reward.Cost);
PointsToday = user.UserAchievements
.Where(ua =>
ua.AwardedAt.Year == DateTime.Now.Year &&
ua.AwardedAt.Month == DateTime.UtcNow.Month &&
ua.AwardedAt.Day == DateTime.UtcNow.Day)
.Sum(ua => ua.Achievement.Value);
PointsThisWeek = user.UserAchievements
.Where(ua => start <= ua.AwardedAt && ua.AwardedAt <= end)
.Sum(ua => ua.Achievement.Value);
PointsThisMonth = user.UserAchievements
.Where(ua => ua.AwardedAt.Year == DateTime.Now.Year && ua.AwardedAt.Month == DateTime.UtcNow.Month)
.Sum(ua => ua.Achievement.Value);
PointsThisYear = user.UserAchievements
.Where(ua => ua.AwardedAt.Year == DateTime.Now.Year)
.Sum(ua => ua.Achievement.Value);
}
}
1 change: 1 addition & 0 deletions src/Common/Shared.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Domain\SSW.Rewards.Domain.csproj" />
<ProjectReference Include="..\SSW.Rewards.Enums\SSW.Rewards.Enums.csproj" />
</ItemGroup>

Expand Down
2 changes: 1 addition & 1 deletion src/Domain/Common/BaseEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
public abstract class BaseEntity
{
public int Id { get; set; }
public System.DateTime CreatedUtc { get; set; }
public DateTime CreatedUtc { get; set; }
}
16 changes: 16 additions & 0 deletions src/MobileUI/Common/DateTimeHelpers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace SSW.Rewards.Mobile.Helpers;

public static class DateTimeHelpers
{
public static string GetTimeElapsed(DateTime occurredAt)
{
return (DateTime.UtcNow - occurredAt) switch
{
{ TotalMinutes: < 5 } ts => "Just now",
{ TotalHours: < 1 } ts => $"{ts.Minutes}m ago",
{ TotalDays: < 1 } ts => $"{ts.Hours}h ago",
{ TotalDays: < 31 } ts => $"{ts.Days}d ago",
_ => occurredAt.ToLocalTime().ToString("dd MMMM yyyy"),
};
}
}
3 changes: 2 additions & 1 deletion src/MobileUI/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ public static class Constants
{
#if DEBUG
// public const string ApiBaseUrl = "https://app-sswrewards-api-staging.azurewebsites.net";
public const string ApiBaseUrl = "https://api.rewards.ssw.com.au";
// public const string ApiBaseUrl = "https://api.rewards.ssw.com.au";
public const string ApiBaseUrl = "https://0a88-180-150-47-47.ngrok-free.app";
public const string AppCenterAndroidId = "285df68b-ea1b-4afb-94c3-2581613c6880";
public const string AppCenterIOSId = "71ea37dd-20c5-40ca-9d68-81b743d81337";

Expand Down
14 changes: 1 addition & 13 deletions src/MobileUI/Features/Activity/ActivityPageViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,18 +98,6 @@ private string GetMessage(UserAchievementDto achievement)
action = char.ToUpper(action[0]) + action.Substring(1);
return $"{action} {name}";
}

private static string GetTimeElapsed(DateTime occurredAt)
{
return (DateTime.UtcNow - occurredAt) switch
{
{ TotalMinutes: < 5 } ts => "Just now",
{ TotalHours: < 1 } ts => $"{ts.Minutes}m ago",
{ TotalDays: < 1 } ts => $"{ts.Hours}h ago",
{ TotalDays: < 31 } ts => $"{ts.Days}d ago",
_ => occurredAt.ToLocalTime().ToString("dd MMMM yyyy"),
};
}

private async Task RefreshFeed()
{
Expand Down Expand Up @@ -163,7 +151,7 @@ private async Task<List<ActivityFeedItemDto>> GetFeedData()
? "v2sophie"
: x.UserAvatar;
x.AchievementMessage = GetMessage(x.Achievement);
x.TimeElapsed = GetTimeElapsed(x.AwardedAt);
x.TimeElapsed = DateTimeHelpers.GetTimeElapsed(x.AwardedAt);
return x;
}).ToList();
}
Expand Down
Loading

0 comments on commit fcb4241

Please sign in to comment.