Controllers_RegisterController.js

const CrudController = require("./CrudController");
const JsonResponse = require("./JsonResponse");
const logger = require("../Logger");
const path = require("path");
const { v4: uuidv4 } = require("uuid");
const ConsultantValidatorAdd = require("../Validators/ConsultantValidatorAdd");
const ConsultantDto = require("../database/dto/ConsultantDto");
const ConsultantValidatorUpdate = require("../Validators/ConsultantValidatorUpdate");
const RegisterService = require("../Services/RegisterService");
const PreRegisterConsultantMail = require("../sms-mail/PregisterConsultantMail");
const PregisterSMS = require("../sms-mail/PreRegisterSMS");



/**
 * @extends Myintranet.Controllers.CrudController
 * @memberOf Myintranet.Controllers
 * @inheritDoc
 */
class RegisterController extends CrudController {

    constructor(db) {
        let service = new RegisterService(db);
        super(service, undefined, db, undefined);
    }


    async PreRegisterConsultant(request, response) {
        try {
            let { db } = this;
            // console.log(request.body)
            const { employeur, user } = request.body;
            if (!employeur.id || !user.login || !user.phone) {
                throw new Error('Data missing')
            }
            const esn = await this.db.Employeur.findOne({ where: { id: employeur.id } });

            let registermailconsultant = new PreRegisterConsultantMail(user, `Inscription chez ${esn.denominationSocial} - My Intranet`, undefined, undefined, esn, db, undefined, undefined);
            let infos = await registermailconsultant.send();


            let registerSMS = new PregisterSMS(user,`Inscription chez  ${esn.denominationSocial} - My Intranet`, undefined, undefined, esn, db, undefined, undefined);
            await registerSMS.send();

            response.json(new JsonResponse(true, infos, "ok"));

        } catch (err) {
            response.json(new JsonResponse(false, err, err.message));
        }


        // this.db.Consultant.create({}).then(consultant => {
        //     if (consultant) {
        //         response.json(new JsonResponse(true, consultant, request.params.slug));
        //     } else {
        //         response.json(new JsonResponse(false, {}, "Aucun consultant N'as été ajouté"));
        //     }
        //
        // }).catch(err => {
        //     response.json(new JsonResponse(false, err, err.message));
        // })


    }


    getEmployeurFromSlug(request, response) {

        this.db.Employeur.findOne({ where: { slug: request.params.slug } }).then(employeur => {
            if (employeur) {
                response.json(new JsonResponse(true, employeur, request.params.slug));
            } else {
                response.json(new JsonResponse(false, {}, "Aucun employeur trouvé"));
            }

        }).catch(err => {
            response.json(new JsonResponse(false, err, err.message));
        })


    }

    toDto(data) {
        return data;
    }

    // downloadDocs(request, response) {
    //     this.service.getFullDocPathname(request.params.id).then((data) => {
    //         console.log(data)
    //         response.setHeader('Content-Disposition', 'attachement; filename=' + path.basename(data));
    //         response.sendFile(data);
    //     }).catch(err => {
    //         logger.error(err.message, err);
    //         response.json(new JsonResponse(false, err, err.message));

    //     })
    // }
}

module.exports = RegisterController;