<commit_before>var mongoose = require('mongoose');
var _ = require('lodash');
var validator = require('validator');

var {SendSmtpEmail, sendEmail} = require('../lib/send-in-blue')
var utils = require('../lib/utils');
var config = require('../config');

var renderInitial = require('../lib/mes-aides/emails/initial').render;
var renderSurvey = require('../lib/mes-aides/emails/survey').render;

var SurveySchema = new mongoose.Schema({
    _id: { type: String },
    createdAt: { type: Date, default: Date.now },
    messageId: { type: String },
    repliedAt: { type: Date },
    error: { type: Object },
    answers: [{
        id: String,
        value: String,
        comments: String
    }],
    type: { type: String },
}, { minimize: false, id: false });

SurveySchema.virtual('returnPath').get(function() {
    return '/suivi?token=' + this._id;
});

var FollowupSchema = new mongoose.Schema({
    situation: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Situation'
    },
    email: {
        type: String,
        validate:{
            validator: validator.isEmail,
            message: '{VALUE} n\'est pas un email valide',
            isAsync: false
        }
    },
    createdAt: { type: Date, default: Date.now },
    sentAt: { type: Date },
    messageId: { type: String },
    surveySentAt: { type: Date },
    benefits: { type: Object },
    surveyOptin: { type: Boolean, default: false },
    surveys: {
        type: [SurveySchema],
        default: []
    },
    error: { type: Object },
    _id: { type: String },
}, { minimize: false, id: false });


FollowupSchema.methods.postInitialEmail = function(messageId) {
    this.sentAt = Date.now();
    if (! this.surveyOptin) {
        this.email = undefined;
    } else {
        this.messageId = messageId;
    }
    this.error = undefined;
    return this.save();
};

FollowupSchema.methods.renderInitialEmail = function() {
    return renderInitial(this);
};

FollowupSchema.methods.sendInitialEmail = function() {
    var followup = this;
    return this.renderInitialEmail()
        .then(render => {
            var email = new SendSmtpEmail()
            email.to = [{email: followup.email}]<commit_msg>Enregistre l'identifiant de l'email dans tous les cas<commit_after>var mongoose = require('mongoose');
var _ = require('lodash');
var validator = require('validator');

var {SendSmtpEmail, sendEmail} = require('../lib/send-in-blue')
var utils = require('../lib/utils');
var config = require('../config');

var renderInitial = require('../lib/mes-aides/emails/initial').render;
var renderSurvey = require('../lib/mes-aides/emails/survey').render;

var SurveySchema = new mongoose.Schema({
    _id: { type: String },
    createdAt: { type: Date, default: Date.now },
    messageId: { type: String },
    repliedAt: { type: Date },
    error: { type: Object },
    answers: [{
        id: String,
        value: String,
        comments: String
    }],
    type: { type: String },
}, { minimize: false, id: false });

SurveySchema.virtual('returnPath').get(function() {
    return '/suivi?token=' + this._id;
});

var FollowupSchema = new mongoose.Schema({
    situation: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Situation'
    },
    email: {
        type: String,
        validate:{
            validator: validator.isEmail,
            message: '{VALUE} n\'est pas un email valide',
            isAsync: false
        }
    },
    createdAt: { type: Date, default: Date.now },
    sentAt: { type: Date },
    messageId: { type: String },
    surveySentAt: { type: Date },
    benefits: { type: Object },
    surveyOptin: { type: Boolean, default: false },
    surveys: {
        type: [SurveySchema],
        default: []
    },
    error: { type: Object },
    _id: { type: String },
}, { minimize: false, id: false });


FollowupSchema.methods.postInitialEmail = function(messageId) {
    this.sentAt = Date.now();
    this.messageId = messageId;
    if (! this.surveyOptin) {
        this.email = undefined;
    }
    this.error = undefined;
    return this.save();
};

FollowupSchema.methods.renderInitialEmail = function() {
    return renderInitial(this);
};

FollowupSchema.methods.sendInitialEmail = function() {
    var followup = this;
    return this.renderInitialEmail()
        .then(render => {
            var email = new SendSmtpEmail()
            email.to = [{email: followup.email}]
{'fraction_non_alphanumeric': 0.15161801312514142, 'fraction_numerical': 0.0, 'mean_word_length': 2.7174095878889823, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3189216', 'n_tokens_mistral': 1352, 'n_tokens_neox': 1377, 'n_words': 319}