MATCH (user:USER {email: $user_email})-[follows:FOLLOWS]->(community:COMMUNITY)
WHERE tolower(follows.role) IN ['owner', 'Owner']
AND community.deleted_at IS NULL
AND user.deleted_at IS NULL
AND NOT EXISTS {
MATCH (community)<-[otherFollows:FOLLOWS]-(otherUser:USER)
WHERE tolower(otherFollows.role) IN ['owner', 'Owner']
AND otherFollows.id < follows.id
}
WITH community, MIN(follows.id) AS minFollowsId, follows
WHERE minFollowsId = follows.id
RETURN DISTINCT community
above community brings all the communities where the user's role is owner or Owner. There is relation named 'follows' between a user and community. A user follows a community
role property is set on that follow relation.
consider a community which has only 1 member 'Kunal' and Kunal is owner of that community.
consider another community which has 1 member Ankit and he is the owner of that community.
Now Kunal follows that community and the role is given as owner.
Now even though Kunal is the owner of 2 communities he is considered as owner of 1st community only because he was the first owner made. if a member is given role as owner and if there is already a owner that role is not considered as owner.
I need to update the above cypher query to fetch communities on which user is the first owner.
We can fetch that by looking into the id property set on follows relationship data. with role id is also getting stored, so the person with lowest id is considered as owner.