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


    friendlyName: 'Create',


    description: 'Create Section.',


    inputs: {
        anneeScolaire: {
            type: 'string',
            require: true
        },
        dateDebu: {
            type: 'string',
        },
        dateFin: {
            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) {

        //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 anneeCreate;
        let annne = await AnneeScolaire.find({ anneeScolaire: inputs.anneeScolaire }).then(_.first)
        if (annne) {

            return exits.notFound({ message: "Cette Année Scolaire existe déja, veuillez la modifier!" })

        } else {
            anneeCreate = await AnneeScolaire.create({ anneeScolaire: inputs.anneeScolaire, dateDebu: inputs.dateDebu, dateFin: inputs.dateFin }).fetch();
        }
        // All done.
        return exits.success(anneeCreate);

    }


};
