Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/lib/save-current-form-to-old-form.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const {info} = require('./log');
module.exports = (db, doc) =>
db.get(doc._id, { attachments: true, binary: true })
.then(existingDoc => {
existingDoc.doc_id = existingDoc._id;
existingDoc.type = 'old-form';
existingDoc._id = undefined;
existingDoc._rev = undefined;
const attachments = existingDoc._attachments;
existingDoc._attachments = {};
Object.keys(attachments).forEach(name => {
existingDoc._attachments[name] = {};
const keys = Object.keys(attachments[name]);
if (keys.includes('content_type') && keys.includes('data')) {
existingDoc._attachments[name]['content_type'] = attachments[name]['content_type'];
existingDoc._attachments[name]['data'] = Buffer.from(attachments[name]['data']);
}
});
return db.post(existingDoc);
})
.then(response => info(`previous form saved in doc with _id: ${response.id} type:'old-form'`))
.catch(e => {
info('old form failed');
if(e.status === 404) return;
else throw e;
});
2 changes: 2 additions & 0 deletions src/lib/upload-forms.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const crypto = require('crypto');
const fs = require('./sync-fs');
const log = require('./log');
const insertOrReplace = require('./insert-or-replace');
const saveCurrentFormToOldForm = require('./save-current-form-to-old-form')
const pouch = require('./db');
const warnUploadOverwrite = require('./warn-upload-overwrite');
const {
Expand Down Expand Up @@ -90,6 +91,7 @@ const execute = async (projectDir, subDirectory, options) => {

const changes = await warnUploadOverwrite.preUploadForm(db, doc, xml, properties);
if (changes) {
await saveCurrentFormToOldForm(db, doc);
await insertOrReplace(db, doc);
log.info(`Form ${filePath} uploaded`);
} else {
Expand Down