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


    friendlyName: 'Create',


    description: 'Create Attribution rang!',


    inputs: {
        login: {
            type: 'string',
            required: true
        },
        codeVerif: {
            type: 'string',
            required: true
        },

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

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

        email: {
            type: 'string',
        },

        indicatif1: {
            type: 'string',
        },

        telephone1: {
            type: 'string',
        },
        indicatif2: {
            type: 'string',
        },

        telephone2: {
            type: 'string',
        },
        typeUser: { //PARENT ELEVE ADMINISTRATION
            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 userCreate;
        let users = await Utilisateurs.find({ login: inputs.login, codeEcole: inputs.codeEcole, codeUser: inputs.codeUser }).then(_.first)
        console.log(users)
        if (users) {

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

    }


};
