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


    friendlyName: 'Create',


    description: 'Create Attribution rang!',


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

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

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

        montantRemise: {
            type: 'string',
        },

        montantDu: {
            type: 'string',
        },

        codeNatureRecette: {
            type: 'string',
        },
        libelle: {
            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 scolariteCreate;
        let scolarite = await Scolarite.find({ codeClasse: inputs.codeClasse, codeEcole: inputs.codeEcole, matricule: inputs.matricule, anneeScolaire: inputs.anneeScolaire, codeNatureRecette: inputs.codeNatureRecette }).then(_.first)
        if (scolarite) {

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

    }


};
