summaryrefslogblamecommitdiffstats
path: root/server/lib/log.js
blob: 782c34fc5e891b9bed342f230366472a4c3bda92 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13



                                                         
                                                                                                







                                 







                                                                                                                



                    
                    
/* global __appdir */
const path = require('path')
var db = require(path.join(__appdir, 'lib', 'sequelize'))

async function log ({ category, description, clientId, userId, groupId, group, client, user }) {
  const entry = db.log.build({
    category,
    description,
    timestamp: Date.now() / 1000,
    groupId,
    clientId,
    userId
  })

  if (group) entry.groupSnapshot = JSON.stringify(group)
  else if (groupId) entry.groupSnapshot = JSON.stringify(await db.group.findOne({ where: { id: groupId } }))
  if (client) entry.clientSnapshot = JSON.stringify(client)
  else if (clientId) entry.clientSnapshot = JSON.stringify(await db.client.findOne({ where: { id: clientId } }))
  if (user) entry.userSnapshot = JSON.stringify(user)
  else if (userId) entry.userSnapshot = JSON.stringify(await db.user.findOne({ where: { id: userId } }))

  await entry.save()
  return entry
}

module.exports = log