EnVisageOnline/Main/Source/PrevuWebAPI/Code/Managers/APIResourceManager.cs

377 lines
17 KiB
C#

using Code.Utils;
using EnVisage;
using EnVisage.Code.BLL;
using EnVisage.Code.ThreadedProcessing;
using EnVisage.Models;
using PrevuWebAPI.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PrevuWebAPI.Code.Managers
{
public class APIResourceManager
{
PeopleResourcesManager _manager =new PeopleResourcesManager(PrevuEFContext.PrevuEntity);
private Guid RequestID = Guid.NewGuid();
private List<string> reason = new List<string>();
public List<APIPeopleResourceModel> getResources()
{
List<APIPeopleResourceModel> rt = new List<APIPeopleResourceModel>();
var resources=_manager.DataTable.ToList();
foreach(PeopleResource r in resources)
{
rt.Add(getAPIModel(r));
}
return rt;
}
public APIPeopleResourceModel getResource(string email)
{
List<APIPeopleResourceModel> rt = new List<APIPeopleResourceModel>();
var r = _manager.DataTable.Where(x => x.Email == email).FirstOrDefault();
return getAPIModel(r);
}
public ResultModel Save(APIPeopleResourceModel m)
{
ResultModel rt = new ResultModel();
rt.RequestID = this.RequestID;
try {
var r = _manager.DataTable.Where(x => x.Email == m.Email).FirstOrDefault();
if (r == null)
rt.RequestType = "Add";
else
rt.RequestType = "Update";
}catch(Exception dds)
{
Settings.Logger.Log(NLog.LogLevel.Debug, "Exception in get api model!");
Settings.Logger.Log(NLog.LogLevel.Error, Environment.NewLine +
"------------------------------------------------------------------------------" + Environment.NewLine + RequestID + Environment.NewLine + dds +
"------------------------------------------------------------------------------" +
Environment.NewLine + Environment.NewLine);
reason.Add("There was an error in the process. Please contact the system admin. Error Code:" + RequestID);
MongoManager.StoreAPIPeopleResourceInMongo(m, reason);
rt.Result = (int) RejectReason.Exception;
rt.Messages = reason;
return rt;
}
List<PeopleResourceAPIModel> prModel = new List<PeopleResourceAPIModel>();
var model = getPrevuPRModel(m);
if (model != null)
{
prModel.Add(model);
BackgroundProcessManager bpm = new BackgroundProcessManager(PrevuEFContext.PrevuEntity);
if (bpm.BulkPeopleResourceRecordEdit(prModel, RequestID, new System.Threading.CancellationToken()))
rt.Result = 0;
else
rt.Result = -1;
}
return rt;
}
public ResultModel Save(List<APIPeopleResourceModel> mList)
{
ResultModel rt = new ResultModel();
rt.RequestID = this.RequestID;
List<PeopleResourceAPIModel> prModel = new List<PeopleResourceAPIModel>();
foreach (APIPeopleResourceModel m in mList)
{
try
{
var r = _manager.DataTable.Where(x => x.Email == m.Email).FirstOrDefault();
if (r == null)
rt.RequestType = "Add";
else
rt.RequestType = "Update";
}
catch (Exception dds)
{
Settings.Logger.Log(NLog.LogLevel.Debug, "Exception in get api model!");
Settings.Logger.Log(NLog.LogLevel.Error, Environment.NewLine +
"------------------------------------------------------------------------------" + Environment.NewLine + RequestID + Environment.NewLine + dds +
"------------------------------------------------------------------------------" +
Environment.NewLine + Environment.NewLine);
reason.Add("There was an error in the process. Please contact the system admin. Error Code:" + RequestID);
MongoManager.StoreAPIPeopleResourceInMongo(m, reason);
rt.Result = (int) RejectReason.Exception;
rt.Messages = reason;
return rt;
}
var model = getPrevuPRModel(m);
if (model != null)
{
prModel.Add(model);
}
}
if (prModel.Count > 0)
{
BackgroundProcessManager bpm = new BackgroundProcessManager(PrevuEFContext.PrevuEntity);
bpm.BulkPeopleResourceRecordEditAsync(prModel, RequestID);
rt.Result = 0;
}
else
rt.Result = -2;
rt.Messages = reason;
return rt;
}
public ResultModel Save(List<APINonProjectTimeModel> mList)
{
ResultModel rt = new ResultModel();
rt.RequestID = this.RequestID;
rt.RequestType = "batch";
rt.Result = (int) RejectReason.OK;
if (mList.Count > 0)
{
List<NonProjectTimeModel> queueModel = new List<NonProjectTimeModel>();
foreach(APINonProjectTimeModel m in mList)
{
var npm = ConvertToNonProjectTimeModel(m);
if (npm != null)
{
bool good = true;
if (npm.Resources.Count == 0)
{
this.reason.Add(m.WorkEmail + " record was skipped (resource record not found) for request type:"+m.LeaveType+" and Date:"+m.LeaveDate);
rt.Result = (int) RejectReason.ResourceNotFound;
good = false;
}
if (npm.NonProjectTimeAllocations.Count == 0)
{
this.reason.Add(m.WorkEmail + " record was skipped (time allocation record not created) for request type:" + m.LeaveType + " and Date:" + m.LeaveDate );
rt.Result = (int) RejectReason.NoTimeAllocationFound;
good = false;
}
if (good)
queueModel.Add(npm);
}
else
{
this.reason.Add(m.WorkEmail + " record was skipped (model record failed to get created) for request type:" + m.LeaveType + " and Date:" + m.LeaveDate+". Please contact Prevu support to check the error");
rt.Result = (int) RejectReason.ResourceNotFound;
}
}
BackgroundProcessManager bpm = new BackgroundProcessManager(PrevuEFContext.PrevuEntity);
bpm.BulkNonProjectTimeAsync(queueModel, RequestID);
rt.Result = 0;
}
else
{
this.reason.Add("No input records provided");
rt.Result = (int) RejectReason.NoInputRecords;
}
rt.Messages = reason;
return rt;
}
private NonProjectTimeModel ConvertToNonProjectTimeModel(APINonProjectTimeModel m)
{
try
{
return new NonProjectTimeModel()
{
NonProjectTimeName=m.LeaveType,
Details=m.RequestStatus,
NonProjectTimeCost=0,
NonProjectTimeCategoryId=getNPTCategoryID(m.LeaveType),
Resources =GetResourceIdList(m.WorkEmail),
EffectiveDateOfChange=m.LeaveDate,
NonProjectTimeEndDate=m.LeaveDate,
NonProjectTimeStartDate=m.LeaveDate,
NonProjectTimeAllocations=BuildAllocationModel(m)
};
}catch(Exception dd)
{
Settings.Logger.Log(NLog.LogLevel.Debug, "Exception in ConvertToNonProjectTimeModel model!");
Guid code = new Guid();
Settings.Logger.Log(NLog.LogLevel.Error, Environment.NewLine +
"------------------------------------------------------------------------------" + Environment.NewLine + code + Environment.NewLine + dd +
"------------------------------------------------------------------------------" +
Environment.NewLine + Environment.NewLine);
reason.Add("There was an error in the process. Please contact the system admin. Error Code:" + code);
MongoManager.StoreAPINonProjectTimeInMongo(m, reason);
}
return null;
}
private List<NonProjectTimeAllocationModel> BuildAllocationModel(APINonProjectTimeModel m)
{
List<NonProjectTimeAllocationModel> rt = new List<NonProjectTimeAllocationModel>();
try
{
int leaveamt = (m.LeaveAmount < 0) ? m.LeaveAmount * -1 : m.LeaveAmount;
var dates = FiscalCalendarManager.GetFiscalCalendarWeekForDate(m.LeaveDate, PrevuEFContext.PrevuEntity);
if (leaveamt > 0)
rt.Add(new NonProjectTimeAllocationModel()
{
HoursOff = leaveamt,
WeekStartDate = dates.StartDate,
WeekEndingDate = dates.EndDate
});
else
{
Settings.Logger.Log(NLog.LogLevel.Debug, "Leave amount is zero: "+m.WorkEmail+" "+m.LeaveDate+" "+m.LeaveType);
Guid code = new Guid();
Settings.Logger.Log(NLog.LogLevel.Error, Environment.NewLine +
"------------------------------------------------------------------------------" + Environment.NewLine + code +
"------------------------------------------------------------------------------" +
Environment.NewLine + Environment.NewLine);
reason.Add("Record skipped for "+m.WorkEmail+" "+m.LeaveDate+" "+m.LeaveType +" Leave amount was set to zero. ref code:"+ code);
MongoManager.StoreAPINonProjectTimeInMongo(m, reason);
}
}
catch (Exception dd)
{
Settings.Logger.Log(NLog.LogLevel.Debug, "Exception in BuildAllocationModel!");
Guid code = new Guid();
Settings.Logger.Log(NLog.LogLevel.Error, Environment.NewLine +
"------------------------------------------------------------------------------" + Environment.NewLine + code + Environment.NewLine + dd +
"------------------------------------------------------------------------------" +
Environment.NewLine + Environment.NewLine);
reason.Add("There was an error in the process. Please contact the system admin. Error Code:" + code);
MongoManager.StoreAPINonProjectTimeInMongo(m, reason);
}
return rt;
}
private List<Guid> GetResourceIdList(string email)
{
List<Guid> rt = new List<Guid>();
try
{
var resource = _manager.RetrieveReadOnlyByEmail(email);
if (resource == null)
return rt;
rt.Add(resource.Id);
}
catch (Exception dd)
{
Settings.Logger.Log(NLog.LogLevel.Debug, "Exception in GetResourceIdList!");
Guid code = new Guid();
Settings.Logger.Log(NLog.LogLevel.Error, Environment.NewLine +
"------------------------------------------------------------------------------" + Environment.NewLine + code + Environment.NewLine + dd +
"------------------------------------------------------------------------------" +
Environment.NewLine + Environment.NewLine);
reason.Add("There was an error in the process. Please contact the system admin. Error Code:" + code);
}
return rt;
}
private Guid getNPTCategoryID(string leavetype)
{
try
{
var context = PrevuEFContext.PrevuEntity;
NonProjectTimeCategoryManager _nptCatManager = new NonProjectTimeCategoryManager(context);
var leaveCatModel = _nptCatManager.RetrieveReadOnlyByName(leavetype);
if (leaveCatModel == null)
{
NonProjectTimeCategoryEditModel e = new NonProjectTimeCategoryEditModel()
{
Name = leavetype
};
leaveCatModel = _nptCatManager.Save(e);
context.SaveChanges();
}
return leaveCatModel.Id;
}
catch (Exception dd)
{
Settings.Logger.Log(NLog.LogLevel.Debug, "Exception in getNPTCategoryID!");
Guid code = new Guid();
Settings.Logger.Log(NLog.LogLevel.Error, Environment.NewLine +
"------------------------------------------------------------------------------" + Environment.NewLine + code + Environment.NewLine + dd +
"------------------------------------------------------------------------------" +
Environment.NewLine + Environment.NewLine);
reason.Add("There was an error in the process. Please contact the system admin. Error Code:" + code);
}
return Guid.Empty;
}
private PeopleResourceAPIModel getPrevuPRModel(APIPeopleResourceModel m)
{
if (m == null)
return null;
try {
return new PeopleResourceAPIModel()
{
CreditNumber = m.CostCenter,
EmailAddress = m.Email,
EmployeeID = m.EmployeeId,
EndDate = m.EndDate,
FirstName = m.FirstName,
IsActiveEmployee = m.isActive,
JobCode = m.JobCode,
LastName = m.LastName,
LineNumber = 1,
ReassignOnDeactivation = getReassignSwitch(),
StartDate = m.StartDate,
Title = m.Title
};
}catch(Exception dd)
{
Settings.Logger.Log(NLog.LogLevel.Debug, "Exception in Update model!");
Guid code = new Guid();
Settings.Logger.Log(NLog.LogLevel.Error, Environment.NewLine +
"------------------------------------------------------------------------------" + Environment.NewLine + code + Environment.NewLine + dd +
"------------------------------------------------------------------------------" +
Environment.NewLine + Environment.NewLine);
reason.Add("There was an error in the process. Please contact the system admin. Error Code:" + code);
MongoManager.StoreAPIPeopleResourceInMongo(m, reason);
}
return null;
}
private bool getReassignSwitch()
{
bool reassign = false;
Settings _settings = new Settings();
var sw = _settings.GetSettingValue(_settings.REASSIGN_ALLOCATIONS);
if (sw == null)
sw = "";
reassign = sw.Trim().ToLower() == "y";
return reassign;
}
private APIPeopleResourceModel getAPIModel(PeopleResource r)
{
if (r == null)
return null;
bool isactive = r.EndDate <= DateTime.Now;
string costcenter = null;
try
{
costcenter = r.ExpenditureCategory.CreditDepartment.Name;
} catch (Exception) { }
string jobcode = null;
try
{
jobcode = r.ExpenditureCategory.Expenditure.jobcode;
}
catch (Exception) { }
return new APIPeopleResourceModel()
{
Email= r.Email,
EmployeeId= r.EmployeeID,
EndDate= r.EndDate,
Title=r.ExpenditureCategory.Name,
FirstName= r.FirstName,
LastName= r.LastName,
StartDate= r.StartDate,
isActive= isactive,
CostCenter= costcenter,
JobCode= jobcode
};
}
}
}