How to query workspace's user database?

I’m trying my hand at writing scripts but can’t seem to find a way to query the user database with GraphQL.

Goal Of Script:
When the name of an entity is updated, I want to take any numbers in the name and put them in a “points” field and take any text in the name and see if it’s contained in the name of any Fibery users. If any user match is found, link them to the entity.

Example 1:
If the entity name was “Jon 2”, I would want to return the Fibery User named “Jonathan” and set the entity’s point value to “2”

Example 2:
If the entity name was “0.5 cam”, I would want to return the Fibery User named “Cameron” and set the entity’s point value to “0.5”

Here’s my script so far, but I don’t see any way to query the user database with GraphQL so I’m hitting a wall. Any help would be appreciated!

const fibery = context.getService('fibery');

for (const entity of args.currentEntities) {

    let originalName = entity["Name"];
    const numPortion = originalName.replace(/[^\d.]+/g, '');
    const textPortion = originalName.replace(/[0-9\s]/g, '');

    if (originalName = null) {

        return ("BLANK NAME");

    }

    else {

        if (numPortion.length > 0) {

            const formattedNum = parseFloat(numPortion);

            await fibery.updateEntity(entity.type, entity.id, {
                "Points": formattedNum,
            })

        }

        if (textPortion.length > 0) {

            const userCheck = await fibery.graphql("Fibery", `{findUsers (name: {contains: "${textPortion}"}){id, name}}`);

            if (userCheck.data.findUsers.length > 0) {

                await fibery.updateEntity(entity.type, entity.id, {
                    "Owner": userCheck.id
                })

            }

        }

    }

}

I’ve been messing with this a bunch more and have come to the conclusion I can’t use the GraphQL API (if I’m wrong, please let me know!).

So assuming I need to use the REST API, what’s the command to query the “fibery/user” database?

“getEntitiesByIds” won’t work for me because I don’t have the specific ID for the user I need yet, hence the need for a query.

Figured it out. Found some of @Chr1sG’s old posts that helped me understand the required syntax, Fibery’s API docs aren’t particularly good for things like this.

Here’s my final code if it helps anyone else looking to query the fibery/user database with a filter:

const fibery = context.getService('fibery');

for (const entity of args.currentEntities) {

    let originalName = entity["Name"];
    const numPortion = originalName.replace(/[^\d.]+/g, '');
    const textPortion = originalName.replace(/[0-9\s.]/g, '');

    console.log("Number Found: " + numPortion);
    console.log("Text Found: " + textPortion)

    if (numPortion.length > 0) {

        // convert the number portion of the string into an actual number
        const formattedNum = parseFloat(numPortion);

        await fibery.updateEntity(entity.type, entity.id, {
            "Points": formattedNum,
        })

        console.log("Points Set: " + formattedNum);

    }

    if (textPortion.length > 0) {

        //search the names of all users in Fibery to see if the text in the entity name is contained within them.

        const userArray = await fibery.executeSingleCommand({
            "command": "fibery.entity/query",
            "args": {
                "query": {
                    "q/from": "fibery/user",
                    "q/select": [
                        "fibery/id",
                        "fibery/public-id",
                        "user/name",
                    ],
                    "q/where": ["q/contains", ["user/name"], "$textPortion"],
                    "q/limit": "q/no-limit"
                },
                "params": {
                    "$textPortion": textPortion
                }
            }
        }
        );

        if (userArray.length > 0) {

            // assign the ID of the first result in the array of users that matched the name

            const idFound = userArray[0]["fibery/id"];
            const nameFound = userArray[0]["user/name"];

            await fibery.updateEntity(entity.type, entity.id, {
                "Owner": idFound
            })

            console.log("Owner Assigned: " + nameFound);

        }

        else {

            console.log("Text did not match any Fibery User");

        }

    }

}
2 Likes

Wondering why you can’t use GraphQL here. Is it because it is the Users database? Or another reason?

Yeah, I think it’s not possible to query the user db using graphql

3 Likes