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


    friendlyName: 'Create',


    description: 'Create Attribution rang!',


    inputs: {
        matricule: {
            type: 'string',
            required: true
        },
        nom: {
            type: 'string',
            required: true
        },
        prenom: {
            type: 'string',
        },
        sexe: {
            type: 'string',
        },
        dateNaissance: {
            type: 'string',
        },
        lieuNaissance: {
            type: 'string',
        },
        indicatif: {
            type: 'string',
        },
        telephone: {
            type: 'string',
        },
        email: {
            type: 'string',
        },
        codeEcole: {
            type: 'string',
            required: true

        },


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

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

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

        },


    },


    exits: {
        notFound: {
            statusCode: 404
        },
    },


    fn: async function (inputs, exits) {

        //console.log("INPUTS 1", inputs)

        // 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 eleveCreate;
        let eleve = await Eleves.find({ matricule: inputs.matricule, codeEcole: inputs.codeEcole }).then(_.first)
        if (eleve) {

            return exits.notFound({ message: "Cet éleve existe déja, veuillez la modifier!" })
        } else {
            eleveCreate = await Eleves.create(inputs).fetch();
        }
        // All done.
        return exits.success(eleveCreate);

    }


};
