How To Add User With Extension From Node.js Back-End?

  • Need to add/register user from Node.js
  • Need to give user a specific ID
  • Need to select protocol and extension all in 1 go.

Example

export const registerAndCreateUser = async (
  res: Response,
  data: UserAuthData
) => {
  const { password, passwordConfirmation } = data;
  if (password !== passwordConfirmation) {
    throw createHttpError(400, 'Passwords do not match');
  }
  try {
    const passwordHash = await hashPassword(password);
    const user = await userRepo().save({ ...data, password: passwordHash });
    const returnedUser = omit(user, ['password', 'passwordConfirmation']);

    const WazoRegisterResponse = await axios({
      method: 'post',
      url: 'https://wazo.example.com:443/api/auth/0.1/users',
      data: {
        context: 'DefaultContext',
        extension: '1000003',
        protocol: 'Webrtc',
        email_address: returnedUser.email,
        username: returnedUser.id,
        firstname: returnedUser.firstname,
        lastname: returnedUser.lastname,
        password,
      },
    });

    return { ...returnedUser, ...WazoRegisterResponse.data }
  } catch (err) {
    throw isDuplicateKeyErr(err) ? createHttpError(403, { err }) : err;
  }
};

The reason I need to do this:
Add user to Wazo for calling on app registration to fit the current flow of my app via the back-end.

How does one do this?

Ps. Thank you Wazo! :smiley:

Have you check the API documentation?
If you want we have a JS SDK, contribution is welcome :slight_smile:

I read through the swagger endpoints and looked through wazo_ui repo to see how it is doing this, when you add a user, but could not figure out how to do this in Node.js, just need to add a user with Context and Extension

Not sure to understand, but this is exactly the same as other language, just using the REST API.

Yes, the same but how do you do this specifically, what is the URL and what is the Payload, and what is the Headers?

Eg this does not work

     {
      method: 'post',
      url: 'https://wazo.example.com:443/api/auth/0.1/users',
      data: {
        context: 'DefaultContext',
        extension: '1000003',
        protocol: 'Webrtc',
        email_address: returnedUser.email,
        username: returnedUser.id,
        firstname: returnedUser.firstname,
        lastname: returnedUser.lastname,
        password,
      },
     headers: {}
    }

That my point, check the swagger spec for the documentation.

The best will be to update our SDK here: https://github.com/wazo-platform/wazo-js-sdk/blob/master/src/api/confd.js

Oh, I have. I cannot find where you add Extension and Context. This is the Swagger spec:

It does not mention Context or Extension or Protocol

And It does not say Headers

So my response is unauthorized:

This is an association. We don’t have one API to do that for the moment, we have completely separated all objects. User, extensions, lines, voicemail etc … So you need to create them and associate it with the API.

I understand now.

But can you show a way to do this with the separated objects?

I understand each part is separated and it’s possible to do, when you use different methods together. But I don’t quite understand how to do it programmatically. What methods/code do I need, to do these things together:

  • Register user (email, username, id, name, lastname)
  • Add extension (eg “111232”)
  • Add protocol (eg “Webrtc”)
  • Add Context (eg “DefaultContext”)

And do this in Node.js?

Please can you explain, or give an example, as it would greatly help me build my application! :smiley:

Ps I don’t see the functionality:

But maybe I can do this with a few HTTP requests, but I can’t work out which and their headers.