You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SelectionProcessService.cs 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. using Diligent.WebAPI.Contracts.DTOs.Stats;
  2. namespace Diligent.WebAPI.Business.Services
  3. {
  4. public class SelectionProcessService : ISelectionProcessService
  5. {
  6. private readonly DatabaseContext _context;
  7. private readonly IMapper _mapper;
  8. private readonly UserManager<User> _userManager;
  9. private readonly ILogger<SelectionProcessService> _logger;
  10. public SelectionProcessService(UserManager<User> userManager,DatabaseContext context, IMapper mapper, ILogger<SelectionProcessService> logger)
  11. {
  12. _context = context;
  13. _mapper = mapper;
  14. _logger = logger;
  15. _userManager = userManager;
  16. }
  17. public SelectionProcessService(DatabaseContext context, IMapper mapper, ILogger<SelectionProcessService> logger)
  18. {
  19. _context = context;
  20. _mapper = mapper;
  21. _logger = logger;
  22. }
  23. public async Task<List<SelectionProcessResposneDto>> GetAllAsync()
  24. {
  25. _logger.LogInformation("Start getting all Selection Processes");
  26. _logger.LogInformation("Getting data from DB");
  27. var fromDB = await _context.SelectionProcesses.ToListAsync();
  28. _logger.LogInformation($"Received {fromDB.Count} processes from db.");
  29. _logger.LogInformation($"Mapping received ads to SelectionProcessResposneDto");
  30. var result = _mapper.Map<List<SelectionProcessResposneDto>>(fromDB);
  31. _logger.LogInformation($"Processes has been mapped and received to client: {result.Count} mapped processes");
  32. return result;
  33. }
  34. public async Task<bool> FinishSelectionProcess(SelectionProcessCreateDto model)
  35. {
  36. _logger.LogInformation($"Start finishing selection process with {model.Id}");
  37. var sp = await _context.SelectionProcesses.FindAsync(model.Id);
  38. if (sp is null)
  39. {
  40. _logger.LogError($"Process with id = {model.Id} not found");
  41. throw new EntityNotFoundException("Selection process not found");
  42. }
  43. _logger.LogInformation($"Changing status for {model.Id}");
  44. sp.Status = "Odrađen";
  45. _logger.LogInformation($"Skipping throught levels to come to next level");
  46. var nextLevel = _context.SelectionLevels.AsEnumerable()
  47. .SkipWhile(obj => obj.Id != sp.SelectionLevelId)
  48. .Skip(1).First();
  49. if (nextLevel is null)
  50. {
  51. _logger.LogError($"Applicant is in the last selection level");
  52. throw new EntityNotFoundException("Candidate came to the last selection level");
  53. // ovde sacuvati promene i return odraditi
  54. // da ne bi pravilo novi proces
  55. // exception treba ukloniti
  56. }
  57. SelectionProcess newProcess = new SelectionProcess
  58. {
  59. Name = model.Name,
  60. SelectionLevelId = nextLevel.Id,
  61. Status = "Čeka na zakazivanje",
  62. ApplicantId = sp.ApplicantId,
  63. //SchedulerId = model.SchedulerId
  64. };
  65. _context.SelectionProcesses.Add(newProcess);
  66. _logger.LogInformation($"Create and add new selection process");
  67. var result = await _context.SaveChangesAsync() > 0;
  68. _logger.LogInformation($"Saved changes to db");
  69. return result;
  70. }
  71. public async Task<List<SelectionLevelInfoDto>> GetCountByLevels(List<string> statuses)
  72. {
  73. _logger.LogInformation("Start getting all Selection levels");
  74. var res = await _context.SelectionLevels.Include(n => n.SelectionProcesses).ToListAsync();
  75. _logger.LogInformation($"Received {res.Count} selection levels");
  76. _logger.LogInformation("Mapping levels with process counts to SelectionLevelInfo");
  77. var resMapped = res.Select(n => new SelectionLevelInfoDto
  78. {
  79. Level = n.Name,
  80. CountAll = n.SelectionProcesses.Count,
  81. CountDone = n.SelectionProcesses.Where(n => statuses.Contains(n.Status)).Count()
  82. }).ToList();
  83. return resMapped;
  84. }
  85. public async Task UpdateSelectionProcessStatusAsync(int id, SelectionProcessUpdateStatusDto selectionProcessUpdateStatusDto)
  86. {
  87. _logger.LogInformation($"Start searching Ad with id = {id}");
  88. var selectionProcess = await _context.SelectionProcesses.FindAsync(id);
  89. if (selectionProcess is null)
  90. {
  91. _logger.LogError($"Selection process with id = {id} not found");
  92. throw new EntityNotFoundException("Selection process not found");
  93. }
  94. _logger.LogInformation($"Mapping Selection process with id = {id}");
  95. _mapper.Map(selectionProcessUpdateStatusDto, selectionProcess);
  96. _logger.LogInformation($"Selection process with id = {id} mapped successfully");
  97. _context.Entry(selectionProcess).State = EntityState.Modified;
  98. var result = _context.SaveChangesAsync();
  99. _logger.LogInformation($"Selection process saved to DB");
  100. await result;
  101. }
  102. public async Task StatusUpdate(StatusChangeDTO model)
  103. {
  104. _logger.LogInformation($"Start searching process with id = {model.ProcessId}");
  105. var process = await _context.SelectionProcesses.FindAsync(model.ProcessId);
  106. if (process is null)
  107. {
  108. _logger.LogError($"Process with id = {model.ProcessId} not found");
  109. throw new EntityNotFoundException("Process does not exist.");
  110. }
  111. _logger.LogInformation($"Check which status is going to be set.");
  112. if (model.NewStatus == "Zakazan" && model.SchedulerId is not null)
  113. {
  114. _logger.LogInformation($"Start searching user with id = {model.SchedulerId}");
  115. var user = await _userManager.FindByIdAsync(model.SchedulerId.ToString());
  116. if (user is null)
  117. throw new EntityNotFoundException("Scheduler does not exist.");
  118. _logger.LogInformation($"Setting user {user.FirstName} {user.LastName} as scheduler for process with id = {model.ProcessId}");
  119. process.SchedulerId = user.Id;
  120. }
  121. process.Status = model.NewStatus;
  122. process.Date = model.Appointment;
  123. _logger.LogInformation($"Processing changes.");
  124. await _context.SaveChangesAsync();
  125. }
  126. public async Task InterviewerUpdate(InterviewerUpdateDTO model)
  127. {
  128. _logger.LogInformation($"Start searching process with id = {model.ProcessId}");
  129. var process = await _context.SelectionProcesses.FindAsync(model.ProcessId);
  130. if (process is null)
  131. throw new EntityNotFoundException("Process does not exist.");
  132. _logger.LogInformation($"Start searching user with id = {model.SchedulerId}");
  133. var user = await _userManager.FindByIdAsync(model.SchedulerId.ToString());
  134. if (user is null)
  135. throw new EntityNotFoundException("Scheduler does not exist.");
  136. _logger.LogInformation($"Setting user {user.FirstName} {user.LastName} as scheduler for process with id = {model.ProcessId}");
  137. process.SchedulerId = user.Id;
  138. _logger.LogInformation("Processing changes...");
  139. await _context.SaveChangesAsync();
  140. }
  141. }
  142. }