forked from fl0zone/template-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilities.js
104 lines (93 loc) · 3.52 KB
/
utilities.js
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
const turf = require('turf');
const axios = require('axios');
const Papa = require('papaparse');
const nodemailer = require('nodemailer');
const { date } = require('zod');
require('dotenv').config();
const areaCoordinates = (lat, long, radius) => {
const point = turf.point([long,lat]);
const distanceToAdd = radius;
let west = turf.destination(point, distanceToAdd, -90);
west = west.geometry.coordinates[0];
let south = turf.destination(point, distanceToAdd, -180);
south = south.geometry.coordinates[1];
let east = turf.destination(point, distanceToAdd, 90);
east = east.geometry.coordinates[0];
let north = turf.destination(point, distanceToAdd, 0);
north = north.geometry.coordinates[1];
return {west, south, east, north};
}
exports.areaCoordinates = areaCoordinates;
const pointInCircle = (x, y, cx, cy, r) => {
const from = turf.point([x, y]);
const to = turf.point([cx, cy]);
const distance = turf.distance(from, to);
return distance <= r;
}
exports.pointInCircle = pointInCircle;
const dateString = (date) => {
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0');
const day = String(date.getDate()).padStart(2, '0');
return `${year}-${month}-${day}`;
}
exports.dateString = dateString;
const buildApiUrl = (coordinates, date) => {
const coordinatesString = `${coordinates.west},${coordinates.south},${coordinates.east},${coordinates.north}`;
const satellite = "VIIRS_NOAA20_NRT";
const mapKey = process.env.MAP_KEY;
return `https://firms.modaps.eosdis.nasa.gov/api/area/csv/${mapKey}/${satellite}/${coordinatesString}/1/${dateString(date)}`;
}
exports.buildApiUrl = buildApiUrl;
const retrieveThermalAnomalies = async (apiUrl) => {
let thermalAnomalies = []
try {
const response = await axios.get(apiUrl);
if (response.status === 200) {
Papa.parse(response.data, {
header: true,
skipEmptyLines: true,
complete: function (results) {
thermalAnomalies = results.data;
},
error: function (error) {
console.error('CSV parsing error:', error.message);
},
});
} else {
console.error('Failed to fetch CSV data from the API.');
}
} catch (error) {
console.error('Error fetching CSV data:', error);
}
return thermalAnomalies
}
exports.retrieveThermalAnomalies = retrieveThermalAnomalies;
const sendNotification = async (organization, newEvent) => {
const interestArea = organization.interestArea;
if (!interestArea) {
return false;
}
const hasSomeAnomaly = newEvent.thermalAnomalies.some(function (point) {
return pointInCircle(interestArea.latitude,interestArea.longitude,point.latitude,point.longitude,interestArea.radius)
})
if (!hasSomeAnomaly) {
return false;
}
const transporter = nodemailer.createTransport({
host: "smtp.sendgrid.net",
port: 465,
secure: true,
auth: {
user: "apikey",
pass: process.env.SENDGRID_API_KEY,
},
});
await transporter.sendMail({
from: process.env.SENDER_EMAIL,
to: organization.email,
subject: "Hay un posible INCENDIO en tu zona de interés",
text: `¡Revise ya la aplicación! El incendio está en ${newEvent.initialLatitude} (latitud) y ${newEvent.initialLongitude} (longitud).`
});
}
exports.sendNotification = sendNotification;