Skip to content

Commit

Permalink
1
Browse files Browse the repository at this point in the history
  • Loading branch information
akbaramd committed Dec 13, 2024
1 parent 8c94a4a commit 79dceec
Show file tree
Hide file tree
Showing 43 changed files with 707 additions and 475 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.EntityFrameworkCore;
using Payeh.SharedKernel.EntityFrameworkCore.UnitOfWork;

namespace Payeh.SharedKernel.EntityFrameworkCore.UnitOfWork;
namespace Payeh.SharedKernel.EntityFrameworkCore;

/// <summary>
/// Provides extension methods to register Unit of Work and repositories in the service container.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ public static class RoleConst
public const string EmployerTitle = "Employer";

public const string EngineerTitle = "Engineer";
public static RoleId EngineerRoleId = RoleId.NewId("engineer");
public static RoleId EmployerRoleId = RoleId.NewId("employer");
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ public UserEmailUpdatedEvent(UserId userId, string? newEmail)
public class UserProfileUpdatedEvent : DomainEvent
{
public UserId UserId { get; }
public UserProfileValue NewProfile { get; }
public UserProfileValue? NewProfile { get; }

public UserProfileUpdatedEvent(UserId userId, UserProfileValue newProfile)
public UserProfileUpdatedEvent(UserId userId, UserProfileValue? newProfile)
{
UserId = userId;
NewProfile = newProfile;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public UserNameValue( string value)
throw new ArgumentException("Username must be between 3 and 20 characters.");

if (!Regex.IsMatch(value, @"^[a-zA-Z0-9_-]+$"))
throw new ArgumentException("Username can only contain alphanumeric characters, underscores, and hyphens.");
throw new ArgumentException($"{value} | Username can only contain alphanumeric characters, underscores, and hyphens.");

Value = value;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,7 @@ private string HashPassword(string password)
public static bool IsValidPassword(string password)
{
// Password must be at least 8 characters long
if (password.Length < 8)
return false;

// Must contain at least one number
if (!Regex.IsMatch(password, @"\d"))
return false;



// Must contain at least one uppercase letter
if (!Regex.IsMatch(password, @"[A-Z]"))
return false;

// Must contain at least one lowercase letter
if (!Regex.IsMatch(password, @"[a-z]"))
if (password.Length < 3)
return false;

// If all conditions are met, return true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,25 @@ namespace Nezam.Modular.ESS.Identity.Domain.Shared.User
public class UserProfileValue : ValueObject
{
protected UserProfileValue() { }
public string Avatar { get; private set; }
public string FirstName { get; private set; }
public string LastName { get; private set; }

// Constructor to create a user profile value object
public UserProfileValue(string avatar, string firstName, string lastName)
public UserProfileValue( string firstName, string lastName)
{
if (string.IsNullOrWhiteSpace(firstName))
throw new ArgumentException("First name cannot be null or empty.");

if (string.IsNullOrWhiteSpace(lastName))
throw new ArgumentException("Last name cannot be null or empty.");

// Optionally, validate Avatar (it can be a URL or path to an image)
if (string.IsNullOrWhiteSpace(avatar))
throw new ArgumentException("Avatar cannot be null or empty.");

Avatar = avatar;
FirstName = firstName;
LastName = lastName;
}

protected override IEnumerable<object?> GetEqualityComponents()
{
yield return Avatar;
yield return FirstName;
yield return LastName;
}
Expand All @@ -44,7 +38,6 @@ public void UpdateProfile(string newAvatar, string newFirstName, string newLastN
if (string.IsNullOrWhiteSpace(newAvatar))
throw new ArgumentException("Avatar cannot be null or empty.");

Avatar = newAvatar;
FirstName = newFirstName;
LastName = newLastName;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public async Task Handle(UserProfileUpdatedEvent @event, CancellationToken cance
else
{
_logger.LogInformation("No member found for UserId: {UserId}, creating new member.", @event.UserId);
member = new MemberEntity(MemberId.NewId(), @event.UserId, @event.NewProfile.FirstName, @event.NewProfile.LastName);
member = new MemberEntity(MemberId.NewId(), @event.UserId, @event.NewProfile?.FirstName, @event.NewProfile?.LastName);
await MemberRepository.AddAsync(member, true);
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,20 @@ public class MemberEntity : AggregateRoot
{
public MemberId MemberId { get; private set; }
public UserId UserId { get; private set; }
public string FirstName { get; private set; }
public string LastName { get; private set; }
public string? FirstName { get; private set; }
public string? LastName { get; private set; }

private MemberEntity() { }

public MemberEntity(MemberId memberId, UserId userId, string firstName, string lastName)
public MemberEntity(MemberId memberId, UserId userId, string? firstName, string? lastName)
{
MemberId = memberId ?? throw new ArgumentNullException(nameof(memberId));
UserId = userId ?? throw new ArgumentNullException(nameof(userId));
SetName(firstName, lastName);
}

public void SetName(string firstName, string lastName)
public void SetName(string? firstName, string? lastName)
{
if (string.IsNullOrWhiteSpace(firstName))
throw new ArgumentException("First name cannot be empty.");
if (string.IsNullOrWhiteSpace(lastName))
throw new ArgumentException("Last name cannot be empty.");

FirstName = firstName;
LastName = lastName;
}
Expand Down
2 changes: 2 additions & 0 deletions Nezam.ESS.sln.DotSettings.user
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AFuture_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003FAppData_003FLocal_003FSymbols_003Fsrc_003Fdotnet_003Fruntime_003F08338fcaa5c9b9a8190abb99222fed12aaba956c_003Fsrc_003Flibraries_003FSystem_002EPrivate_002ECoreLib_003Fsrc_003FSystem_003FThreading_003FTasks_003FFuture_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AIBonDbContextProvider_00601_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003F73926448008f4654ba366e96c5f78f0a13200_003F3f_003Fc0aa34cd_003FIBonDbContextProvider_00601_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ALifetimeScope_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003Fd499e58f2ca0abe5f3273d5ea266296015c34f889364c67aae9449be9c9dd31_003FLifetimeScope_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AMonitor_002ECoreCLR_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003F36f346b6c0454bc8a6afa7aed38119fe5bbffcc983298d9bfa4dbd5f49461f_003FMonitor_002ECoreCLR_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ARuntimeType_002ECoreCLR_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003F955ec549fe664629353c3b5424b6ad6c7dfcec4ab59bae709ab962c228cf45_003FRuntimeType_002ECoreCLR_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AServiceProvider_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003Fce37be1a06b16c6faa02038d2cc477dd3bca5b217ceeb41c5f2ad45c1bf9_003FServiceProvider_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AType_002ECoreCLR_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003F394cf11a64687141fd58fb926ff8b3d5a299eba3bbadaf2de9b93928d523d4_003FType_002ECoreCLR_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
Expand All @@ -28,6 +29,7 @@
<s:Boolean x:Key="/Default/ResxEditorPersonal/CheckedGroups/=New_002FModules_002FSecretariat_002FNezam_002EModular_002EESS_002ESecretariat_002EApplciation_002FResources_002FDocumentTranslates/@EntryIndexRemoved">True</s:Boolean>
<s:Boolean x:Key="/Default/ResxEditorPersonal/CheckedGroups/=New_002FModules_002FSecretariat_002FNezam_002EModular_002EESS_002ESecretariat_002EApplication_002FResources_002FDocumentTranslates/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/ResxEditorPersonal/Initialized/@EntryValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UnloadedProject/UnloadedProjects/=769e9fba_002D714c_002D4dcb_002D92b7_002D398348a25308_0023Nezam_002EModular_002EESS_002EIdentity_002EDomain_002ETests/@EntryIndexedValue">True</s:Boolean>



Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
using Dapper;
using Microsoft.Data.SqlClient;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Nezam.Modular.ESS.Identity.Domain.Employer;
using Nezam.Modular.ESS.Identity.Domain.Roles;
using Nezam.Modular.ESS.Identity.Domain.Shared.Roles;
using Nezam.Modular.ESS.Identity.Domain.Shared.User;
using Nezam.Modular.ESS.Identity.Domain.User;
using Payeh.SharedKernel.EntityFrameworkCore.UnitOfWork;

namespace Nezam.Modular.ESS.Identity.Application.Employers.Jobs
{
public class EmployerSynchronizerJob : BackgroundService
{
private const string ConnectionString = "Data Source=85.185.6.4;Initial Catalog=map;User ID=new_site_user;Password=111qqqAAAn;Trust Server Certificate=True;";

private readonly IEmployerRepository _employerRepository;
private readonly IRoleDomainService _roleDomainService;
private readonly IUserDomainService _userDomainService;
private readonly IUnitOfWorkManager _workManager;
private readonly ILogger<EmployerSynchronizerJob> _logger;

public EmployerSynchronizerJob(IEmployerRepository employerRepository, IRoleDomainService roleDomainService, IUserDomainService userDomainService, IUnitOfWorkManager workManager, ILogger<EmployerSynchronizerJob> logger)
{
_employerRepository = employerRepository;
_roleDomainService = roleDomainService;
_userDomainService = userDomainService;
_workManager = workManager;
_logger = logger;
}

protected override async Task ExecuteAsync(CancellationToken cancellationToken = default)
{

using var roleUow = _workManager.Begin();

await _roleDomainService.CreateRoleAsync(new RoleEntity(RoleConst.EmployerRoleId, RoleConst.EmployerTitle));
await using var connection = new SqlConnection(ConnectionString);
var employers = (await connection.QueryAsync<KarbaranDataDto>(
@"SELECT
tbl_karbaran.kname AS UserName,
tbl_karbaran.pwd AS Password,
tbl_karbaran.comment AS FullName
FROM tbl_karbaran
WHERE is_personel = 1")).ToList();
await roleUow.CommitAsync();

int processedCount = 0;
var totalEmployers = employers.Count;

foreach (var employerDto in employers)
{
if (string.IsNullOrEmpty(employerDto.UserName))
{
_logger.LogWarning("Employer entry with a null or empty username is skipped.");
continue;
}

try
{ using var uow = _workManager.Begin();

var username = new UserNameValue(employerDto.UserName);
var user = await _userDomainService.GetUserByUsernameAsync(username);
var userId = user.IsSuccess ? user.Data.UserId : UserId.NewId();

var existingUser = await _employerRepository.FindOneAsync(x=>x.UserName.Value == username.Value);
EmployerEntity userEntity;

if (existingUser == null)
{
// Create a new UserEntity
string password = !string.IsNullOrEmpty(employerDto.Password) && employerDto.Password.Length >= 3
? employerDto.Password
: employerDto.UserName;
userEntity = new EmployerEntity( userId,username,new UserPasswordValue(password));
await _employerRepository.AddAsync(userEntity,true);
await _userDomainService.AssignRoleAsync(userEntity, [RoleConst.EmployerRoleId]);
}
processedCount++;
_logger.LogInformation("Processed {ProcessedCount}/{TotalEmployers} employers.", processedCount, totalEmployers);
await uow.CommitAsync();
}
catch (Exception ex)
{
_logger.LogError(ex, "An error occurred while processing employer with username {Username}.", employerDto.UserName);
}
}



_logger.LogInformation("Employer synchronization completed. Total processed: {TotalCount}", processedCount);
}
}

public class KarbaranDataDto
{
public string? UserName { get; set; }
public string? Password { get; set; }
public string? FullName { get; set; }
}
}
Loading

0 comments on commit 79dceec

Please sign in to comment.