module.exports = {


  friendlyName: 'Modify',


  description: 'Modify user.',


  inputs: {
    id: {
      type: 'number',
      required: true
    },


    nom: {
      type: 'string',
      required: true
    },
    prenom: {
      type: 'string',
      required: true
    },
    sexe: {
      type: 'string',
      isIn: ['M', 'F']
    },
    email: {
      type: 'string',
      required: true

    },

    dateNaissance: {
      type: 'string'
    },
    qualification: {
      type: 'string',
      //required: true
    },

    tel1: {
      type: 'string',

    },
    tel2: {
      type: 'string',
      //required: true
    },
    estSuperviseur: {
      type: 'boolean',
      //required: true
    },
    role: {
      type: 'number',
      //required: true
    },
    roleSlug: {
      type: 'string',
      required: true
    },
    section: {
      type: 'number',
      //required: true
    },

  },


  exits: {
    notFound: {
      statusCode: 404
    },

  },


  fn: async function (inputs, exits) {
    console.log('INPUTS', inputs)
    let user = await User.findOne(inputs.id)
    if (!user) {
      return exits.notFound({ message: "Utilisateur  non retrouvé" })
    }
    console.log("INPUTS", inputs.section)

    if (inputs.section) {
      let section = await Section.find({ id: inputs.section }).then(_.first)
      if (!section) {
        throw { notFound: { message: 'Section non retrouvée!' } }
      }
    }


    let role = await Role.find({ slug: inputs.roleSlug }).then(_.first)
    //console.log('agence', agence)
    if (!role) {
      throw { notFound: { message: 'Role non retrouvée!' } }
    }



    let userUpdate = await User.update({ id: user.id },
      {
        nom: inputs.nom,
        prenom: inputs.prenom,
        email: inputs.email,
        dateNaissance: inputs.dateNaissance,
        qualification: inputs.qualification,
        username: inputs.email,
        //password: password,
        sexe: inputs.sexe,
        tel1: inputs.tel1,
        tel2: inputs.tel2,
        estSuperviseur: inputs.estSuperviseur,
        role: role.id,
        roleSlug: inputs.roleSlug,
        section: inputs.section,
        nomPrenom: `${inputs.nom.toUpperCase()} ${inputs.prenom.toUpperCase()}`
      }
    ).fetch()

    // All done.
    return exits.success({
      ...userUpdate,
      //..._.omit(userUpdate,'id')
    });

  }

};
