This repository has been archived on 2024-05-27. You can view files and clone it, but cannot push or open issues or pull requests.
QRQuest/Database.js
2022-12-22 18:15:45 +03:00

133 lines
4.0 KiB
JavaScript

const pg = require('pg');
class Database {
constructor(connectionStr) {
this.pool = new pg.Pool({connectionString: connectionStr});
}
async registerQRTeam(name, owner) {
let client = await this.pool.connect();
try {
await client.query('BEGIN');
await client.query('INSERT INTO qr_team (team_name, owner_id) values ($1,$2)', [name, owner]);
await client.query('COMMIT');
} finally {
client.release();
}
}
/**
*
* @param code
* @returns {Promise<null|{id:number,task_text:number,code:string}>}
*/
async getQRTask(code) {
let client = await this.pool.connect();
let task = null;
try {
await client.query('BEGIN');
let res = await client.query('SELECT * FROM qr_tasks WHERE code = $1', [code]);
await client.query('COMMIT');
task = res.rows[0];
} finally {
client.release();
}
return task;
}
async getTask(id) {
if(id === 0) return {short_name: "Начало"}
let client = await this.pool.connect();
let task = null;
try {
await client.query('BEGIN');
let res = await client.query('SELECT * FROM qr_tasks WHERE id = $1', [id]);
await client.query('COMMIT');
task = res.rows[0];
} finally {
client.release();
}
return task;
}
async getTop() {
let client = await this.pool.connect();
let teams = [];
try {
await client.query('BEGIN');
let res = await client.query('SELECT qr_team.*, COUNT(task) as count\n' +
'FROM qr_team\n' +
' join qr_completion as com on qr_team.id = com.group_id\n' +
'group by id, team_name, owner_id\n' +
'ORDER BY count DESC', []);
await client.query('COMMIT');
teams = res.rows;
} finally {
client.release();
}
return teams;
}
/**
*
* @param team
* @param task
* @returns {Promise<boolean>}
*/
async checkPreviousTasks(team, task) {
if(task === 1) return true;
let client = await this.pool.connect();
let prevtasks = 0;
try {
await client.query('BEGIN');
let res = await client.query('SELECT * FROM qr_view WHERE task = $1 AND group_id = $2', [task-1, team]);
prevtasks = res.rowCount;
await client.query('COMMIT');
} finally {
client.release();
}
return prevtasks > 0;
}
async getTeam(user) {
let client = await this.pool.connect();
let team = null;
try {
await client.query('BEGIN');
let res = await client.query('SELECT * FROM qr_team WHERE owner_id = $1', [user]);
await client.query('COMMIT');
team = res.rows[0];
} finally {
client.release();
}
return team;
}
async addCompletion(team, task) {
let client = await this.pool.connect();
try {
await client.query('BEGIN');
//add completion if not exists
let res = await client.query('SELECT * FROM qr_completion WHERE group_id = $1 AND task = $2', [team, task]);
if(res.rowCount === 0) {
await client.query('INSERT INTO qr_completion (group_id, task) values ($1,$2)', [team, task]);
}
await client.query('COMMIT');
} finally {
client.release();
}
}
async addView(team, task) {
let client = await this.pool.connect();
try {
await client.query('BEGIN');
await client.query('INSERT INTO qr_view (group_id, task) values ($1,$2)', [team, task]);
await client.query('COMMIT');
} finally {
client.release();
}
}
}
module.exports = Database;