When simplest operation turns difficult

Hi,

Making a simple linked (:NEXT relations) list should be just that, but isn't. How can I achieve such a thing?

In the test code below the 'unwind as event' part is there to resemble what's coming from a Neo4j kafka sink connector, and problem seems to be that unwind is not acting in a sequential manner (i.e. creating the TST nodes, checks and sets 'last' parameter for then in next run links up previous last with new by :NEXT.

The problem is contrived in its simplest form by only trying to link :TST (tag) nodes. Target is to achieve :NEXT links of both these and the 'c.e' ones

Any tips as to how one would go about making such a thing?

unwind [{tag:'123', c:[{e:1}]},{tag:'234', c:[{e:2},{e:3}]},{tag:'345',c:[{e:5}]},{tag:'456',c:[{e:4},{e:6}]}] as event
merge (p:TST {tag:event.tag})
with p, event
match (h:P) where h.n='n'
merge (h)-[:RELATES_TO]->(p)
with p, event, h
optional match (h)-[:RELATES_TO]->(p_last:TST) where p_last.last=TRUE 
foreach(_ in case when exists(p_last.last) 
    THEN [1] ELSE [] END | 
    set p_last.last=FALSE 
    merge (p_last)-[:NEXT]->(p))
set p.last=TRUE