public async create()

in source/packages/services/assetlibrary/src/groups/groups.full.service.ts [241:356]


    public async create(group: GroupItem, applyProfile?: string): Promise<string> {
        logger.debug(
            `groups.full.service create: in: group:${JSON.stringify(
                group
            )}, applyProfile:${applyProfile}`
        );

        ow(group, ow.object.nonEmpty);
        ow(group.templateId, ow.string.nonEmpty);
        ow(group.name, ow.string.nonEmpty);

        // if a profile to apply has been provided, apply it first
        if (applyProfile !== undefined) {
            group = await this.applyProfile(group, applyProfile);
        }

        // remove any non printable characters from the id
        group.name = group.name.replace(/[^\x20-\x7E]+/g, '');

        // any ids need to be lowercase
        this.setIdsToLowercase(group);

        await this.authServiceFull.authorizationCheck(
            [],
            [group.parentPath, ...group.listRelatedGroupPaths()],
            ClaimAccess.C
        );

        // perform validation of the group...
        const template = await this.typesService.get(
            group.templateId,
            TypeCategory.Group,
            TypeDefinitionStatus.published
        );
        if (template === undefined) {
            throw new TemplateNotFoundError(group.templateId);
        }
        const validateSubTypeFuture = this.validator.validateSubType(
            template,
            group,
            Operation.CREATE
        );
        if (this.validateAllowedParentPaths) {
            if (group.groups === undefined) {
                group.groups = { out: {} };
            } else if (group.groups.out === undefined) {
                group.groups.out = {};
            }
            group.groups.out.parent = [{ id: group.parentPath }];
        }
        const validateRelationshipsFuture = this.validator.validateRelationshipsByIds(
            template,
            group.groups,
            undefined
        );
        const [subTypeValidation, validateRelationships] = await Promise.all([
            validateSubTypeFuture,
            validateRelationshipsFuture,
        ]);

        // schema validation results
        if (!subTypeValidation.isValid) {
            throw new SchemaValidationError(subTypeValidation.errors);
        }

        // validate the id associations
        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
        if (this.isAuthzEnabled) {
            const incomingAuthRelations = template.schema.relations.incomingAuthRelations();
            const outgoingAuthRelations = template.schema.relations.outgoingAuthRelations();
            this.authServiceFull.updateRelsIdentifyingAuth(
                group.groups?.in,
                validateRelationships.groupLabels,
                incomingAuthRelations
            );
            this.authServiceFull.updateRelsIdentifyingAuth(
                group.groups?.out,
                validateRelationships.groupLabels,
                outgoingAuthRelations
            );
        }

        // ensure parent exists
        const parent = await this.get(group.parentPath, false);
        if (parent === undefined) {
            throw new GroupNotFoundError(group.parentPath);
        }

        group.groupPath =
            group.parentPath === '/'
                ? '/' + group.name.toLowerCase()
                : `${group.parentPath}/${group.name.toLowerCase()}`;

        // Assemble item into node
        group.category = TypeCategory.Group;
        const node = this.groupsAssembler.toNode(group);

        // Save to datastore
        logger.debug(`groups.full.service create: *****: ${JSON.stringify(node)}`);
        await this.groupsDao.create(node, group.groups);

        // fire event
        await this.eventEmitter.fire({
            objectId: group.groupPath,
            type: Type.group,
            event: Event.create,
            payload: JSON.stringify(group),
        });

        logger.debug(`groups.full.service create: exit: ${group.groupPath}`);
        return group.groupPath;
    }