paleobot / pbot-api

MIT License
2 stars 0 forks source link

Update of CharacterInstance does not record previous value for quantities #11

Closed NoisyFlowers closed 2 years ago

NoisyFlowers commented 2 years ago

The HAS_STATE relationship that connects a CharacterInstance to a State will contain a value property if the State is a quantity. The current update logic does not record old values of this in the ENTERED_BY relationship when it is changed.

This problem applies to both the newer javascript-based update and the original cypher code. In fact, the javascript approach doesn't even record the new value in the HAS_STATE relationship!

I'm not sure this can be handled by the javascript approach, as it does not fit well into the generic approach to update that approach relies on. This might be an instance where we need to keep the specific cypher code in schema.graphql.

In the cypher code, I think we need to do something like this:

                WITH characterInstance, eb, oldState, **oldStateRelationship**
                    CALL apoc.do.case([
                            oldState IS NULL AND $data.stateID IS NOT NULL,
                            "SET eb.state = 'not present' RETURN eb",
                            oldState IS NOT NULL AND ($data.stateID IS NULL OR oldState.pbotID  <> $data.stateID),
                            "SET eb.state = oldState.pbotID, **eb.quantity = oldStateRelationship.value** RETURN eb"],
                            "RETURN eb",
                            {oldState: oldState, eb: eb}
                        ) YIELD value
                WITH characterInstance
                MATCH
                    (character:Character {pbotID: $data.characterID})<-[:STATE_OF*1..100]-(state:State {pbotID: $data.stateID})

This should work even if the State is not quantitative, since, in Neo4J, assigning null to a property is the same as the property not existing.

There will also have to be some reordering of the deletion of oldStateRelationship.

NoisyFlowers commented 2 years ago

This is fixed in f421bfaf976825db2f75eaf6c37f6839ea25b949.