WS finial test

This commit is contained in:
Mr Swift
2024-09-30 12:55:01 +03:30
parent b1f792f493
commit 9fa38c8adb
6 changed files with 209 additions and 63 deletions
+35
View File
@@ -41,6 +41,7 @@
"nuxt-leaflet": "^0.0.27", "nuxt-leaflet": "^0.0.27",
"read-excel-file": "^5.2.2", "read-excel-file": "^5.2.2",
"socket.io": "^4.4.1", "socket.io": "^4.4.1",
"socket.io-client": "^4.8.0",
"validator": "^13.12.0", "validator": "^13.12.0",
"vue-persian-datetime-picker": "^2.10.3" "vue-persian-datetime-picker": "^2.10.3"
}, },
@@ -8774,6 +8775,18 @@
"node": ">=10.2.0" "node": ">=10.2.0"
} }
}, },
"node_modules/engine.io-client": {
"version": "6.6.1",
"resolved": "https://registry.npmjs.org/engine.io-client/-/engine.io-client-6.6.1.tgz",
"integrity": "sha512-aYuoak7I+R83M/BBPIOs2to51BmFIpC1wZe6zZzMrT2llVsHy5cvcmdsJgP2Qz6smHu+sD9oexiSUAVd8OfBPw==",
"dependencies": {
"@socket.io/component-emitter": "~3.1.0",
"debug": "~4.3.1",
"engine.io-parser": "~5.2.1",
"ws": "~8.17.1",
"xmlhttprequest-ssl": "~2.1.1"
}
},
"node_modules/engine.io-parser": { "node_modules/engine.io-parser": {
"version": "5.2.3", "version": "5.2.3",
"resolved": "https://registry.npmjs.org/engine.io-parser/-/engine.io-parser-5.2.3.tgz", "resolved": "https://registry.npmjs.org/engine.io-parser/-/engine.io-parser-5.2.3.tgz",
@@ -18688,6 +18701,20 @@
"ws": "~8.17.1" "ws": "~8.17.1"
} }
}, },
"node_modules/socket.io-client": {
"version": "4.8.0",
"resolved": "https://registry.npmjs.org/socket.io-client/-/socket.io-client-4.8.0.tgz",
"integrity": "sha512-C0jdhD5yQahMws9alf/yvtsMGTaIDBnZ8Rb5HU56svyq0l5LIrGzIDZZD5pHQlmzxLuU91Gz+VpQMKgCTNYtkw==",
"dependencies": {
"@socket.io/component-emitter": "~3.1.0",
"debug": "~4.3.2",
"engine.io-client": "~6.6.1",
"socket.io-parser": "~4.2.4"
},
"engines": {
"node": ">=10.0.0"
}
},
"node_modules/socket.io-parser": { "node_modules/socket.io-parser": {
"version": "4.2.4", "version": "4.2.4",
"resolved": "https://registry.npmjs.org/socket.io-parser/-/socket.io-parser-4.2.4.tgz", "resolved": "https://registry.npmjs.org/socket.io-parser/-/socket.io-parser-4.2.4.tgz",
@@ -22292,6 +22319,14 @@
"node": ">=4.0" "node": ">=4.0"
} }
}, },
"node_modules/xmlhttprequest-ssl": {
"version": "2.1.1",
"resolved": "https://registry.npmjs.org/xmlhttprequest-ssl/-/xmlhttprequest-ssl-2.1.1.tgz",
"integrity": "sha512-ptjR8YSJIXoA3Mbv5po7RtSYHO6mZr8s7i5VGmEk7QY2pQWyT1o0N+W1gKbOyJPUCGXGnuw0wqe8f0L6Y0ny7g==",
"engines": {
"node": ">=0.4.0"
}
},
"node_modules/xtend": { "node_modules/xtend": {
"version": "4.0.2", "version": "4.0.2",
"resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.2.tgz", "resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.2.tgz",
+1
View File
@@ -48,6 +48,7 @@
"nuxt-leaflet": "^0.0.27", "nuxt-leaflet": "^0.0.27",
"read-excel-file": "^5.2.2", "read-excel-file": "^5.2.2",
"socket.io": "^4.4.1", "socket.io": "^4.4.1",
"socket.io-client": "^4.8.0",
"validator": "^13.12.0", "validator": "^13.12.0",
"vue-persian-datetime-picker": "^2.10.3" "vue-persian-datetime-picker": "^2.10.3"
}, },
@@ -1,19 +1,48 @@
const Notification = require('../../models/GPS.Notif') const Notification = require('../../models/GPS.Notif')
module.exports.getAllNotifs = async socket => {
try {
console.log('4');
module.exports.notifyGPS = async (type, gps_id) => { const notif = await Notification.find({ userID: socket.userId, read: false }).sort({ updated_at: -1 })
console.log(notif);
socket.emit(`gps:allNotifs`, notif)
} catch (e) {
console.log(e)
}
}
module.exports.notifyGPS = async (type, userId) => {
try { try {
const io = global._io const io = global._io
const gps = io.of('/gps') const gps = io.of('/gps')
const data = { type } const data = { type }
if (type === 'updateGPSInbox') { if (type === 'updateGPSInbox') {
const gpsInbox = await Notification.find({ userID:gps_id, seen: false }).sort({updated_at: -1}) const gpsInbox = await Notification.find({ userID: userId, read: false }).sort({ updated_at: -1 })
data.gpsInbox = gpsInbox data.gpsInbox = gpsInbox
} }
gps.to(gps_id.toString()).emit(`GPS:notify`, data) gps.to(userId.toString()).emit(`gps:notify`, data)
} catch (e) { } catch (e) {
console.log(e) console.log(e)
} }
} }
module.exports.fetchNotifications = async (socket) => {
try {
console.log('5');
const notif = await Notification.find({ userID: socket.userId, read: false }).sort({ updated_at: -1 })
console.log(notif);
socket.emit('gps:notifications', notif)
} catch (e) {
console.log(e)
}
}
@@ -69,16 +69,26 @@ module.exports.isAgent = async (socket, next) => {
module.exports.isGPS = async (socket, next) => { module.exports.isGPS = async (socket, next) => {
try { try {
console.log('1');
const token = socket.handshake?.auth?.token const token = socket.handshake?.auth?.token
if (!token) throw new Error('unauthenticated') if (!token) {
console.log('unauthorized 1');
throw new Error('unauthorized')
}
// verifies secret and checks if the token is expired // verifies secret and checks if the token is expired
const decodedToken = await jwt.verify(token.replace(/^Bearer\s/, ''), authentication.secretKey) const decodedToken = await jwt.verify(token.replace(/^Bearer\s/, ''), authentication.secretKey)
if (!decodedToken) throw new Error('unauthorized') if (!decodedToken) {
console.log('unauthorized 2');
throw new Error('unauthorized')
}
// find user // find user
const user = await UserGPS.findById(decodedToken._id) const user = await UserGPS.findById(decodedToken._id)
if (!user || !user.active) throw new Error('unauthorized') if (!user || !user.active) {
console.log('unauthorized 3');
throw new Error('unauthorized')
}
socket.userId = decodedToken._id.toString() socket.userId = decodedToken._id.toString()
return next() return next()
+4 -3
View File
@@ -4,13 +4,15 @@ const gpsEventsController = require('../controllers/GPS_EventsController')
module.exports.gpsNamespace = () => { module.exports.gpsNamespace = () => {
const io = global._io const io = global._io
const gps = io.of('/gps') const gps = io.of('/gps')
console.log('2');
// gpss middlewares // gpss middlewares
gps.use(isGPS) gps.use(isGPS)
console.log('3');
// attach events // attach events
gps.on('connection', socket => { gps.on('connection', socket => {
socket.join(socket.gpsId) socket.join(socket.userId)
gpsEventsController.getAllNotifs(socket) gpsEventsController.getAllNotifs(socket)
/// /////////////////////////////////////////////////////// handle connection error /// /////////////////////////////////////////////////////// handle connection error
@@ -19,7 +21,6 @@ module.exports.gpsNamespace = () => {
}) })
/// ////////////////////////////////////////////////////// events /// ////////////////////////////////////////////////////// events
socket.on('gps:fetchAnnouncements', data => gpsEventsController.fetchAnnouncements(socket)) socket.on('gps:fetchNotifications', data => gpsEventsController.fetchNotifications(socket))
socket.on('gps:updateAnnouncement', data => gpsEventsController.updateAnnouncement(socket, data))
}) })
} }
+122 -52
View File
@@ -1,59 +1,129 @@
const nodemailer = require('nodemailer') const { io } = require("socket.io-client");
function sendConfirmationEmail() { const init = ()=>{
return new Promise(async (resolve, reject) => { const socket = io(`ws://127.0.0.1:5000/gps`, {
try { reconnectionDelay: 20000,
reconnectionDelayMax: 20000,
auth: {
token: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI2NmM2ZmYyY2NkMDJiNTNkMzRkMjNiYTYiLCJpYXQiOjE3MjczNTQ3OTYsImV4cCI6MTcyOTk0Njc5Nn0.7W3ZsjZbW-fbbZWCX1wxJ8Wfmdro5wM7ow-b7xwE4Fk"
}
})
// eslint-disable-next-line node/handle-callback-err
socket.on('connect_error', err => {})
/// /////////////////////////////////////////// events
socket.on('connect', () => {
// console.log('**************** You have access to agent notifications')
socket.emit('gps:fetchNotifications')
})
socket.on(`gps:allNotifs`, data => {
console.log(data);
})
socket.on(`gps:notifications`, data => {
console.log(data);
})
socket.on(`gps:notify`, data => {
if (data.type === 'updateAgentInbox') {
// update store
console.log(data);
}
if (data.type === 'newAgentInbox') {
console.log(data);
}
})
}
init()
// const nodemailer = require('nodemailer')
// function sendConfirmationEmail() {
// return new Promise(async (resolve, reject) => {
// try {
/// //// email configs // /// //// email configs
// const hostURL = 'www.asan-service.com' // // const hostURL = 'www.asan-service.com'
const transporter = nodemailer.createTransport({ // const transporter = nodemailer.createTransport({
host: 'smtp.c1.liara.email', // host: 'smtp.c1.liara.email',
port: 587, // port: 587,
secure: false, // true for 465, false for other ports // secure: false, // true for 465, false for other ports
auth: { // auth: {
user: 'awesome_bell_ncc1hi', // user: 'awesome_bell_ncc1hi',
pass: '509f8938-db95-4b8d-83e7-9ea8eccfd28b' // pass: '509f8938-db95-4b8d-83e7-9ea8eccfd28b'
}, // },
tls: { // tls: {
rejectUnauthorized: false // rejectUnauthorized: false
} // }
}) // })
const emailHTMLTemplate = ` // const emailHTMLTemplate = `
<div style="direction: rtl;background-color: #065495;overflow: hidden;"> // <div style="direction: rtl;background-color: #065495;overflow: hidden;">
<div // <div
style="width: 70%;border-radius: 10px;margin: 150px auto;background: #fff;text-align: center;padding: 20px;box-shadow: 0 15px 30px #000;" // style="width: 70%;border-radius: 10px;margin: 150px auto;background: #fff;text-align: center;padding: 20px;box-shadow: 0 15px 30px #000;"
> // >
<h1 style="color: #000;font-size: 25px;margin-bottom: 20px;"> // <h1 style="color: #000;font-size: 25px;margin-bottom: 20px;">
<span> این ایمیل در وبسایت</span> // <span> این ایمیل در وبسایت</span>
<strong style="color: #065495"> آسان سرویس </strong> // <strong style="color: #065495"> آسان سرویس </strong>
<span> ثبت شده است. </span> // <span> ثبت شده است. </span>
</h1> // </h1>
<p style="color: #000;">کد تایید آدرس ایمیل شما:</p> // <p style="color: #000;">کد تایید آدرس ایمیل شما:</p>
<div style="text-align: center;margin-top: 5px;"> // <div style="text-align: center;margin-top: 5px;">
<p style="color: #065495;text-align: center;font-weight: bold;font-size: 20px;direction: ltr;"> // <p style="color: #065495;text-align: center;font-weight: bold;font-size: 20px;direction: ltr;">
${54534} // ${54534}
</p> // </p>
</div> // </div>
</div> // </div>
</div> // </div>
` // `
// send mail with defined transport object // // send mail with defined transport object
await transporter.sendMail({ // await transporter.sendMail({
from: '"AsanService" <confirmation@asan-service.com>', // sender address // from: '"AsanService" <confirmation@asan-service.com>', // sender address
to: "www.amir.com007@gmail.com", // list of receivers // to: "www.amir.com007@gmail.com", // list of receivers
subject: 'تایید ایمیل', // subject: 'تایید ایمیل',
text: emailHTMLTemplate, // text: emailHTMLTemplate,
html: emailHTMLTemplate // html: emailHTMLTemplate
}) // })
resolve() // resolve()
} catch (err) { // } catch (err) {
reject(err) // reject(err)
} // }
}) // })
} // }
sendConfirmationEmail() // sendConfirmationEmail()