public async attachToGroup()

in source/packages/services/assetlibrary/src/groups/groups.full.service.ts [570:674]


    public async attachToGroup(
        sourceGroupPath: string,
        relationship: string,
        targetGroupPath: string
    ): Promise<void> {
        logger.debug(
            `groups.full.service attachToGroup: in: sourceGroupPath:${sourceGroupPath}, relationship:${relationship}, targetGroupPath:${targetGroupPath}`
        );

        ow(sourceGroupPath, 'sourceGroupPath', ow.string.nonEmpty);
        ow(relationship, 'relationship', ow.string.nonEmpty);
        ow(targetGroupPath, 'targetGroupPath', ow.string.nonEmpty);

        // any ids need to be lowercase
        sourceGroupPath = sourceGroupPath.toLowerCase();
        relationship = relationship.toLowerCase();
        targetGroupPath = targetGroupPath.toLowerCase();

        await this.authServiceFull.authorizationCheck(
            [],
            [sourceGroupPath, targetGroupPath],
            ClaimAccess.U
        );

        // fetch the existing groups
        const sourceGroupFuture = this.get(sourceGroupPath, false);
        const targetGroupFuture = this.get(targetGroupPath, false);
        const [sourceGroup, targetGroup] = await Promise.all([
            sourceGroupFuture,
            targetGroupFuture,
        ]);

        // make sure they exist
        if (sourceGroup === undefined) {
            throw new GroupNotFoundError(sourceGroupPath);
        }
        if (targetGroup === undefined) {
            throw new GroupNotFoundError(targetGroupPath);
        }

        // if the relation already exists, there's no need to continue
        if (sourceGroup.groups?.out?.[relationship]?.find((e) => e.id === targetGroupPath)) {
            logger.debug(`groups.full.service attachToGroup: relation already exits:`);
            return;
        }

        // ensure that the group relation is allowed
        const relatedGroup: DirectionToRelatedEntityArrayMap = {
            out: {
                [relationship]: [
                    {
                        id: targetGroupPath,
                    },
                ],
            },
        };

        const template = await this.typesService.get(
            sourceGroup.templateId,
            TypeCategory.Group,
            TypeDefinitionStatus.published
        );
        const validateRelationships = await this.validator.validateRelationshipsByIds(
            template,
            relatedGroup,
            undefined
        );
        if (!validateRelationships.isValid) {
            throw new RelationValidationError(validateRelationships);
        }

        // if fgac is enabled, we need to ensure any relations configured as identifying auth in its template are flagged to be saved as so
        let isAuthCheck = true;
        if (this.isAuthzEnabled) {
            const authRelations = template.schema.relations.outgoingAuthRelations();
            this.authServiceFull.updateRelsIdentifyingAuth(
                relatedGroup.out,
                validateRelationships.groupLabels,
                authRelations
            );
            isAuthCheck = relatedGroup.out[relationship][0].isAuthCheck ?? false;
        }

        // Save to datastore
        await this.groupsDao.attachToGroup(
            sourceGroupPath,
            relationship,
            targetGroupPath,
            isAuthCheck
        );

        // fire event
        await this.eventEmitter.fire({
            objectId: sourceGroupPath,
            type: Type.group,
            event: Event.modify,
            attributes: {
                sourceGroupPath,
                attachedToGroup: targetGroupPath,
                relationship,
            },
        });

        logger.debug(`groups.full.service attachToGroup: exit:`);
    }