require('dotenv').config();
module.exports = {


    friendlyName: 'Modify',


    description: 'Modify user.',


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

        codeEcole: {
            type: 'string',
            required: true

        },
        codeClasse: {
            type: 'string',
            required: true

        },
        matricule: {
            type: 'string',
            required: true
        },

        trimSem: {
            type: 'string',
        },

        anneeScolaire: {
            type: 'string',
        },

        systemSco: {
            type: 'string',
        },

        logiUsername: {
            type: 'string',
            require: true

        },
        key: {
            type: 'string',
            require: true

        },
        logiPassword: {
            type: 'string',
            require: true

        },


    },


    exits: {
        notFound: {
            statusCode: 404
        },

    },


    fn: async function (inputs, exits) {

        // TOKEN VERIFY
        let user = await Utilisateurs.find({ login: inputs.logiUsername, codeUser: inputs.logiPassword }).then(_.first)
        if (!user) {
            return exits.notFound('Utilisateur non trouvé')
        }
        console.log("INPUTS 2", inputs)

        let keyVerify = process.env.KEYVERIFY
        //
        if (keyVerify !== inputs.key) {
            return exits.notFound({ message: "Token non valide" })
        }


        ///////////////////

        let attributionClasse = await AttributionClasse.find({ codeEcole: inputs.codeEcole, codeClasse: inputs.codeClasse, matricule: inputs.matricule, trimSem: inputs.trimSem, anneeScolaire: inputs.anneeScolaire, systemSco: inputs.systemSco, }).then(_.first)
        if (!attributionClasse) {

            return exits.notFound({ message: "Cette Attribution n\'existe pas, veuillez la modifier!" })
        }

        let attributionClasseUpdate = await AttributionClasse.update({ id: attributionClasse.id }, inputs).fetch()

        // All done.
        return exits.success({
            ...attributionClasseUpdate,
        });

    }

};
