Skip to content
This repository has been archived by the owner on Oct 8, 2024. It is now read-only.

Commit

Permalink
[Fix] downgrade mongodb query 4.4 to 3.2.
Browse files Browse the repository at this point in the history
  • Loading branch information
dvgamerr committed Jul 12, 2021
1 parent e55b751 commit dadd543
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 65 deletions.
2 changes: 1 addition & 1 deletion api/route-check/stats-notify.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const getStats = async (botname) => {
created: { $gte: date.toISOString() }
})

return { usage: push, limited: 1000, reply: 0, push, updated: dayjs().toDate() }
return { usage: push, limited: 1000, reply: 0, push: push || 0, updated: dayjs().toDate() }
}

module.exports = async (req) => {
Expand Down
97 changes: 33 additions & 64 deletions api/route-db/service/dashboard.js
Original file line number Diff line number Diff line change
@@ -1,73 +1,42 @@
const { notice } = require('@touno-io/db/schema')

module.exports = async (req) => {
const { LineBot, LineBotRoom, ServiceBot, ServiceBotOauth, ChatWebhook } = notice.get()
const { LineBot, LineBotRoom, ServiceBot, ServiceBotOauth } = notice.get()
const userId = req.headers['x-id']
if (userId) {
const bot = await LineBot.aggregate([
{ $match: { active: true, userId } },
{ $project: { _id: 1, text: '$name', value: '$botname', type: 'bot', stats: '$options.stats' } },
{
$lookup: {
as: 'room',
from: 'db-line-bot-room',
let: { botname: '$value' },
pipeline: [
{ $match: { active: true } },
{ $match: { $expr: { $eq: ['$botname', '$$botname'] } } },
{ $project: { _id: 0, name: 1, botname: 1 } },
{ $sort: { name: 1 } }
]
}
},
{ $sort: { botname: 1, name: 1 } }
])
const notify = await ServiceBot.aggregate([
{ $match: { active: true, userId } },
{ $project: { _id: 1, client: 1, secret: 1, text: '$name', value: '$service', type: 'notify' } },
{ $sort: { service: 1 } },
{
$lookup: {
as: 'room',
from: 'db-service-oauth',
let: { service: '$value' },
pipeline: [
{ $match: { accessToken: { $ne: null } } },
{ $match: { $expr: { $eq: ['$service', '$$service'] } } },
{ $project: { _id: 0, accessToken: 1, value: '$room', name: 1 } },
{ $sort: { value: 1, name: 1 } }
]
}
}
])
return { bot, notify }

if (!userId) { return { bot: [], notify: [] } }

const bot = await LineBot.aggregate([
{ $match: { active: true, userId } },
{ $project: { _id: 1, text: '$name', value: '$botname', type: '$bot', stats: '$options.stats' } },
{ $sort: { botname: 1, name: 1 } }
])
const room = await LineBotRoom.aggregate([
{ $match: { active: true } },
{ $project: { _id: 0, name: 1, botname: 1 } },
{ $sort: { name: 1 } }
])
for (const b of bot) {
b.type = 'bot'
b.room = room.filter(e => e.botname === b.botname)
}

const webhook = await ChatWebhook.find({ active: true }, null, { sort: { botname: 1 } })
const bot = await LineBot.find({ active: true }, null, { sort: { botname: 1 } })
const room = await LineBotRoom.find({ active: true }, null, { sort: { botname: 1, name: 1 } })
const service = await ServiceBot.find({ active: true }, null, { sort: { name: 1 } })
const oauth = await ServiceBotOauth.find({ accessToken: { $ne: null } }, null, { sort: { service: 1, name: 1 } })
const notify = await ServiceBot.aggregate([
{ $match: { active: true, userId } },
{ $project: { _id: 1, client: 1, secret: 1, text: '$name', value: '$service', type: '$notify' } },
{ $sort: { service: 1 } }
])

return {
service: service.filter(e => e.service !== 'log').map(e => ({
_id: e._id,
text: e.name,
value: e.service,
room: oauth.filter(r => r.service === e.service && r.accessToken).map(e => ({ service: e.service, value: e.room, text: e.name, _id: e._id }))
})),
bot: bot.map(e => ({
_id: e._id,
text: e.name,
value: e.botname,
stats: e.options && e.options.stats ? e.options.stats : {},
room: room.filter(r => r.botname === e.botname && r.active).map(e => ({ botname: e.botname, value: e.name, text: e.name, _id: e._id }))
})),
webhook: webhook.map(e => ({
_id: e._id,
botname: e.botname,
name: e.name,
type: e.type
}))
const service = await ServiceBotOauth.aggregate([
{ $match: { accessToken: { $ne: null } } },
{ $project: { _id: 0, accessToken: 1, value: '$room', name: 1, service: 1 } },
{ $sort: { value: 1, name: 1 } }
])

for (const n of notify) {
n.type = 'notify'
n.room = service.filter(e => e.service === n.value)
}

return { bot, notify }
}

0 comments on commit dadd543

Please sign in to comment.