How to use the @hapi/boom.conflict function in @hapi/boom

To help you get started, we’ve selected a few @hapi/boom examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github JKHeadley / rest-hapi / utilities / handler-helper.js View on Github external
)
    }

    if (config.enableCreatedAt) {
      for (const document of payload) {
        document.createdAt = new Date()
      }
    }

    let data
    try {
      data = await model.create(payload)
    } catch (err) {
      Log.error(err)
      if (err.code === 11000) {
        throw Boom.conflict('There was a duplicate key error.')
      } else {
        throw Boom.badImplementation(
          'There was an error creating the resource.'
        )
      }
    }

    // EXPL: rather than returning the raw "create" data, we filter the data through a separate query
    const attributes = QueryHelper.createAttributesFilter({}, model, Log)

    data = data.map(item => {
      return item._id
    })

    const result = await model
      .find()
github JKHeadley / rest-hapi / utilities / handler-helper.js View on Github external
Log
      )
    }

    if (config.enableUpdatedAt) {
      payload.updatedAt = new Date()
    }
    let result
    try {
      result = await model.findByIdAndUpdate(_id, payload, {
        runValidators: config.enableMongooseRunValidators
      })
    } catch (err) {
      Log.error(err)
      if (err.code === 11000) {
        throw Boom.conflict('There was a duplicate key error.')
      } else {
        throw Boom.badImplementation(
          'There was an error updating the resource.'
        )
      }
    }
    if (result) {
      const attributes = QueryHelper.createAttributesFilter({}, model, Log)

      result = await model.findOne({ _id: result._id }, attributes).lean()

      try {
        if (
          model.routeOptions &&
          model.routeOptions.update &&
          model.routeOptions.update.post
github jedireza / frame / server / api / accounts.js View on Github external
const user = await User.findByUsername(request.payload.username);

                    if (!user) {
                        throw Boom.notFound('User not found.');
                    }

                    if (user.roles.account &&
                        user.roles.account.id !== request.params.id) {

                        throw Boom.conflict('User is linked to an account. Unlink first.');
                    }

                    if (request.pre.account.user &&
                        request.pre.account.user.id !== `${user._id}`) {

                        throw Boom.conflict('Account is linked to a user. Unlink first.');
                    }

                    return user;
                }
            }]
github jedireza / frame / server / api / signup.js View on Github external
method: async function (request, h) {

                    const user = await User.findByUsername(request.payload.username);

                    if (user) {
                        throw Boom.conflict('Username already in use.');
                    }

                    return h.continue;
                }
            }, {
github jedireza / frame / server / api / admins.js View on Github external
const user = await User.findByUsername(request.payload.username);

                        if (!user) {
                            throw Boom.notFound('User not found.');
                        }

                        if (user.roles.admin &&
                            user.roles.admin.id !== request.params.id) {

                            throw Boom.conflict('User is linked to an admin. Unlink first.');
                        }

                        if (request.pre.admin.user &&
                            request.pre.admin.user.id !== `${user._id}`) {

                            throw Boom.conflict('Admin is linked to a user. Unlink first.');
                        }

                        return user;
                    }
                }
github jedireza / frame / server / api / users.js View on Github external
method: async function (request, h) {

                        const user = await User.findByEmail(request.payload.email);

                        if (user) {
                            throw Boom.conflict('Email already in use.');
                        }

                        return h.continue;
                    }
                }
github jedireza / frame / server / api / accounts.js View on Github external
method: async function (request, h) {

                    const user = await User.findByUsername(request.payload.username);

                    if (!user) {
                        throw Boom.notFound('User not found.');
                    }

                    if (user.roles.account &&
                        user.roles.account.id !== request.params.id) {

                        throw Boom.conflict('User is linked to an account. Unlink first.');
                    }

                    if (request.pre.account.user &&
                        request.pre.account.user.id !== `${user._id}`) {

                        throw Boom.conflict('Account is linked to a user. Unlink first.');
                    }

                    return user;
                }
            }]
github Fcmam5 / algeria-api / server / controllers / UserController.js View on Github external
register: async (req, res, next) => {
    const { name, email, password } = req.body;

    try {
      const user = await service.create(name, email, password);
      return res.status(201).json({ success: true, user });
    } catch (error) {
      switch (error.name) {
        case 'EmailIsTakenError':
          return next(boom.conflict(error.message));

        default:
          return next(boom.internal(error));
      }
    }
  },
  login: async (req, res, next) => {
github jedireza / frame / server / api / admins.js View on Github external
method: async function (request, h) {

                        const user = await User.findByUsername(request.payload.username);

                        if (!user) {
                            throw Boom.notFound('User not found.');
                        }

                        if (user.roles.admin &&
                            user.roles.admin.id !== request.params.id) {

                            throw Boom.conflict('User is linked to an admin. Unlink first.');
                        }

                        if (request.pre.admin.user &&
                            request.pre.admin.user.id !== `${user._id}`) {

                            throw Boom.conflict('Admin is linked to a user. Unlink first.');
                        }

                        return user;
                    }
                }