-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmock-notifications-server.mjs
More file actions
148 lines (135 loc) · 4.2 KB
/
mock-notifications-server.mjs
File metadata and controls
148 lines (135 loc) · 4.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/**
* Mock Notifications Server
*
* Servidor mock para desenvolvimento local sem backend.
* Roda na porta 3001 e serve rotas de notificações.
*
* Uso:
* node mock-notifications-server.mjs
*
* Atualize o vite.config.ts para apontar o proxy /api/notifications para
* http://localhost:3001 durante o desenvolvimento, ou aponte o proxy /api
* para este servidor se o backend real não estiver disponível.
*/
import http from "http";
const PORT = 3001;
const notifications = [
{
_id: "1",
type: "success",
title: "Reserva confirmada",
message: "Sua reserva no Chalé da Montanha foi confirmada para 15/08.",
read: false,
createdAt: new Date(Date.now() - 1000 * 60 * 5).toISOString(),
link: "/account/bookings",
},
{
_id: "2",
type: "info",
title: "Nova avaliação recebida",
message: "João deixou uma avaliação 5 estrelas na sua acomodação.",
read: false,
createdAt: new Date(Date.now() - 1000 * 60 * 60 * 2).toISOString(),
link: "/account/places",
},
{
_id: "3",
type: "warning",
title: "Pagamento pendente",
message: "Sua reserva aguarda confirmação de pagamento.",
read: true,
createdAt: new Date(Date.now() - 1000 * 60 * 60 * 24).toISOString(),
link: "/account/bookings",
},
{
_id: "4",
type: "info",
title: "Bem-vindo ao DormeAqui!",
message: "Explore acomodações incríveis e faça sua primeira reserva.",
read: true,
createdAt: new Date(Date.now() - 1000 * 60 * 60 * 24 * 3).toISOString(),
link: "/",
},
{
_id: "5",
type: "error",
title: "Pagamento recusado",
message: "O pagamento da reserva #5678 foi recusado. Verifique seu cartão.",
read: true,
createdAt: new Date(Date.now() - 1000 * 60 * 60 * 24 * 7).toISOString(),
link: "/account/bookings",
},
];
function sendJson(res, statusCode, data) {
const body = JSON.stringify(data);
res.writeHead(statusCode, {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET, POST, PATCH, DELETE, OPTIONS",
"Access-Control-Allow-Headers": "Content-Type, Authorization",
});
res.end(body);
}
const server = http.createServer((req, res) => {
const url = new URL(req.url, `http://localhost:${PORT}`);
const method = req.method.toUpperCase();
if (method === "OPTIONS") {
res.writeHead(204, {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET, POST, PATCH, DELETE, OPTIONS",
"Access-Control-Allow-Headers": "Content-Type, Authorization",
});
res.end();
return;
}
// GET /api/notifications?page=1&limit=10
if (method === "GET" && url.pathname === "/api/notifications") {
const page = parseInt(url.searchParams.get("page") ?? "1", 10);
const limit = parseInt(url.searchParams.get("limit") ?? "10", 10);
const start = (page - 1) * limit;
const slice = notifications.slice(start, start + limit);
sendJson(res, 200, {
notifications: slice,
total: notifications.length,
page,
});
return;
}
// PATCH /api/notifications/:id/read
if (method === "PATCH" && url.pathname.match(/^\/api\/notifications\/[^/]+\/read$/)) {
const id = url.pathname.split("/")[3];
const n = notifications.find((x) => x._id === id);
if (n) n.read = true;
sendJson(res, 200, { success: true });
return;
}
// PATCH /api/notifications/read-all
if (method === "PATCH" && url.pathname === "/api/notifications/read-all") {
notifications.forEach((n) => {
n.read = true;
});
sendJson(res, 200, { success: true });
return;
}
// DELETE /api/notifications/:id
if (method === "DELETE" && url.pathname.match(/^\/api\/notifications\/[^/]+$/)) {
const id = url.pathname.split("/")[3];
const idx = notifications.findIndex((x) => x._id === id);
if (idx !== -1) notifications.splice(idx, 1);
sendJson(res, 200, { success: true });
return;
}
// DELETE /api/notifications (clear all)
if (method === "DELETE" && url.pathname === "/api/notifications") {
notifications.splice(0, notifications.length);
sendJson(res, 200, { success: true });
return;
}
sendJson(res, 404, { error: "Not found" });
});
server.listen(PORT, () => {
console.log(`✅ Mock Notifications Server running at http://localhost:${PORT}`);
console.log(
` Proxy /api/notifications requests from Vite to this server for local dev.`,
);
});