From 530c479369907cbf412e51d19d02497e125170a8 Mon Sep 17 00:00:00 2001 From: victor Date: Tue, 13 Sep 2022 21:20:24 +0000 Subject: [PATCH] saving current form --- src/lib/save-current-form-to-old-form.js | 26 ++++++++++++++++++++++++ src/lib/upload-forms.js | 2 ++ 2 files changed, 28 insertions(+) create mode 100644 src/lib/save-current-form-to-old-form.js diff --git a/src/lib/save-current-form-to-old-form.js b/src/lib/save-current-form-to-old-form.js new file mode 100644 index 000000000..435ad3a68 --- /dev/null +++ b/src/lib/save-current-form-to-old-form.js @@ -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; + }); diff --git a/src/lib/upload-forms.js b/src/lib/upload-forms.js index dfb675153..12f320385 100644 --- a/src/lib/upload-forms.js +++ b/src/lib/upload-forms.js @@ -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 { @@ -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 {