DemocracyOS / core

1 stars 1 forks source link

Update a Document (With and without contributions) #50

Closed guillecro closed 6 years ago

guillecro commented 6 years ago

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

{
  "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
  }
}

In the database, this is stored like this:

In the Document collection, like:

{ 
    "_id" : ObjectId("5bcb58b23e93a95b1180e665"), 
    "lastVersion" : 1, 
    "published" : true, 
    "closed" : false, 
    "author" : ObjectId("5bcb57e83e93a95b1180e664"), 
    "customForm" : ObjectId("5bcb56444b2c6b58e053f6c2"), 
    "createdAt" : ISODate("2018-10-20T16:32:50.021+0000"), 
    "updatedAt" : ISODate("2018-10-20T16:32:50.021+0000"), 
    "__v" : 0
}

In the DocumentVersion collection, like:

{ 
    "_id" : ObjectId("5bcb58b23e93a95b1180e666"), 
    "contributions" : [

    ], 
    "document" : ObjectId("5bcb58b23e93a95b1180e665"), 
    "version" : 1, 
    "content" : {
        "title": "Qua tu etiam inprudens utebare non numquam",
        "imageCover": "https://placeimg.com/1000/350/arch",
        "youtubeId": "P6lefqnqgSg",
        "fundation": { ...  },
        "articles": { ...  },
        "closure": null,    
        "closingDate": null
      }
    "createdAt" : ISODate("2018-10-20T16:32:50.046+0000"), 
    "updatedAt" : ISODate("2018-10-20T16:32:50.046+0000"), 
    "__v" : 0
}

So we have a endpoint for: PUT /api/v1/documents/5bcb58b23e93a95b1180e665

There are 2 types of updates for a document:

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:

In the case that contributions comes with stuff, you should be able to:

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!

guillecro commented 6 years ago

Resolved in PR #62