Files
CMS/src/CMSMicroservice.WebApi/Services/NetworkMembershipService.cs
T
masoodafar-web b2d676b555 feat: Implement customer profile and referral queries
- Add GetCustomerProfileResponseDto for retrieving customer profile information.
- Create GetCustomerReferralsQuery and GetCustomerReferralsQueryHandler to fetch customer referrals with pagination and filtering options.
- Introduce GetCustomerReferralsResponseDto to structure the response for customer referrals.
- Implement GetCustomerSettingsQuery and GetCustomerSettingsQueryHandler to retrieve user settings.
- Add GetCustomerOrder and GetCustomerOrderQueryHandler for fetching specific customer orders.
- Create GetCustomerOrderHistoryQuery and GetCustomerOrderHistoryQueryHandler to retrieve order history with filtering options.
- Implement GetCustomerOrdersQuery and GetCustomerOrdersQueryHandler for fetching multiple customer orders with filters.
- Add GetCustomerWalletChangeLogQuery and GetCustomerWalletChangeLogQueryHandler for retrieving wallet change logs.
- Implement GetCustomerWithdrawalSettingsQuery and GetCustomerWithdrawalSettingsQueryHandler for fetching withdrawal settings.
- Create GetCustomerWithdrawalsQuery and GetCustomerWithdrawalsQueryHandler to retrieve customer withdrawal requests.
2026-02-05 23:01:50 +03:30

195 lines
7.4 KiB
C#

