<commit_before>'use strict';

const { Pool } = require('pg');
const pool = new Pool();

const validateString = (val, name) => {
	if (typeof val !== 'string')
		throw new TypeError(`${name} must be a string.`);
}

const validateInteger = (val, name) => {
	if (typeof val !== 'number')
		throw new TypeError(`${name} must be an integer.`);
};

const validateEnum = (val, name, max) => {
	if (typeof val !== 'number' || val > max)
		throw new TypeError(`${name} must be an integer in the range of [0..${max}].`);
};

module.exports = {
	user: {
		add: async({ github_login, slack_id }) => {
			validateString(github_login, 'user.github_login');
			validateString(slack_id, 'user.slack_id');
			await pool.query('INSERT INTO Users(github_login, slack_id) VALUES($1, $2)', [github_login, slack_id]);
		}
	},

	issue: {
		add: async({ github_id, status, title, url }) => {
			validateInteger(github_id, 'issue.github_id');
			validateEnum(status, 1, 'issue.status');
			validateString(title, 'issue.title');
			validateString(url, 'issue.url');
			await pool.query('INSERT INTO Issues(github_id, status, title, url) VALUES($1, $2, $3, $4)', [github_id, status, title, url]);
		},

		addRel: async({ user_id, issue_github_id, type }) => {
			validateInteger(user_id, 'rel.user_id');
			validateInteger(issue_github_id, 'rel.issue_github_id');
			validateEnum(type, 3, 'rel.type');
		}
	}
};<commit_msg>Updated db.issue.add method to automatically add user-issue relations.<commit_after>'use strict';

const { Pool } = require('pg');
const pool = new Pool();

const validateString = (val, name) => {
	if (typeof val !== 'string')
		throw new TypeError(`${name} must be a string.`);
}

const validateInteger = (val, name) => {
	if (typeof val !== 'number')
		throw new TypeError(`${name} must be an integer.`);
};

const validateEnum = (val, name, max) => {
	if (typeof val !== 'number' || val > max)
		throw new TypeError(`${name} must be an integer in the range of [0..${max}].`);
};

const db = module.exports = {
	rel_types: ['assigned', 'created', 'mentioned', 'subscribed'],

	user: {
		add: async({ github_login, slack_id }) => {
			validateString(github_login, 'user.github_login');
			validateString(slack_id, 'user.slack_id');
			await pool.query('INSERT INTO User(github_login, slack_id) VALUES($1, $2)', [github_login, slack_id]);
		}
	},

	issue: {
		add: async({
			github_id,
			status,
			title,
			url,

			assignee = null,
			creator = null
		}) => {
			validateInteger(github_id, 'issue.github_id');
			validateEnum(status, 1, 'issue.status');
			validateString(title, 'issue.title');
			validateString(url, 'issue.url');
			await pool.query('INSERT INTO Issue(github_id, status, title, url) VALUES($1, $2, $3, $4)', [github_id, status, title, url]);

			async function addRel(user_github_login, type) {
				let res = await pool.query('SELECT id FROM User WHERE github_login = $1', [user_github_login]);
				if (res.rows.length > 0) {
					let id = res.rows[0][0];
					let typeId = db.rel_types.indexOf(type);
					await pool.query('INSERT INTO user_issue_rel(user_id, issue_github_id, type) VALUES($1, $2, $3)', [id, github_id, typeId]);
				} else {
					console.warn(`No user entry found for github_login = ${user_github_login}`);
				}
			}
			await addRel(assignee, 'assigned');
			await addRel(creator, 'created');
		}
	}
};
{'fraction_non_alphanumeric': 0.17629452259802456, 'fraction_numerical': 0.007183478000598623, 'mean_word_length': 4.3472, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, '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': '4177444', 'n_tokens_mistral': 1271, 'n_tokens_neox': 1145, 'n_words': 304}