public bool Create(DealCreateApiModel apiModel, int createdUser) { var validator = _dealValidator.Validate(apiModel); if (validator.IsValid) { return(_dealRepository.Create(apiModel, createdUser)); } return(false); }
public bool Update(int id, DealCreateApiModel apiModel, int modifiedUser) { var validator = _dealValidator.Validate(apiModel); if (validator.IsValid) { return(_dealRepository.Update(id, apiModel, modifiedUser)); } return(false); }
public bool Update(int dealId, DealCreateApiModel apiModel, int modifiedUser) { var dbDeal = db.DEALs.Find(dealId); if (dbDeal != null) { if (apiModel.account != 0) { dbDeal.ACCOUNT_ID = apiModel.account; } dbDeal.Amount = apiModel.amount; if (apiModel.closingDate != null) { dbDeal.ClosingDate = DbDateHelper.ToNullIfTooEarlyForDb(apiModel.closingDate.Value); } if (apiModel.expectedClosingDate != null) { dbDeal.ExpectedClosingDate = DbDateHelper.ToNullIfTooEarlyForDb(apiModel.expectedClosingDate.Value); } if (apiModel.contact != 0) { dbDeal.Contact_ID = apiModel.contact; } if (apiModel.owner != 0) { dbDeal.DealOwner = apiModel.owner; } dbDeal.Description = apiModel.description; dbDeal.ExpectedRevenue = apiModel.expectedRevenue; if (apiModel.stage != 0) { var dbStage = db.STAGEs.Find(apiModel.stage); dbDeal.ExpectedRevenue = (long)((apiModel.amount * dbStage.Probability) / 100); var newStageHistory = new STAGE_HISTORY(); newStageHistory.ModifiedBy = modifiedUser; newStageHistory.ModifiedAt = DateTime.Now; newStageHistory.STAGE_ID = apiModel.stage; if (apiModel.stage == (int)EnumStage.LOST) { dbDeal.isLost = true; dbDeal.ClosingDate = newStageHistory.ModifiedAt; } else { dbDeal.isLost = false; } if (apiModel.stage == (int)EnumStage.WON) { dbDeal.ClosingDate = newStageHistory.ModifiedAt; } dbDeal.STAGE_HISTORY.Add(newStageHistory); if (!String.IsNullOrEmpty(apiModel.lostReason)) { //find lost reason var lostReason = db.LOST_REASON.Where(c => c.Reason.ToLower().Contains(apiModel.lostReason.ToLower())).FirstOrDefault(); if (lostReason == null) { var newLostReason = new LOST_REASON(); newLostReason.Reason = apiModel.lostReason; db.LOST_REASON.Add(newLostReason); dbDeal.LOST_REASON = newLostReason; //newLostReason.DEALs.Add(dbDeal); } else { dbDeal.LOST_REASON_ID = lostReason.ID; //lostReason.DEALs.Add(dbDeal); } //dbDeal.LOST_REASON_ID = apiModel.lostReason; } } dbDeal.Name = apiModel.name; if (apiModel.priority != 0) { dbDeal.PRIORITY_ID = apiModel.priority; } if (apiModel.campaign != 0) { dbDeal.CAMPAIGN_ID = apiModel.campaign; } if (apiModel.contact != 0) { dbDeal.Contact_ID = apiModel.contact; } dbDeal.ModifiedAt = DateTime.Now; dbDeal.ModifiedBy = modifiedUser; db.SaveChanges(); var owner = db.USERs.Find(dbDeal.DealOwner); var modifyUser = db.USERs.Find(modifiedUser); var creator = db.USERs.Find(dbDeal.CreatedBy); var notifyModel = new NotificationApiModel(); notifyModel.title = "Deal updated"; notifyModel.content = $"Deal {dbDeal.Name} has been updated by {modifyUser?.Username}."; notifyModel.createdAt = DateTime.Now; notifyModel.module = "deals"; notifyModel.moduleObjectId = dbDeal.ID; NotificationManager.SendNotification(notifyModel, new List <USER> { owner, creator }); NotificationManager.ReloadDashboardSale(); return(true); } else { return(false); } }