Skip to content

Commit fdffe46

Browse files
atualizacoes para corrigir a funcionalidade notificacoes
1 parent 937c0cc commit fdffe46

6 files changed

Lines changed: 51 additions & 31 deletions

File tree

src/backend/Emprestimos.API/Controllers/EmprestimosController.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,11 @@ public async Task<IActionResult> MarcarComoPago(int id, string nomeCobrador)
201201
Tipo = "Pagamento",
202202
Data = DateTime.UtcNow,
203203
Lida = false,
204-
Mensagem = mensagem
205-
};
204+
Mensagem = mensagem,
205+
NumeroParcela = parcelasPagas,
206+
TotalParcelas = totalParcelas
207+
};
208+
206209

207210
await colNotificacoes.InsertOneAsync(notificacaoPagamento);
208211

src/backend/Emprestimos.API/Models/Notificacao.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ public class Notificacao
1717
public DateTime Data { get; set; } = DateTime.UtcNow;
1818
public DateTime DataCriacao { get; set; } = DateTime.UtcNow;
1919
public int? EmprestimoId { get; set; }
20-
public string Tipo { get; set; } = "Cobranca";
20+
public string Tipo { get; set; } = "Cobrança"; // ← com acento
21+
public int NumeroParcela { get; set; } = 0;
22+
public int TotalParcelas { get; set; } = 0;
2123
public decimal Valor { get; set; }
2224
public DateTime DataVencimento { get; set; }
2325
}

src/backend/Notificacoes.API/Controllers/NotificacoesController.cs

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
using Microsoft.AspNetCore.Mvc;
22
using MongoDB.Driver;
33
using Notificacoes.API.Models;
4-
using Microsoft.AspNetCore.Authorization;
54

65
namespace Notificacoes.API.Controllers;
7-
//[Authorize]
6+
87
[ApiController]
98
[Route("api/[controller]")]
109
public class NotificacoesController : ControllerBase
@@ -16,7 +15,6 @@ public NotificacoesController(IMongoDatabase database)
1615
_notificacoes = database.GetCollection<Notificacao>("notificacoes");
1716
}
1817

19-
2018
[HttpGet("cobrador/{nomeCobrador}")]
2119
public async Task<ActionResult<IEnumerable<Notificacao>>> GetPorCobrador(string nomeCobrador)
2220
{
@@ -28,25 +26,43 @@ public async Task<ActionResult<IEnumerable<Notificacao>>> GetPorCobrador(string
2826
return Ok(lista);
2927
}
3028

31-
// 3. Marca como lida (PATCH é melhor que PUT aqui)
29+
[HttpGet("cobrador/{nomeCobrador}/nao-lidas")]
30+
public async Task<IActionResult> GetNaoLidas(string nomeCobrador)
31+
{
32+
var count = await _notificacoes
33+
.CountDocumentsAsync(x => x.Cobrador == nomeCobrador && !x.Lida);
34+
35+
return Ok(new { total = count });
36+
}
37+
3238
[HttpPatch("{id:int}/lida")]
3339
public async Task<IActionResult> MarcarComoLida(int id)
3440
{
3541
var result = await _notificacoes.UpdateOneAsync(
36-
x => x.Id == id,
42+
x => x.Id == id,
3743
Builders<Notificacao>.Update.Set(x => x.Lida, true)
3844
);
3945

4046
if (result.MatchedCount == 0) return NotFound();
4147
return NoContent();
4248
}
4349

50+
[HttpPatch("cobrador/{nomeCobrador}/marcar-todas-lidas")]
51+
public async Task<IActionResult> MarcarTodasLidas(string nomeCobrador)
52+
{
53+
await _notificacoes.UpdateManyAsync(
54+
x => x.Cobrador == nomeCobrador && !x.Lida,
55+
Builders<Notificacao>.Update.Set(x => x.Lida, true)
56+
);
57+
58+
return NoContent();
59+
}
60+
4461
[HttpDelete("{id:int}")]
4562
public async Task<IActionResult> Delete(int id)
4663
{
4764
var resultado = await _notificacoes.DeleteOneAsync(x => x.Id == id);
4865
if (resultado.DeletedCount == 0) return NotFound();
4966
return NoContent();
5067
}
51-
52-
}
68+
}

