public JsonResult SubmitTemplateSurveyResultByRefKey(UserSurveyResultRequest surveyResult) { //var username = "******"; var refKey = Request.QueryString["refKey"].ToString(CultureInfo.InvariantCulture); var headers = new HeaderManager(Request); M2ESession session = TokenManager.getSessionInfo(headers.AuthToken, headers); var userTemplateList = new UserProductSurveyTemplateService(); var isValidToken = TokenManager.IsValidSession(headers.AuthToken); if (isValidToken) { return Json(userTemplateList.SubmitTemplateSurveyResultByRefKey(surveyResult, refKey, session.UserName)); } else { ResponseModel<string> response = new ResponseModel<string>(); response.Status = 401; response.Message = "Unauthorized"; return Json(response); } }
public ResponseModel<string> SubmitTemplateSurveyResultByRefKey(UserSurveyResultRequest EntireSurveyResult, string refKey,string username) { var response = new ResponseModel<string>(); const string status_done = "done"; const string status_assigned = "assigned"; const string status_reviewed = "reviewed"; if (EntireSurveyResult == null) { response.Status = 403; response.Message = "No data to save to database."; return response; } var UserAlreadySubmittedForThisSurvey = _db.UserJobMappings.SingleOrDefault(x => x.refKey == refKey && x.username == username && x.status == status_done); if(UserAlreadySubmittedForThisSurvey != null) { response.Status = 405; response.Message = "You cann't submit same survey Twice."; return response; } if (EntireSurveyResult.surveySingleAnswerQuestion != null) { foreach (var userSurveyResult in EntireSurveyResult.surveySingleAnswerQuestion) { var surveyResponse = new UserSurveyResultToBeRevieweds1(); surveyResponse.questionId = userSurveyResult.key.Split('-')[1]; surveyResponse.type = userSurveyResult.key.Split('-')[0]; surveyResponse.answer = userSurveyResult.value; surveyResponse.refKey = refKey; surveyResponse.username = username; _db.UserSurveyResultToBeRevieweds1.Add(surveyResponse); } } if (EntireSurveyResult.surveyMultipleAnswerQuestion != null) { foreach (var userSurveyResult in EntireSurveyResult.surveyMultipleAnswerQuestion) { string[] optionValues = userSurveyResult.value.Split(';'); for (int i = 0; i < optionValues.Length - 1; i++) { var surveyResponse = new UserSurveyResultToBeRevieweds1(); surveyResponse.questionId = userSurveyResult.key.Split('-')[1]; surveyResponse.type = userSurveyResult.key.Split('-')[0]; surveyResponse.answer = optionValues[i]; surveyResponse.refKey = refKey; surveyResponse.username = username; _db.UserSurveyResultToBeRevieweds1.Add(surveyResponse); } } } if (EntireSurveyResult.surveyListBoxAnswerQuestion != null) { foreach (var userSurveyResult in EntireSurveyResult.surveyListBoxAnswerQuestion) { var surveyResponse = new UserSurveyResultToBeRevieweds1(); surveyResponse.questionId = userSurveyResult.key.Split('-')[1]; surveyResponse.type = userSurveyResult.key.Split('-')[0]; surveyResponse.answer = userSurveyResult.value; surveyResponse.refKey = refKey; surveyResponse.username = username; _db.UserSurveyResultToBeRevieweds1.Add(surveyResponse); } } if (EntireSurveyResult.surveyTextBoxAnswerQuestion != null) { foreach (var userSurveyResult in EntireSurveyResult.surveyTextBoxAnswerQuestion) { var surveyResponse = new UserSurveyResultToBeRevieweds1(); surveyResponse.questionId = userSurveyResult.key.Split('-')[1]; surveyResponse.type = userSurveyResult.key.Split('-')[0]; surveyResponse.answer = userSurveyResult.value; surveyResponse.refKey = refKey; surveyResponse.username = username; _db.UserSurveyResultToBeRevieweds1.Add(surveyResponse); } } var surveyThreadUserJobMapping = _db.UserJobMappings.SingleOrDefault(x => x.username == username && x.refKey == refKey); surveyThreadUserJobMapping.status = status_done; surveyThreadUserJobMapping.endTime = DateTime.Now.ToString(); try { _db.SaveChanges(); var clientJobInfo = _db.CreateTemplateQuestionInfoes.SingleOrDefault(x => x.referenceId == refKey); //var payment = new UserReputationService().UpdateUserBalance(Constants.userType_user, username, Convert.ToDouble(_db.CreateTemplateQuestionInfoes.SingleOrDefault(x => x.referenceId == refKey).payPerUser), 0, Constants.payment_credit, clientJobInfo.title, clientJobInfo.type, clientJobInfo.subType); //if (!payment) //logger.Info("payment failed for user : "******" of amount : " + _db.CreateTemplateQuestionInfoes.SingleOrDefault(x => x.referenceId == refKey).payPerUser); long JobId = clientJobInfo.Id; long JobCompleted = _db.UserJobMappings.Where(x => x.refKey == refKey && x.status == status_done).Count(); long JobAssigned = _db.UserJobMappings.Where(x => x.refKey == refKey && x.status == status_assigned).Count(); long JobReviewed = (JobCompleted > 1) ? (JobCompleted) / 2 : 0; // currently hard coded. bool status = new UserUpdatesClientRealTimeData().UpdateClientRealTimeData(JobId, JobCompleted, JobAssigned, JobReviewed, clientJobInfo.totalThreads, clientJobInfo.username); response.Status = 200; response.Message = "success-"; response.Payload = refKey; } catch (DbEntityValidationException e) { DbContextException.LogDbContextException(e); response.Status = 500; response.Message = "Failed"; response.Payload = "Exception Occured"; } return response; }