Configurar sistema de alertas
Implementa notificaciones y gestión de alertas:
// Sistema de alertas con persistencia
class ConcertAlertManager {
constructor(apiKey, notificationService) {
this.alertSystem = new ConcertAlertSystem(apiKey);
this.notificationService = notificationService;
this.alertedConcerts = this.loadAlertedConcerts();
}
async setupAlerts(artists, location) {
// Añadir artistas
for (const artist of artists) {
await this.alertSystem.addArtist(artist);
}
// Configurar ubicación
this.alertSystem.userLocation = location;
// Guardar configuración
this.saveConfiguration();
}
async checkAndAlert() {
const concerts = await this.alertSystem.checkConcerts();
const newConcerts = concerts.filter(concert =>
!this.alertedConcerts.has(concert.id)
);
// Enviar notificaciones
for (const concert of newConcerts) {
await this.sendAlert(concert);
this.alertedConcerts.set(concert.id, {
concert: concert,
alertedAt: new Date()
});
}
this.saveAlertedConcerts();
return newConcerts;
}
async sendAlert(concert) {
const message = this.formatAlertMessage(concert);
// Enviar notificación (email, push, etc.)
await this.notificationService.send({
title: `¡Concierto de ${concert.artist} anunciado!`,
message: message,
data: {
type: 'concert_alert',
concert: concert
}
});
}
formatAlertMessage(concert) {
const date = new Date(concert.date).toLocaleDateString('es-ES', {
weekday: 'long',
year: 'numeric',
month: 'long',
day: 'numeric'
});
return `
🎸 ¡Concierto de ${concert.artist}!
📅 Fecha: ${date}
📍 Lugar: ${concert.venue}, ${concert.city}
🎫 Entradas: ${concert.url}
¡No te lo pierdas!
`.trim();
}
// Persistencia en localStorage
loadAlertedConcerts() {
const stored = localStorage.getItem('alertedConcerts');
return stored ? new Map(JSON.parse(stored)) : new Map();
}
saveAlertedConcerts() {
localStorage.setItem('alertedConcerts', JSON.stringify([...this.alertedConcerts]));
}
}
// Programar verificación periódica
function scheduleConcertChecks(alertManager, intervalHours = 6) {
// Verificar inmediatamente
alertManager.checkAndAlert();
// Programar verificaciones
setInterval(() => {
alertManager.checkAndAlert();
}, intervalHours * 60 * 60 * 1000);
}