-
Notifications
You must be signed in to change notification settings - Fork 707
/
3.1.1.js
148 lines (141 loc) · 3.85 KB
/
3.1.1.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
'use strict';
/**
* Update 3.1.1
*
* Run this using "node ./src/db/manual/3.1.1
*
* @param db
* @param config
* @param tools
* @param done
*/
const utils = require('formiojs/utils').default;
const config = require('config');
const async = require('async');
const mongodb = require('mongodb');
const MongoClient = mongodb.MongoClient;
MongoClient.connect(config.mongo, {useNewUrlParser: true}, (err, client) => {
if (err) {
/* eslint-disable no-console */
return console.log(`Could not connect to database ${config.mongo}`);
/* eslint-enable no-console */
}
const db = client.db(client.s.options.dbName);
const formCollection = db.collection('forms');
const submissionCollection = db.collection('submissions');
const setDateField = (field, data) => {
if (!data) {
return false;
}
const parts = field.split('.');
const value = data[parts[0]];
// Handle Datagrids
if (Array.isArray(value)) {
return value.reduce((changed, row) => {
return setDateField(parts.slice(1).join('.'), row) || changed;
}, false);
}
// Handle Containers
if (parts.length > 1) {
return setDateField(parts.slice(1).join('.'), value);
}
if (value && !(value instanceof Date)) {
data[parts[0]] = new Date(value);
return true;
}
else {
return false;
}
};
const getNext = function(cursor, next) {
cursor.hasNext().then((hasNext) => {
if (hasNext) {
return cursor.next().then(next);
}
else {
return next();
}
});
};
let hasNextForm = true;
const formCursor = formCollection.find({deleted: {$eq: null}});
async.doWhilst((nextForm) => {
getNext(formCursor, (form) => {
if (!form) {
hasNextForm = false;
return nextForm();
}
const fields = [];
utils.eachComponent(form.components, function(component, path) {
// We only care about non-layout components, which are not unique, and have not been blacklisted.
if (component.type === 'datetime') {
fields.push(path);
}
}, true);
if (!fields.length) {
return nextForm();
}
const submissionCursor = submissionCollection.find({
form: form._id,
deleted: {$eq: null}
});
let hasNextSubmission = true;
async.doWhilst((nextSubmission) => {
getNext(submissionCursor, (submission) => {
if (!submission) {
hasNextSubmission = false;
return nextSubmission();
}
const changed = fields.reduce((changed, field) => {
return setDateField(field, submission.data) || changed;
}, false);
if (changed) {
/* eslint-disable no-console */
console.log(`Updating date fields for submission: ${submission._id}`);
/* eslint-enable no-console */
submissionCollection.updateOne(
{
_id: submission._id
},
{
$set: {
data: submission.data
}
},
{
upsert: false
},
(err) => {
if (err) {
return nextSubmission(err);
}
return nextSubmission();
}
);
}
else {
return nextSubmission();
}
});
}, () => {
return hasNextSubmission;
}, (err) => {
if (err) {
return nextForm(err);
}
return nextForm();
});
});
}, () => {
return hasNextForm;
}, (err) => {
if (err) {
/* eslint-disable no-console */
return console.log('ERROR', err);
/* eslint-enable no-console */
}
/* eslint-disable no-console */
console.log('Done');
/* eslint-enable no-console */
});
});