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