So we have a endpoint for: PUT /api/v1/documents/5bcb58b23e93a95b1180e665
There are 2 types of updates for a document:
An update that triggers a new version: This happens when inside the req.body.contributions comes with some Comment ids (its an array)
An update that doesn't triggers a new version: This happens when inside the req.body.contributions comes empty (or undefined, in many cases)
So lets say we have this req.body
{
"author": "5bcb57e83e93a95b1180e664",
"published": true,
"closed": false,
"customForm": "5bcb56444b2c6b58e053f6c2",
"content": {
"title": "Qua tu etiam inprudens utebare non numquam",
"imageCover": "https://placeimg.com/1000/350/arch",
"youtubeId": "P6lefqnqgSg",
"fundation": { ... },
"articles": { ... },
"closure": null,
"closingDate": null,
}
"contributions": [] // Might or might not come in the req.body, have in mind that.
}
This shouldn't trigger a version, because there are no contributions in this id.
So we should:
Get the document
Update the document's fields with this object { published: req.body.published, closed: req.body.closed} (this might change in an edit)
Get the current version (query: { document: retrievedDocument._id, version: retrievedDocument.lastVersion }
Update the content of the version DocumentVersion.update( ... )
Note: Inside the db-apis method DocumentVersion.update there has to be a validation that needs to be done using this function and the customForm that the Document uses, so remember to retrieve it first!
validator.isDataValid(
customForm.fields,
content // this is the content that came inside the req.body!
)
In the case that contributions comes with stuff, you should be able to:
Increment the documents lastVersion
Create a new version, saving the content that came in req.body.content and the number of the version is the incremented number of lastVersion
In the new version remember to save in contributions the req.body.contributions where the IDs of the comments are!
The return of this put should be a OK.
You can "build" the document to reflect what is in database or just manipulate objects and construct the same structure that a Document.get() should give you, with the field content and the content of the version with that document to return in the body of the response.
Updating a document should be easy, but at the same time it might be tricky. For now, we will asume that the whole document comes in the req.body
Say we have this document saved in our DB
In the database, this is stored like this:
In the Document collection, like:
In the DocumentVersion collection, like:
So we have a endpoint for:
PUT /api/v1/documents/5bcb58b23e93a95b1180e665
There are 2 types of updates for a document:
req.body.contributions
comes with someComment
ids (its an array)req.body.contributions
comes empty (or undefined, in many cases)So lets say we have this req.body
This shouldn't trigger a version, because there are no contributions in this id. So we should:
{ published: req.body.published, closed: req.body.closed}
(this might change in an edit){ document: retrievedDocument._id, version: retrievedDocument.lastVersion }
DocumentVersion.update( ... )
DocumentVersion.update
there has to be a validation that needs to be done using this function and the customForm that the Document uses, so remember to retrieve it first!In the case that
contributions
comes with stuff, you should be able to:lastVersion
req.body.content
and the number of the version is the incremented number oflastVersion
contributions
thereq.body.contributions
where the IDs of the comments are!The return of this put should be a OK. You can "build" the document to reflect what is in database or just manipulate objects and construct the same structure that a Document.get() should give you, with the field
content
and the content of the version with that document to return in the body of the response.Its a huge task! Dont hesitate asking for help!