'use strict';

// model.js - A mongoose model
//
// See http://mongoosejs.com/docs/models.html
// for more of what you can do here.

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const consentForm = ['analog', 'digital'];

const consentSchema = new Schema({
	userId: {type: Schema.Types.ObjectId, ref: 'user', required: true},
	userConsent: {
		form: {type: String, enum: consentForm},
		dateOfPrivacyConsent: {type: Date, 'default': Date.now},
		dateOfTermsOfUseConsent: {type: Date, 'default': Date.now},
		dateOfThirdPartyConsent: {type: Date, 'default': Date.now},
		dateOfResearchConsent: {type: Date, 'default': Date.now},
		privacyConsent: {type: Boolean},
		termsOfUseConsent: {type: Boolean},
		thirdPartyConsent: {type: Boolean},
		researchConsent: {type: Boolean}
	},
	parentConsents: [{
		parentId: {type: Schema.Types.ObjectId, ref: 'user'},
		form: {type: String, enum: consentForm, default: 'digital'},
		dateOfPrivacyConsent: {type: Date, 'default': Date.now},
		dateOfTermsOfUseConsent: {type: Date, 'default': Date.now},
		dateOfThirdPartyConsent: {type: Date, 'default': Date.now},
		dateOfResearchConsent: {type: Date, 'default': Date.now},
		privacyConsent: {type: Boolean},
		termsOfUseConsent: {type: Boolean},
		thirdPartyConsent: {type: Boolean},
		researchConsent: {type: Boolean},
	}]
});

const consentVersionSchema = new Schema({
	versionNumber: {type: String},
	consentText: {type: String},
	date: {type: Date}
});

const consentModel = mongoose.model('consent', consentSchema);
const consentVersionModel = mongoose.model('consentVersion', consentVersionSchema);

module.exports = {
	consentModel,
	consentVersionModel
};

@@ -915,23 +915,25 @@
 ntForm, 
+'
 default
+'
 : 'digit
@@ -1333,24 +1333,228 @@
 %7D,%0A%09%7D%5D%0A%7D);%0A%0A
+consentSchema.pre('save', function(next) %7B%0A%09if (this.parentConsents.length == 0)%0A%09%09this.parentConsents = undefined;%0A%09if (this.userConsent.form == undefined)%0A%09%09this.userConsent = undefined;%0A%0A%09next();%0A%7D);%0A%0A
 const consen

add model pre hook