public async Task <DataMatchUploadResponse> Post([FromBody] DataMatchUploadRequest dataMatchUploadRequest) { DataMatchUploadResponse dataMatchUploadResponse = new DataMatchUploadResponse(); dataMatchUploadResponse.fileId = GetFileIdFromHeader(); dataMatchUploadResponse.businessId = GetBusinessIdFromHeader(); dataMatchUploadResponse.ResponseHeader = new ResponseHeader(dataMatchUploadRequest.RequestHeader); dataMatchUploadResponse.ResponseDetails = new List <ResponseDetail>(); foreach (Detail detail in dataMatchUploadRequest.RequestDetail) { ResponseDetail responseDetail = new ResponseDetail(detail); dataMatchUploadResponse.ResponseDetails.Add(responseDetail); } dataMatchUploadResponse = Validate(dataMatchUploadResponse); AddRecord(dataMatchUploadRequest); return(dataMatchUploadResponse); }
private DataMatchUploadResponse Validate(DataMatchUploadResponse dataMatchUploadResponse) { List <ErrorData> _errors = new List <ErrorData>(); int _totalRecords = dataMatchUploadResponse.ResponseDetails.Count; int _errorRecords = 0; #region HeaderValidations if (dataMatchUploadResponse.ResponseHeader.orderid == null) { _errors.Add(new ErrorData() { errorField = "orderId", errorCause = Constants.ErrorCause, errorExplanation = "orderId field is not present", errorValidationType = Constants.MissingFieldCategory }); dataMatchUploadResponse.ResponseHeader.orderid = "null"; } else { if (dataMatchUploadResponse.ResponseHeader.orderid == string.Empty) { _errors.Add(new ErrorData() { errorField = "orderid", errorCause = Constants.ErrorCause, errorExplanation = "value provided in orderId field is not valid", errorValidationType = Constants.InvalidFieldCategory }); dataMatchUploadResponse.ResponseHeader.orderid = "null"; } } if (dataMatchUploadResponse.ResponseHeader.ordertype == null) { _errors.Add(new ErrorData() { errorField = "orderType", errorCause = Constants.ErrorCause, errorExplanation = "orderType field is not present", errorValidationType = Constants.MissingFieldCategory }); dataMatchUploadResponse.ResponseHeader.ordertype = "null"; } if (dataMatchUploadResponse.ResponseHeader.ordertype != null && !(dataMatchUploadResponse.ResponseHeader.ordertype.ToLower() == "new" || dataMatchUploadResponse.ResponseHeader.ordertype.ToLower() == "existing")) { _errors.Add(new ErrorData() { errorField = "orderType", errorCause = Constants.ErrorCause, errorExplanation = "value provided in orderType field is not valid", errorValidationType = Constants.InvalidFieldCategory }); } if (dataMatchUploadResponse.ResponseHeader.ordertype.ToLower() == "new" && OrderIdExists(dataMatchUploadResponse.ResponseHeader.orderid)) { _errors.Add(new ErrorData() { errorField = "orderId", errorCause = Constants.ErrorCause, errorExplanation = "value provided in orderId field is duplicate", errorValidationType = Constants.InvalidFieldCategory }); } if (dataMatchUploadResponse.ResponseHeader.ordertype.ToLower() == "existing" && !OrderIdExists(dataMatchUploadResponse.ResponseHeader.orderid)) { _errors.Add(new ErrorData() { errorField = "orderid", errorCause = Constants.ErrorCause, errorExplanation = "value provided in orderId field does not exist", errorValidationType = Constants.MissingFieldCategory }); } if (dataMatchUploadResponse.ResponseHeader.businessid == null) { _errors.Add(new ErrorData() { errorField = "businessId", errorCause = Constants.ErrorCause, errorExplanation = "businessId field is not present", errorValidationType = Constants.MissingFieldCategory }); } else { if (dataMatchUploadResponse.ResponseHeader.businessid == string.Empty) { _errors.Add(new ErrorData() { errorField = "businessId", errorCause = Constants.ErrorCause, errorExplanation = "value provided in businessId field is not valid", errorValidationType = Constants.InvalidFieldCategory }); } } if (dataMatchUploadResponse.ResponseHeader.matchtype == string.Empty) { _errors.Add(new ErrorData() { errorField = "matchType", errorCause = Constants.ErrorCause, errorExplanation = "value provided in matchType is not valid", errorValidationType = Constants.InvalidFieldCategory }); } if (dataMatchUploadResponse.ResponseHeader.noofrecords == null) { _errors.Add(new ErrorData() { errorField = "noofRecords", errorCause = Constants.ErrorCause, errorExplanation = "noofRecords is not present", errorValidationType = Constants.MissingFieldCategory }); } else { if (dataMatchUploadResponse.ResponseHeader.noofrecords == string.Empty) { _errors.Add(new ErrorData() { errorField = "noofRecords", errorCause = Constants.ErrorCause, errorExplanation = "value provided in noofRecords field is not valid", errorValidationType = Constants.InvalidFieldCategory }); } } if (!IsValidEmail(dataMatchUploadResponse.ResponseHeader.email)) { _errors.Add(new ErrorData() { errorField = "email", errorCause = Constants.ErrorCause, errorExplanation = "value provided in email field is not valid", errorValidationType = Constants.InvalidFieldCategory }); } if (_errors.Any()) { dataMatchUploadResponse.ResponseHeader.errorData.AddRange(_errors); dataMatchUploadResponse.ResponseDetails = null; return(dataMatchUploadResponse); } #endregion #region DetailValidation for (int i = 0; i < _totalRecords; i++) { _errors = new List <ErrorData>(); if (dataMatchUploadResponse.ResponseDetails[i].errorData == null) { dataMatchUploadResponse.ResponseDetails[i].errorData = new List <ErrorData>(); } dataMatchUploadResponse.ResponseDetails[i].errorData.Clear(); if (dataMatchUploadResponse.ResponseDetails[i].id == null) { _errors.Add(new ErrorData() { errorField = "id", errorCause = Constants.ErrorCause, errorExplanation = "id is not present", errorValidationType = Constants.MissingFieldCategory }); } if (dataMatchUploadResponse.ResponseHeader.ordertype == "new" && dataMatchUploadResponse.ResponseDetails[i].id != null && (dataMatchUploadResponse.ResponseDetails.Where(_ => _.id == dataMatchUploadResponse.ResponseDetails[i].id).Count() > 1)) { _errors.Add(new ErrorData() { errorField = "id", errorCause = Constants.ErrorCause, errorExplanation = "value provided in id field is duplicate", errorValidationType = Constants.InvalidFieldCategory }); } if (dataMatchUploadResponse.ResponseHeader.ordertype == "existing" && OrderIdAndIdExists(dataMatchUploadResponse.ResponseHeader.orderid, dataMatchUploadResponse.ResponseDetails[i].id)) { _errors.Add(new ErrorData() { errorField = "id", errorCause = Constants.ErrorCause, errorExplanation = "value provided in id field does not exist in the order", errorValidationType = Constants.InvalidFieldCategory }); } if (dataMatchUploadResponse.ResponseDetails[i].requesttype == null) { _errors.Add(new ErrorData() { errorField = "requesttype", errorCause = Constants.ErrorCause, errorExplanation = "requestType is not present", errorValidationType = Constants.MissingFieldCategory }); dataMatchUploadResponse.ResponseDetails[i].requesttype = string.Empty; } else if (dataMatchUploadResponse.ResponseDetails[i].requesttype == string.Empty) { _errors.Add(new ErrorData() { errorField = "requesttype", errorCause = Constants.ErrorCause, errorExplanation = "value provided in requestType field is not valid", errorValidationType = Constants.InvalidFieldCategory }); } if ((dataMatchUploadResponse.ResponseHeader.ordertype.ToLower() == "new") && ((dataMatchUploadResponse.ResponseDetails[i].requesttype.ToLower() == "link") || (dataMatchUploadResponse.ResponseDetails[i].requesttype.ToLower() == "update"))) { _errors.Add(new ErrorData() { errorField = "requesttype", errorCause = Constants.ErrorCause, errorExplanation = "value provided in requestType field cannot be 'update' or 'link' ", errorValidationType = Constants.InvalidFieldCategory }); } if ((dataMatchUploadResponse.ResponseHeader.ordertype.ToLower() == "existing") && (dataMatchUploadResponse.ResponseDetails[i].requesttype.ToLower() == "update") && !IdTrackIdExists(dataMatchUploadResponse.ResponseDetails[i].id, dataMatchUploadResponse.ResponseDetails[i].trackid)) { _errors.Add(new ErrorData() { errorField = "trackid", errorCause = Constants.ErrorCause, errorExplanation = "value provided in the trackId field for requesting updates does not exist", errorValidationType = Constants.InvalidFieldCategory }); } //if (((dataMatchUploadResponse.ResponseDetails[i].requesttype.ToLower() == "update") || // (dataMatchUploadResponse.ResponseDetails[i].requesttype.ToLower() == "link")) && // (dataMatchUploadResponse.ResponseDetails[i].trackid == null)) //{ // _errors.Add(new ErrorData() // { // errorField = "trackid", // errorCause = CONSTANTS.ErrorCause, // errorExplanation = "value provided in the trackId field for requesting updates is not for a purchased record", // errorValidationType = CONSTANTS.MissingFieldCategory // }); //} if (((dataMatchUploadResponse.ResponseDetails[i].requesttype.ToLower() == "update") || (dataMatchUploadResponse.ResponseDetails[i].requesttype.ToLower() == "link")) && (dataMatchUploadResponse.ResponseDetails[i].trackid == null)) { _errors.Add(new ErrorData() { errorField = "trackid", errorCause = Constants.ErrorCause, errorExplanation = "trackId is not present", errorValidationType = Constants.MissingFieldCategory }); } else if (((dataMatchUploadResponse.ResponseDetails[i].requesttype.ToLower() == "update") || (dataMatchUploadResponse.ResponseDetails[i].requesttype.ToLower() == "link")) && (dataMatchUploadResponse.ResponseDetails[i].trackid == string.Empty)) { _errors.Add(new ErrorData() { errorField = "trackid", errorCause = Constants.ErrorCause, errorExplanation = "value provided in the trackId field is not valid", errorValidationType = Constants.InvalidFieldCategory }); } if (dataMatchUploadResponse.ResponseDetails[i].companyname == null) { _errors.Add(new ErrorData() { errorField = "companyName", errorCause = Constants.ErrorCause, errorExplanation = "companyName is not present", errorValidationType = Constants.MissingFieldCategory }); } if (dataMatchUploadResponse.ResponseDetails[i].monitoringType == string.Empty) { _errors.Add(new ErrorData() { errorField = "monitoringType", errorCause = Constants.ErrorCause, errorExplanation = "value provided in monitoring field is not valid", errorValidationType = Constants.InvalidFieldCategory }); } if (dataMatchUploadResponse.ResponseHeader.ordertype.ToLower() == "existing" && dataMatchUploadResponse.ResponseDetails[i].requesttype.ToLower() == "link" && dataMatchUploadResponse.ResponseDetails[i].trackid == null && dataMatchUploadResponse.ResponseDetails[i].linking != null && dataMatchUploadResponse.ResponseDetails[i].linking.linkcompliance.Any() && string.IsNullOrEmpty(dataMatchUploadResponse.ResponseDetails[i].linking.linkcompliance[0].referenceId)) { _errors.Add(new ErrorData() { errorField = "requestType", errorCause = Constants.ErrorCause, errorExplanation = "trackId or referenceId not present in the request for linking", errorValidationType = Constants.InvalidFieldCategory }); } if (dataMatchUploadResponse.ResponseHeader.ordertype.ToLower() == "existing" && dataMatchUploadResponse.ResponseDetails[i].requesttype.ToLower() == "link" && dataMatchUploadResponse.ResponseDetails[i].trackid != null && dataMatchUploadResponse.ResponseDetails[i].linking.linkcompliance != null && dataMatchUploadResponse.ResponseDetails[i].linking.linkcompliance.Count > 0 && dataMatchUploadResponse.ResponseDetails[i].linking.linkcompliance[0].referenceId != null) { _errors.Add(new ErrorData() { errorField = "link", errorCause = Constants.ErrorCause, errorExplanation = "trackId and referenceId should not be provided in the same request for linking", errorValidationType = Constants.InvalidFieldCategory }); } if (_errors.Any()) { dataMatchUploadResponse.ResponseDetails[i].errorData.AddRange(_errors); _errorRecords++; } } if (dataMatchUploadResponse.ResponseHeader.matchStatistics == null) { dataMatchUploadResponse.ResponseHeader.matchStatistics = new matchStatistics(); } dataMatchUploadResponse.ResponseHeader.matchStatistics.totalRecords = _totalRecords; dataMatchUploadResponse.ResponseHeader.matchStatistics.errorRecords = _errorRecords; #endregion return(dataMatchUploadResponse); }