<commit_before>import { validate } from '../../common/schemas/user.js';
import { BadRequestError } from '../../common/errors.js';
import config from '../config.js';
import { getLogger } from '../log.js';

const log = getLogger('backend:models:users');

/**
 * Initialize the QueueManager data model
 *
 * @class
 */
export default class User {
  constructor(db) {
    this._db = db;
  }

  async create(user) {
    console.log('Attempting to create user:', user);
    try {
      const isValid = await validate(user);

      if (isValid) {
        log.debug(`Creating user: ${user}`);
        const new_user = await this._db.transaction(async trx => {
          const query = {
            username: user.username,
            role_name: user.role_name,
          };

          log.debug('Inserting user');
          await trx('users').insert(query);

          return trx('users')
            .select()
            .where({ username: user.username })
            .first();
        });
        return new_user;
      } else {
        log.debug(`Cannot create user`);
        throw new BadRequestError('User information is not valid.');
      }
    } catch (err) {
      throw new BadRequestError('Failed to create user: ', err);
    }
  }

  async update(user) {
    try {
      const isValid = await validate(user);
      if (isValid) {
        const updated_user = await this._db.transaction(async trx => {
          const query = {
            username: user.username,
            role_name: user.role_name,
          };

          log.debug('Updating user');
          await trx('users')
            .update(query, ['id', 'username', 'role_name'])
            .where({ username: user.username });

          return trx('users')
            .select()
            .where({ username: user.username })
            .first();
        });
        return updated_user;
      } else {
        throw new BadRequestError('User information is not valid.');<commit_msg>User validation tweaks.<commit_after>import { validate } from '../../common/schemas/user.js';
import { BadRequestError } from '../../common/errors.js';
import config from '../config.js';
import { getLogger } from '../log.js';

const log = getLogger('backend:models:users');

/**
 * Initialize the QueueManager data model
 *
 * @class
 */
export default class User {
  constructor(db) {
    this._db = db;
  }

  async create(user) {
    console.log('Attempting to create user:', user);
    let data;
    try {
      data = await validate(user);
    } catch (err) {
      log.debug('Cannot create user');
      throw new BadRequestError('User information is not valid: ', err);
    }
    try {
      log.debug(`Creating user: ${user}`);
      const new_user = await this._db.transaction(async trx => {
        const query = {
          username: data.username,
          role_name: data.role_name,
        };

        log.debug('Inserting user');
        await trx('users').insert(query);

        return trx('users')
          .select()
          .where({ username: data.username })
          .first();
      });
      return new_user;
    } catch (err) {
      throw new BadRequestError('Failed to create user: ', err);
    }
  }

  async update(user) {
    try {
      const isValid = await validate(user);
      if (isValid) {
        const updated_user = await this._db.transaction(async trx => {
          const query = {
            username: user.username,
            role_name: user.role_name,
          };

          log.debug('Updating user');
          await trx('users')
            .update(query, ['id', 'username', 'role_name'])
            .where({ username: user.username });

          return trx('users')
            .select()
            .where({ username: user.username })
            .first();
        });
        return updated_user;
      } else {
        throw new BadRequestError('User information is not valid.');
{'fraction_non_alphanumeric': 0.13974358974358975, 'fraction_numerical': 0.0, 'mean_word_length': 1.9984627209838586, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1434384', 'n_tokens_mistral': 1085, 'n_tokens_neox': 1082, 'n_words': 285}