Search code examples
smooch

How can I check if a user already exists before calling smooch.appUsers.get?


I'm developing a service using Smooch using the smooch-core NPM package.

Before I create a user, I want to check if that specific user id already exists in my app, and if not, I wish to create the new user.

Here is my code:

try {
      await this.smooch.appUsers.get(appId, userId);
    } catch (err) {
      await this.smooch.appUsers.create(appId, userId);
      await this.smooch.appUsers.get(appId, userId);
    }

I always get a 404 error when I try to get user info with non-existant id (smooch.appUsers.get), which I don't find to be a nice way.

Is there another way to check if user already exists?


Solution

  • 404 user_not_found is the officially supported way to know when a user does not exist. Alternatively you could make your call to appUsers.create without doing the appUsers.get first, and you will receive 409 conflict in the case where the user already exists.

    Note also that the call to .create returns the created user, so you shouldn't need to do a second .get after that (referring to the snippet you posted)