| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- using Diligent.WebAPI.Contracts.DTOs.Stats;
-
- namespace Diligent.WebAPI.Business.Services
- {
- public class SelectionProcessService : ISelectionProcessService
- {
- private readonly DatabaseContext _context;
- private readonly IMapper _mapper;
- private readonly UserManager<User> _userManager;
- private readonly ILogger<SelectionProcessService> _logger;
-
- public SelectionProcessService(UserManager<User> userManager,DatabaseContext context, IMapper mapper, ILogger<SelectionProcessService> logger)
- {
- _context = context;
- _mapper = mapper;
- _logger = logger;
- _userManager = userManager;
- }
-
- public SelectionProcessService(DatabaseContext context, IMapper mapper, ILogger<SelectionProcessService> logger)
- {
- _context = context;
- _mapper = mapper;
- _logger = logger;
- }
-
- public async Task<List<SelectionProcessResposneDto>> GetAllAsync()
- {
- _logger.LogInformation("Start getting all Selection Processes");
- _logger.LogInformation("Getting data from DB");
- var fromDB = await _context.SelectionProcesses.ToListAsync();
- _logger.LogInformation($"Received {fromDB.Count} processes from db.");
- _logger.LogInformation($"Mapping received ads to SelectionProcessResposneDto");
- var result = _mapper.Map<List<SelectionProcessResposneDto>>(fromDB);
- _logger.LogInformation($"Processes has been mapped and received to client: {result.Count} mapped processes");
- return result;
- }
- public async Task<bool> FinishSelectionProcess(SelectionProcessCreateDto model)
- {
- _logger.LogInformation($"Start finishing selection process with {model.Id}");
- var sp = await _context.SelectionProcesses.FindAsync(model.Id);
-
- if (sp is null)
- {
- _logger.LogError($"Process with id = {model.Id} not found");
- throw new EntityNotFoundException("Selection process not found");
- }
-
- _logger.LogInformation($"Changing status for {model.Id}");
- sp.Status = "Odrađen";
-
- _logger.LogInformation($"Skipping throught levels to come to next level");
- var nextLevel = _context.SelectionLevels.AsEnumerable()
- .SkipWhile(obj => obj.Id != sp.SelectionLevelId)
- .Skip(1).First();
-
- if (nextLevel is null)
- {
- _logger.LogError($"Applicant is in the last selection level");
- throw new EntityNotFoundException("Candidate came to the last selection level");
- // ovde sacuvati promene i return odraditi
- // da ne bi pravilo novi proces
- // exception treba ukloniti
- }
-
- SelectionProcess newProcess = new SelectionProcess
- {
- Name = model.Name,
- SelectionLevelId = nextLevel.Id,
- Status = "Čeka na zakazivanje",
- ApplicantId = sp.ApplicantId,
- //SchedulerId = model.SchedulerId
- };
- _context.SelectionProcesses.Add(newProcess);
- _logger.LogInformation($"Create and add new selection process");
- var result = await _context.SaveChangesAsync() > 0;
- _logger.LogInformation($"Saved changes to db");
- return result;
- }
-
- public async Task<List<SelectionLevelInfoDto>> GetCountByLevels(List<string> statuses)
- {
- _logger.LogInformation("Start getting all Selection levels");
- var res = await _context.SelectionLevels.Include(n => n.SelectionProcesses).ToListAsync();
-
- _logger.LogInformation($"Received {res.Count} selection levels");
- _logger.LogInformation("Mapping levels with process counts to SelectionLevelInfo");
- var resMapped = res.Select(n => new SelectionLevelInfoDto
- {
- Level = n.Name,
- CountAll = n.SelectionProcesses.Count,
- CountDone = n.SelectionProcesses.Where(n => statuses.Contains(n.Status)).Count()
- }).ToList();
-
- return resMapped;
- }
-
- public async Task UpdateSelectionProcessStatusAsync(int id, SelectionProcessUpdateStatusDto selectionProcessUpdateStatusDto)
- {
- _logger.LogInformation($"Start searching Ad with id = {id}");
- var selectionProcess = await _context.SelectionProcesses.FindAsync(id);
-
- if (selectionProcess is null)
- {
- _logger.LogError($"Selection process with id = {id} not found");
- throw new EntityNotFoundException("Selection process not found");
- }
-
- _logger.LogInformation($"Mapping Selection process with id = {id}");
- _mapper.Map(selectionProcessUpdateStatusDto, selectionProcess);
- _logger.LogInformation($"Selection process with id = {id} mapped successfully");
-
- _context.Entry(selectionProcess).State = EntityState.Modified;
-
- var result = _context.SaveChangesAsync();
- _logger.LogInformation($"Selection process saved to DB");
- await result;
- }
- public async Task StatusUpdate(StatusChangeDTO model)
- {
- _logger.LogInformation($"Start searching process with id = {model.ProcessId}");
- var process = await _context.SelectionProcesses.FindAsync(model.ProcessId);
-
- if (process is null)
- {
- _logger.LogError($"Process with id = {model.ProcessId} not found");
- throw new EntityNotFoundException("Process does not exist.");
- }
-
- _logger.LogInformation($"Check which status is going to be set.");
- if (model.NewStatus == "Zakazan" && model.SchedulerId is not null)
- {
- _logger.LogInformation($"Start searching user with id = {model.SchedulerId}");
- var user = await _userManager.FindByIdAsync(model.SchedulerId.ToString());
-
- if (user is null)
- throw new EntityNotFoundException("Scheduler does not exist.");
-
- _logger.LogInformation($"Setting user {user.FirstName} {user.LastName} as scheduler for process with id = {model.ProcessId}");
- process.SchedulerId = user.Id;
- }
-
- process.Status = model.NewStatus;
- process.Date = model.Appointment;
-
- _logger.LogInformation($"Processing changes.");
- await _context.SaveChangesAsync();
- }
-
- public async Task InterviewerUpdate(InterviewerUpdateDTO model)
- {
- _logger.LogInformation($"Start searching process with id = {model.ProcessId}");
- var process = await _context.SelectionProcesses.FindAsync(model.ProcessId);
-
- if (process is null)
- throw new EntityNotFoundException("Process does not exist.");
-
- _logger.LogInformation($"Start searching user with id = {model.SchedulerId}");
- var user = await _userManager.FindByIdAsync(model.SchedulerId.ToString());
-
- if (user is null)
- throw new EntityNotFoundException("Scheduler does not exist.");
-
- _logger.LogInformation($"Setting user {user.FirstName} {user.LastName} as scheduler for process with id = {model.ProcessId}");
- process.SchedulerId = user.Id;
-
- _logger.LogInformation("Processing changes...");
- await _context.SaveChangesAsync();
- }
- }
- }
|