fix(#10132): update how api handles form updates on change#10171
fix(#10132): update how api handles form updates on change#10171nKataraia wants to merge 17 commits intomedic:masterfrom
Conversation
|
@dianabarsan Do you have bandwidth to review this PR (you logged the original issue)? If not, I can have a look. 👍 |
dianabarsan
left a comment
There was a problem hiding this comment.
Very nice! One request below and please add unit test coverage for this!
api/src/services/generate-xform.js
Outdated
| }); | ||
| }); | ||
| const updateAttachments = async (doc) => { | ||
| let generated = null; |
There was a problem hiding this comment.
You don't need to define this variable here, as it's not reused outside of the inner block.
| 'xform.xml': { data: Buffer.from(formXml) }, | ||
| 'form.html': { data: Buffer.from(currentForm) }, | ||
| 'model.xml': { data: Buffer.from(currentModel) } |
There was a problem hiding this comment.
As above, the get will only return attachment stubs, not full contents.
api/src/services/generate-xform.js
Outdated
| const updateAttachment = (doc, updated, name, type) => { | ||
| const attachmentData = doc._attachments && | ||
| const updateAttachment = (doc, update, name, type) => { | ||
| const attachmentData = doc && |
There was a problem hiding this comment.
This function will end up comparing new attachment contents with the existent ones. The problem now is that you're not reading all attachments, because you've changed the get command, and you're only reading the main xml attachment. This means that you never have the data of the model.xml or the form.html attachments, so the app will always think they need to updated, and you end up in an infinite loop. you will need to read the contents of those two attachments too to compare.
…e-changes' into 10132-api-form-update-changes
dianabarsan
left a comment
There was a problem hiding this comment.
Nice! Great improvements!
I did find one issue which I've detailed in the comments.
| const newForm = '<html><title>Hello</title></html>'; | ||
| const newModel = '<instance><multimedia/></instance>'; | ||
|
|
||
| sinon.stub(db.medic, 'getAttachment').resolves({ _attachments: {}}); |
There was a problem hiding this comment.
How would getAttachment return a an empty list of attachments?
| sinon.stub(db.medic, 'put'); | ||
|
|
||
| await service.update('form:exists'); | ||
| expect(db.medic.put.callCount).to.equal(1); |
There was a problem hiding this comment.
The test title says that the form should not be updated if the attachment is not xml, but then this assertion contradicts the test name, with the form actually getting updated.
I'm confused.
api/src/services/generate-xform.js
Outdated
| if (enketoForm) { | ||
| const name = formsService.getXFormAttachmentName(doc); | ||
| const rawXML = await db.medic.getAttachment(doc._id, name, { rev: doc._rev }); | ||
| const form = await db.medic.getAttachment(doc._id, 'form.html', { rev: doc._rev }); |
There was a problem hiding this comment.
It's possible that any of these two calls fail with a 404 if the form has just been uploaded. This is causing a lot of e2e test failures now.
You will need to wrap this in a try-catch and always create these attachments if they don't yet exist.
…rrected the assertion
api/src/services/generate-xform.js
Outdated
| const getAttachment = async (doc, name) => { | ||
| try { | ||
| return await db.medic.getAttachment(doc._id, name, { rev: doc._rev }); | ||
| } catch (error) { |
There was a problem hiding this comment.
I think we shouldn't catch all errors, just 404s, and throw all others.
…wn when fetching attachments
dianabarsan
left a comment
There was a problem hiding this comment.
Thanks for all the changes! Awesome work!
I believe this is done and we can proceed to acceptance test this along with the changes to cht-conf to support uploading large attachments.
The focus of this testing is:
- forms with large attachments get uploaded
- the model.xml and form.html do not get updated when subsequent attachments are uploaded
| }, | ||
| _rev: '1-rev' | ||
| }); | ||
| // sinon.stub(db.medic, 'getAttachment').resolves(Buffer.from(formXml)); |
There was a problem hiding this comment.
Please remove this commented code.
| expect(db.medic.getAttachment.callCount).to.equal(3); | ||
| expect(service.generate.callCount).to.equal(0); | ||
| expect(db.medic.put.callCount).to.equal(0); | ||
| // expectAttachments(db.medic.put.args[0][0], newForm, newModel); |
There was a problem hiding this comment.
Please remove this commented code.
|
This PR is now marked "stale" after 30 days without activity. It will be closed automatically in 10 days unless you add a comment, push new changes or remove the "stale" label. |
Co-authored-by: Diana Barsan <35681649+dianabarsan@users.noreply.github.com>
|
closing in favor of #10248 |
closes #10132