mirror of
https://github.com/Sea-Haven-Industries/shoc-backend.git
synced 2026-10-01 23:13:22 +00:00
466 lines
19 KiB
C#
466 lines
19 KiB
C#
|
|
|
|||
|
|
using Data.SeaHavenIndustries;
|
|||
|
|
using Microsoft.AspNetCore.Components.Forms;
|
|||
|
|
using Microsoft.AspNetCore.Identity;
|
|||
|
|
using Microsoft.EntityFrameworkCore;
|
|||
|
|
using SeaHavenIndustries.Data.IRepository;
|
|||
|
|
using SeaHavenIndustries.Helper;
|
|||
|
|
using SeaHavenIndustries.Helper.ViewModels;
|
|||
|
|
using SeaHavenIndustries.ViewModel;
|
|||
|
|
using System.Security.Claims;
|
|||
|
|
|
|||
|
|
namespace SeaHavenIndustries.Data.Services
|
|||
|
|
{
|
|||
|
|
public class SettingsService : ISettingsRepository
|
|||
|
|
{
|
|||
|
|
private readonly ApplicationDbContext _context;
|
|||
|
|
private readonly IHttpContextAccessor _httpContextAccessor;
|
|||
|
|
private UserManager<ApplicationUser> _userManager;
|
|||
|
|
private readonly UploadFileHp _uploadFileHelper;
|
|||
|
|
private readonly RoleManager<IdentityRole> _roleManager;
|
|||
|
|
|
|||
|
|
public SettingsService(ApplicationDbContext context, IHttpContextAccessor httpContextAccessor, UserManager<ApplicationUser> userManager, UploadFileHp uploadFileHelper, RoleManager<IdentityRole> roleManager)
|
|||
|
|
{
|
|||
|
|
_context = context;
|
|||
|
|
_httpContextAccessor = httpContextAccessor;
|
|||
|
|
_userManager = userManager;
|
|||
|
|
_uploadFileHelper = uploadFileHelper;
|
|||
|
|
_roleManager = roleManager;
|
|||
|
|
}
|
|||
|
|
public async Task<ApplicationUser> GetCurrentUserInfo()
|
|||
|
|
{
|
|||
|
|
var userId = _httpContextAccessor?.HttpContext?.User?.FindFirst(ClaimTypes.NameIdentifier)?.Value ?? "";
|
|||
|
|
var data = await _context.Users.Where(w => w.Id == userId).FirstOrDefaultAsync();
|
|||
|
|
return data ?? new ApplicationUser();
|
|||
|
|
}
|
|||
|
|
public async Task<string> UpdateUser(ApplicationUser model, IBrowserFile file)
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
var userId = _httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value ?? "";
|
|||
|
|
var exist = await _context.Users.Where(w => w.Id == userId).FirstOrDefaultAsync();
|
|||
|
|
if (exist != null)
|
|||
|
|
{
|
|||
|
|
exist.FirstName = model.FirstName;
|
|||
|
|
exist.LastName = model.LastName;
|
|||
|
|
exist.Contact = model.Contact;
|
|||
|
|
exist.Locations = model.Locations;
|
|||
|
|
|
|||
|
|
if (file != null)
|
|||
|
|
{
|
|||
|
|
var result = await _uploadFileHelper.UploadFiles(file, null);
|
|||
|
|
if (result.StatusCode == 200)
|
|||
|
|
{
|
|||
|
|
exist.UserImage = result.Domain;
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
return result.Message;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
_context.Update(exist);
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
await _context.SaveChangesAsync();
|
|||
|
|
return "success";
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
return ex.Message;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
#region Category
|
|||
|
|
public async Task<string> AddCategory(Category category)
|
|||
|
|
{
|
|||
|
|
_context.ChangeTracker.Clear();
|
|||
|
|
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
var exist = _context.Categories.Where(w => w.Id == category.Id).FirstOrDefault();
|
|||
|
|
if (exist != null)
|
|||
|
|
{
|
|||
|
|
exist.Name = category.Name;
|
|||
|
|
_context.Update(exist);
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
_context.Categories.Add(category);
|
|||
|
|
}
|
|||
|
|
await _context.SaveChangesAsync();
|
|||
|
|
return "success";
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
return ex.Message;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
public async Task<List<Category>> GetAllCategories()
|
|||
|
|
{
|
|||
|
|
var list = await _context.Categories.ToListAsync();
|
|||
|
|
return list;
|
|||
|
|
}
|
|||
|
|
public async Task<Category> GetCategorById(int id)
|
|||
|
|
{
|
|||
|
|
var data = await _context.Categories.Where(w => w.Id == id).FirstOrDefaultAsync();
|
|||
|
|
return data ?? new Category();
|
|||
|
|
}
|
|||
|
|
public async Task<string> DeleteCategory(int categoryId)
|
|||
|
|
{
|
|||
|
|
_context.ChangeTracker.Clear();
|
|||
|
|
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
var data = await _context.Categories.Where(w => w.Id == categoryId).FirstOrDefaultAsync();
|
|||
|
|
if (data != null)
|
|||
|
|
{
|
|||
|
|
_context.Categories.Remove(data);
|
|||
|
|
}
|
|||
|
|
await _context.SaveChangesAsync();
|
|||
|
|
return "success";
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
return ex.Message;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
#endregion
|
|||
|
|
#region Template
|
|||
|
|
public async Task<string> AddTemplate(Template model, List<FileUploadViewModel> files)
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
var exist = _context.Templates.Where(w => w.Id == model.Id).FirstOrDefault();
|
|||
|
|
if (exist == null)
|
|||
|
|
{
|
|||
|
|
if (files != null && files.Count > 0)
|
|||
|
|
{
|
|||
|
|
foreach (var file in files)
|
|||
|
|
{
|
|||
|
|
if (file.file != null)
|
|||
|
|
{
|
|||
|
|
var result = await _uploadFileHelper.UploadFiles(file.file, null);
|
|||
|
|
|
|||
|
|
if (result.StatusCode == 200)
|
|||
|
|
{
|
|||
|
|
if (file.Name == "attachment1")
|
|||
|
|
{
|
|||
|
|
model.Attachments = result.Domain;
|
|||
|
|
}
|
|||
|
|
else if (file.Name == "attachment2")
|
|||
|
|
{
|
|||
|
|
model.BeforPhotoAttachment = result.Domain;
|
|||
|
|
}
|
|||
|
|
else if (file.Name == "attachment3")
|
|||
|
|
{
|
|||
|
|
model.AfterPhotoAttachment = result.Domain;
|
|||
|
|
}
|
|||
|
|
else if (file.Name == "attachment4")
|
|||
|
|
{
|
|||
|
|
model.SignOffAttachment = result.Domain;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
return result.Message;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
model.Status = "Open";
|
|||
|
|
_context.Templates.Add(model);
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
exist.WorkerOrderNumber = model.WorkerOrderNumber;
|
|||
|
|
exist.WorkerOrderTitle = model.WorkerOrderTitle;
|
|||
|
|
exist.Description = model.Description;
|
|||
|
|
exist.PO = model.PO;
|
|||
|
|
exist.TT = model.TT;
|
|||
|
|
exist.POC = model.POC;
|
|||
|
|
exist.AssignTo = model.AssignTo;
|
|||
|
|
exist.DueDate = model.DueDate;
|
|||
|
|
exist.Priority = model.Priority;
|
|||
|
|
exist.LocationId = model.LocationId;
|
|||
|
|
exist.CategoryId = model.CategoryId;
|
|||
|
|
exist.LocationId = model.LocationId;
|
|||
|
|
//
|
|||
|
|
exist.BeforPhotoIssue = model.BeforPhotoIssue;
|
|||
|
|
exist.AfterPhotoIssue = model.AfterPhotoIssue;
|
|||
|
|
exist.SignOffName = model.SignOffName;
|
|||
|
|
exist.SignOffSignature = model.SignOffSignature;
|
|||
|
|
if (files != null && files.Count > 0)
|
|||
|
|
{
|
|||
|
|
foreach (var file in files)
|
|||
|
|
{
|
|||
|
|
if (file.Name == "attachment1")
|
|||
|
|
{
|
|||
|
|
var result = await _uploadFileHelper.UploadFiles(file.file, exist.Attachments);
|
|||
|
|
if (result.StatusCode == 200)
|
|||
|
|
{
|
|||
|
|
model.Attachments = result.Domain;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
else if (file.Name == "attachment2")
|
|||
|
|
{
|
|||
|
|
var result = await _uploadFileHelper.UploadFiles(file.file, exist.BeforPhotoAttachment);
|
|||
|
|
if (result.StatusCode == 200)
|
|||
|
|
{
|
|||
|
|
model.Attachments = result.Domain;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
else if (file.Name == "attachment3")
|
|||
|
|
{
|
|||
|
|
var result = await _uploadFileHelper.UploadFiles(file.file, exist.AfterPhotoAttachment);
|
|||
|
|
if (result.StatusCode == 200)
|
|||
|
|
{
|
|||
|
|
model.Attachments = result.Domain;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
else if (file.Name == "attachment4")
|
|||
|
|
{
|
|||
|
|
var result = await _uploadFileHelper.UploadFiles(file.file, exist.SignOffAttachment);
|
|||
|
|
if (result.StatusCode == 200)
|
|||
|
|
{
|
|||
|
|
model.Attachments = result.Domain;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
await _context.SaveChangesAsync();
|
|||
|
|
return "success";
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
return ex.Message;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
public async Task<List<WorkOrder>> GetTemplates()
|
|||
|
|
{
|
|||
|
|
var data = await _context.workOrders.Where(w => w.istemplate == true).Include(w => w.Locations).ToListAsync();
|
|||
|
|
return data;
|
|||
|
|
}
|
|||
|
|
public async Task<WorkOrder> ViewTemplate(int id)
|
|||
|
|
{
|
|||
|
|
var exist = await _context.workOrders.AsSplitQuery().Where(w => w.Id == id).Include(i=>i.workOrderAttachments).Include(i=>i.WorkOrderCategories).ThenInclude(i=>i.Category).Include(i=>i.WorkOrderContacts).ThenInclude(i=>i.POC).Include(i=>i.Locations).Include(i=>i.AssignToUser).AsNoTracking().FirstOrDefaultAsync();
|
|||
|
|
return exist ?? new WorkOrder();
|
|||
|
|
}
|
|||
|
|
public async Task<string> DeleteTemplate(int id)
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
var exist = await _context.workOrders.AsSplitQuery().Where(w => w.Id == id).Include(w=>w.WorkOrderCategories).Include(w=>w.WorkOrderContacts).Include(w=>w.workOrderAttachments).FirstOrDefaultAsync();
|
|||
|
|
if (exist != null)
|
|||
|
|
{
|
|||
|
|
_context.workOrders.Remove(exist);
|
|||
|
|
}
|
|||
|
|
await _context.SaveChangesAsync();
|
|||
|
|
return "success";
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
return ex.Message;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
#endregion
|
|||
|
|
#region dropdowns of template
|
|||
|
|
public async Task<IEnumerable<ApplicationUser>> GetUsers()
|
|||
|
|
{
|
|||
|
|
var filteredUsers = await (from user in _context.Users
|
|||
|
|
where !(from userRole in _context.UserRoles
|
|||
|
|
join role in _context.Roles on userRole.RoleId equals role.Id
|
|||
|
|
where role.Name == "Admin"
|
|||
|
|
select userRole.UserId).Contains(user.Id)
|
|||
|
|
select user).ToListAsync();
|
|||
|
|
|
|||
|
|
return filteredUsers;
|
|||
|
|
}
|
|||
|
|
public async Task<IEnumerable<Locations>> GetLocations()
|
|||
|
|
{
|
|||
|
|
var data = await _context.Locations.ToListAsync();
|
|||
|
|
return data;
|
|||
|
|
}
|
|||
|
|
public async Task<IEnumerable<Contacts>> GetContacts()
|
|||
|
|
{
|
|||
|
|
var data = await _context.Contacts.ToListAsync();
|
|||
|
|
return data;
|
|||
|
|
}
|
|||
|
|
#endregion
|
|||
|
|
public async Task<List<RolesViewModel>> GetRoles()
|
|||
|
|
{
|
|||
|
|
var rolesData = await _context.Roles.ToListAsync();
|
|||
|
|
var roleOrder = new List<string> { "Admin", "Supervisor", "Manager", "User" };
|
|||
|
|
var orderedRolesData = rolesData.OrderBy(role => roleOrder.IndexOf(role.Name)).ToList();
|
|||
|
|
|
|||
|
|
var rolesViewModels = orderedRolesData.Select(role => new RolesViewModel
|
|||
|
|
{
|
|||
|
|
RoleName = role.Name ?? "",
|
|||
|
|
Id = role.Id
|
|||
|
|
}).ToList();
|
|||
|
|
return rolesViewModels;
|
|||
|
|
}
|
|||
|
|
public async Task<string> Changepassword(ChangePasswords changepassword)
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
var uid = _httpContextAccessor?.HttpContext?.User?.FindFirst(ClaimTypes.NameIdentifier)?.Value ?? "";
|
|||
|
|
if (changepassword.Newpassword != changepassword.Confirmpassword)
|
|||
|
|
{
|
|||
|
|
return "notmatched";
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
var user = await _userManager.FindByIdAsync(uid);
|
|||
|
|
if (user == null)
|
|||
|
|
{
|
|||
|
|
return "usernull";
|
|||
|
|
}
|
|||
|
|
var validatepassword = await _userManager.CheckPasswordAsync(user, changepassword.Currentpassword ?? "");
|
|||
|
|
if (validatepassword == true)
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
var code = await _userManager.GeneratePasswordResetTokenAsync(user);
|
|||
|
|
var result = await _userManager.ResetPasswordAsync(user, code, changepassword.Confirmpassword ?? "");
|
|||
|
|
if (result.Succeeded)
|
|||
|
|
{
|
|||
|
|
return "success";
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
var message = "";
|
|||
|
|
foreach (var itm in result.Errors)
|
|||
|
|
{
|
|||
|
|
message += itm.Description;
|
|||
|
|
}
|
|||
|
|
return message;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
return "Invalid Old Password";
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
return ex.Message;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
public async Task<string> AddRole(string roleName)
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
if (string.IsNullOrEmpty(roleName))
|
|||
|
|
{
|
|||
|
|
return "Role name cannot be empty.";
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
var roleExists = await _roleManager.RoleExistsAsync(roleName);
|
|||
|
|
if (roleExists)
|
|||
|
|
{
|
|||
|
|
return "Role already exists.";
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
var role = new IdentityRole(roleName);
|
|||
|
|
var result = await _roleManager.CreateAsync(role);
|
|||
|
|
|
|||
|
|
if (result.Succeeded)
|
|||
|
|
{
|
|||
|
|
return "success";
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
return "failed";
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
// Handle exception and log it if necessary
|
|||
|
|
return ex.Message;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
public async Task<IdentityRole> GetRoleById(string roleId)
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
var role = await _roleManager.FindByIdAsync(roleId);
|
|||
|
|
if (role == null)
|
|||
|
|
{
|
|||
|
|
throw new Exception("Role with ID not found.");
|
|||
|
|
}
|
|||
|
|
return role;
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
throw new Exception($"An error occurred while fetching the role: {ex.Message}");
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
public async Task<DashboardViewModel> Getdashboarddata()
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
var workOrdersQuery = _context.workOrders
|
|||
|
|
.Where(w => w.istemplate!=true) // Filter non-template work orders
|
|||
|
|
.AsSplitQuery()
|
|||
|
|
.Include(i => i.AssignToUser)
|
|||
|
|
.AsQueryable();
|
|||
|
|
|
|||
|
|
// Group by Status and Priority to avoid multiple queries
|
|||
|
|
var workOrders = await workOrdersQuery
|
|||
|
|
.GroupBy(w => new { w.Status, w.Priority })
|
|||
|
|
.Select(g => new
|
|||
|
|
{
|
|||
|
|
g.Key.Status,
|
|||
|
|
g.Key.Priority,
|
|||
|
|
Count = g.Count()
|
|||
|
|
})
|
|||
|
|
.ToListAsync();
|
|||
|
|
|
|||
|
|
// Initialize dashboard
|
|||
|
|
DashboardViewModel dashboard = new DashboardViewModel();
|
|||
|
|
|
|||
|
|
// Total count
|
|||
|
|
dashboard.Total = workOrders.Sum(w => w.Count);
|
|||
|
|
|
|||
|
|
// Status counts
|
|||
|
|
dashboard.Completed = workOrders.Where(w => w.Status == "Done").Sum(w => w.Count);
|
|||
|
|
dashboard.Inprogress = workOrders.Where(w => w.Status == "In Progress").Sum(w => w.Count);
|
|||
|
|
dashboard.Open = workOrders.Where(w => w.Status == "Open").Sum(w => w.Count);
|
|||
|
|
dashboard.Onhold = workOrders.Where(w => w.Status == "On Hold").Sum(w => w.Count);
|
|||
|
|
|
|||
|
|
// Priority counts
|
|||
|
|
dashboard.Sev1 = workOrders.Where(w => w.Priority == "Sev 1").Sum(w => w.Count);
|
|||
|
|
dashboard.Sev2 = workOrders.Where(w => w.Priority == "Sev 2").Sum(w => w.Count);
|
|||
|
|
dashboard.Sev3 = workOrders.Where(w => w.Priority == "Sev 3").Sum(w => w.Count);
|
|||
|
|
dashboard.Sev4 = workOrders.Where(w => w.Priority == "Sev 4").Sum(w => w.Count);
|
|||
|
|
dashboard.Sev5 = workOrders.Where(w => w.Priority == "Sev 5").Sum(w => w.Count);
|
|||
|
|
|
|||
|
|
// Calculate percentages
|
|||
|
|
dashboard.Sev1Per = Math.Round((double)dashboard.Sev1.Value / dashboard.Total.Value * 100, 2);
|
|||
|
|
dashboard.Sev2Per = Math.Round((double)dashboard.Sev2.Value / dashboard.Total.Value * 100, 2);
|
|||
|
|
dashboard.Sev3Per = Math.Round((double)dashboard.Sev3.Value / dashboard.Total.Value * 100, 2);
|
|||
|
|
dashboard.Sev4Per = Math.Round((double)dashboard.Sev4.Value / dashboard.Total.Value * 100, 2);
|
|||
|
|
dashboard.Sev5Per = Math.Round((double)dashboard.Sev5.Value / dashboard.Total.Value * 100, 2);
|
|||
|
|
|
|||
|
|
// Fetch top 5 work orders for the dashboard (minimized data retrieval)
|
|||
|
|
dashboard.Dashboardworkorders = await workOrdersQuery
|
|||
|
|
.OrderBy(o => o.Id)
|
|||
|
|
.Take(5)
|
|||
|
|
.Select(s => new DashboardworkorderViewModel
|
|||
|
|
{
|
|||
|
|
Name = s.WorkerOrderTitle,
|
|||
|
|
UserName = s.AssignToUser.FirstName,
|
|||
|
|
Status = s.Status
|
|||
|
|
})
|
|||
|
|
.ToListAsync();
|
|||
|
|
|
|||
|
|
return dashboard;
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
throw new Exception($"An error occurred while fetching the dashboard data: {ex.Message}");
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
}
|