'use strict'; /** * Module dependencies. */ var mongoose = require('mongoose'), FieldSchema = require('./form_field.server.model.js'), Schema = mongoose.Schema, pdfFiller = require('pdfFiller'), _ = require('lodash'), config = require('../../config/config'), path = require('path'), fs = require('fs-extra'), async = require('async'), Field = mongoose.model('Field', FieldSchema); /** * Form Schema */ var FormSchema = new Schema({ created: { type: Date, default: Date.now }, lastModified: { type: Date, default: Date.now }, title: { type: String, trim: true, unique: true, required: 'Title cannot be blank' }, language: { type: String, enum: ['english', 'french', 'spanish'], required: 'Form must have a language' }, description: { type: String, default: '', }, form_fields: { type: [Schema.Types.Mixed], set: function(form_fields) { this._previousFormFields = this.form_fields; return form_fields; } }, submissions: [{ type: Schema.Types.ObjectId, ref: 'FormSubmission' }], admin: { type: Schema.Types.ObjectId, ref: 'User' }, pdf: { type: Schema.Types.Mixed }, pdfFieldMap: { type: Schema.Types.Mixed }, hideFooter: { type: Boolean, default: false, }, isGenerated: { type: Boolean, default: false, }, isLive: { type: Boolean, default: false, }, autofillPDFs: { type: Boolean, default: false, }, }); //Delete template PDF of current Form FormSchema.pre('remove', function (next) { if(this.pdf){ //Delete template form fs.unlink(this.pdf.path, function(err){ if (err) throw err; console.log('successfully deleted', this.pdf.path); }); } }); //Update lastModified and created everytime we save FormSchema.pre('save', function (next) { var now = new Date(); this.lastModified = now; if( !this.created ){ this.created = now; } next(); }); //Concatenate submission and form's form_fields // FormSchema.pre('save', function (next) { // if(this.isModified('form_fields')){ // if(this.submissions.length){ // for(var i=0; i