src/backend/Notificacoes.API/Models/Notificacoes.cs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,21 @@
33

44
namespace Notificacoes.API.Models;
55

6-
76
public class Notificacao
87
{
9-
108
[BsonId]
119
public int Id { get; set; }
12-
public int ClienteId {get; set; }
13-
public string ClienteNome {get; set; } = null!;
14-
public string Cobrador {get; set; } = null!;
15-
10+
public int ClienteId { get; set; }
11+
public string ClienteNome { get; set; } = null!;
12+
public string Cobrador { get; set; } = null!;
1613
public string? Mensagem { get; set; }
17-
1814
public bool Lida { get; set; } = false;
19-
public DateTime Data { get; set;} = DateTime.UtcNow;
20-
15+
public DateTime Data { get; set; } = DateTime.UtcNow;
2116
public DateTime DataCriacao { get; set; } = DateTime.UtcNow;
22-
2317
public int? EmprestimoId { get; set; }
24-
public string Tipo {get; set; } = "Cobranca";
18+
public string Tipo { get; set; } = "Cobrança";
2519
public decimal Valor { get; set; }
26-
public DateTime DataVencimento {get; set;}
27-
}
28-
20+
public DateTime DataVencimento { get; set; }
21+
public int NumeroParcela { get; set; } = 0;
22+
public int TotalParcelas { get; set; } = 0;
23+
}

src/frontend/src/pages/Notificacoes.jsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { useEffect, useState } from "react";
22
import { getUsuarioLogado } from "../services/authService";
3-
import { getNotificacoes, marcarLida, deletarNotif } from "../services/NotificacoesService";
3+
import { getNotificacoes, marcarLida, marcarTodasLidas, deletarNotif } from "../services/NotificacoesService";
44

55

66
const fmt = (v) =>
@@ -54,10 +54,11 @@ export default function Notificacoes() {
5454
}
5555

5656
async function marcarTodas() {
57-
const naoLidas = lista.filter((n) => !n.lida);
58-
await Promise.all(naoLidas.map((n) => marcarLida(n.id)));
57+
try {
58+
await marcarTodasLidas(cobrador);
5959
setLista((prev) => prev.map((n) => ({ ...n, lida: true })));
60-
}
60+
} catch { }
61+
}
6162

6263
const naoLidas = lista.filter((n) => !n.lida).length;
6364

src/frontend/src/services/NotificacoesService.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { getToken } from "./authService";
22

33
const BASE_URL = `${import.meta.env.VITE_GATEWAY_URL ?? "http://localhost:5046"}/backend/Notificacoes`;
4+
45
function headers() {
56
return {
67
"Content-Type": "application/json",
@@ -14,6 +15,8 @@ async function req(url, options = {}) {
1415
return res.status === 204 ? null : res.json();
1516
}
1617

17-
export const getNotificacoes = (cobrador) => req(`${BASE_URL}/cobrador/${encodeURIComponent(cobrador)}`);
18-
export const marcarLida = (id) => req(`${BASE_URL}/${id}/lida`, { method: "PATCH" });
19-
export const deletarNotif = (id) => req(`${BASE_URL}/${id}`, { method: "DELETE" });
18+
export const getNotificacoes = (cobrador) => req(`${BASE_URL}/cobrador/${encodeURIComponent(cobrador)}`);
19+
export const getNaoLidas = (cobrador) => req(`${BASE_URL}/cobrador/${encodeURIComponent(cobrador)}/nao-lidas`);
20+
export const marcarLida = (id) => req(`${BASE_URL}/${id}/lida`, { method: "PATCH" });
21+
export const marcarTodasLidas = (cobrador) => req(`${BASE_URL}/cobrador/${encodeURIComponent(cobrador)}/marcar-todas-lidas`, { method: "PATCH" });
22+
export const deletarNotif = (id) => req(`${BASE_URL}/${id}`, { method: "DELETE" });

0 commit comments

Comments
 (0)