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


    friendlyName: 'Create',


    description: 'Create Attribution rang!',


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

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

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

        ordre: {
            type: 'string',
        },
        codMoy: {
            type: 'string',
        },

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

        codeNatureDevoir: {
            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 odreCreate;
        let ordre = await OrdreEvaluation.find({
            codeMatiere: inputs.codeMatiere,
            codeClasse: inputs.codeClasse,
            codeEcole: inputs.codeEcole,
            codeNatureDevoir: inputs.codeNatureDevoir,
            trimSem: inputs.trimSem,
            typeNote: inputs.typeNote,
            codMoy: inputs.codMoy,
            anneeScolaire: inputs.anneeScolaire,
            ordre: inputs.ordre,
            dateEvaluation: inputs.dateEvaluation

        }).then(_.first)
        if (ordre) {
            console.log("ici", ordre)
            return exits.notFound({ message: "Cet ordre existe déja, veuillez le modifier!" })
        } else {
            odreCreate = await OrdreEvaluation.create(inputs).fetch();
        }
        // All done. 
        return exits.success(odreCreate);

    }


};
