Attributes from a MultiPass ID using TypeScript?

Dear Community,

Maybe I’m overcomplicating this, but for a very simple use case of producing the Initials of given user based on it’s MultiPass id, I created a function that looks like this:

@Function()
public reduceToInitials(firstName: string, lastName: string): string {
    // Helper function to get the first letter of a word
    const getFirstLetter = (word: string): string => word.charAt(0).toUpperCase();

    // Handle compound last names (e.g., "de la Vega", "Smith-Jones")
    const processLastName = (lastName: string): string => {
        // Split by spaces or hyphens and take the first letter of each significant word
        return lastName
        .split(/[\s-]+/)
        .filter(word => word.length > 2) // Ignore short prepositions like 'de', 'la'
        .map(getFirstLetter)
        .join(''); // Join all initials from last name parts
    };

    const firstInitial = getFirstLetter(firstName);
    const lastInitials = processLastName(lastName);

    return `${firstInitial}${lastInitials}`;
}

and…

@Function()
public reduceToInitialsColumns(tasks: ObjectSet<Task>): FunctionsMap<Task, string> {

    const map = new FunctionsMap<Task, string>();

    tasks.all().forEach(t => {
        const assignee: Multipass = t.assignee;
        
        const firstName = assignee.givenName;
        const lastName = assignee.familyName;

        map.set(t, this.reduceToInitials(firstName, lastName));
    });

    return map;

}

AIP Assist suggest me to import this:

import { Multipass } from "@foundry/ontology-api";

… which doesn’t exist.

What is the appropriate way to obtain attributes from a MultiPass ID using TypeScript?

Thanks

1 Like

This is a hallucination. We don’t provide a Multipass API, but we do provide an API to resolve a User object from a user ID: https://www.palantir.com/docs/foundry/functions/input-output-types/#searching-for-foundry-users

So this is how the code looks after using it:

@Function()
public reduceToInitialsColumns(tasks: ObjectSet<Task>): FunctionsMap<Task, string> {

    const map = new FunctionsMap<Task, string>();

    tasks.all().forEach(t => {

        map.set(t, '');

        if (t.assignee) {
            const user = Users.getUserById(t.assignee);

            const firstName = user?.firstName;
            const lastName = user?.lastName;

            if (firstName && lastName) {
                map.set(t, this.reduceToInitials(firstName, lastName));
            }
        }
    });

    return map;

}

Importing:

import { Users } from "@foundry/functions-api";

Thanks!

Nice. I would also recommend using the getUserByIdAsync method instead and parallelizing those calls across your Task objects using Promise.all

Final result:

@Function()
public async reduceToInitialsColumns(tasks: ObjectSet<Task>): 
Promise<FunctionsMap<Task, string>> {

    const map = new FunctionsMap<Task, string>();

    const allTasks: Task[] = tasks.all();

    // Launch parallel computations for each task
    const promises = allTasks.map(async (task) => {
        if (task.assignee) {
            const user = await Users.getUserByIdAsync(task.assignee); 

            const firstName = user?.firstName;
            const lastName = user?.lastName;
            
            // Ensure the user object has both firstName and lastName
            if (firstName && lastName) {
                // Return the initials
                return this.reduceToInitials(firstName, lastName);
            }
        }
        // Return an empty string if no assignee or name is found
        return '';
    });
    // Wait for all promises to resolve
    const allResolvedPromises = await Promise.all(promises)

    // Populate map w/its values.
    for (let i = 0; i < allTasks.length; i++) {
        map.set(allTasks[i], allResolvedPromises[i]);
    }

    return map;