เพิ่ม link สำนักตาม keycloak

This commit is contained in:
Kittapath 2023-05-12 22:03:53 +07:00
parent 0b5c7038a6
commit 86a6ab9514
10 changed files with 712 additions and 289 deletions

View file

@ -84,43 +84,98 @@ namespace BMA.EHR.Recurit.Exam.Service.Services
}
}
private List<Guid?> GetAllIdByRoot(Guid id)
{
try
{
var ret = new List<Guid?>();
var oc = _contextMetadata.Organizations.FirstOrDefault(x => x.Id == id && x.IsActive);
if (oc != null)
ret.Add(oc.Id);
var child = _contextMetadata.Organizations.AsQueryable().Where(x => x.ParentId == id && x.IsActive).ToList();
if (child.Any())
{
foreach (var item in child)
{
ret.AddRange(GetAllIdByRoot(item.Id));
}
}
return ret;
}
catch
{
throw;
}
}
public async Task<IEnumerable<PeriodExamCandidateResponseItem>> GetsAsync(string type, bool showAll = true)
{
return await _context.PeriodExams.AsQueryable()
.Where(p => p.IsActive)
.Where(p => p.CheckDisability == false)
.Where(p => type.ToUpper() == "ALL" ? (p.AnnouncementExam == true || p.AnnouncementExam == false) : (type.ToUpper() == "EXAM" ? p.AnnouncementExam == true : p.AnnouncementExam == false))
.OrderByDescending(d => d.CreatedAt)
.Select(x => new PeriodExamCandidateResponseItem
{
ExamDate = x.ExamDate,
AnnouncementEndDate = x.AnnouncementEndDate,
AnnouncementStartDate = x.AnnouncementStartDate,
AnnouncementDate = x.AnnouncementDate,
CheckDisability = x.CheckDisability,
CheckDocument = x.CheckDocument,
Detail = x.Detail,
Fee = x.Fee,
Id = x.Id,
IsActive = x.IsActive,
Name = x.Name,
Note = x.Note,
OrganizationCodeId = x.OrganizationCodeId,
OrganizationCodeName = x.OrganizationCodeName,
OrganizationId = x.OrganizationId,
OrganizationName = x.OrganizationName,
PaymentEndDate = x.PaymentEndDate,
PaymentKrungThai = x.PaymentKrungThai,
AnnouncementExam = x.AnnouncementExam,
Category = x.Category,
PaymentStartDate = x.PaymentStartDate,
RegisterEndDate = x.RegisterEndDate,
RegisterStartDate = x.RegisterStartDate,
Round = x.Round,
SetSeat = x.SetSeat,
Year = x.Year,
})
.ToListAsync();
var periodExams = await _context.PeriodExams.AsQueryable()
.Where(p => p.IsActive)
.Where(p => p.CheckDisability == false)
.Where(p => type.ToUpper() == "ALL" ? (p.AnnouncementExam == true || p.AnnouncementExam == false) : (type.ToUpper() == "EXAM" ? p.AnnouncementExam == true : p.AnnouncementExam == false))
.OrderByDescending(d => d.CreatedAt).ToListAsync();
var profileOrganizations = await _contextMetadata.ProfileOrganizations.AsQueryable()
.ToListAsync();
var _periodExams = (from x in periodExams
join po in profileOrganizations on Guid.Parse(x.CreatedUserId) equals po?.UserId into poGroup
from po in poGroup.DefaultIfEmpty()
select new PeriodExamCandidateResponseItem
{
ExamDate = x.ExamDate,
AnnouncementEndDate = x.AnnouncementEndDate,
AnnouncementStartDate = x.AnnouncementStartDate,
AnnouncementDate = x.AnnouncementDate,
CheckDisability = x.CheckDisability,
CheckDocument = x.CheckDocument,
Detail = x.Detail,
Fee = x.Fee,
Id = x.Id,
IsActive = x.IsActive,
Name = x.Name,
Note = x.Note,
OrganizationCodeId = x.OrganizationCodeId,
OrganizationCodeName = x.OrganizationCodeName,
OrganizationId = x.OrganizationId,
OrganizationName = x.OrganizationName,
PaymentEndDate = x.PaymentEndDate,
PaymentKrungThai = x.PaymentKrungThai,
AnnouncementExam = x.AnnouncementExam,
Category = x.Category,
PaymentStartDate = x.PaymentStartDate,
RegisterEndDate = x.RegisterEndDate,
RegisterStartDate = x.RegisterStartDate,
Round = x.Round,
SetSeat = x.SetSeat,
Year = x.Year,
OcId = po == null ? null : po.OrganizationId,
CreatedUserId = x.CreatedUserId,
}).AsQueryable()
.ToList();
var roles = _httpContextAccessor?.HttpContext?.User?.FindAll(ClaimTypes.Role)?.Select(c => c.Value).ToList();
if (!roles.Contains("head"))
{
var criteria = new List<Guid?>();
var profileOrganization = await _contextMetadata.ProfileOrganizations.AsQueryable()
.FirstOrDefaultAsync(x => x.UserId == Guid.Parse(UserId));
if (profileOrganization == null)
throw new Exception(GlobalMessages.OrganizationNotFound);
var ocId = _contextMetadata.Organizations.AsQueryable()
.FirstOrDefault(x => x.Id == profileOrganization.OrganizationId);
if (ocId == null)
throw new Exception(GlobalMessages.OrganizationNotFound);
criteria = GetAllIdByRoot(ocId.Id);
if (criteria.Any())
_periodExams = _periodExams.Where(x => x.CreatedUserId == UserId || criteria.Contains(x.OcId == null ? Guid.Parse("00000000-0000-0000-0000-000000000000") : x.OcId)).ToList();
}
return _periodExams;
}
public async Task<PeriodExamCandidateResponseItem?> GetsExamAndCandidateAsync(string examId, bool showAll = true)