mirror of
https://github.com/Sea-Haven-Industries/shoc-backend.git
synced 2026-09-30 15:23:12 +00:00
139 lines
6.6 KiB
C#
139 lines
6.6 KiB
C#
|
|
using Data.SeaHavenIndustries;
|
||
|
|
using Microsoft.EntityFrameworkCore;
|
||
|
|
using SeaHaven.DataServices.Interfaces;
|
||
|
|
|
||
|
|
namespace SeaHaven.DataServices.Implementation
|
||
|
|
{
|
||
|
|
public class UpliftDataService : IUpliftDataService
|
||
|
|
{
|
||
|
|
private readonly ApplicationDbContext _context;
|
||
|
|
|
||
|
|
public UpliftDataService(ApplicationDbContext context)
|
||
|
|
{
|
||
|
|
_context = context;
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<(int TotalCount, IReadOnlyList<UpliftListItemData> Items)> GetPagedAsync(
|
||
|
|
string? status, int? tier, int page, int pageSize, CancellationToken cancellationToken)
|
||
|
|
{
|
||
|
|
var query = from u in _context.DispatchUpliftRequests
|
||
|
|
join d in _context.Dispatches on u.DispatchId equals d.Id
|
||
|
|
join v in _context.Vendors on d.VendorId equals v.Id into vendors
|
||
|
|
from v in vendors.DefaultIfEmpty()
|
||
|
|
where (u.IsDeleted == null || u.IsDeleted == false)
|
||
|
|
select new { u, d, v };
|
||
|
|
|
||
|
|
if (!string.IsNullOrWhiteSpace(status))
|
||
|
|
query = query.Where(x => x.u.Status == status);
|
||
|
|
if (tier.HasValue)
|
||
|
|
query = query.Where(x => x.u.RequiredTier == tier.Value);
|
||
|
|
|
||
|
|
var total = await query.CountAsync(cancellationToken);
|
||
|
|
|
||
|
|
var items = await query
|
||
|
|
.OrderByDescending(x => x.u.CreatedDate)
|
||
|
|
.Skip((Math.Max(page, 1) - 1) * pageSize)
|
||
|
|
.Take(pageSize)
|
||
|
|
.Select(x => new UpliftListItemData
|
||
|
|
{
|
||
|
|
Id = x.u.Id,
|
||
|
|
DispatchId = x.d.Id,
|
||
|
|
DispatchNumber = x.d.DispatchNumber,
|
||
|
|
PONumber = x.d.PONumber,
|
||
|
|
WorkOrderId = x.d.WorkOrderId,
|
||
|
|
VendorCompanyName = x.v != null ? x.v.CompanyName : x.u.RequestedByVendorName,
|
||
|
|
CurrentNTE = x.u.CurrentNTE,
|
||
|
|
RequestedNTE = x.u.RequestedNTE,
|
||
|
|
VendorReason = x.u.VendorReason,
|
||
|
|
RequiredTier = x.u.RequiredTier,
|
||
|
|
Status = x.u.Status,
|
||
|
|
CreatedDate = x.u.CreatedDate,
|
||
|
|
DecidedAt = x.u.DecidedAt,
|
||
|
|
DecisionNote = x.u.DecisionNote
|
||
|
|
})
|
||
|
|
.ToListAsync(cancellationToken);
|
||
|
|
|
||
|
|
return (total, items);
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<IReadOnlyList<UpliftForDispatchData>> GetForDispatchAsync(int dispatchId, CancellationToken cancellationToken)
|
||
|
|
{
|
||
|
|
return await (from u in _context.DispatchUpliftRequests
|
||
|
|
where u.DispatchId == dispatchId && (u.IsDeleted == null || u.IsDeleted == false)
|
||
|
|
join dec in _context.Users on u.DecidedByUserId equals dec.Id into decs
|
||
|
|
from dec in decs.DefaultIfEmpty()
|
||
|
|
orderby u.CreatedDate descending
|
||
|
|
select new UpliftForDispatchData
|
||
|
|
{
|
||
|
|
Id = u.Id,
|
||
|
|
DispatchId = u.DispatchId,
|
||
|
|
CurrentNTE = u.CurrentNTE,
|
||
|
|
RequestedNTE = u.RequestedNTE,
|
||
|
|
VendorReason = u.VendorReason,
|
||
|
|
Status = u.Status,
|
||
|
|
RequiredTier = u.RequiredTier,
|
||
|
|
RequestedByVendorName = u.RequestedByVendorName,
|
||
|
|
CreatedDate = u.CreatedDate,
|
||
|
|
DecidedAt = u.DecidedAt,
|
||
|
|
DecisionNote = u.DecisionNote,
|
||
|
|
DecidedByFirstName = dec != null ? dec.FirstName : null,
|
||
|
|
DecidedByLastName = dec != null ? dec.LastName : null
|
||
|
|
}).ToListAsync(cancellationToken);
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<IReadOnlyList<PortalUpliftData>> GetForVendorDispatchAsync(int dispatchId, CancellationToken cancellationToken)
|
||
|
|
{
|
||
|
|
return await (from u in _context.DispatchUpliftRequests
|
||
|
|
where u.DispatchId == dispatchId && (u.IsDeleted == null || u.IsDeleted == false)
|
||
|
|
join dec in _context.Users on u.DecidedByUserId equals dec.Id into decs
|
||
|
|
from dec in decs.DefaultIfEmpty()
|
||
|
|
orderby u.CreatedDate descending
|
||
|
|
select new PortalUpliftData
|
||
|
|
{
|
||
|
|
Id = u.Id,
|
||
|
|
CurrentNTE = u.CurrentNTE,
|
||
|
|
RequestedNTE = u.RequestedNTE,
|
||
|
|
VendorReason = u.VendorReason,
|
||
|
|
Status = u.Status,
|
||
|
|
RequiredTier = u.RequiredTier,
|
||
|
|
RequestedByVendorName = u.RequestedByVendorName,
|
||
|
|
CreatedDate = u.CreatedDate,
|
||
|
|
DecidedAt = u.DecidedAt,
|
||
|
|
DecisionNote = u.DecisionNote,
|
||
|
|
DecidedByFirstName = dec != null ? dec.FirstName : null,
|
||
|
|
DecidedByLastName = dec != null ? dec.LastName : null
|
||
|
|
}).ToListAsync(cancellationToken);
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<DispatchUpliftRequest?> GetByIdAsync(int id, CancellationToken cancellationToken)
|
||
|
|
{
|
||
|
|
return await _context.DispatchUpliftRequests
|
||
|
|
.FirstOrDefaultAsync(u => u.Id == id, cancellationToken);
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<DispatchUpliftRequest?> GetByIdAndDispatchAsync(int requestId, int dispatchId, CancellationToken cancellationToken)
|
||
|
|
{
|
||
|
|
return await _context.DispatchUpliftRequests
|
||
|
|
.FirstOrDefaultAsync(u => u.Id == requestId && u.DispatchId == dispatchId, cancellationToken);
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<bool> HasPendingAsync(int dispatchId, CancellationToken cancellationToken)
|
||
|
|
{
|
||
|
|
return await _context.DispatchUpliftRequests
|
||
|
|
.AnyAsync(u => u.DispatchId == dispatchId
|
||
|
|
&& u.Status == "Pending"
|
||
|
|
&& (u.IsDeleted == null || u.IsDeleted == false), cancellationToken);
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task StageAsync(DispatchUpliftRequest request, CancellationToken cancellationToken)
|
||
|
|
{
|
||
|
|
await _context.DispatchUpliftRequests.AddAsync(request, cancellationToken);
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task SaveChangesAsync(CancellationToken cancellationToken)
|
||
|
|
{
|
||
|
|
await _context.SaveChangesAsync(cancellationToken);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|