message: create users in keycloak and delete them again
diff: const R = require('ramda');
  -const { pickNonNil } = require('../util/pickNonNil');
  +const pickNonNil = require('../util/pickNonNil');
  const { query, isPatchEmpty, knex } = require('./utils');
  +const logger = require('../logger');
  const {
  getProjectIdByName,
  @@ -205,12 +206,31 @@ const addUser = ({ sqlClient, keycloakClient }) => async (
  const rows = await query(sqlClient, Sql.selectUser(insertId));
  const user = R.prop(0, rows);
  + try {
  await keycloakClient.users.create({
  // Create the group in the `lagoon` realm.
  // TODO: Switch out if the `keycloak-admin` PR to override config gets merged https://github.com/Canner/keycloak-admin/pull/4
  + username: R.prop('email', user),
  realm: 'lagoon',
  + enabled: true,
  + "attributes": {
  + "lagoon-uid": [ R.prop('id', user) ]
  + },
  ...pickNonNil(['email', 'firstName', 'lastName'], user),
  });
  + } catch (err) {
  + if (err.response.status === 409) {
  + logger.warn(
  + `Failed to create already existing Keycloak user "${R.prop(
  + 'email',
  + user,
  + )}"`,
  + );
  + } else {
  + logger.error(`SearchGuard create user error: ${err}`)
  + throw new Error(`SearchGuard create user error: ${err}`)
  + }
  + }
  return user;
  };
  @@ -248,11 +268,15 @@ const updateUser = ({ sqlClient }) => async (
  return R.prop(0, rows);
  };
  -const deleteUser = ({ sqlClient }) => async ({ role, userId }, { id }) => {
  +const deleteUser = ({ sqlClient, keycloakClient }) => async ({ role, userId }, { id }) => {
  if (role !== 'admin' && !R.equals(userId, id)) {
  throw new Error('Unauthorized.');
  }
  + // Load the full user as we need it to remove it later from keyCloak
  + const rows = await query(sqlClient, Sql.selectUser(id));
  + const user = R.prop(0, rows);
  +
  await query(sqlClient, Sql.removeUserFromAllProjects({ id }));
  await query(sqlClient, Sql.removeUserFromAllCustomers({ id }));
  @@ -263,6 +287,23 @@ const deleteUser = ({ sqlClient }) => async ({ role, userId }, { id }) => {
  }),
  );
  + try {
  + // Load the Keycloak User ID based on the username (which is the email)
  + const keycloakUserList = await keycloakClient.users.find({
  + username: R.prop('email', user),
  + realm: 'lagoon',
  + });
  +
  + const keycloakUser = R.prop(0, keycloakUserList);
  + await keycloakClient.users.del({
  + id: R.prop('id', keycloakUser),
  + realm: 'lagoon',
  + });
  + } catch (err) {
  + logger.error(`SearchGuard delete user error: ${err}`)
  + throw new Error(`SearchGuard delete user error: ${err}`)
  + }
  +
  return 'success';
  };
  