somewhere
This commit is contained in:
@@ -0,0 +1,112 @@
|
||||
const User = require('../../models/User')
|
||||
const ContactPageMessage = require('../../models/ContactPageMessage')
|
||||
const TicketConversation = require('../../models/TicketConversation')
|
||||
const Representation = require('../../models/Representation')
|
||||
const PieceRequest = require('../../models/PieceRequest')
|
||||
const OldPieceRequest = require('../../models/OldPieceRequest')
|
||||
const BufferRequest = require('../../models/BufferRequest')
|
||||
const GuaranteeReport = require('../../models/GuaranteeReport')
|
||||
|
||||
module.exports.getAllNotifs = async socket => {
|
||||
try {
|
||||
const allData = await Promise.all([
|
||||
// unreadCuMsgs
|
||||
ContactPageMessage.find({ read: false }),
|
||||
|
||||
// unreadTickets
|
||||
TicketConversation.find({ messages: { $elemMatch: { isUser: true, read: false } } }),
|
||||
|
||||
// newCustomers
|
||||
User.find({ scope: ['user'], seenByAdmin: false, confirmed: true }),
|
||||
|
||||
// newRepresentations
|
||||
Representation.find({ status: 'registered' }),
|
||||
|
||||
// newPieceRequests
|
||||
PieceRequest.find({ status: 'sent' }),
|
||||
|
||||
// newOldPieceRequests
|
||||
OldPieceRequest.find({ status: 'sent' }),
|
||||
|
||||
// newBufferRequests
|
||||
BufferRequest.find({ status: 'sent' }),
|
||||
|
||||
// newGuaranteeReports
|
||||
GuaranteeReport.find({ status: 'sent', seen: false })
|
||||
])
|
||||
|
||||
const unreadCuMsgs = allData[0]
|
||||
const unreadTickets = allData[1]
|
||||
const newCustomers = allData[2]
|
||||
const newRepresentations = allData[3]
|
||||
const newPieceRequests = allData[4]
|
||||
const newOldPieceRequests = allData[5]
|
||||
const newBufferRequests = allData[6]
|
||||
const newGuaranteeReports = allData[7]
|
||||
|
||||
socket.emit(`admin:allNotifs`, {
|
||||
unreadCuMsgs: unreadCuMsgs.length,
|
||||
unreadTickets: unreadTickets.length,
|
||||
newCustomers: newCustomers.length,
|
||||
newRepresentations: newRepresentations.length,
|
||||
newPieceRequests: newPieceRequests.length,
|
||||
newOldPieceRequests: newOldPieceRequests.length,
|
||||
newBufferRequests: newBufferRequests.length,
|
||||
newGuaranteeReports: newGuaranteeReports.length
|
||||
})
|
||||
} catch (e) {
|
||||
console.log(e)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports.notifyAdmin = async type => {
|
||||
try {
|
||||
const io = global._io
|
||||
const admin = io.of('/admin')
|
||||
const data = { type }
|
||||
|
||||
if (type === 'updateNewCustomers') {
|
||||
const newCustomers = await User.find({ scope: ['user'], seenByAdmin: false })
|
||||
data.newCustomers = newCustomers.length
|
||||
}
|
||||
|
||||
if (type === 'updateUnreadCuMsgs') {
|
||||
const unreadCuMsgs = await ContactPageMessage.find({ read: false })
|
||||
data.unreadCuMsgs = unreadCuMsgs.length
|
||||
}
|
||||
|
||||
if (type === 'updateUnreadTickets') {
|
||||
const unreadTickets = await TicketConversation.find({ messages: { $elemMatch: { isUser: true, read: false } } })
|
||||
data.unreadTickets = unreadTickets.length
|
||||
}
|
||||
|
||||
if (type === 'updateNewRepresentations') {
|
||||
const newRepresentations = await Representation.find({ status: 'registered' })
|
||||
data.newRepresentations = newRepresentations.length
|
||||
}
|
||||
|
||||
if (type === 'updateNewPieceRequests') {
|
||||
const newPieceRequests = await PieceRequest.find({ status: 'sent' })
|
||||
data.newPieceRequests = newPieceRequests.length
|
||||
}
|
||||
|
||||
if (type === 'updateNewOldPieceRequests') {
|
||||
const newOldPieceRequests = await OldPieceRequest.find({ status: 'sent' })
|
||||
data.newOldPieceRequests = newOldPieceRequests.length
|
||||
}
|
||||
|
||||
if (type === 'updateNewBufferRequests') {
|
||||
const newBufferRequests = await BufferRequest.find({ status: 'sent' })
|
||||
data.newBufferRequests = newBufferRequests.length
|
||||
}
|
||||
|
||||
if (type === 'updateNewGuaranteeReports') {
|
||||
const newGuaranteeReports = await GuaranteeReport.find({ status: 'sent', seen: false })
|
||||
data.newGuaranteeReports = newGuaranteeReports.length
|
||||
}
|
||||
|
||||
admin.emit(`admin:notify`, data)
|
||||
} catch (e) {
|
||||
console.log(e)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
const Transaction = require('../../models/Transaction')
|
||||
const Announcement = require('../../models/Announcement')
|
||||
|
||||
module.exports.getAllNotifs = async socket => {
|
||||
try {
|
||||
const agentInbox = await Transaction.find({ agent_id: socket.agentId, seen: false })
|
||||
|
||||
socket.emit(`agent:allNotifs`, {
|
||||
agentInbox: agentInbox.length
|
||||
})
|
||||
} catch (e) {
|
||||
console.log(e)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports.notifyAgent = async (type, agent_id) => {
|
||||
try {
|
||||
const io = global._io
|
||||
const agent = io.of('/agent')
|
||||
const data = { type }
|
||||
|
||||
if (type === 'updateAgentInbox') {
|
||||
const agentInbox = await Transaction.find({ agent_id, seen: false })
|
||||
data.agentInbox = agentInbox.length
|
||||
}
|
||||
|
||||
agent.to(agent_id.toString()).emit(`agent:notify`, data)
|
||||
} catch (e) {
|
||||
console.log(e)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports.notifyAllAgents = type => {
|
||||
try {
|
||||
const io = global._io
|
||||
const agent = io.of('/agent')
|
||||
const data = { type }
|
||||
|
||||
agent.emit('agent:notifyAll', data)
|
||||
} catch (e) {
|
||||
console.log(e)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports.fetchAnnouncements = async socket => {
|
||||
try {
|
||||
const announcements = await Announcement.find({ expired: false, isForAgents: true })
|
||||
const modifiedAnnouncements = []
|
||||
let newAnnouncementsCount = 0
|
||||
for await (const announcement of announcements) {
|
||||
const notif = { ...announcement._doc, seen: true }
|
||||
if (!notif.seenBy.includes(socket.userId)) {
|
||||
notif.seen = false
|
||||
newAnnouncementsCount++
|
||||
}
|
||||
delete notif.seenBy
|
||||
delete notif.expired
|
||||
delete notif.isForAgents
|
||||
delete notif._creator
|
||||
modifiedAnnouncements.push(notif)
|
||||
}
|
||||
|
||||
socket.emit('agent:announcements', {
|
||||
newAgentAnnosCount: newAnnouncementsCount,
|
||||
agentAnnouncements: modifiedAnnouncements.reverse()
|
||||
})
|
||||
} catch (e) {
|
||||
console.log(e)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports.updateAnnouncement = async (socket, id) => {
|
||||
try {
|
||||
if (id) {
|
||||
const announcment = await Announcement.findById(id)
|
||||
if (!announcment) throw new Error('invalid announcemnet id')
|
||||
if (!announcment.seenBy.includes(socket.userId)) announcment.seenBy.push(socket.userId)
|
||||
await announcment.save()
|
||||
socket.emit('agent:fetchNewAnnouncements')
|
||||
}
|
||||
} catch (e) {
|
||||
console.log(e)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
const TransactionDrafts = require('../../models/TransactionDraft')
|
||||
const TicketConversation = require('../../models/TicketConversation')
|
||||
const Announcement = require('../../models/Announcement')
|
||||
|
||||
module.exports.getAllNotifs = async socket => {
|
||||
try {
|
||||
const drafts = await TransactionDrafts.find({ user_id: socket.userId })
|
||||
const unreadTickets = await TicketConversation.find({
|
||||
user_id: socket.userId,
|
||||
messages: { $elemMatch: { isUser: false, read: false } }
|
||||
})
|
||||
|
||||
socket.emit(`user:allNotifs`, {
|
||||
unreadTickets: unreadTickets.length,
|
||||
draftsCount: drafts.length
|
||||
})
|
||||
} catch (e) {
|
||||
console.log(e)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports.notifyUser = async (type, user_id) => {
|
||||
try {
|
||||
const io = global._io
|
||||
const user = io.of('/user')
|
||||
const data = { type }
|
||||
|
||||
if (type === 'updateUnreadTickets') {
|
||||
const unreadTickets = await TicketConversation.find({
|
||||
user_id,
|
||||
messages: { $elemMatch: { isUser: false, read: false } }
|
||||
})
|
||||
|
||||
data.unreadTickets = unreadTickets.length
|
||||
}
|
||||
if (type === 'updateDraftsCount') {
|
||||
const drafts = await TransactionDrafts.find({ user_id })
|
||||
data.draftsCount = drafts.length
|
||||
}
|
||||
|
||||
user.to(user_id.toString()).emit(`user:notify`, data)
|
||||
} catch (e) {
|
||||
console.log(e)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports.notifyAllUsers = type => {
|
||||
try {
|
||||
const io = global._io
|
||||
const user = io.of('/user')
|
||||
const data = { type }
|
||||
|
||||
user.emit(`user:notifyAll`, data)
|
||||
} catch (e) {
|
||||
console.log(e)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports.fetchAnnouncements = async socket => {
|
||||
try {
|
||||
const announcements = await Announcement.find({ expired: false, isForAgents: false })
|
||||
const modifiedAnnouncements = []
|
||||
let newAnnouncementsCount = 0
|
||||
for await (const announcement of announcements) {
|
||||
const notif = { ...announcement._doc, seen: true }
|
||||
if (!notif.seenBy.includes(socket.userId)) {
|
||||
notif.seen = false
|
||||
newAnnouncementsCount++
|
||||
}
|
||||
delete notif.seenBy
|
||||
delete notif.expired
|
||||
delete notif.isForAgents
|
||||
delete notif._creator
|
||||
modifiedAnnouncements.push(notif)
|
||||
}
|
||||
|
||||
socket.emit('user:announcements', {
|
||||
newUserAnnosCount: newAnnouncementsCount,
|
||||
userAnnouncements: modifiedAnnouncements.reverse()
|
||||
})
|
||||
} catch (e) {
|
||||
console.log(e)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports.updateAnnouncement = async (socket, id) => {
|
||||
try {
|
||||
if (id) {
|
||||
const announcment = await Announcement.findById(id)
|
||||
if (!announcment) throw new Error('invalid announcemnet id')
|
||||
if (!announcment.seenBy.includes(socket.userId)) announcment.seenBy.push(socket.userId)
|
||||
await announcment.save()
|
||||
socket.emit('user:fetchNewAnnouncements')
|
||||
}
|
||||
} catch (e) {
|
||||
console.log(e)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user