<?xml version="1.0" encoding="UTF-8"?>
<changelog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://www.liquigraph.org/schema/1.0/liquigraph.xsd">
    <changeset id="AS-1859-collaborators_profile_backfill:1" author="jbrooks" run-on-change="true">
        <query>
            <![CDATA[
                MATCH (i:Identity {active: 'Y'})-[:HAS_PROFILE]->(lp:Profile {profileType: 'LabelProfile'})-[:HAS_ACCESS_TO]->(acc:Vendor)
                WHERE NOT (i)-[:HAS_PROFILE]->(:Profile {profileType: 'CollaboratorsProfile'})
                AND ('administrator' IN lp.roles OR 'accounting' IN lp.roles)
                WITH DISTINCT i, acc

                MATCH (inc:IncrementId {nodeName: 'Profile'})
                MERGE (pNew:Profile {
                    profileType: 'CollaboratorsProfile', 
                    profileId: inc.id,
                    profileName: i.name + ' CollaboratorsProfile',
                    lastModifiedAt: datetime(), lastModifiedBy: 'database/AS-1859-collaborators_profile_backfill:1',
                    roles: ['royalties']
                })
                ON CREATE SET pNew.uuid = apoc.create.uuid(),
                    pNew.createdAt = datetime(), 
                    pNew.createdBy = 'database/AS-1859-collaborators_profile_backfill:1', 
                    inc.id = inc.id + 1
                MERGE (i)-[:HAS_PROFILE]->(pNew)-[:HAS_ACCESS_TO]->(acc)
            ]]>
        </query>
    </changeset>
</changelog>
