farmOS / farmOS.js

A JavaScript library for working with farmOS data structures and interacting with farmOS servers.
MIT License
15 stars 13 forks source link

Subrequests #68

Closed jgaehring closed 1 year ago

jgaehring commented 1 year ago

This still has some issues, but I couldn't help committing some changes to the overall project structure along the way, which I'm too lazy to try and disentangle, so I just added some warnings instead.

This branches off #67 and should be merged subsequently.

jgaehring commented 1 year ago

This still has some issues

Namely:

https://github.com/farmOS/farmOS.js/blob/d1d4ee3913ba9017ec125ab3d2aa2f0a9bf88631/src/client/subrequest.js#L184-L202

@mstenta or @paul121, any guess why a POST request to a an entity's relationship endpoint returns a 204 w/o updating that relationship?

Here's the example I tested locally, with a preexisting input log and quantity:

https://github.com/farmOS/farmOS.js/blob/d1d4ee3913ba9017ec125ab3d2aa2f0a9bf88631/test/client/relationships.js#L19-L25

Here's the relevant JSON:API spec: https://jsonapi.org/format/#crud-updating-to-many-relationships

paul121 commented 1 year ago

Log quantities are a bit unique because they have a revision ID as well. When you update an existing quantity then the log needs to be updated to that new revision as well. I haven't tried creating or updating log quantities via the relationship endpoint so there might be some gotchas there.

Some python code I have where I first create a quantity entity and then create/update a log in a second request. This is the part that specifies the quantity relationship & quantity revision ID:

# Add quantity to log.
log["relationships"]["quantity"] = {
    "data": [
        {
            "type": "quantity--standard",
            "id": harvest_quantity["data"]["id"],
            "meta": {
                "target_revision_id": harvest_quantity["data"]["attributes"]["drupal_internal__revision_id"]
            }
        },
    ]
}

Maybe this would work, assuming a quantityRevisionId variable is populated:

 const uri = '/api/log/input/f9f44a71-4373-4a60-ac36-37f5564b86e8/relationships/quantity'; 
 const quantity = { 
   id: '9fb2963d-7185-4da7-9ccd-d6e29e0f22c1', 
   type: 'quantity--standard',
   meta: {
     target_revision_id: quantityRevisionId,
   },
 }; 
 const data = JSON.stringify({ data: [quantity] });
 return farm.remote.request(uri, { method: 'POST', data }); 
jgaehring commented 1 year ago

Oh that sounds like a good lead! Thanks, @paul121, I will try that out tomorrow.