
let moment = require('moment');

module.exports = {


    friendlyName: 'Login',
  
  
    description: 'Login auth.',
  
  
    inputs: {
      
      ip:{
        type:'string',
      },
      location:{
        type:'string',
      },
      departement:{
        type:'string',
      },
      longitude:{
        type:'string',
      },
      latitude:{
        type:'string',
      },
      action:{
        type:'string',
      },
    },
  
  
    exits: {
      notFound:{
        description:'User not found',
        statusCode: 404
      },
      encryptionError:{
        statusCode:500
      }
    },
  
  
    fn: async function ({ ip, location, departement, longitude, latitude, action},exits) {
      
      let ouestDate = moment.utc(new Date()).utcOffset('+01:00')
      let date = moment(ouestDate, 'DD/MM/YYYY').format('YYYY-MM-DD')
      let heure = moment(ouestDate).format('HH:mm:ss')
        //let heureCommande = `${new Date(ouestDate).getHours()}:${new Date(ouestDate).getMinutes()}:${new Date(ouestDate).getSeconds()}`
        //console.log("DATE",ouestDate, date,  heure)
       
        let idUser = this.req.token.id
        let user = await User.findOne(idUser)
      //console.log('USER', user)
      if(!user){
        return exits.notFound('Utilisateur non trouvé')
      }
      // All done.

      let activityCreate = await ActivityLogs.create({
        ip: ip,
        location: location,
        date: date,
        heure: heure,
        username: user.username,
        departement: departement,
        latitude: latitude,
        longitude: longitude,
        action: action,
       
  
    }).fetch()

    console.log("activityCreate",activityCreate)

     
      return  exits.success({activityCreate})
  
    }
  
  
  };
  