I have a method where I do a certain validation and depending on the scenario, I need to return only one alert and continue with the request or a possible BadRequest also returning a message.
I thought of trying to access a variable within my if using the angular. Or send some feedback to my controller of the angular .
I need to get the variable that is in if :
(if (damMesPrevious! = null))
In case it meets this validation.
private async Task VerificarDam(NotaViewModel nota, Prestador prestador)
{
var timezone = TimeZoneInfo.FindSystemTimeZoneById("E. South America Standard Time");
var dataAtual = TimeZoneInfo.ConvertTime(DateTime.Now, timezone);
if (dataAtual.Day >= 16)
{
var damDoisMesesAnterior = await (DbContext.ValidarPagamentoDAM(nota.Competencia.Value, prestador.Id));
var damMesAnterior = await (DbContext.VerificarDamCompAnterior(nota.Competencia.Value, prestador.Id));
if (damMesAnterior != null)
{
//Nesse bloco que preciso enviar essa variavel para o front
var msg = "Mensagem de teste atraso.....";
}
if (damDoisMesesAnterior != null)
{
if (prestador.DataLiberacao != null)
{
if (prestador.DataLiberacao < dataAtual)
{
BadRequest(
"Existem pendências. Entre em contato com ...");
}
}
}
}
}
Angular controller where I get the request data:
$scope.emitir = function () {
notaService.emitir($scope.nota).then(function (results) {
var nota = results.data;
logService.success('teste mensagem de sucesso.');
var email = nota.user.email;
notaService.emitirXML(nota.value2.prestador.value1, nota.value2, nota.value3).then(function (resultsXML) {
if (email) {
var url = 'nota.html#/nota/' + nota.prestador.value1 + '/' + nota.value2 + '/' + notaGerada.verificador;
var baseLen = $location.absUrl().length - $location.url().length;
url = $location.absUrl().substring(0, baseLen - 1) + url;
notaService.enviarNota(url, email).then(function (results) {
notaService.apagarXML(nota.prestador.value1, nota.value2, nota.value3).then(function (result) {
}, function (error)
{
logService.log(error);
console.log(error);
});
},
function (error)
{
logService.log(error);
console.log(error);
});
}
}, function (error) {
logService.log(error);
});
$scope.newNota();
$scope.initNota();
$window.open('nota.html#/nota/' + nota.prestador.value1 + '/' + nota.value2 + '/' + nota.value3);
}, function (error) {
logService.log(error);
$scope.nota.valid = true;
});
};
Method is where I make certain validations and return to the public method (which is my action), which is accessed by angular:
public async Task<IHttpActionResult> Emitir(NotaViewModel nota)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
var prestador = await DbContext.GetContribuinteByUserIdAsync(this.User.Identity.GetUserId());
await VerificarDam(nota, prestador);
var valueNota = new Nota
{
Valor1 = nota.Valor1,
Valor2 = nota.Valor2,
Valor3 = nota.Valor3,
Valor = nota.CofiValorns,
};
DbContext.Servicos.Add(valueNota);
await DbContext.SaveChangesAsync();
nota = await GetNotaAsync(valueNota);
return Ok(nota);
}