This repository has been archived by the owner on Nov 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 213
/
Copy pathrecords-queue.js
475 lines (417 loc) · 14.8 KB
/
records-queue.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
/**
* Deals with browser storage
*/
import store from './store';
import connection from './connection';
import gui from './gui';
import events from './event';
import settings from './settings';
import exporter from './exporter';
import { t } from './translator';
import $ from 'jquery';
import formCache from './form-cache';
import { setLastSavedRecord } from './last-saved';
let $exportButton;
let $uploadButton;
let $recordList;
let $queueNumber;
let uploadProgress;
let finalRecordPresent;
let uploadOngoing = false;
/**
* @typedef {import('../../app/models/record-model').EnketoRecord} EnketoRecord
*/
function init() {
_setUploadIntervals();
// TODO: Add export feature
$exportButton = $( '.record-list__button-bar__button.export' );
$uploadButton = $( '.record-list__button-bar__button.upload' );
$queueNumber = $( '.offline-enabled__queue-length' );
return _updateRecordList();
}
/**
* Obtains a record
*
* @param { string } instanceId - instanceID of record
* @return {Promise<EnketoRecord|undefined>} a Promise that resolves with a record object or undefined
*/
function get( instanceId ) {
return store.record.get( instanceId );
}
/**
* Stores a new record. Overwrites (media) files from auto-saved record.
*
* @param { EnketoRecord } record - a record object
* @return {Promise<undefined>} a promise that resolves with undefined
*/
function set( record ) {
return getAutoSavedRecord()
.then( autoSavedRecord => {
// Add files from autoSavedRecord in case this record was recovered.
// A more intelligent way to do is to maintain and check a recovered flag
// first, and only then replace the files.
if ( autoSavedRecord ) {
record.files = autoSavedRecord.files;
}
return store.record.set( record );
} );
}
/**
* Creates (sets) or updates a record.
*
* @param { 'set' | 'update' } action - determines whether to create or update the record
* @param { EnketoRecord } record - the record to save
*
* @return { Promise<EnketoRecord> }
*/
function save( action, record ) {
/** @type { Promise<EnketoRecord> } */
let promise;
/** @type { EnketoRecord } */
let result;
if ( action === 'set' ) {
promise = set( record );
} else {
promise = store.record.update( record );
}
return promise
.then( record => {
result = record;
return result;
} )
.then( ( { enketoId } ) => formCache.get( { enketoId } ) )
.then( survey => setLastSavedRecord( survey, record ) )
.then( _updateRecordList )
.then( () => result );
}
/**
* Removes a record
*
* @param { string } instanceId - instanceID of record
* @return { Promise<undefined> } a promise that resolves with undefined
*/
function remove( instanceId ) {
return store.record.remove( instanceId )
.then( _updateRecordList );
}
/**
* Obtains auto-saved record key
*/
function getAutoSavedKey() {
return `__autoSave_${settings.enketoId}`;
}
/**
* Obtains auto-saved record.
*/
function getAutoSavedRecord() {
return get( getAutoSavedKey() );
}
/**
* Updates auto-saved record
*
* @param { EnketoRecord } record - record object created from the current state of the form
* @return { Promise<Record> }
*/
function updateAutoSavedRecord( record ) {
// prevent this record from accidentally being submitted
record.draft = true;
// give an internal name
record.name = `__autoSave_${Date.now()}`;
// use the pre-defined key
record.instanceId = getAutoSavedKey();
// make the record valid
record.enketoId = settings.enketoId;
return store.record.update( record );
// do not update recordList
}
/**
* Removes auto-saved record
*/
function removeAutoSavedRecord() {
return store.record.remove( getAutoSavedKey() );
// do not update recordList
}
/**
* Gets the countervalue of a new record (guaranteed to be unique)
*
* @param { string } enketoId - Enketo ID
* @return {Promise<undefined>} Promise that resolves with undefined
*/
function getCounterValue( enketoId ) {
return store.property.getSurveyStats( enketoId )
.then( stats => !stats || isNaN( stats.recordCount ) ? 1 : stats.recordCount + 1 );
}
/**
* Marks a record as active (opened)
*
* @param { string } instanceId - instanceID of a record
*/
function setActive( instanceId ) {
settings.recordId = instanceId;
$( '.record-list__records' )
.find( '.active' ).removeClass( 'active' )
.addBack().find( `[data-id="${instanceId}"]` ).addClass( 'active' );
}
/**
* Sets the interval to upload queued records
*/
function _setUploadIntervals() {
// one quick upload attempt soon after page load
setTimeout( () => {
uploadQueue();
}, 30 * 1000 );
// interval to check upload queued records
setInterval( () => {
uploadQueue();
}, 5 * 60 * 1000 );
}
/**
* Uploads all final records in the queue
*
* @return {Promise<undefined>} a Promise that resolves with undefined
*/
function uploadQueue() {
let errorMsg;
const successes = [];
const fails = [];
let authRequired;
if ( uploadOngoing || !finalRecordPresent ) {
return;
}
uploadOngoing = true;
$uploadButton.prop( 'disabled', true );
return connection.getOnlineStatus()
.then( appearsOnline => {
if ( !appearsOnline ) {
return;
}
return getDisplayableRecordList( settings.enketoId, { finalOnly: true } );
} )
.then( records => {
if ( !records || records.length === 0 ) {
uploadOngoing = false;
return;
}
console.debug( `Uploading queue of ${records.length} records.` );
// Perform record uploads sequentially for nicer feedback and to avoid issues when connections are very poor
return records.reduce( ( prevPromise, record ) => prevPromise.then( () => // get the whole record including files
store.record.get( record.instanceId )
.then( record => {
// convert record.files to a simple <File> array
record.files = record.files.map( object => {
// do not add name property if already has one (a File will throw exception)
if ( typeof object.item.name === 'undefined' ) {
object.item.name = object.name;
}
return object.item;
} );
uploadProgress.update( record.instanceId, 'ongoing', '', successes.length + fails.length, records.length );
return connection.uploadQueuedRecord( record );
} )
.then( () => {
successes.push( record.name );
uploadProgress.update( record.instanceId, 'success', '', successes.length + fails.length, records.length );
return store.record.remove( record.instanceId )
.then( () => store.property.addSubmittedInstanceId( record ) );
} )
.catch( result => {
// catch 401 responses (1 of them)
if ( result.status === 401 ) {
authRequired = true;
}
// if any non HTTP error occurs, output the error.message
errorMsg = result.message || gui.getErrorResponseMsg( result.status );
fails.push( record.name );
uploadProgress.update( record.instanceId, 'error', errorMsg, successes.length + fails.length, records.length );
} )
.then( () => {
if ( successes.length + fails.length === records.length ) {
uploadOngoing = false;
if ( authRequired ) {
gui.confirmLogin();
} else if ( successes.length > 0 ) {
// let gui send a feedback message
document.dispatchEvent( events.QueueSubmissionSuccess( successes ) );
}
// update the list by properly removing obsolete records, reactivating button(s)
_updateRecordList();
}
} ) ), Promise.resolve() );
} );
}
/**
* Creates a zip file of all locally-saved records.
*
* @param { string } formTitle - the title of the form
* @return {Promise<Blob>} a Promise that resolves with a zip file as Blob
*/
function exportToZip( formTitle ) {
$exportButton.prop( 'disabled', true );
return exporter.recordsToZip( settings.enketoId, formTitle )
.then( blob => {
$exportButton.prop( 'disabled', false );
return blob;
} )
.catch( error => {
$exportButton.prop( 'disabled', false );
throw error;
} );
}
/**
* Shows upload progress and record-specific feedback
*
* @type { object }
*/
uploadProgress = {
_getLi( instanceId ) {
return $( `.record-list__records__record[data-id="${instanceId}"]` );
},
_reset( instanceId ) {
const $allLis = $( '.record-list__records' ).find( 'li' );
//if the current record, is the first in the list, reset the list
if ( $allLis.first().attr( 'data-id' ) === instanceId ) {
$allLis.removeClass( 'ongoing success error' ).filter( function() {
return !$( this ).hasClass( 'record-list__records__record' );
} ).remove();
}
},
_updateClass( $el, status ) {
$el.removeClass( 'ongoing success error' ).addClass( status );
},
_updateProgressBar( index, total ) {
let $progress;
$progress = $( '.record-list__upload-progress' ).attr( {
'max': total,
'value': index
} );
if ( index === total || total === 1 ) {
$progress.css( 'visibility', 'hidden' );
} else {
$progress.css( 'visibility', 'visible' );
}
},
_getMsg( status, msg ) {
return ( status === 'error' ) ? msg : '';
},
update( instanceId, status, msg, index, total ) {
let $result;
const $li = this._getLi( instanceId );
const displayMsg = this._getMsg( status, msg );
this._reset( instanceId );
// add display messages (always showing end status)
if ( displayMsg ) {
$result = $( `<li data-id="${instanceId}" class="record-list__records__msg ${status}">${displayMsg}</li>` ).insertAfter( $li );
window.setTimeout( () => {
$result.hide( 600 );
}, 3000 );
}
// update the status class
this._updateClass( $li, status );
// hide succesful submissions from record list in side bar
// they will be properly removed later in _updateRecordList
if ( status === 'success' ) {
$li.hide( 1500 );
}
// update the submissions progress bar
if ( index && total ) {
this._updateProgressBar( index, total );
}
}
};
/**
* Retrieves a list of records for the active form, excluding auto-saved records.
* This was isolated from the `_updateRecordList` function to allow testing, and
* reused in `uploadQueue` to share the behavior.
*
* @param { string } enketoId
* @param { { finalOnly?: boolean } } [options] - Only included records that are 'final' (i.e. not 'draft')
* @return { Promise<Record[]> } - records to be displayed in the UI
*/
function getDisplayableRecordList( enketoId, { finalOnly = false } = {} ) {
const autoSavedKey = getAutoSavedKey();
const records = store.record.getAll( enketoId, finalOnly )
.then( records => {
return records.filter( record => record.instanceId !== autoSavedKey );
} );
return records;
}
/**
* Updates the record list in the UI
*
* @return { Promise<undefined> } [description]
*/
function _updateRecordList() {
let $li;
// reset the list
$exportButton.prop( 'disabled', true );
$uploadButton.prop( 'disabled', true );
$recordList = $( '.record-list__records' );
finalRecordPresent = false;
// rebuild the list
return getDisplayableRecordList( settings.enketoId )
.then( records => {
// update queue number
$queueNumber.text( records.length );
// add 'no records' message
if ( records.length === 0 ) {
$recordList.empty().append( `<li class="record-list__records--none" data-i18n="record-list.norecords">${t( 'record-list.norecords' )}</li>` );
} else {
$recordList.find( '.record-list__records--none' ).remove();
$exportButton.prop( 'disabled', false );
}
// remove records that no longer exist
$recordList.find( '.record-list__records__record' ).each( function() {
const $rec = $( this );
if ( !records.some( rec => $rec.attr( 'data-id' ) === rec.instanceId ) ) {
$rec.next( '.msg' ).addBack().remove();
}
} );
records.forEach( record => {
// if there is at least one record not marked as draft
if ( !record.draft ) {
finalRecordPresent = true;
$uploadButton.prop( 'disabled', false );
}
$li = uploadProgress._getLi( record.instanceId );
// Add the record to the list if it doesn't exist already
// Any submission error messages and class will remain present for existing records.
if ( $li.length === 0 ) {
$li = $( '<li class="record-list__records__record" />' )
.attr( 'data-id', record.instanceId )
.appendTo( $recordList );
}
// add or update properties
$li.text( record.name )
.attr( 'data-draft', !!record.draft );
} );
} );
}
/**
* Completely flush the form cache (not the record storage)
*
* @return {Promise<undefined>} a Promise that resolves with undefined
*/
function flush() {
return store.flushTable( 'records' )
.then( () => store.flushTable( 'files' ) )
.then( () => {
console.log( 'Done! The record store is empty now.' );
return;
} );
}
export default {
init,
get,
save,
remove,
getAutoSavedKey,
getAutoSavedRecord,
getDisplayableRecordList,
updateAutoSavedRecord,
removeAutoSavedRecord,
flush,
getCounterValue,
setActive,
uploadQueue,
exportToZip
};