using CMSMicroservice.Protobuf.Protos.NetworkMembership;
using CMSMicroservice.WebApi.Common.Services;
using CMSMicroservice.Application.NetworkMembershipCQ.Commands.JoinNetwork;
using CMSMicroservice.Application.NetworkMembershipCQ.Commands.MoveInNetwork;
using CMSMicroservice.Application.NetworkMembershipCQ.Commands.RemoveFromNetwork;
using CMSMicroservice.Application.NetworkMembershipCQ.Queries.GetUserNetworkPosition;
using CMSMicroservice.Application.NetworkMembershipCQ.Queries.GetNetworkTree;
using CMSMicroservice.Application.NetworkMembershipCQ.Queries.GetNetworkMembershipHistory;
using CMSMicroservice.Application.NetworkMembershipCQ.Queries.GetNetworkStatistics;
using CMSMicroservice.Application.NetworkMembershipCQ.Queries.GetMyNetworkTree;
using Mapster;
using CMSMicroservice.Domain.Enums;
namespace CMSMicroservice.WebApi.Services;
public class NetworkMembershipService : NetworkMembershipContract.NetworkMembershipContractBase
{
private readonly IDispatchRequestToCQRS _dispatchRequestToCQRS;
private readonly ISender _sender;
public NetworkMembershipService(
IDispatchRequestToCQRS dispatchRequestToCQRS,
ISender sender)
{
_dispatchRequestToCQRS = dispatchRequestToCQRS;
_sender = sender;
}
public override async Task<Empty> JoinNetwork(JoinNetworkRequest request, ServerCallContext context)
{
return await _dispatchRequestToCQRS.Handle<JoinNetworkRequest, JoinNetworkCommand>(request, context);
}
public override async Task<Empty> ChangeNetworkParent(ChangeNetworkParentRequest request, ServerCallContext context)
{
return await _dispatchRequestToCQRS.Handle<ChangeNetworkParentRequest, MoveInNetworkCommand>(request, context);
}
public override async Task<Empty> RemoveFromNetwork(RemoveFromNetworkRequest request, ServerCallContext context)
{
return await _dispatchRequestToCQRS.Handle<RemoveFromNetworkRequest, RemoveFromNetworkCommand>(request, context);
}
public override async Task<GetUserNetworkResponse> GetUserNetwork(GetUserNetworkRequest request, ServerCallContext context)
{
return await _dispatchRequestToCQRS.Handle<GetUserNetworkRequest, GetUserNetworkPositionQuery, GetUserNetworkResponse>(request, context);
}
public override async Task<GetNetworkTreeResponse> GetNetworkTree(GetNetworkTreeRequest request, ServerCallContext context)
{
return await _dispatchRequestToCQRS.Handle<GetNetworkTreeRequest, GetNetworkTreeQuery, GetNetworkTreeResponse>(request, context);
}
public override async Task<GetNetworkMembershipHistoryResponse> GetNetworkMembershipHistory(GetNetworkMembershipHistoryRequest request, ServerCallContext context)
{
return await _dispatchRequestToCQRS.Handle<GetNetworkMembershipHistoryRequest, GetNetworkMembershipHistoryQuery, GetNetworkMembershipHistoryResponse>(request, context);
}
public override async Task<GetNetworkStatisticsResponse> GetNetworkStatistics(GetNetworkStatisticsRequest request, ServerCallContext context)
{
return await _dispatchRequestToCQRS.Handle<GetNetworkStatisticsRequest, GetNetworkStatisticsQuery, GetNetworkStatisticsResponse>(request, context);
}
// ============= Customer-specific Methods =============
public override async Task<GetMyNetworkTreeResponse> GetMyNetworkTree(GetMyNetworkTreeRequest request, ServerCallContext context)
{
// Use Customer-specific Query that gets UserId from ICurrentUserService
var query = new GetMyNetworkTreeQuery
{
MaxDepth = request.MaxDepth > 0 ? request.MaxDepth : 3
};
var tree = await _sender.Send(query, context.CancellationToken);
if (tree == null)
{
return new GetMyNetworkTreeResponse
{
RootNode = null,
TotalMembers = 0,
CurrentDepth = 0
};
}
// Convert NetworkTreeDto to NetworkTreeNodeModel
var rootNode = ConvertToNodeModel(tree);
return new GetMyNetworkTreeResponse
{
RootNode = rootNode,
TotalMembers = CountNodes(tree),
CurrentDepth = tree.CurrentDepth
};
}
public override async Task<GetMyNetworkTreeResponse> GetSubordinateTree(GetSubordinateTreeRequest request, ServerCallContext context)
{
// Get tree for a specific subordinate user
var query = new GetNetworkTreeQuery
{
UserId = request.TargetUserId,
MaxDepth = request.MaxDepth > 0 ? request.MaxDepth : 3
};
var tree = await _sender.Send(query, context.CancellationToken);
if (tree == null)
{
return new GetMyNetworkTreeResponse
{
RootNode = null,
TotalMembers = 0,
CurrentDepth = 0
};
}
var rootNode = ConvertToNodeModel(tree);
return new GetMyNetworkTreeResponse
{
RootNode = rootNode,
TotalMembers = CountNodes(tree),
CurrentDepth = tree.CurrentDepth
};
}
public override async Task<GetMyNetworkStatisticsResponse> GetMyNetworkStatistics(Google.Protobuf.WellKnownTypes.Empty request, ServerCallContext context)
{
// Get statistics for current user's network
var query = new GetNetworkStatisticsQuery { UserId = 0 }; // Will use ICurrentUserService
var stats = await _sender.Send(query, context.CancellationToken);
return stats.Adapt<GetMyNetworkStatisticsResponse>();
}
// Helper methods for tree conversion
private NetworkTreeNodeModel ConvertToNodeModel(NetworkTreeDto dto)
{
var node = new NetworkTreeNodeModel
{
UserId = dto.UserId,
UserName = $"{dto.FirstName} {dto.LastName}".Trim(),
FullName = $"{dto.FirstName} {dto.LastName}".Trim(),
NetworkLeg = dto.LegPosition.HasValue ? (int)dto.LegPosition.Value : 0,
NetworkLevel = dto.CurrentDepth,
Level = dto.CurrentDepth,
IsActive = dto.IsClubActive,
IsClubActive = dto.IsClubActive,
ReferralCode = dto.ReferralCode ?? "",
Mobile = dto.Mobile ?? "",
Position = dto.LegPosition.HasValue ? dto.LegPosition.Value.ToString() : "Root"
};
if (dto.ClubActivatedAt.HasValue)
{
node.ClubActivatedAt = Google.Protobuf.WellKnownTypes.Timestamp.FromDateTime(
DateTime.SpecifyKind(dto.ClubActivatedAt.Value, DateTimeKind.Utc));
}
if (dto.UserCreated != null)
{
node.UserCreated = Google.Protobuf.WellKnownTypes.Timestamp.FromDateTime(
dto.UserCreated.UtcDateTime);
}
if (dto.ActivationWeekDefinitionId.HasValue)
{
node.ActivationWeekDefinitionId = dto.ActivationWeekDefinitionId.Value;
}
node.IsActivatedInTargetWeek = dto.IsActivatedInTargetWeek;
// Recursively convert children
if (dto.LeftChild != null)
{
node.LeftChild = ConvertToNodeModel(dto.LeftChild);
}
if (dto.RightChild != null)
{
node.RightChild = ConvertToNodeModel(dto.RightChild);
}
return node;
}
private int CountNodes(NetworkTreeDto tree)
{
if (tree == null) return 0;
return 1 + CountNodes(tree.LeftChild) + CountNodes(tree.RightChild);
}
}