Spaces:
Sleeping
Sleeping
File size: 5,953 Bytes
da13799 068485b 1a7e978 4433399 068485b da13799 068485b da13799 ccc2569 068485b da13799 068485b 1a7e978 068485b da13799 068485b 1a7e978 068485b da13799 068485b 1a7e978 c312181 1a7e978 068485b da13799 068485b da13799 068485b da13799 068485b ccc2569 068485b 0cba109 068485b 0cba109 068485b | 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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 | using DbConnect.Data;
using DbConnect.Entities;
using Microsoft.EntityFrameworkCore;
using Backend.Features.Subscriptions;
using Backend.Features.Loyalty;
using LibraryManagement.Shared.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Backend.Features.Users
{
public interface IUserService
{
Task<IEnumerable<UserDto>> GetAllUsersAsync();
Task<UserDto?> GetUserByIdAsync(Guid id);
Task<UserDto> CreateUserAsync(UserCreateRequest request);
Task<UserDto?> UpdateUserAsync(Guid id, UserUpdateRequest request);
Task<bool> UpdateUserRoleAsync(Guid id, string role);
Task<bool> DeleteUserAsync(Guid id);
Task<bool> UpdateFcmTokenAsync(Guid id, string fcmToken);
}
public class UserService : IUserService
{
private readonly AppDbContext _context;
private readonly ISubscriptionService _subscriptionService;
private readonly ILoyaltyService _loyaltyService;
public UserService(AppDbContext context, ISubscriptionService subscriptionService, ILoyaltyService loyaltyService)
{
_context = context;
_subscriptionService = subscriptionService;
_loyaltyService = loyaltyService;
}
public async Task<IEnumerable<UserDto>> GetAllUsersAsync()
{
var users = await _context.Users.ToListAsync();
return users.Select(MapToDto);
}
public async Task<UserDto?> GetUserByIdAsync(Guid id)
{
var user = await _context.Users.FindAsync(id);
return user == null ? null : MapToDto(user);
}
public async Task<UserDto> CreateUserAsync(UserCreateRequest request)
{
if (await _context.Users.AnyAsync(u => u.Email == request.Email))
{
throw new Exception("Email already registered.");
}
var user = new User
{
FullName = request.FullName,
Email = request.Email,
PasswordHash = BCrypt.Net.BCrypt.HashPassword(request.Password),
PhoneNumber = request.PhoneNumber,
Address = request.Address,
StudentId = request.StudentId,
Role = "Member", // Default role
IsActive = true,
CreatedAt = DateTime.UtcNow
};
_context.Users.Add(user);
await _context.SaveChangesAsync();
if (!string.IsNullOrEmpty(user.StudentId))
{
await _subscriptionService.SubscribeUserAsync(user.Id, 3); // 3 is "Basic Yearly"
}
// Loyalty Integration: Register the new user and process SIGNUP event
string externalUserId = user.Id.ToString();
string userMobile = user.PhoneNumber ?? "0000000000";
await _loyaltyService.RegisterUserAsync(externalUserId, user.Email, userMobile);
await _loyaltyService.ProcessEventAsync(
externalUserId: externalUserId,
eventKey: "SIGNUP",
eventValue: 20,
referenceId: $"USR-{user.Id}",
description: "New User Registration",
email: user.Email,
mobile: userMobile
);
return MapToDto(user);
}
public async Task<UserDto?> UpdateUserAsync(Guid id, UserUpdateRequest request)
{
var user = await _context.Users.FindAsync(id);
if (user == null) return null;
bool isNewlyStudent = string.IsNullOrEmpty(user.StudentId) && !string.IsNullOrEmpty(request.StudentId);
user.FullName = request.FullName;
user.PhoneNumber = request.PhoneNumber;
user.Address = request.Address;
user.StudentId = request.StudentId;
user.UpdatedAt = DateTime.UtcNow;
await _context.SaveChangesAsync();
if (isNewlyStudent)
{
await _subscriptionService.SubscribeUserAsync(user.Id, 3);
}
return MapToDto(user);
}
public async Task<bool> UpdateUserRoleAsync(Guid id, string role)
{
var user = await _context.Users.FindAsync(id);
if (user == null) return false;
user.Role = role;
user.UpdatedAt = DateTime.UtcNow;
await _context.SaveChangesAsync();
return true;
}
public async Task<bool> DeleteUserAsync(Guid id)
{
var user = await _context.Users.FindAsync(id);
if (user == null) return false;
// Soft delete
user.IsActive = false;
user.UpdatedAt = DateTime.UtcNow;
await _context.SaveChangesAsync();
return true;
}
public async Task<bool> UpdateFcmTokenAsync(Guid id, string fcmToken)
{
var user = await _context.Users.FindAsync(id);
if (user == null) return false;
user.FcmToken = fcmToken;
user.UpdatedAt = DateTime.UtcNow;
await _context.SaveChangesAsync();
return true;
}
private static UserDto MapToDto(User user)
{
return new UserDto
{
Id = user.Id,
FullName = user.FullName,
Email = user.Email,
PhoneNumber = user.PhoneNumber,
Role = user.Role,
IsActive = user.IsActive,
StudentId = user.StudentId,
Address = user.Address,
CreatedAt = user.CreatedAt,
UpdatedAt = user.UpdatedAt,
BanStatus = user.BanStatus,
SuspensionEndDate = user.SuspensionEndDate,
Balance = user.Balance
};
}
}
}
|