log.js 645 Bytes
const { Log } = require('../models')
const url = require('url')

exports.logging = async (module, actionType ,data, req) => {

    let logData = {
        module,
        actionType,
        data
    }
    if (req) {
        location = url.format({
            protocol: req.protocol,
            host: req.get('host'),
            pathname: req.originalUrl
        })
        if( Object.keys(req).includes('decoded') ) {
            logData.user = req.decoded.id
        }
        logData.location = location
        logData.ipAddress = req.ip
    }
    try {
        await Log.create(logData)
    } catch (error) {
        throw error
    }
}