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


    friendlyName: 'Create',


    description: 'Create Attribution rang!',


    inputs: {
        codeMatiere: {
            type: 'string',
            //required: true
        },

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

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

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

        dateEvaluation: {
            type: 'string',
        },

        moyennePeriodique: {
            type: 'string',
        },
        moyenneBilanLettre: {
            type: 'string',
        },

        appreciationBilanLettre: {
            type: 'string',
        },
        moyenneBilanSciences: {
            type: 'string',
        },
        appreciationBilanSciences: {
            type: 'string',
        },

        moyenneBilanAutres: {
            type: 'string',
        },

        appreciationBilanAutres: {
            type: 'string',
        },

        appreciationChefEtab: {
            type: 'string',
        },
        appreciationPp: {
            type: 'string',
        },

        mentionConseilProf: {
            type: 'string',
        },

        statusPassant: {
            type: 'string',
        },

        totalCoef: {
            type: 'string',
        },

        moyenneFaible: {
            type: 'string',
        },

        moyenneForte: {
            type: 'string',
        },

        moyenneClasse: {
            type: 'string',
        },

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

        rang: {
            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 attributionRangCreate;
        let attributionRang = await AttributionRang.find({ codeMatiere: inputs.codeMatiere, codeEcole: inputs.codeEcole, anneeScolaire: inputs.anneeScolaire, codeClasse: inputs.codeClasse, trimSem: inputs.trimSem, matricule: inputs.matricule }).then(_.first)
        if (attributionRang) {

            return exits.notFound({ message: "Cette Attribution existe déja, veuillez la modifier!" })
        } else {
            attributionRangCreate = await AttributionRang.create(inputs).fetch();
        }
        // All done.
        return exits.success(attributionRangCreate);

    }


};
