<commit_before>import { ParameterizedContext } from 'koa';
import { pool } from '../../database';
import { SQL } from 'sql-template-strings';
import { randomBytes } from 'crypto';

export async function login(
  ctx: ParameterizedContext,
  dbField: string,
  dbValue: string,
  authFields: string,
  authValues: string[],
  name0: string,
  email0: string,
  lat0: number,
  lon0: number,
  language0?: string | null,
  preventTips0 = false,
) {
  const [user] = await pool.query(
    SQL`SELECT id, name, email, isAdmin, lat, lon, settings, sendGalleryEmails, preventTips, language FROM user WHERE `
      .append(dbField)
      .append(SQL` = ${dbValue}`),
  );

  const now = new Date();

  let userId;
  let name;
  let email;
  let isAdmin;
  let lat;
  let lon;
  let settings;
  let preventTips;
  let sendGalleryEmails;
  let language;

  if (user) {
    ({ name, email, lat, lon } = user);
    settings = JSON.parse(user.settings);
    userId = user.id;
    isAdmin = !!user.isAdmin;
    preventTips = !!user.preventTips;
    sendGalleryEmails = !!user.sendGalleryEmails;
    language = user.language;
  } else {
    settings = ctx.request.body.settings || {};
    lat = lat0 || settings.lat;
    lon = lon0 || settings.lon;
    name = name0;
    email = email0;
    isAdmin = false;
    preventTips = preventTips0;
    sendGalleryEmails = true;
    language = language0;

    userId = (
      await pool.query(
        SQL`INSERT INTO user SET `.append(dbField).append(SQL` = ${dbValue},
          name = ${name},
          email = ${email},
          createdAt = ${now},
          lat = ${lat ?? null},
          lon = ${lon ?? null},
          settings = ${JSON.stringify(settings)}
          language = ${language},
          preventTips = ${preventTips},<commit_msg>added some types<commit_after>import { ParameterizedContext } from 'koa';
import { pool } from '../../database';
import { SQL } from 'sql-template-strings';
import { randomBytes } from 'crypto';

export async function login(
  ctx: ParameterizedContext,
  dbField: string,
  dbValue: string,
  authFields: string,
  authValues: string[],
  name0: string,
  email0: string,
  lat0: number,
  lon0: number,
  language0?: string | null,
  preventTips0 = false,
) {
  const [user] = await pool.query(
    SQL`SELECT id, name, email, isAdmin, lat, lon, settings, sendGalleryEmails, preventTips, language FROM user WHERE `
      .append(dbField)
      .append(SQL` = ${dbValue}`),
  );

  const now = new Date();

  let userId: number;
  let name: string;
  let email: string | null;
  let isAdmin: boolean;
  let lat: number;
  let lon: number;
  let settings;
  let preventTips: boolean;
  let sendGalleryEmails: boolean;
  let language: string | null;

  if (user) {
    ({ name, email, lat, lon } = user);
    settings = JSON.parse(user.settings);
    userId = user.id;
    isAdmin = !!user.isAdmin;
    preventTips = !!user.preventTips;
    sendGalleryEmails = !!user.sendGalleryEmails;
    language = user.language;
  } else {
    settings = ctx.request.body.settings || {};
    lat = lat0 || settings.lat;
    lon = lon0 || settings.lon;
    name = name0;
    email = email0;
    isAdmin = false;
    preventTips = preventTips0;
    sendGalleryEmails = true;
    language = language0;

    userId = (
      await pool.query(
        SQL`INSERT INTO user SET `.append(dbField).append(SQL` = ${dbValue},
          name = ${name},
          email = ${email},
          createdAt = ${now},
          lat = ${lat ?? null},
          lon = ${lon ?? null},
          settings = ${JSON.stringify(settings)}
          language = ${language},
          preventTips = ${preventTips},
{'fraction_non_alphanumeric': 0.13437328954570335, 'fraction_numerical': 0.006568144499178982, 'mean_word_length': 2.815240083507307, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '6700382', 'n_tokens_mistral': 1161, 'n_tokens_neox': 1164, 'n_words': 322}