fixed submissions fields

This commit is contained in:
David Baldwynn 2015-10-06 13:14:38 -07:00
parent 2638af607f
commit 5a52425c10
10 changed files with 472 additions and 412 deletions

View File

@ -9,6 +9,7 @@ var mongoose = require('mongoose'),
_ = require('lodash'),
config = require('../../config/config'),
path = require('path'),
mUtilities = require('mongoose-utilities'),
fs = require('fs-extra'),
async = require('async'),
util = require('util');
@ -44,13 +45,13 @@ var ButtonSchema = new Schema({
* Form Schema
*/
var FormSchema = new Schema({
created: {
type: Date,
default: Date.now
},
lastModified: {
type: Date,
},
// created: {
// type: Date,
// default: Date.now
// },
// lastModified: {
// type: Date,
// },
title: {
type: String,
trim: true,
@ -66,9 +67,9 @@ var FormSchema = new Schema({
type: String,
default: '',
},
form_fields: [{
type: Schema.Types.Mixed
}],
form_fields: {
type: [FieldSchema],
},
submissions: [{
type: Schema.Types.ObjectId,
@ -173,6 +174,12 @@ var FormSchema = new Schema({
}
});
FormSchema.plugin(mUtilities.timestamp, {
createdPath: 'created',
modifiedPath: 'lastModified',
useVirtual: false
});
//Delete template PDF of current Form
FormSchema.pre('remove', function (next) {
if(this.pdf && process.env.NODE_ENV === 'development'){
@ -203,16 +210,6 @@ FormSchema.pre('save', function (next) {
});
});
//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();
});
function getDeletedIndexes(needle, haystack){
var deletedIndexes = [];
@ -298,7 +295,7 @@ FormSchema.pre('save', function (next) {
field.fieldType = _typeConvMap[ field.fieldType+'' ];
}
field = new Field(field);
// field = new Field(field);
field.required = false;
_form_fields[i] = field;
}
@ -343,8 +340,8 @@ FormSchema.pre('save', function (next) {
// var _original = this._original;
// console.log('_original\n------------');
// console.log(_original);
// console.log('field has been deleted: ');
// console.log(this.isModified('form_fields') && !!this.form_fields && !!_original);
//console.log('field has been deleted: ');
//console.log(this.isModified('form_fields') && !!this.form_fields && !!_original);
if(this.isModified('form_fields') && this.form_fields.length >= 0 && _original){
@ -354,12 +351,12 @@ FormSchema.pre('save', function (next) {
deletedIds = getDeletedIndexes(old_ids, new_ids),
that = this;
// console.log('deletedId Indexes\n--------');
// console.log(deletedIds);
// console.log('old_ids\n--------');
// console.log(old_ids);
// console.log('new_ids\n--------');
// console.log(new_ids);
console.log('deletedId Indexes\n--------');
console.log(deletedIds);
console.log('old_ids\n--------');
console.log(old_ids);
console.log('new_ids\n--------');
console.log(new_ids);
//Preserve fields that have at least one submission
if( deletedIds.length > 0 ){
@ -387,25 +384,26 @@ FormSchema.pre('save', function (next) {
// callback(null, modifiedSubmissions);
// } else{
//Find FormSubmissions that contain field with _id equal to 'deleted_id'
FormSubmission.
find({ form: that._id, admin: that.admin, form_fields: {$elemMatch: {_id: deleted_id} } }).
find({ form: that._id, admin: that.admin, form_fields: {$elemMatch: {_id: deleted_id} } }).
exec(function(err, submissions){
if(err){
console.error(err);
return callback(err);
}
// console.log(submissions);
//Delete field if there are no submission(s) found
if(submissions.length) {
//Push old form_field to start of array
// that.form_fields.unshift(old_form_fields[deletedIdIndex]);
modifiedSubmissions.push.apply(modifiedSubmissions, submissions);
}
//Delete field if there are no submission(s) found
if(submissions.length) {
console.log('adding submissions');
console.log(submissions);
//Add submissions
modifiedSubmissions.push.apply(modifiedSubmissions, submissions);
}
callback(null);
});
callback(null);
});
// }
},
function (err) {
@ -417,34 +415,32 @@ FormSchema.pre('save', function (next) {
// console.log('modifiedSubmissions\n---------\n\n');
// console.log(modifiedSubmissions);
// console.log('preserved deleted fields');
// console.log(submissions);
//Iterate through all submissions with modified form_fields
async.forEachOfSeries(modifiedSubmissions, function (submission, key, callback) {
//Iterate through ids of deleted fields
for(var i = 0; i < deletedIds.length; i++){
//Get index of deleted field
var index = _.findIndex(submission.form_fields, function(field) {
var tmp_id = field._id+'';
// console.log(tmp_id === old_ids[ deletedIds[i] ]);
return tmp_id === old_ids[ deletedIds[i] ];
});
// console.log('index: '+index);
var deletedField = submission.form_fields[index];
var tmpField = submission.form_fields[index];
if(tmpField){
// console.log('tmpField\n-------\n\n');
// console.log(tmpField);
//Hide field if it exists
if(deletedField){
console.log('deletedField\n-------\n\n');
console.log(deletedField);
//Delete old form_field
submission.form_fields.splice(index, 1);
tmpField.deletePreserved = true;
//Move old form_field to start
submission.form_fields.unshift(tmpField);
deletedField.deletePreserved = true;
that.form_fields.unshift(tmpField);
//Move deleted form_field to start
submission.form_fields.unshift(deletedField);
that.form_fields.unshift(deletedField);
// console.log('form.form_fields\n--------\n\n');
// console.log(that.form_fields);
}

View File

@ -5,11 +5,27 @@
*/
var mongoose = require('mongoose'),
relationship = require('mongoose-relationship'),
mUtilities = require('mongoose-utilities'),
_ = require('lodash'),
Schema = mongoose.Schema;
var FieldOptionSchema = new Schema({
option_id: {
type: Number,
},
option_title: {
type: String,
},
option_value: {
type: String,
trim: true,
},
});
/**
* Question Schema
* FormField Schema
*/
var FormFieldSchema = new Schema({
// formSubmission: {
@ -18,18 +34,10 @@ var FormFieldSchema = new Schema({
// childPath: 'form_fields'
// },
created: {
type: Date,
default: Date.now
},
lastModified: {
type: Date,
default: Date.now
},
title: {
type: String,
trim: true,
required: 'Field title cannot be blank'
required: 'Field Title cannot be blank',
},
description: {
type: String,
@ -41,10 +49,7 @@ var FormFieldSchema = new Schema({
ref: 'LogicJump'
},
//DAVID: TODO: SEMI-URGENT: Need to come up with a schema for field options
fieldOptions: [{
type: Schema.Types.Mixed
}],
fieldOptions: [FieldOptionSchema],
required: {
type: Boolean,
default: true,
@ -60,14 +65,18 @@ var FormFieldSchema = new Schema({
},
fieldType: {
type: String,
required: 'Field type cannot be blank',
required: true,
validate: [validateFormFieldType, 'Invalid field type']
},
fieldValue: Schema.Types.Mixed
});
// FormFieldSchema.plugin(relationship, { relationshipPathName:'formSubmission' });
FormFieldSchema.plugin(mUtilities.timestamp, {
createdPath: 'created',
modifiedPath: 'lastModified',
useVirtual: false
});
FormFieldSchema.static('validTypes', function(){
return [
'textfield',
@ -124,8 +133,8 @@ function validateFormFieldType(value) {
return false;
};
var cloneFieldSchema = _.cloneDeep(FormFieldSchema);
// var cloneFieldSchema = _.cloneDeep(FormFieldSchema);
mongoose.model('Field', FormFieldSchema);
module.exports = cloneFieldSchema;
module.exports = FormFieldSchema;

View File

@ -11,60 +11,59 @@ var mongoose = require('mongoose'),
config = require('../../config/config'),
path = require('path'),
fs = require('fs-extra'),
Field = mongoose.model('Field'),
mUtilities = require('mongoose-utilities'),
soap = require('soap'),
async = require('async'),
FieldSchema = require('./form_field.server.model.js'),
OscarSecurity = require('../../scripts/oscarhost/OscarSecurity');
var newDemoTemplate = {
"activeCount": 0,
"address": "",
"alias": "",
"anonymous": "",
"chartNo": "",
"children":"",
"citizenship":"",
"city": "",
"dateJoined": null,
"dateOfBirth": "",
"email": "",
"firstName": "",
"hin": 9146509343,
"lastName": "",
"lastUpdateDate": null,
"monthOfBirth": "",
"officialLanguage": "",
"phone": "",
"phone2": "",
"providerNo": 0,
"province": "",
"sex": "",
"spokenLanguage": "",
"postal": "",
"yearOfBirth": ""
'activeCount': 0,
'address': '',
'alias': '',
'anonymous': '',
'chartNo': '',
'children':'',
'citizenship':'',
'city': '',
'dateJoined': null,
'dateOfBirth': '',
'email': '',
'firstName': '',
'hin': 9146509343,
'lastName': '',
'lastUpdateDate': null,
'monthOfBirth': '',
'officialLanguage': '',
'phone': '',
'phone2': '',
'providerNo': 0,
'province': '',
'sex': '',
'spokenLanguage': '',
'postal': '',
'yearOfBirth': ''
};
/**
* Form Submission Schema
*/
var FormSubmissionSchema = new Schema({
title:{
title: {
type: String
},
created: {
type: Date,
default: Date.now
},
admin: {
type: Schema.Types.ObjectId,
ref: 'User',
required: true
},
form_fields: [FieldSchema],
form_fields: {
type: [Schema.Types.Mixed],
},
form: {
type:Schema.Types.ObjectId,
ref:'Form',
type: Schema.Types.ObjectId,
ref: 'Form',
required: true
},
@ -99,74 +98,104 @@ var FormSubmissionSchema = new Schema({
});
FormSubmissionSchema.plugin(mUtilities.timestamp, {
createdPath: 'created',
modifiedPath: 'lastModified',
useVirtual: false
});
//Oscarhost API hook
FormSubmissionSchema.post('save', function (next) {
if(this.form.plugins.oscarhost.baseUrl){
FormSubmissionSchema.pre('save', function (next) {
var self = this;
mongoose.model('Form').findById(self.form, function(err, _form){
var form_ids = _.map(_.pluck(_form.form_fields, '_id'), function(id){ return ''+id;}),
submission_ids = _.pluck(self.form_fields, '_id');
var url_login = this.form.plugins.oscarhost.baseUrl+'/LoginService?wsdl',
url_demo = this.form.plugins.oscarhost.baseUrl+'/DemographicService?wsdl';
var options = {
ignoredNamespaces: {
namespaces: ['targetNamespace', 'typedNamespace'],
override: true
}
}
console.log('Form form_field ids\n--------');
console.log(form_ids);
console.log('FormSubmission form_field ids\n--------');
console.log(submission_ids);
//Generate demographics from hashmap
var generateDemo = function(formFields, conversionMap, demographicsTemplate){
var _generatedDemo = {};
for(var field in formFields){
if(demographicsTemplate.hasOwnProperty(conversionMap[field._id])){
var propertyName = conversionMap[field._id];
if(err) next(err);
// console.log(_form);
// console.log('should push to api');
// console.log( (!this.oscarDemoNum && !!_form.plugins.oscarhost.baseUrl && !!_form.plugins.oscarhost.settings.fieldMap) );
if(!this.oscarDemoNum && _form.plugins.oscarhost.baseUrl && _form.plugins.oscarhost.settings.fieldMap){
console.log('OSCARHOST API HOOK');
var url_login = _form.plugins.oscarhost.baseUrl+'/LoginService?wsdl',
url_demo = _form.plugins.oscarhost.baseUrl+'/DemographicService?wsdl';
if(propertyName === "unparsedDOB"){
var date = Date.parse(field.fieldValue);
generatedDemo['dateOfBirth'] = date.getDate();
generatedDemo['yearOfBirth'] = date.getFullYear();
generatedDemo['monthOfBirth'] = date.getMonth();
}else{
generatedDemo[propertyName] = field.fieldValue;
}
var args_login = {arg0: config.oscarhost.auth.user, arg1: config.oscarhost.auth.pass};
}
}
return _generatedDemo;
}
var options = {
ignoredNamespaces: {
namespaces: ['targetNamespace', 'typedNamespace'],
override: true
}
};
var submissionDemographic = generateDemo(this.form_fields, this.form.plugin.oscarhost.settings.fieldMap, newDemoTemplate);
//Generate demographics from hashmap
var generateDemo = function(formFields, conversionMap, demographicsTemplate){
var _generatedDemo = {};
for(var field in formFields){
if(demographicsTemplate.hasOwnProperty(conversionMap[field._id])){
var propertyName = conversionMap[field._id];
async.waterfall([
function (callback) {
//Authenticate with API
soap.createClient(url_login, options, function(err, client) {
client.login(args_login, function (err, result) {
if(err) callback(err);
callback(null, result.return);
});
});
},
if(propertyName === 'unparsedDOB'){
var date = Date.parse(field.fieldValue);
_generatedDemo['dateOfBirth'] = date.getDate();
_generatedDemo['yearOfBirth'] = date.getFullYear();
_generatedDemo['monthOfBirth'] = date.getMonth();
}else{
_generatedDemo[propertyName] = field.fieldValue;
}
function (security_obj, callback) {
//Force Add Demographic
if(this.plugins.oscarhost.settings.updateType === 'force_add'){
soap.createClient(url_demo, options, function(err, client) {
client.setSecurity(new OscarSecurity(security_obj.securityId, security_obj.securityTokenKey) );
}
}
return _generatedDemo;
};
client.addDemographic({ arg0: exampleDemo }, function (err, result) {
var submissionDemographic = generateDemo(self.form_fields, _form.plugins.oscarhost.settings.fieldMap, newDemoTemplate);
async.waterfall([
function (callback) {
//Authenticate with API
soap.createClient(url_login, options, function(err, client) {
client.login(args_login, function (err, result) {
if(err) callback(err);
callback(null, result);
callback(null, result.return);
});
});
}
},
},
function (security_obj, callback) {
//Force Add Demographic
if(_form.plugins.oscarhost.settings.updateType === 'force_add'){
soap.createClient(url_demo, options, function(err, client) {
client.setSecurity(new OscarSecurity(security_obj.securityId, security_obj.securityTokenKey) );
client.addDemographic({ arg0: submissionDemographic }, function (err, result) {
if(err) callback(err);
callback(null, result);
});
});
}
},
], function(err, result) {
if(err) next(err);
console.log(result);
console.log('hello');
this.oscarDemoNum = parseInt(result.return, 10);
next();
});
}else{
next();
}
});
], function(err, result) {
if(err) throw err;
console.log(result);
this.oscarDemoNum = parseInt(result.return, 10);
});
}
});
//Check for IP Address of submitting person

View File

@ -8,6 +8,7 @@ var mongoose = require('mongoose'),
crypto = require('crypto'),
config = require('../../config/config'),
fs = require('fs-extra'),
mUtilities = require('mongoose-utilities'),
path = require('path');
/**
@ -102,6 +103,11 @@ UserSchema.virtual('displayName').get(function () {
return this.firstName + ' ' + this.lastName;
});
UserSchema.plugin(mUtilities.timestamp, {
createdPath: 'created',
modifiedPath: 'lastModified',
useVirtual: false
});
//Create folder for user's pdfs
UserSchema.pre('save', function (next) {

View File

@ -12,44 +12,6 @@ var should = require('should'),
config = require('../../config/config'),
FormSubmission = mongoose.model('FormSubmission');
var exampleDemo = {
activeCount: 1,
unparsedDOB: '',
address: '880-9650 Velit. St.',
chartNo: '',
city: '',
dateJoined: Date.now(),
dateOfBirth: '10',
displayName: 'LITTLE, URIAH',
email: '',
familyDoctor: '<rdohip></rdohip><rd></rd>',
firstName: 'Uriah F.',
hcType: 'BC',
hin: '',
hsAlertCount: 0,
lastName: 'Little',
lastUpdateDate: Date.now(),
lastUpdateUser: '',
links: '',
monthOfBirth: '05',
officialLanguage: 'English',
patientStatus: 'AC',
patientStatusDate: Date.now(),
phone: '250-',
phone2: '',
postal: "S4M 7T8",
providerNo: '4',
province: 'BC',
rosterStatus: '',
sex: 'M',
sexDesc: 'Female',
sin: '',
spokenLanguage: 'English',
title: 'MS.',
yearOfBirth: '2015'
}
/**
* Globals
*/
@ -59,50 +21,64 @@ var user, myForm, mySubmission;
* Unit tests
*/
describe('Form Model Unit Tests:', function() {
beforeEach(function(done) {
Form.remove().exec(function() {
User.remove().exec(done);
});
});
beforeEach(function(done) {
user = new User({
firstName: 'Full',
lastName: 'Name',
displayName: 'Full Name',
email: 'test@test.com',
username: 'username',
password: 'password'
password: 'password',
provider: 'local'
});
user.save(function() {
user.save(function(err) {
if(err){
console.log(err.errors);
done(err);
}
myForm = new Form({
title: 'Form Title',
admin: user,
language: 'english',
form_fields: [
{'fieldType':'textfield', 'title':'First Name', 'fieldValue': ''},
{'fieldType':'checkbox', 'title':'nascar', 'fieldValue': ''},
{'fieldType':'checkbox', 'title':'hockey', 'fieldValue': ''}
{'fieldType':'textfield', title:'First Name', 'fieldValue': ''},
{'fieldType':'checkbox', title:'nascar', 'fieldValue': ''},
{'fieldType':'checkbox', title:'hockey', 'fieldValue': ''}
]
});
done();
});
});
describe('Method Save', function() {
it('should be able to save without problems', function(done) {
return myForm.save(function(err) {
myForm.save(function(err) {
should.not.exist(err);
done();
});
});
it('should be able to show an error when try to save without title', function(done) {
var _form = myForm;
var _form = new Form({
title: 'Form Title',
admin: user,
language: 'english',
form_fields: [
{'fieldType':'textfield', title:'First Name', 'fieldValue': ''},
{'fieldType':'checkbox', title:'nascar', 'fieldValue': ''},
{'fieldType':'checkbox', title:'hockey', 'fieldValue': ''}
]
});
_form.title = '';
return _form.save(function(err) {
_form.save(function(err) {
should.exist(err);
should.equal(err.errors.title.message, 'Form Title cannot be blank');
done();

View File

@ -38,7 +38,7 @@ var exampleDemo = {
patientStatusDate: Date.now(),
phone: '250-',
phone2: '',
postal: "S4M 7T8",
postal: 'S4M 7T8',
providerNo: '4',
province: 'BC',
rosterStatus: '',
@ -48,7 +48,24 @@ var exampleDemo = {
spokenLanguage: 'English',
title: 'MS.',
yearOfBirth: '2015'
}
};
var sampleFormFields = [
{'fieldType':'textfield', 'title':'What\'s your first name', 'fieldValue': ''},
{'fieldType':'textfield', 'title':'And your last name', 'fieldValue': ''},
{'fieldType':'radio', 'title':'And your sex', 'fieldOptions': [{ 'option_id': 0, 'option_title': 'Male', 'option_value': 'M' }, { 'option_id': 1, 'option_title': 'Female', 'option_value': 'F' }], 'fieldValue': ''},
{'fieldType':'date', 'title':'When were you born?', 'fieldValue': ''},
{'fieldType':'number', 'title':'What\'s your phone #?', 'fieldValue': ''}
];
var sampleSubmission = [
{'fieldType':'textfield', 'title':'What\'s your first name', 'fieldValue': 'David'},
{'fieldType':'textfield', 'title':'And your last name', 'fieldValue': 'Baldwynn'},
{'fieldType':'radio', 'title':'And your sex', 'fieldValue': 0, 'fieldOptions': [{ 'option_id': 0, 'option_title': 'Male', 'option_value': 'M' }, { 'option_id': 1, 'option_title': 'Female', 'option_value': 'F' }]},
{'fieldType':'date', 'title':'When were you born?', 'fieldValue': Date.now()},
{'fieldType':'number', 'title':'What\'s your phone #?', 'fieldValue': '6043158008'}
];
/**
* Globals
@ -64,17 +81,20 @@ describe('FormSubmission Model Unit Tests:', function() {
firstName: 'Full',
lastName: 'Name',
displayName: 'Full Name',
email: 'test1@test.com',
username: 'test1@test.com',
password: 'password'
email: 'test1@test.com'+Date.now(),
username: 'test1@test.com'+Date.now(),
password: 'password',
provider: 'local'
});
user.save(function(err, _user) {
if(err) done(err);
user.save(function(err) {
if(err){
console.log(err.errors);
done(err);
}
myForm = new Form({
title: 'Form Title',
admin: _user,
title: 'Form Title1',
admin: user,
language: 'english',
form_fields: [
{'fieldType':'textfield', 'title':'What\'s your first name', 'fieldValue': ''},
@ -87,7 +107,6 @@ describe('FormSubmission Model Unit Tests:', function() {
oscarhost: {
baseUrl: config.oscarhost.baseUrl,
settings: {
lookupField: '',
updateType: 'force_add',
},
auth: config.oscarhost.auth,
@ -95,13 +114,34 @@ describe('FormSubmission Model Unit Tests:', function() {
}
});
done();
myForm.save(function(err, form){
if(err){
console.log(err.errors);
done(err);
}
var submissionFields = _.clone(myForm.form_fields);
for(var z=0; z<submissionFields.length; z++){
submissionFields[z] = _.extend(myForm.form_fields[z], submissionFields[z]);
}
mySubmission = new FormSubmission({
admin: user,
form: myForm,
timeElapsed: 17.55,
form_fields: submissionFields
});
done();
});
});
});
describe('Method Save', function() {
var myFieldMap = {};
beforeEach(function(done){
var myFieldMap = {};
myFieldMap[myForm.form_fields[0]._id+''] = 'firstName';
myFieldMap[myForm.form_fields[1]._id+''] = 'lastName';
myFieldMap[myForm.form_fields[2]._id+''] = 'sex';
@ -114,14 +154,14 @@ describe('FormSubmission Model Unit Tests:', function() {
if(err) done(err);
// var submission_fields = _.cloneDeep(form.toObject().form_fields);
// var submission_fields = [
// {'fieldType':'textfield', 'title':'What\'s your first name', 'fieldValue': ''},
// {'fieldType':'textfield', 'title':'And your last name', 'fieldValue': ''},
// {'fieldType':'radio', 'title':'And your sex', 'fieldOptions': [{ 'option_id': 0, 'option_title': 'Male', 'option_value': 'M' }, { 'option_id': 1, 'option_title': 'Female', 'option_value': 'F' }], 'fieldValue': ''},
// {'fieldType':'date', 'title':'When were you born?', 'fieldValue': ''},
// {'fieldType':'number', 'title':'What\'s your phone #?', 'fieldValue': ''}
// ];
/*
var submission_fields = [
{'fieldType':'textfield', 'title':'What\'s your first name', 'fieldValue': ''},
{'fieldType':'textfield', 'title':'And your last name', 'fieldValue': ''},
{'fieldType':'radio', 'title':'And your sex', 'fieldOptions': [{ 'option_id': 0, 'option_title': 'Male', 'option_value': 'M' }, { 'option_id': 1, 'option_title': 'Female', 'option_value': 'F' }], 'fieldValue': ''},
{'fieldType':'date', 'title':'When were you born?', 'fieldValue': ''},
{'fieldType':'number', 'title':'What\'s your phone #?', 'fieldValue': ''}
];
submission_fields[0].fieldValue = 'David';
submission_fields[1].fieldValue = 'Baldwynn'+Date.now();
@ -132,19 +172,19 @@ describe('FormSubmission Model Unit Tests:', function() {
mySubmission = new FormSubmission({
admin: user,
form: form,
timeElapsed: 17.55
});
// for(var i=0; i<submission_fields.length; i++){
// mySubmission.form_fields.create(submission_fields[i]);
// }
timeElapsed: 17.55,
form_fields: submission_fields
});
*/
done();
});
});
it('should be able to save a FormSubmission without problems', function(done) {
return mySubmission.save(function(err, submission) {
if(err) done(err);
should.not.exist(err);
should.exist(submission);
should.exist(submission.oscarDemoNum);
done();
});
@ -153,117 +193,108 @@ describe('FormSubmission Model Unit Tests:', function() {
it('should add Patient to OscarHost EMR after save');
});
// describe('Method Find', function(){
// beforeEach(function(done){
// myForm.save(function(err) {
// done();
// });
// });
// // it('should be able to findOne my form without problems', function(done) {
// // return Form.findOne({_id: myForm._id}, function(err,form) {
// // should.not.exist(err);
// // should.exist(form);
// // should.deepEqual(form.toObject(), myForm.toObject());
// // done();
// // });
// // });
// });
describe('Method Find', function(){
beforeEach(function(done){
mySubmission.save(function(err) {
done();
});
});
it('should be able to findOne FormSubmission without problems', function(done) {
return FormSubmission.findOne({_id: mySubmission._id}, function(err,submission) {
should.not.exist(err);
should.exist(submission);
should.deepEqual(submission.toObject(), mySubmission.toObject());
done();
});
});
// describe('Test FormField and Submission Logic', function() {
// var new_form_fields_add1, new_form_fields_del, submission_fields, old_fields, form;
it('should be able to find FormSubmission by $elemMatch on form_fields id', function(done){
return FormSubmission.findOne({ form: myForm._id, admin: user, form_fields: {$elemMatch: {_id: myForm.form_fields[0]._id} } })
.exec(function(err, submission){
should.not.exist(err);
should.exist(submission);
should.deepEqual(submission.toObject(), mySubmission.toObject());
done();
});
});
});
// before(function(){
// new_form_fields_add1 = _.clone(myForm.toObject().form_fields);
// new_form_fields_add1.push(
// {'fieldType':'textfield', 'title':'Last Name', 'fieldValue': ''}
// );
describe('Test FormField and Submission Logic', function() {
var new_form_fields_add1, new_form_fields_del;
// new_form_fields_del = _.clone(myForm.toObject().form_fields);
// new_form_fields_del.splice(0, 1);
beforeEach(function(done){
new_form_fields_add1 = _.clone(myForm.toObject().form_fields);
new_form_fields_add1.push(
{'fieldType':'textfield', 'title':'Last Name', 'fieldValue': ''}
);
// //Create Submission
// submission_fields = _.clone(myForm.toObject().form_fields);
// for(var i=0; i<submission_fields.length; i++){
// submission_fields[i] = new Field(submission_fields[i]);
// }
// submission_fields[0].fieldValue = 'David';
// submission_fields[1].fieldValue = true;
// submission_fields[2].fieldValue = true;
// mySubmission = new FormSubmission({
// form_fields: [],
// admin: user,
// form: myForm,
// timeElapsed: 17.55
// });
//Create Submission
// for(var i=0; i<submission_fields.length; i++){
// mySubmission.form_fields.push(submission_fields[i]);
// }
// });
mySubmission = new FormSubmission({
form_fields: sampleSubmission,
admin: user,
form: myForm,
timeElapsed: 17.55
});
// beforeEach(function(done){
// myForm.save(function(){
// console.log(mySubmission);
// mySubmission.save(function(err){
// if(err) done(err);
// done();
// });
// });
// });
mySubmission.save(function(err){
if(err) done(err);
done();
});
});
it('should preserve deleted form_fields that have submissions without any problems', function(done) {
// afterEach(function(done){
// mySubmission.remove(function(){
// done();
// });
// });
var old_fields = myForm.toObject().form_fields;
var new_form_fields = _.clone(myForm.toObject().form_fields);
new_form_fields.splice(0, 1);
// it('something', function(done){
// done();
// });
myForm.form_fields = new_form_fields;
// // it('should preserve deleted form_fields that have submissions without any problems', function(done) {
myForm.save(function(err, _form) {
// // old_fields = myForm.toObject().form_fields;
should.not.exist(err);
should.exist(_form);
// // // var expected_fields = old_fields.slice(1,3).concat(old_fields.slice(0,1));
// var actual_fields = _.map(_form.toObject().form_fields, function(o){ _.omit(o, '_id')});
// old_fields = _.map(old_fields, function(o){ _.omit(o, '_id')});
// // myForm.form_fields = new_form_fields_del;
// console.log(old_fields);
should.deepEqual(JSON.stringify(_form.toObject().form_fields), JSON.stringify(old_fields), 'old form_fields not equal to newly saved form_fields');
done();
});
});
// // myForm.save(function(err, form) {
// it('should delete \'preserved\' form_fields whose submissions have been removed without any problems', function(done) {
// // should.not.exist(err);
// // should.exist(_form);
// // // var actual_fields = _.map(_form.toObject().form_fields, function(o){ _.omit(o, '_id')});
// // // old_fields = _.map(old_fields, function(o){ _.omit(o, '_id')});
// // // console.log(old_fields);
// // should.deepEqual(JSON.stringify(_form.toObject().form_fields), JSON.stringify(old_fields), 'old form_fields not equal to newly saved form_fields');
// // done();
// // });
// // });
// // it('should delete \'preserved\' form_fields whose submissions have been removed without any problems', function(done) {
// // myForm.form_fields = new_form_fields_del;
// // myForm.save(function(err, form
// // should.not.exist(err);
// // (form.form_fields).should.be.eql(old_fields, 'old form_fields not equal to newly saved form_fields');
// myForm.form_fields = new_form_fields_del;
// myForm.save(function(err, form
// should.not.exist(err);
// (form.form_fields).should.be.eql(old_fields, 'old form_fields not equal to newly saved form_fields');
// // //Remove submission
// // mySubmission.remove(function(err){
// // myForm.submissions.should.have.length(0);
// // myForm.form_fields.should.not.containDeep(old_fields[0]);
// // });
// // });
// // });
// });
// //Remove submission
// mySubmission.remove(function(err){
// myForm.submissions.should.have.length(0);
// myForm.form_fields.should.not.containDeep(old_fields[0]);
// });
// });
// });
afterEach(function(done){
mySubmission.remove(function(){
done();
});
});
});
afterEach(function(done) {
Form.remove().exec(function() {
User.remove().exec(done);
User.remove().exec(function() {
FormSubmission.remove().exec(done);
});
});
});
});

View File

@ -1,90 +1,90 @@
// 'use strict';
'use strict';
// /**
// * Module dependencies.
// */
// var should = require('should'),
// mongoose = require('mongoose'),
// User = mongoose.model('User');
/**
* Module dependencies.
*/
var should = require('should'),
mongoose = require('mongoose'),
User = mongoose.model('User');
// /**
// * Globals
// */
// var user, user2;
/**
* Globals
*/
var user, user2;
// /**
// * Unit tests
// */
// describe('User Model Unit Tests:', function() {
// beforeEach(function(done) {
// user = new User({
// firstName: 'Full',
// lastName: 'Name',
// email: 'test@test.com',
// username: 'test@test.com',
// password: 'password',
// provider: 'local'
// });
// user2 = new User({
// firstName: 'Full',
// lastName: 'Name',
// email: 'test@test.com',
// username: 'test@test.com',
// password: 'password',
// provider: 'local'
// });
/**
* Unit tests
*/
describe('User Model Unit Tests:', function() {
beforeEach(function(done) {
user = new User({
firstName: 'Full',
lastName: 'Name',
email: 'test@test.com',
username: 'test@test.com',
password: 'password',
provider: 'local'
});
user2 = new User({
firstName: 'Full',
lastName: 'Name',
email: 'test@test.com',
username: 'test@test.com',
password: 'password',
provider: 'local'
});
// done();
// });
done();
});
// describe('Method Save', function() {
// it('should begin with no users', function(done) {
// User.find({}, function(err, users) {
// users.should.have.length(0);
// done();
// });
// });
describe('Method Save', function() {
it('should begin with no users', function(done) {
User.find({}, function(err, users) {
users.should.have.length(0);
done();
});
});
// it('should be able to save without problems', function(done) {
// user.save(done);
// });
it('should be able to save without problems', function(done) {
user.save(done);
});
// it('should fail to save an existing user again', function(done) {
// user.save(function() {
// user2.save(function(err) {
// should.exist(err);
// done();
// });
// });
// });
it('should fail to save an existing user again', function(done) {
user.save(function() {
user2.save(function(err) {
should.exist(err);
done();
});
});
});
// it('should be able to show an error when try to save without first name', function(done) {
// user.firstName = '';
// return user.save(function(err) {
// should.exist(err);
// done();
// });
// });
// });
it('should be able to show an error when try to save without first name', function(done) {
user.firstName = '';
return user.save(function(err) {
should.exist(err);
done();
});
});
});
// describe('Method findUniqueUsername', function() {
// beforeEach(function(done) {
// User.find({}, function(err, users) {
// users.should.have.length(0);
// user.save(done);
// });
// });
describe('Method findUniqueUsername', function() {
beforeEach(function(done) {
User.find({}, function(err, users) {
users.should.have.length(0);
user.save(done);
});
});
// it('should be able to find unique version of existing username without problems', function(done) {
// User.findUniqueUsername(user.username, null, function (availableUsername) {
// availableUsername.should.not.equal(user.username);
// done();
// });
// });
it('should be able to find unique version of existing username without problems', function(done) {
User.findUniqueUsername(user.username, null, function (availableUsername) {
availableUsername.should.not.equal(user.username);
done();
});
});
// });
});
// afterEach(function(done) {
// User.remove().exec(done);
// });
// });
afterEach(function(done) {
User.remove().exec(done);
});
});

10
config/env/test.js vendored
View File

@ -56,5 +56,15 @@ module.exports = {
pass: process.env.MAILER_PASSWORD || ''
}
}
},
oscarhost: {
baseUrl: process.env.OSCARHOST_BASEURL || 'OSCARHOST_BASEURL',
settings: {
updateType: process.env.OSCARHOST_UPDATETYPE || 'OSCARHOST_UPDATETYPE',
},
auth:{
user: process.env.OSCARHOST_USER || 'process.env.OSCARHOST_USER',
pass: process.env.OSCARHOST_PASS || 'process.env.OSCARHOST_PASS',
}
}
};

View File

@ -144,7 +144,9 @@ module.exports = function(grunt) {
src: watchFiles.mochaTests,
options: {
reporter: 'spec',
require: 'server.js'
quiet: false,
require: 'server.js',
ui: 'bdd'
}
},
karma: {

View File

@ -66,6 +66,7 @@
"method-override": "~2.3.0",
"mocha": ">=1.20.0",
"mongoose": "~3.8.8",
"mongoose-utilities": "^0.1.1",
"morgan": "~1.4.1",
"multer": "~0.1.8",
"node-pdffiller": "~0.0.5",