252 lines
		
	
	
		
			14 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			252 lines
		
	
	
		
			14 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
using Marco.Pms.DataAccess.Data;
 | 
						|
using Marco.Pms.Model.Directory;
 | 
						|
using Marco.Pms.Model.Dtos.Master;
 | 
						|
using Marco.Pms.Model.Employees;
 | 
						|
using Marco.Pms.Model.Mapper;
 | 
						|
using Marco.Pms.Model.Utilities;
 | 
						|
using Marco.Pms.Model.ViewModels.Master;
 | 
						|
using MarcoBMS.Services.Helpers;
 | 
						|
using MarcoBMS.Services.Service;
 | 
						|
using Microsoft.EntityFrameworkCore;
 | 
						|
 | 
						|
namespace Marco.Pms.Services.Helpers
 | 
						|
{
 | 
						|
    public class MasterHelper
 | 
						|
    {
 | 
						|
        private readonly ApplicationDbContext _context;
 | 
						|
        private readonly ILoggingService _logger;
 | 
						|
        private readonly UserHelper _userHelper;
 | 
						|
 | 
						|
 | 
						|
        public MasterHelper(ApplicationDbContext context, ILoggingService logger, UserHelper userHelper)
 | 
						|
        {
 | 
						|
            _context = context;
 | 
						|
            _logger = logger;
 | 
						|
            _userHelper = userHelper;
 | 
						|
        }
 | 
						|
        // -------------------------------- Contact Category  --------------------------------
 | 
						|
        public async Task<ApiResponse<object>> CreateContactCategory(CreateContactCategoryDto contactCategoryDto)
 | 
						|
        {
 | 
						|
            Guid tenantId = _userHelper.GetTenantId();
 | 
						|
            var LoggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
 | 
						|
            if (contactCategoryDto != null)
 | 
						|
            {
 | 
						|
                ContactCategoryMaster? existingContactCategory = await _context.ContactCategoryMasters.FirstOrDefaultAsync(c => c.TenantId == tenantId && c.Name.ToLower() == (contactCategoryDto.Name != null ? contactCategoryDto.Name.ToLower() : ""));
 | 
						|
                if (existingContactCategory == null)
 | 
						|
                {
 | 
						|
                    ContactCategoryMaster contactCategory = contactCategoryDto.ToContactCategoryMasterFromCreateContactCategoryDto(tenantId);
 | 
						|
                    _context.ContactCategoryMasters.Add(contactCategory);
 | 
						|
                    await _context.SaveChangesAsync();
 | 
						|
                    ContactCategoryVM categoryVM = contactCategory.ToContactCategoryVMFromContactCategoryMaster();
 | 
						|
 | 
						|
                    _logger.LogInfo("Employee ID {LoggedInEmployeeId} created a contact category {ContactCategoryId}.", LoggedInEmployee.Id, contactCategory.Id);
 | 
						|
                    return ApiResponse<object>.SuccessResponse(categoryVM, "Category Created Successfully", 200);
 | 
						|
                }
 | 
						|
                _logger.LogWarning("Employee ID {LoggedInEmployeeId} attempted to create an existing contact category.", LoggedInEmployee.Id);
 | 
						|
                return ApiResponse<object>.ErrorResponse("Category already existed", "Category already existed", 409);
 | 
						|
            }
 | 
						|
            _logger.LogWarning("Employee with ID {LoggedInEmployeeId} sended empty payload", LoggedInEmployee.Id);
 | 
						|
            return ApiResponse<object>.ErrorResponse("User Send empty Payload", "User Send empty Payload", 400);
 | 
						|
        }
 | 
						|
        public async Task<ApiResponse<object>> UpdateContactCategory(Guid id, UpdateContactCategoryDto contactCategoryDto)
 | 
						|
        {
 | 
						|
            Guid tenantId = _userHelper.GetTenantId();
 | 
						|
            var LoggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
 | 
						|
            if (contactCategoryDto != null && id == contactCategoryDto.Id)
 | 
						|
            {
 | 
						|
                ContactCategoryMaster? contactCategory = await _context.ContactCategoryMasters.FirstOrDefaultAsync(c => c.TenantId == tenantId && c.Id == id);
 | 
						|
                if (contactCategory != null)
 | 
						|
                {
 | 
						|
                    contactCategory.Name = contactCategoryDto.Name ?? "";
 | 
						|
                    contactCategory.Description = contactCategoryDto.Description ?? "";
 | 
						|
 | 
						|
                    _context.DirectoryUpdateLogs.Add(new DirectoryUpdateLog
 | 
						|
                    {
 | 
						|
                        RefereanceId = contactCategory.Id,
 | 
						|
                        UpdatedById = LoggedInEmployee.Id,
 | 
						|
                        UpdateAt = DateTime.UtcNow
 | 
						|
                    });
 | 
						|
 | 
						|
                    await _context.SaveChangesAsync();
 | 
						|
                    ContactCategoryVM categoryVM = contactCategory.ToContactCategoryVMFromContactCategoryMaster();
 | 
						|
 | 
						|
                    _logger.LogInfo("Employee ID {LoggedInEmployeeId} created a contact category {ContactCategoryId}.", LoggedInEmployee.Id, contactCategory.Id);
 | 
						|
                    return ApiResponse<object>.SuccessResponse(categoryVM, "Category Created Successfully", 200);
 | 
						|
                }
 | 
						|
                _logger.LogWarning("Employee ID {LoggedInEmployeeId} attempted to update a contact category but not found in database.", LoggedInEmployee.Id);
 | 
						|
                return ApiResponse<object>.ErrorResponse("Category not found", "Category not found", 404);
 | 
						|
            }
 | 
						|
            _logger.LogWarning("Employee with ID {LoggedInEmployeeId} sended empty payload", LoggedInEmployee.Id);
 | 
						|
            return ApiResponse<object>.ErrorResponse("User Send empty Payload", "User Send empty Payload", 400);
 | 
						|
        }
 | 
						|
        public async Task<ApiResponse<object>> GetContactCategoriesList()
 | 
						|
        {
 | 
						|
            Guid tenantId = _userHelper.GetTenantId();
 | 
						|
            var LoggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
 | 
						|
 | 
						|
            var categoryList = await _context.ContactCategoryMasters.Where(c => c.TenantId == tenantId).ToListAsync();
 | 
						|
            List<ContactCategoryVM> contactCategories = new List<ContactCategoryVM>();
 | 
						|
            foreach (var category in categoryList)
 | 
						|
            {
 | 
						|
                ContactCategoryVM categoryVM = category.ToContactCategoryVMFromContactCategoryMaster();
 | 
						|
                contactCategories.Add(categoryVM);
 | 
						|
            }
 | 
						|
            _logger.LogInfo("{count} contact categoires are fetched by Employee with ID {LoggedInEmployeeId}", contactCategories.Count, LoggedInEmployee.Id);
 | 
						|
            return ApiResponse<object>.SuccessResponse(contactCategories, System.String.Format("{0} contact categories fetched successfully", contactCategories.Count), 200);
 | 
						|
        }
 | 
						|
        public async Task<ApiResponse<object>> GetContactCategoryById(Guid id)
 | 
						|
        {
 | 
						|
            Guid tenantId = _userHelper.GetTenantId();
 | 
						|
            var LoggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
 | 
						|
 | 
						|
            var category = await _context.ContactCategoryMasters.FirstOrDefaultAsync(c => c.Id == id && c.TenantId == tenantId);
 | 
						|
            if (category != null)
 | 
						|
            {
 | 
						|
                ContactCategoryVM categoryVM = category.ToContactCategoryVMFromContactCategoryMaster();
 | 
						|
                _logger.LogInfo("Employee {EmployeeId} fetched contact category {ContactCategoryID}", LoggedInEmployee.Id, category.Id);
 | 
						|
                return ApiResponse<object>.SuccessResponse(categoryVM, "Category fetched successfully", 200);
 | 
						|
            }
 | 
						|
 | 
						|
            _logger.LogWarning("Employee {EmployeeId} attempted to fetch contact category {ContactCategoryID} but not found in database", LoggedInEmployee.Id, id);
 | 
						|
            return ApiResponse<object>.ErrorResponse("Category not found", "Category not found", 404);
 | 
						|
        }
 | 
						|
        public async Task<ApiResponse<object>> DeleteContactCategory(Guid id)
 | 
						|
        {
 | 
						|
            Guid tenantId = _userHelper.GetTenantId();
 | 
						|
            var LoggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
 | 
						|
            ContactCategoryMaster? contactCategory = await _context.ContactCategoryMasters.FirstOrDefaultAsync(c => c.Id == id && c.TenantId == tenantId);
 | 
						|
            if (contactCategory != null)
 | 
						|
            {
 | 
						|
                List<Contact>? existingContacts = await _context.Contacts.AsNoTracking().Where(c => c.ContactCategoryId == contactCategory.Id).ToListAsync();
 | 
						|
                if (existingContacts.Count > 0)
 | 
						|
                {
 | 
						|
                    List<Contact>? contacts = new List<Contact>();
 | 
						|
                    foreach (var contact in existingContacts)
 | 
						|
                    {
 | 
						|
                        contact.ContactCategoryId = null;
 | 
						|
                        contacts.Add(contact);
 | 
						|
                    }
 | 
						|
                    _context.Contacts.UpdateRange(contacts);
 | 
						|
                }
 | 
						|
                _context.ContactCategoryMasters.Remove(contactCategory);
 | 
						|
 | 
						|
                _context.DirectoryUpdateLogs.Add(new DirectoryUpdateLog
 | 
						|
                {
 | 
						|
                    RefereanceId = id,
 | 
						|
                    UpdatedById = LoggedInEmployee.Id,
 | 
						|
                    UpdateAt = DateTime.UtcNow
 | 
						|
                });
 | 
						|
                await _context.SaveChangesAsync();
 | 
						|
                _logger.LogInfo("Employee {EmployeeId} deleted contact category {ContactCategoryId}", LoggedInEmployee.Id, id);
 | 
						|
            }
 | 
						|
 | 
						|
            _logger.LogWarning("Employee {EmployeeId} tries to delete Category {CategoryId} but not found in database", LoggedInEmployee.Id, id);
 | 
						|
            return ApiResponse<object>.SuccessResponse(new { }, "Category deleted successfully", 200);
 | 
						|
        }
 | 
						|
 | 
						|
        // -------------------------------- Contact Tag  --------------------------------
 | 
						|
 | 
						|
 | 
						|
        public async Task<ApiResponse<Object>> GetContactTags()
 | 
						|
        {
 | 
						|
            Guid tenantId = _userHelper.GetTenantId();
 | 
						|
 | 
						|
            var LoggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
 | 
						|
 | 
						|
            var taglist = await _context.ContactTagMasters.Where(t => t.TenantId == tenantId).ToListAsync();
 | 
						|
            List<ContactTagVM> contactTags = new List<ContactTagVM>();
 | 
						|
            foreach (var tag in taglist)
 | 
						|
            {
 | 
						|
                ContactTagVM tagVm = tag.ToContactTagVMFromContactTagMaster();
 | 
						|
                contactTags.Add(tagVm);
 | 
						|
            }
 | 
						|
            _logger.LogInfo("{count} contact Tags are fetched by Employee with ID {LoggedInEmployeeId}", contactTags.Count, LoggedInEmployee.Id);
 | 
						|
            return ApiResponse<object>.SuccessResponse(contactTags, System.String.Format("{0} contact tags fetched successfully", contactTags.Count), 200);
 | 
						|
        }
 | 
						|
        public async Task<ApiResponse<object>> CreateContactTag(CreateContactTagDto contactTagDto)
 | 
						|
        {
 | 
						|
            Guid tenantId = _userHelper.GetTenantId();
 | 
						|
            var LoggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
 | 
						|
            if (contactTagDto != null)
 | 
						|
            {
 | 
						|
                ContactTagMaster? existingContactTag = await _context.ContactTagMasters.FirstOrDefaultAsync(c => c.TenantId == tenantId && c.Name.ToLower() == (contactTagDto.Name != null ? contactTagDto.Name.ToLower() : ""));
 | 
						|
                if (existingContactTag == null)
 | 
						|
                {
 | 
						|
                    ContactTagMaster contactTag = contactTagDto.ToContactTagMasterFromCreateContactTagDto(tenantId);
 | 
						|
                    _context.ContactTagMasters.Add(contactTag);
 | 
						|
                    await _context.SaveChangesAsync();
 | 
						|
                    ContactTagVM tagVM = contactTag.ToContactTagVMFromContactTagMaster();
 | 
						|
 | 
						|
                    _logger.LogInfo("Employee ID {LoggedInEmployeeId} created a contact tag {ContactTagId}.", LoggedInEmployee.Id, contactTag.Id);
 | 
						|
                    return ApiResponse<object>.SuccessResponse(tagVM, "Tag Created Successfully", 200);
 | 
						|
                }
 | 
						|
                _logger.LogWarning("Employee ID {LoggedInEmployeeId} attempted to create an existing contact tag.", LoggedInEmployee.Id);
 | 
						|
                return ApiResponse<object>.ErrorResponse("Tag already existed", "Tag already existed", 409);
 | 
						|
            }
 | 
						|
            _logger.LogWarning("Employee with ID {LoggedInEmployeeId} sended empty payload", LoggedInEmployee.Id);
 | 
						|
            return ApiResponse<object>.ErrorResponse("User Send empty Payload", "User Send empty Payload", 400);
 | 
						|
        }
 | 
						|
        public async Task<ApiResponse<object>> UpdateContactTag(Guid id, UpdateContactTagDto contactTagDto)
 | 
						|
        {
 | 
						|
            var tenantId = _userHelper.GetTenantId();
 | 
						|
            Employee LoggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
 | 
						|
            if (contactTagDto != null && contactTagDto.Id == id)
 | 
						|
            {
 | 
						|
                ContactTagMaster? contactTag = await _context.ContactTagMasters.AsNoTracking().FirstOrDefaultAsync(t => t.TenantId == tenantId && t.Id == contactTagDto.Id);
 | 
						|
                if (contactTag != null)
 | 
						|
                {
 | 
						|
                    contactTag = contactTagDto.ToContactTagMasterFromUpdateContactTagDto(tenantId);
 | 
						|
                    _context.ContactTagMasters.Update(contactTag);
 | 
						|
 | 
						|
                    _context.DirectoryUpdateLogs.Add(new DirectoryUpdateLog
 | 
						|
                    {
 | 
						|
                        RefereanceId = contactTag.Id,
 | 
						|
                        UpdatedById = LoggedInEmployee.Id,
 | 
						|
                        UpdateAt = DateTime.UtcNow
 | 
						|
                    });
 | 
						|
                    await _context.SaveChangesAsync();
 | 
						|
 | 
						|
                    ContactTagVM contactTagVm = contactTag.ToContactTagVMFromContactTagMaster();
 | 
						|
 | 
						|
 | 
						|
 | 
						|
                    _logger.LogInfo("Work category master {ConatctTagId} updated successfully by employee {EmployeeId}", contactTagVm.Id, LoggedInEmployee.Id);
 | 
						|
                    ApiResponse<object>.SuccessResponse(contactTagVm, "Contact Tag master updated successfully", 200);
 | 
						|
                }
 | 
						|
                _logger.LogError("Contact Tag master {ContactTagId} not found in database", id);
 | 
						|
                ApiResponse<object>.ErrorResponse("Contact Tag master not found", "Contact tag master not found", 404);
 | 
						|
            }
 | 
						|
            _logger.LogWarning("Employee with ID {LoggedInEmployeeId} sended empty payload", LoggedInEmployee.Id);
 | 
						|
            return ApiResponse<object>.ErrorResponse("User Send empty Payload", "User Send empty Payload", 400);
 | 
						|
        }
 | 
						|
        public async Task<ApiResponse<object>> DeleteContactTag(Guid id)
 | 
						|
        {
 | 
						|
            Guid tenantId = _userHelper.GetTenantId();
 | 
						|
            var LoggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
 | 
						|
            ContactTagMaster? contactTag = await _context.ContactTagMasters.FirstOrDefaultAsync(c => c.Id == id && c.TenantId == tenantId);
 | 
						|
            if (contactTag != null)
 | 
						|
            {
 | 
						|
                List<ContactTagMapping>? tagMappings = await _context.ContactTagMappings.Where(t => t.ContactTagId == contactTag.Id).ToListAsync();
 | 
						|
 | 
						|
                _context.ContactTagMasters.Remove(contactTag);
 | 
						|
                if (tagMappings.Any())
 | 
						|
                {
 | 
						|
                    _context.ContactTagMappings.RemoveRange(tagMappings);
 | 
						|
                }
 | 
						|
                _context.DirectoryUpdateLogs.Add(new DirectoryUpdateLog
 | 
						|
                {
 | 
						|
                    RefereanceId = id,
 | 
						|
                    UpdatedById = LoggedInEmployee.Id,
 | 
						|
                    UpdateAt = DateTime.UtcNow
 | 
						|
                });
 | 
						|
                await _context.SaveChangesAsync();
 | 
						|
                _logger.LogInfo("Employee {EmployeeId} deleted contact tag {ContactTagId}", LoggedInEmployee.Id, id);
 | 
						|
            }
 | 
						|
 | 
						|
            _logger.LogWarning("Employee {EmployeeId} tries to delete Tag {ContactTagId} but not found in database", LoggedInEmployee.Id, id);
 | 
						|
            return ApiResponse<object>.SuccessResponse(new { }, "Tag deleted successfully", 200);
 | 
						|
        }
 | 
						|
 | 
						|
    }
 | 
						|
}
 |