<?xml version="1.0" encoding="UTF-8"?>
<changelog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <changeset id="AS-2671-backfill_collabs_access:1" author="jbrooks" run-always="true">
        <cypherquery>
            <![CDATA[
            MATCH (i:Identity{ active: 'Y' })-[:HAS_PROFILE]->(p:Profile{ profileType: 'LabelProfile' })
                WHERE ('administrator' in p.roles OR 'accounting' in p.roles)
                AND NOT EXISTS ((i)-[:HAS_PROFILE]->(:Profile{ profileType:'CollaboratorsProfile' }))
            WITH DISTINCT i
            CALL {
                WITH i
                MERGE (inc:IncrementId{ nodeName: 'Profile' })
                    ON CREATE SET inc.id = 2
                    ON MATCH SET inc.id = inc.id + 1
                CREATE (p:Profile{
                    profileId: (inc.id-1),
                    profileType: 'CollaboratorsProfile',
                    profileName: i.name,
                    uuid: apoc.create.uuid(),
                    roles: ['royalties'],
                    brand: i.defaultBrand,
                    createdAt: datetime(),
                    lastModifiedAt: datetime(),
                    createdBy: 'AS-2637-backfill_collabs_access:1',
                    lastModifiedBy: 'AS-2637-backfill_collabs_access:1'
                })
                MERGE (i)-[:HAS_PROFILE]->(p)
                RETURN p
            }
            RETURN i, p
            ]]>
        </cypherquery>
    </changeset>
</changelog>
