Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

users.create() returns Zendesk Error (422): Unprocessable Entity #420

Open
Joshua-Shepherd opened this issue Jul 11, 2024 · 3 comments
Open
Labels

Comments

@Joshua-Shepherd
Copy link

Describe the Bug
Create user returns Zendesk Error (422): Unprocessable Entity

Example Code

// Client
export function getZendeskUtilClient(): ZendeskClient {
  const clientOptions: ZendeskClientOptions = {
    token: process.env.ZENDESK_SANDBOX_TOKEN,
    oauth: false,
    subdomain: 'sandbox1718988640',
    username: '[email protected]',
    apiType: ['core'],
  };

  return createClient(clientOptions)
}

async function createUser() {
  const zendesk = client;
  try {
    const response = await zendesk.users.create({ name:'Test TestUser', email: '[email protected]' , role: 'end-user'});
    console.dir(response, {depth: null});
} catch (error) {
    console.error(`ERROR in CREATE: ${error}`);
  }
}

// Without Role
async function createUser() {
  const zendesk = client;
  try {
    const response = await zendesk.users.create({ name:'Test TestUser', email: '[email protected]'});
    console.dir(response, {depth: null});
} catch (error) {
    console.error(`ERROR in CREATE: ${error}`);
  }
}

// My usage above doesn't seem to be different from the example in the JSDoc

Creates a new user.

@param user — The user details.

@returns — The created user's details.

@async

@see — [https://developer.zendesk.com/api-reference/ticketing/users/users/#create-user](vscode-file://vscode-app/Applications/Visual%20Studio%20Code.app/Contents/Resources/app/out/vs/code/electron-sandbox/workbench/workbench.html)

@example

const newUser = await client.users.create({name: 'John Doe', email: '[email protected]'});

Expected Behavior
Create the user..
Actual Behavior
ERROR in CREATE: Error: Request processing failed: Zendesk Error (422): Unprocessable Entity

Environment Information

  • node-zendesk version: ^5.0.9
  • Node.js version: v21.1.0
  • Operating System: MacOS
  • Any other relevant software versions? Nah

Additional Context
I'll update when I find out what the problem is. However, it also spits out this giant js blob along with the Zendesk response:
SS-Visual Studio Code-07-11+0559PM

@Joshua-Shepherd
Copy link
Author

This is just a bug in the JSDoc usage or handling of the object parameter:
It actually works with an Object called 'user' with those properties:

async function createUser() {
  const zendesk = client;
  let user = {
      "user": {
        "name": "John Doe",
        "email": "[email protected]"
      }
    };

  try {
    const response = await zendesk.users.create(user);
    console.dir(response, {depth: null});
} catch (error) {
    console.error(`ERROR in CREATE: ${error}`);
  }
}

SS-Visual Studio Code-07-11+0607PM

@Joshua-Shepherd
Copy link
Author

EZ Fix - Might be more methods like this

@Joshua-Shepherd
Copy link
Author

Joshua-Shepherd commented Jul 11, 2024

Since this method also returns the default API Response object.. It will mess up all the type assertions.
To work around this, you basically can just create your own:

interface ZendeskAPIUserResponse {
    response: {
        json: Function;
        status: number;
        headers: {
            get: Function;
        };
        statusText: string;
    };
    result: User; // Imported the node-zendesk User type
}
async function handleCreateUserResponse(createUser: { user: { name: string; email: string } }) {
  const zendesk = client;
    try {
        // Here, we have to cast the response to the correct type after unknown conversion
        const response = await zendesk.users.create(createUser) as unknown as ZendeskAPIUserResponse;
        const newUser = response.result;  // Now this should correctly reference the user details

        console.log('New User Created:', newUser);
    } catch (error) {
        console.error('Error creating user:', error);
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant