Better connection handling
Jenkins Production Deployment Details

This commit is contained in:
Patrick Müller 2022-06-26 14:48:00 +02:00
parent fc65474930
commit 0325534d53
Signed by: Paddy
GPG Key ID: 37ABC11275CAABCE
64 changed files with 1697 additions and 1505 deletions

View File

@ -13,7 +13,7 @@ export namespace BetterzonDB {
connectionLimit: 5
});
export function getConnection() {
return pool;
}
export const getConnection = async () => {
return pool.getConnection();
};
}

View File

@ -18,7 +18,7 @@ dotenv.config();
* Fetches and returns all known categories
*/
export const findAll = async (): Promise<Categories> => {
let conn = BetterzonDB.getConnection();
let conn = await BetterzonDB.getConnection();
let categRows = [];
try {
const rows = await conn.query('SELECT category_id, name FROM categories');
@ -38,6 +38,9 @@ export const findAll = async (): Promise<Categories> => {
} catch (err) {
throw err;
} finally {
// Return connection
await conn.end();
}
return categRows;
@ -48,7 +51,7 @@ export const findAll = async (): Promise<Categories> => {
* @param id The id of the category to fetch
*/
export const find = async (id: number): Promise<Category> => {
let conn = BetterzonDB.getConnection();
let conn = await BetterzonDB.getConnection();
let categ: any;
try {
const rows = await conn.query('SELECT category_id, name FROM categories WHERE category_id = ?', id);
@ -60,6 +63,9 @@ export const find = async (id: number): Promise<Category> => {
} catch (err) {
throw err;
} finally {
// Return connection
await conn.end();
}
return categ;
@ -70,7 +76,7 @@ export const find = async (id: number): Promise<Category> => {
* @param term the term to match
*/
export const findBySearchTerm = async (term: string): Promise<Categories> => {
let conn = BetterzonDB.getConnection();
let conn = await BetterzonDB.getConnection();
let categRows = [];
try {
term = '%' + term + '%';
@ -83,6 +89,9 @@ export const findBySearchTerm = async (term: string): Promise<Categories> => {
} catch (err) {
throw err;
} finally {
// Return connection
await conn.end();
}
return categRows;

View File

@ -18,7 +18,7 @@ dotenv.config();
* Fetches and returns all known contact persons
*/
export const findAll = async (): Promise<Contact_Persons> => {
let conn = BetterzonDB.getConnection();
let conn = await BetterzonDB.getConnection();
let contRows = [];
try {
const rows = await conn.query('SELECT contact_person_id, first_name, last_name, gender, email, phone, vendor_id FROM contact_persons');
@ -30,6 +30,9 @@ export const findAll = async (): Promise<Contact_Persons> => {
} catch (err) {
throw err;
} finally {
// Return connection
await conn.end();
}
return contRows;
@ -40,7 +43,7 @@ export const findAll = async (): Promise<Contact_Persons> => {
* @param id The id of the contact person to fetch
*/
export const find = async (id: number): Promise<Contact_Person> => {
let conn = BetterzonDB.getConnection();
let conn = await BetterzonDB.getConnection();
let cont: any;
try {
const rows = await conn.query('SELECT contact_person_id, first_name, last_name, gender, email, phone, vendor_id FROM contact_persons WHERE contact_person_id = ?', id);
@ -52,6 +55,9 @@ export const find = async (id: number): Promise<Contact_Person> => {
} catch (err) {
throw err;
} finally {
// Return connection
await conn.end();
}
return cont;
@ -62,7 +68,7 @@ export const find = async (id: number): Promise<Contact_Person> => {
* @param id The id of the vendor to fetch contact persons for
*/
export const findByVendor = async (id: number): Promise<Contact_Persons> => {
let conn = BetterzonDB.getConnection();
let conn = await BetterzonDB.getConnection();
let contRows = [];
try {
const rows = await conn.query('SELECT contact_person_id, first_name, last_name, gender, email, phone, vendor_id FROM contact_persons WHERE vendor_id = ?', id);
@ -74,6 +80,9 @@ export const findByVendor = async (id: number): Promise<Contact_Persons> => {
} catch (err) {
throw err;
} finally {
// Return connection
await conn.end();
}
return contRows;
@ -90,7 +99,7 @@ export const findByVendor = async (id: number): Promise<Contact_Persons> => {
* @param phone The phone number of the contact person
*/
export const createContactEntry = async (user_id: number, vendor_id: number, first_name: string, last_name: string, gender: string, email: string, phone: string): Promise<Boolean> => {
let conn = BetterzonDB.getConnection();
let conn = await BetterzonDB.getConnection();
try {
// Check if the user is authorized to manage the requested vendor
const user_vendor_rows = await conn.query('SELECT vendor_id FROM vendors WHERE vendor_id = ? AND admin_id = ?', [vendor_id, user_id]);
@ -105,6 +114,9 @@ export const createContactEntry = async (user_id: number, vendor_id: number, fir
return res.affectedRows === 1;
} catch (err) {
throw err;
} finally {
// Return connection
await conn.end();
}
};
@ -120,7 +132,7 @@ export const createContactEntry = async (user_id: number, vendor_id: number, fir
* @param phone The phone number of the contact person
*/
export const updateContactEntry = async (user_id: number, contact_person_id: number, vendor_id: number, first_name: string, last_name: string, gender: string, email: string, phone: string): Promise<Boolean> => {
let conn = BetterzonDB.getConnection();
let conn = await BetterzonDB.getConnection();
try {
// Check if the user is authorized to manage the requested vendor
const user_vendor_rows = await conn.query('SELECT vendor_id FROM vendors WHERE vendor_id = ? AND admin_id = ?', [vendor_id, user_id]);
@ -135,5 +147,8 @@ export const updateContactEntry = async (user_id: number, contact_person_id: num
return res.affectedRows === 1;
} catch (err) {
throw err;
} finally {
// Return connection
await conn.end();
}
};

View File

@ -17,7 +17,7 @@ dotenv.config();
* Fetches and returns the current crawling status if the issuing user is an admin
*/
export const getCurrent = async (): Promise<Crawling_Status> => {
let conn = BetterzonDB.getConnection();
let conn = await BetterzonDB.getConnection();
try {
// Get the current crawling process
let process_info = {
@ -55,5 +55,8 @@ export const getCurrent = async (): Promise<Crawling_Status> => {
} catch (err) {
throw err;
} finally {
// Return connection
await conn.end();
}
};

View File

@ -19,13 +19,16 @@ dotenv.config();
* @param vendor_id The id of the vendor to set as favorite
*/
export const createFavoriteShop = async (user_id: number, vendor_id: number): Promise<boolean> => {
let conn = BetterzonDB.getConnection();
let conn = await BetterzonDB.getConnection();
try {
const res = await conn.query('INSERT INTO favorite_shops (vendor_id, user_id) VALUES (?, ?)', [vendor_id, user_id]);
return res.affectedRows === 1;
} catch (err) {
throw err;
} finally {
// Return connection
await conn.end();
}
};
@ -34,7 +37,7 @@ export const createFavoriteShop = async (user_id: number, vendor_id: number): Pr
* @param user_id
*/
export const getFavoriteShops = async (user_id: number): Promise<FavoriteShops> => {
let conn = BetterzonDB.getConnection();
let conn = await BetterzonDB.getConnection();
let shops = [];
try {
const rows = await conn.query('SELECT favorite_id, vendor_id, user_id FROM favorite_shops WHERE user_id = ?', user_id);
@ -47,6 +50,9 @@ export const getFavoriteShops = async (user_id: number): Promise<FavoriteShops>
return shops;
} catch (err) {
throw err;
} finally {
// Return connection
await conn.end();
}
};
@ -56,12 +62,15 @@ export const getFavoriteShops = async (user_id: number): Promise<FavoriteShops>
* @param favorite_id The favorite shop to delete
*/
export const deleteFavoriteShop = async (user_id: number, favorite_id: number): Promise<boolean> => {
let conn = BetterzonDB.getConnection();
let conn = await BetterzonDB.getConnection();
try {
const res = await conn.query('DELETE FROM favorite_shops WHERE favorite_id = ? AND user_id = ?', [favorite_id, user_id]);
return res.affectedRows === 1;
} catch (err) {
throw err;
} finally {
// Return connection
await conn.end();
}
};

View File

@ -18,7 +18,7 @@ dotenv.config();
* Fetches and returns all known manufacturers
*/
export const findAll = async (): Promise<Manufacturers> => {
let conn = BetterzonDB.getConnection();
let conn = await BetterzonDB.getConnection();
let manRows = [];
try {
const rows = await conn.query('SELECT manufacturer_id, name FROM manufacturers');
@ -38,6 +38,9 @@ export const findAll = async (): Promise<Manufacturers> => {
} catch (err) {
throw err;
} finally {
// Return connection
await conn.end();
}
return manRows;
@ -48,7 +51,7 @@ export const findAll = async (): Promise<Manufacturers> => {
* @param id The id of the manufacturer to fetch
*/
export const find = async (id: number): Promise<Manufacturer> => {
let conn = BetterzonDB.getConnection();
let conn = await BetterzonDB.getConnection();
let man: any;
try {
const rows = await conn.query('SELECT manufacturer_id, name FROM manufacturers WHERE manufacturer_id = ?', id);
@ -60,6 +63,9 @@ export const find = async (id: number): Promise<Manufacturer> => {
} catch (err) {
throw err;
} finally {
// Return connection
await conn.end();
}
return man;
@ -70,7 +76,7 @@ export const find = async (id: number): Promise<Manufacturer> => {
* @param term the term to match
*/
export const findBySearchTerm = async (term: string): Promise<Manufacturers> => {
let conn = BetterzonDB.getConnection();
let conn = await BetterzonDB.getConnection();
let manRows = [];
try {
term = '%' + term + '%';
@ -83,6 +89,9 @@ export const findBySearchTerm = async (term: string): Promise<Manufacturers> =>
} catch (err) {
throw err;
} finally {
// Return connection
await conn.end();
}
return manRows;

View File

@ -20,13 +20,16 @@ dotenv.config();
* @param defined_price The defined price for the price alarm
*/
export const createPriceAlarm = async (user_id: number, product_id: number, defined_price: number): Promise<boolean> => {
let conn = BetterzonDB.getConnection();
let conn = await BetterzonDB.getConnection();
try {
const res = await conn.query('INSERT INTO price_alarms (user_id, product_id, defined_price) VALUES (?, ?, ?)', [user_id, product_id, defined_price]);
return res.affectedRows === 1;
} catch (err) {
throw err;
} finally {
// Return connection
await conn.end();
}
};
@ -35,7 +38,7 @@ export const createPriceAlarm = async (user_id: number, product_id: number, defi
* @param user_id
*/
export const getPriceAlarms = async (user_id: number): Promise<PriceAlarms> => {
let conn = BetterzonDB.getConnection();
let conn = await BetterzonDB.getConnection();
let priceAlarms = [];
try {
const rows = await conn.query('SELECT alarm_id, user_id, product_id, defined_price FROM price_alarms WHERE user_id = ?', user_id);
@ -48,6 +51,9 @@ export const getPriceAlarms = async (user_id: number): Promise<PriceAlarms> => {
return priceAlarms;
} catch (err) {
throw err;
} finally {
// Return connection
await conn.end();
}
};
@ -58,13 +64,16 @@ export const getPriceAlarms = async (user_id: number): Promise<PriceAlarms> => {
* @param defined_price The defined price for the price alarm
*/
export const updatePriceAlarm = async (alarm_id: number, user_id: number, defined_price: number): Promise<boolean> => {
let conn = BetterzonDB.getConnection();
let conn = await BetterzonDB.getConnection();
try {
const res = await conn.query('UPDATE price_alarms SET defined_price = ? WHERE alarm_id = ? AND user_id = ?', [defined_price, alarm_id, user_id]);
return res.affectedRows === 1;
} catch (err) {
throw err;
} finally {
// Return connection
await conn.end();
}
};
@ -74,12 +83,15 @@ export const updatePriceAlarm = async (alarm_id: number, user_id: number, define
* @param user_id The id of the user that wants to update the price alarm
*/
export const deletePriceAlarm = async (alarm_id: number, user_id: number): Promise<boolean> => {
let conn = BetterzonDB.getConnection();
let conn = await BetterzonDB.getConnection();
try {
const res = await conn.query('DELETE FROM price_alarms WHERE alarm_id = ? AND user_id = ?', [alarm_id, user_id]);
return res.affectedRows === 1;
} catch (err) {
throw err;
} finally {
// Return connection
await conn.end();
}
};

View File

@ -18,7 +18,7 @@ dotenv.config();
* Fetches and returns all known prices
*/
export const findAll = async (): Promise<Prices> => {
let conn = BetterzonDB.getConnection();
let conn = await BetterzonDB.getConnection();
let priceRows = [];
try {
const rows = await conn.query('SELECT price_id, product_id, v.vendor_id, price_in_cents, timestamp FROM prices p LEFT OUTER JOIN vendors v ON v.vendor_id = p.vendor_id WHERE active_listing = true AND v.isActive = true');
@ -44,6 +44,9 @@ export const findAll = async (): Promise<Prices> => {
} catch (err) {
throw err;
} finally {
// Return connection
await conn.end();
}
return priceRows;
@ -54,7 +57,7 @@ export const findAll = async (): Promise<Prices> => {
* @param id The id of the price to fetch
*/
export const find = async (id: number): Promise<Price> => {
let conn = BetterzonDB.getConnection();
let conn = await BetterzonDB.getConnection();
let price: any;
try {
const rows = await conn.query('SELECT price_id, product_id, p.vendor_id, price_in_cents, timestamp FROM prices p LEFT OUTER JOIN vendors v ON v.vendor_id = p.vendor_id WHERE price_id = ? AND active_listing = true AND v.isActive = true', id);
@ -66,6 +69,9 @@ export const find = async (id: number): Promise<Price> => {
} catch (err) {
throw err;
} finally {
// Return connection
await conn.end();
}
return price;
@ -76,7 +82,7 @@ export const find = async (id: number): Promise<Price> => {
* @param product the product to fetch the prices for
*/
export const findByProduct = async (product: number): Promise<Prices> => {
let conn = BetterzonDB.getConnection();
let conn = await BetterzonDB.getConnection();
let priceRows = [];
try {
const rows = await conn.query('SELECT price_id, product_id, p.vendor_id, price_in_cents, timestamp FROM prices p LEFT OUTER JOIN vendors v ON v.vendor_id = p.vendor_id WHERE product_id = ? AND active_listing = true AND v.isActive = true', product);
@ -88,6 +94,9 @@ export const findByProduct = async (product: number): Promise<Prices> => {
} catch (err) {
throw err;
} finally {
// Return connection
await conn.end();
}
return priceRows;
@ -102,7 +111,7 @@ export const findByProduct = async (product: number): Promise<Prices> => {
* @param type The type of prices, e.g. newest / lowest
*/
export const findByType = async (product: string, type: string): Promise<Prices> => {
let conn = BetterzonDB.getConnection();
let conn = await BetterzonDB.getConnection();
let priceRows = [];
try {
let rows = [];
@ -138,6 +147,9 @@ export const findByType = async (product: string, type: string): Promise<Prices>
} catch (err) {
throw err;
} finally {
// Return connection
await conn.end();
}
return priceRows;
@ -153,7 +165,7 @@ export const findByType = async (product: string, type: string): Promise<Prices>
* @param type The type of prices, e.g. newest / lowest
*/
export const findByVendor = async (product: string, vendor: string, type: string): Promise<Prices> => {
let conn = BetterzonDB.getConnection();
let conn = await BetterzonDB.getConnection();
let priceRows = [];
try {
let rows = [];
@ -176,6 +188,9 @@ export const findByVendor = async (product: string, vendor: string, type: string
} catch (err) {
throw err;
} finally {
// Return connection
await conn.end();
}
return priceRows;
@ -187,7 +202,7 @@ export const findByVendor = async (product: string, vendor: string, type: string
* @param amount The amount of deals to return
*/
export const getBestDeals = async (amount: number): Promise<Prices> => {
let conn = BetterzonDB.getConnection();
let conn = await BetterzonDB.getConnection();
let priceRows = [];
try {
let allPrices: Record<number, Price[]> = {};
@ -268,6 +283,9 @@ export const getBestDeals = async (amount: number): Promise<Prices> => {
} catch (err) {
console.log(err);
throw err;
} finally {
// Return connection
await conn.end();
}
return priceRows;
@ -278,7 +296,7 @@ export const getBestDeals = async (amount: number): Promise<Prices> => {
* @param productIds the ids of the products
*/
export const findListByProducts = async (productIds: [number]): Promise<Prices> => {
let conn = BetterzonDB.getConnection();
let conn = await BetterzonDB.getConnection();
let priceRows: Price[] = [];
try {
let allPrices: Record<number, Price[]> = {};
@ -325,13 +343,16 @@ export const findListByProducts = async (productIds: [number]): Promise<Prices>
});
} catch (err) {
throw err;
} finally {
// Return connection
await conn.end();
}
return priceRows;
};
export const createPriceEntry = async (user_id: number, vendor_id: number, product_id: number, price_in_cents: number): Promise<Boolean> => {
let conn = BetterzonDB.getConnection();
let conn = await BetterzonDB.getConnection();
try {
// Check if the user is authorized to manage the requested vendor
const user_vendor_rows = await conn.query('SELECT vendor_id FROM vendors WHERE vendor_id = ? AND admin_id = ?', [vendor_id, user_id]);
@ -346,5 +367,8 @@ export const createPriceEntry = async (user_id: number, vendor_id: number, produ
return res.affectedRows === 1;
} catch (err) {
throw err;
} finally {
// Return connection
await conn.end();
}
};

View File

@ -19,7 +19,7 @@ dotenv.config();
* Fetches and returns all known products
*/
export const findAll = async (): Promise<Products> => {
let conn = BetterzonDB.getConnection();
let conn = await BetterzonDB.getConnection();
let prodRows = [];
try {
const rows = await conn.query('SELECT product_id, name, asin, is_active, short_description, long_description, image_guid, date_added, last_modified, manufacturer_id, selling_rank, category_id FROM products');
@ -59,6 +59,9 @@ export const findAll = async (): Promise<Products> => {
} catch (err) {
throw err;
} finally {
// Return connection
await conn.end();
}
return prodRows;
@ -69,7 +72,7 @@ export const findAll = async (): Promise<Products> => {
* @param id The id of the product to fetch
*/
export const find = async (id: number): Promise<Product> => {
let conn = BetterzonDB.getConnection();
let conn = await BetterzonDB.getConnection();
let prod: any;
try {
const rows = await conn.query('SELECT product_id, name, asin, is_active, short_description, long_description, image_guid, date_added, last_modified, manufacturer_id, selling_rank, category_id FROM products WHERE product_id = ?', id);
@ -81,6 +84,9 @@ export const find = async (id: number): Promise<Product> => {
} catch (err) {
throw err;
} finally {
// Return connection
await conn.end();
}
return prod;
@ -91,7 +97,7 @@ export const find = async (id: number): Promise<Product> => {
* @param term the term to match
*/
export const findBySearchTerm = async (term: string): Promise<Products> => {
let conn = BetterzonDB.getConnection();
let conn = await BetterzonDB.getConnection();
let prodRows = [];
try {
term = '%' + term + '%';
@ -105,6 +111,9 @@ export const findBySearchTerm = async (term: string): Promise<Products> => {
} catch (err) {
console.log(err);
throw err;
} finally {
// Return connection
await conn.end();
}
return prodRows;
@ -115,7 +124,7 @@ export const findBySearchTerm = async (term: string): Promise<Products> => {
* @param ids The list of product ids to fetch the details for
*/
export const findList = async (ids: [number]): Promise<Products> => {
let conn = BetterzonDB.getConnection();
let conn = await BetterzonDB.getConnection();
let prodRows = [];
try {
const rows = await conn.query('SELECT product_id, name, asin, is_active, short_description, long_description, image_guid, date_added, last_modified, manufacturer_id, selling_rank, category_id FROM products WHERE product_id IN (?)', [ids]);
@ -127,6 +136,9 @@ export const findList = async (ids: [number]): Promise<Products> => {
} catch (err) {
throw err;
} finally {
// Return connection
await conn.end();
}
return prodRows;
@ -137,7 +149,7 @@ export const findList = async (ids: [number]): Promise<Products> => {
* @param id The id of the vendor to fetch the products for
*/
export const findByVendor = async (id: number): Promise<Products> => {
let conn = BetterzonDB.getConnection();
let conn = await BetterzonDB.getConnection();
let prodRows = [];
try {
// Get the relevant product ids
@ -159,6 +171,9 @@ export const findByVendor = async (id: number): Promise<Products> => {
} catch (err) {
throw err;
} finally {
// Return connection
await conn.end();
}
return prodRows;

View File

@ -21,7 +21,7 @@ dotenv.config();
* Creates a user record in the database, also creates a session. Returns the session if successful.
*/
export const createUser = async (username: string, password: string, email: string, ip: string): Promise<Session> => {
let conn = BetterzonDB.getConnection();
let conn = await BetterzonDB.getConnection();
try {
// Hash password and generate + hash session key
const pwHash = bcrypt.hashSync(password, 10);
@ -63,6 +63,9 @@ export const createUser = async (username: string, password: string, email: stri
} catch (err) {
throw err;
} finally {
// Return connection
await conn.end();
}
};
@ -71,7 +74,7 @@ export const createUser = async (username: string, password: string, email: stri
* Returns the session information in case of a successful login
*/
export const login = async (username: string, password: string, ip: string): Promise<Session> => {
let conn = BetterzonDB.getConnection();
let conn = await BetterzonDB.getConnection();
try {
// Get saved password hash
const query = 'SELECT user_id, bcrypt_password_hash FROM users WHERE username = ?';
@ -123,6 +126,9 @@ export const login = async (username: string, password: string, ip: string): Pro
} catch (err) {
throw err;
} finally {
// Return connection
await conn.end();
}
};
@ -130,7 +136,7 @@ export const login = async (username: string, password: string, ip: string): Pro
* Checks if the given session information are valid and returns the user information if they are
*/
export const checkSession = async (sessionId: string, sessionKey: string, ip: string): Promise<User> => {
let conn = BetterzonDB.getConnection();
let conn = await BetterzonDB.getConnection();
try {
// Get saved session key hash
const query = 'SELECT user_id, session_key_hash, validUntil FROM sessions WHERE session_id = ?';
@ -198,6 +204,9 @@ export const checkSession = async (sessionId: string, sessionKey: string, ip: st
} catch (err) {
throw err;
} finally {
// Return connection
await conn.end();
}
};
@ -230,7 +239,7 @@ export interface Status {
* @param email The email to check
*/
export const checkUsernameAndEmail = async (username: string, email: string): Promise<Status> => {
let conn = BetterzonDB.getConnection();
let conn = await BetterzonDB.getConnection();
try {
// Create user entry in SQL
const usernameQuery = 'SELECT username FROM users WHERE username = ?';
@ -278,5 +287,8 @@ export const checkUsernameAndEmail = async (username: string, email: string): Pr
} catch (err) {
throw err;
} finally {
// Return connection
await conn.end();
}
};

View File

@ -18,7 +18,7 @@ dotenv.config();
* Fetches and returns all known vendors
*/
export const findAll = async (): Promise<Vendors> => {
let conn = BetterzonDB.getConnection();
let conn = await BetterzonDB.getConnection();
let vendorRows = [];
try {
const rows = await conn.query('SELECT vendor_id, name, streetname, zip_code, city, country_code, phone, website FROM vendors WHERE isActive = true');
@ -50,6 +50,9 @@ export const findAll = async (): Promise<Vendors> => {
} catch (err) {
throw err;
} finally {
// Return connection
await conn.end();
}
return vendorRows;
@ -60,7 +63,7 @@ export const findAll = async (): Promise<Vendors> => {
* @param id The id of the vendor to fetch
*/
export const find = async (id: number): Promise<Vendor> => {
let conn = BetterzonDB.getConnection();
let conn = await BetterzonDB.getConnection();
let vendor: any;
try {
const rows = await conn.query('SELECT vendor_id, name, streetname, zip_code, city, country_code, phone, website FROM vendors WHERE vendor_id = ? AND isActive = true', id);
@ -72,6 +75,9 @@ export const find = async (id: number): Promise<Vendor> => {
} catch (err) {
throw err;
} finally {
// Return connection
await conn.end();
}
return vendor;
@ -82,7 +88,7 @@ export const find = async (id: number): Promise<Vendor> => {
* @param term the term to match
*/
export const findBySearchTerm = async (term: string): Promise<Vendors> => {
let conn = BetterzonDB.getConnection();
let conn = await BetterzonDB.getConnection();
let vendorRows = [];
try {
term = '%' + term + '%';
@ -95,6 +101,9 @@ export const findBySearchTerm = async (term: string): Promise<Vendors> => {
} catch (err) {
throw err;
} finally {
// Return connection
await conn.end();
}
return vendorRows;
@ -105,7 +114,7 @@ export const findBySearchTerm = async (term: string): Promise<Vendors> => {
* @param user The user to return the managed shops for
*/
export const getManagedShops = async (user_id: number): Promise<Vendors> => {
let conn = BetterzonDB.getConnection();
let conn = await BetterzonDB.getConnection();
let vendorRows = [];
try {
const rows = await conn.query('SELECT vendor_id, name, streetname, zip_code, city, country_code, phone, website FROM vendors WHERE admin_id LIKE ?', user_id);
@ -117,6 +126,9 @@ export const getManagedShops = async (user_id: number): Promise<Vendors> => {
} catch (err) {
throw err;
} finally {
// Return connection
await conn.end();
}
return vendorRows;
@ -129,7 +141,7 @@ export const getManagedShops = async (user_id: number): Promise<Vendors> => {
* @param product_id The product id of the product to deactivate the listing for
*/
export const deactivateListing = async (user_id: number, vendor_id: number, product_id: number): Promise<Boolean> => {
let conn = BetterzonDB.getConnection();
let conn = await BetterzonDB.getConnection();
try {
// Check if the user is authorized to manage the requested vendor
const user_vendor_rows = await conn.query('SELECT vendor_id FROM vendors WHERE vendor_id = ? AND admin_id = ?', [vendor_id, user_id]);
@ -142,6 +154,9 @@ export const deactivateListing = async (user_id: number, vendor_id: number, prod
return status.affectedRows > 0;
} catch (err) {
throw err;
} finally {
// Return connection
await conn.end();
}
};
@ -152,7 +167,7 @@ export const deactivateListing = async (user_id: number, vendor_id: number, prod
* @param isActive The new active state
*/
export const setShopStatus = async (user_id: number, vendor_id: number, isActive: boolean): Promise<Boolean> => {
let conn = BetterzonDB.getConnection();
let conn = await BetterzonDB.getConnection();
try {
// Check if the user is authorized to manage the requested vendor
const user_vendor_rows = await conn.query('SELECT vendor_id FROM vendors WHERE vendor_id = ? AND admin_id = ?', [vendor_id, user_id]);
@ -166,5 +181,8 @@ export const setShopStatus = async (user_id: number, vendor_id: number, isActive
return status.affectedRows > 0;
} catch (err) {
throw err;
} finally {
// Return connection
await conn.end();
}
};

View File

@ -13,7 +13,7 @@ export namespace ClimbingRouteRatingDB {
connectionLimit: 5
});
export function getConnection() {
return pool;
}
export const getConnection = async () => {
return pool.getConnection();
};
}

View File

@ -6,11 +6,14 @@ import {ClimbingGym} from './ClimbingGym.interface';
* @return Promise<ClimbingHall[]> The climbing halls
*/
export const findAll = async (): Promise<ClimbingGym[]> => {
let conn = ClimbingRouteRatingDB.getConnection();
let conn = await ClimbingRouteRatingDB.getConnection();
try {
return await conn.query('SELECT gym_id, name, city, verified FROM climbing_gyms');
} catch (err) {
throw err;
} finally {
// Return connection
await conn.end();
}
};
@ -21,7 +24,7 @@ export const findAll = async (): Promise<ClimbingGym[]> => {
* @return number The id of the climbing hall
*/
export const createGym = async (name: string, city: string): Promise<number> => {
let conn = ClimbingRouteRatingDB.getConnection();
let conn = await ClimbingRouteRatingDB.getConnection();
try {
let res = await conn.query('INSERT INTO climbing_gyms (name, city) VALUES (?, ?) RETURNING gym_id', [name, city]);
@ -29,5 +32,8 @@ export const createGym = async (name: string, city: string): Promise<number> =>
return res[0].gym_id;
} catch (err) {
throw err;
} finally {
// Return connection
await conn.end();
}
};

View File

@ -7,11 +7,14 @@ import random from 'random-words';
* @return Promise<ClimbingRoute[]> The climbing routes
*/
export const findAll = async (): Promise<ClimbingRoute[]> => {
let conn = ClimbingRouteRatingDB.getConnection();
let conn = await ClimbingRouteRatingDB.getConnection();
try {
return await conn.query('SELECT route_id, gym_id, name, difficulty, route_setting_date FROM climbing_routes');
} catch (err) {
throw err;
} finally {
// Return connection
await conn.end();
}
};
@ -21,11 +24,14 @@ export const findAll = async (): Promise<ClimbingRoute[]> => {
* @return Promise<ClimbingRoute> The climbing route
*/
export const findById = async (route_id: string): Promise<ClimbingRoute> => {
let conn = ClimbingRouteRatingDB.getConnection();
let conn = await ClimbingRouteRatingDB.getConnection();
try {
return await conn.query('SELECT route_id, gym_id, name, difficulty, route_setting_date FROM climbing_routes WHERE route_id = ?', route_id);
} catch (err) {
throw err;
} finally {
// Return connection
await conn.end();
}
};
@ -37,7 +43,7 @@ export const findById = async (route_id: string): Promise<ClimbingRoute> => {
* @return string The id of the created route
*/
export const createRoute = async (gym_id: number, name: string, difficulty: string): Promise<string> => {
let conn = ClimbingRouteRatingDB.getConnection();
let conn = await ClimbingRouteRatingDB.getConnection();
// Generate route id
let route_id = '';
@ -55,5 +61,8 @@ export const createRoute = async (gym_id: number, name: string, difficulty: stri
return route_id;
} catch (err) {
throw err;
} finally {
// Return connection
await conn.end();
}
};

View File

@ -6,11 +6,14 @@ import {RouteComment} from './RouteComment.interface';
* @return Promise<RouteComment[]> The comments
*/
export const findByRoute = async (route_id: string): Promise<RouteComment[]> => {
let conn = ClimbingRouteRatingDB.getConnection();
let conn = await ClimbingRouteRatingDB.getConnection();
try {
return await conn.query('SELECT comment_id, route_id, comment, timestamp FROM route_comments WHERE route_id = ?', route_id);
} catch (err) {
throw err;
} finally {
// Return connection
await conn.end();
}
};
@ -21,12 +24,15 @@ export const findByRoute = async (route_id: string): Promise<RouteComment[]> =>
* @return number The id of the comment
*/
export const createComment = async (route_id: string, comment: string): Promise<number> => {
let conn = ClimbingRouteRatingDB.getConnection();
let conn = await ClimbingRouteRatingDB.getConnection();
try {
let res = await conn.query('INSERT INTO route_comments (route_id, comment) VALUES (?, ?) RETURNING comment_id', [route_id, comment]);
return res[0].comment_id;
} catch (err) {
throw err;
} finally {
// Return connection
await conn.end();
}
};

View File

@ -7,11 +7,14 @@ import {RouteRating} from './RouteRating.interface';
* @return Promise<RouteRating[]> The ratings
*/
export const findByRoute = async (route_id: string): Promise<RouteRating[]> => {
let conn = ClimbingRouteRatingDB.getConnection();
let conn = await ClimbingRouteRatingDB.getConnection();
try {
return await conn.query('SELECT rating_id, route_id, stars, timestamp FROM route_ratings WHERE route_id = ?', route_id);
} catch (err) {
throw err;
} finally {
// Return connection
await conn.end();
}
};
@ -41,12 +44,15 @@ export const getStarsForRoute = async (route_id: string): Promise<number> => {
* @return number The id of the created rating
*/
export const createRating = async (route_id: string, stars: number): Promise<number> => {
let conn = ClimbingRouteRatingDB.getConnection();
let conn = await ClimbingRouteRatingDB.getConnection();
try {
let res = await conn.query('INSERT INTO route_ratings (route_id, stars) VALUES (?, ?) RETURNING rating_id', [route_id, stars]);
return res[0].comment_id;
} catch (err) {
throw err;
} finally {
// Return connection
await conn.end();
}
};

View File

@ -13,7 +13,7 @@ export namespace RaPlaChangesDB {
connectionLimit: 5
});
export function getConnection() {
return pool;
}
export const getConnection = async () => {
return pool.getConnection();
};
}

View File

@ -6,7 +6,7 @@ import {RaPlaChangesDB} from '../DHBWRaPlaChanges.db';
dotenv.config();
export const getChanges = async (course: string, week: string): Promise<Event[]> => {
let conn = RaPlaChangesDB.getConnection();
let conn = await RaPlaChangesDB.getConnection();
try {
let relevantEventsRows = await conn.query('SELECT DISTINCT(entry_id) FROM rapla_changes WHERE new_start > ? AND new_start < DATE_ADD(?, INTERVAL 7 DAY)', [week, week]);
@ -68,11 +68,14 @@ export const getChanges = async (course: string, week: string): Promise<Event[]>
return Array.from(eventsMap.values()) as Event[];
} catch (err) {
throw err;
} finally {
// Return connection
await conn.end();
}
};
export const getEventById = async (course: string, id: string): Promise<Event> => {
let conn = RaPlaChangesDB.getConnection();
let conn = await RaPlaChangesDB.getConnection();
try {
let rows = await conn.query('SELECT c.change_id, c.entry_id, c.change_timestamp, c.isDeleted, c.new_summary, c.new_description, c.new_start, c.new_last_modified, c.new_end, c.new_created, c.new_location, c.new_organizer, c.new_categories, e.uid FROM rapla_changes c LEFT OUTER JOIN rapla_entries e ON c.entry_id = e.entry_id WHERE e.uid = ? ORDER BY c.change_id', id);
@ -123,5 +126,8 @@ export const getEventById = async (course: string, id: string): Promise<Event> =
return Array.from(eventsMap.values())[0];
} catch (err) {
throw err;
} finally {
// Return connection
await conn.end();
}
};

View File

@ -20,10 +20,10 @@ export namespace PartyPlanerDB {
connectionLimit: 5
});
export function getConnection(useDev: boolean = false) {
export const getConnection = async (useDev: boolean = false) => {
if (useDev) {
return dev_pool;
}
return prod_pool;
return dev_pool.getConnection();
}
return prod_pool.getConnection();
};
}

View File

@ -11,7 +11,7 @@ dotenv.config();
* @return Event[] A list of events
*/
export const getEventData = async (useDev: boolean, userId: string): Promise<Event[]> => {
let conn = PartyPlanerDB.getConnection(useDev);
let conn = await PartyPlanerDB.getConnection(useDev);
try {
let eventRows = await conn.query('SELECT event_id, name, description, takes_place_date, registration_until_date, max_participants FROM events WHERE creator_id = ?', userId);
@ -69,5 +69,8 @@ export const getEventData = async (useDev: boolean, userId: string): Promise<Eve
return eventsList;
} catch (err) {
throw err;
} finally {
// Return connection
await conn.end();
}
};

View File

@ -11,7 +11,7 @@ dotenv.config();
* @return Friendship[] A list of friends
*/
export const getFriendshipData = async (useDev: boolean, userId: string): Promise<Friendship[]> => {
let conn = PartyPlanerDB.getConnection(useDev);
let conn = await PartyPlanerDB.getConnection(useDev);
try {
let rows = await conn.query('SELECT f.friendship_id, f.friend_id, u.first_name as friend_first_name, u.last_name as friend_last_name, u.username as friend_username FROM friendships f LEFT OUTER JOIN users u ON f.friend_id = u.user_id WHERE f.user_id = ?', userId);
@ -30,5 +30,8 @@ export const getFriendshipData = async (useDev: boolean, userId: string): Promis
return friends;
} catch (err) {
throw err;
} finally {
// Return connection
await conn.end();
}
};

View File

@ -11,7 +11,7 @@ dotenv.config();
* @return ReceivedInvite[] A list of invites
*/
export const getInvitesData = async (useDev: boolean, userId: string): Promise<ReceivedInvite[]> => {
let conn = PartyPlanerDB.getConnection(useDev);
let conn = await PartyPlanerDB.getConnection(useDev);
try {
let rows = await conn.query('SELECT i.invite_id, i.valid_until, i.already_used, i.invite_key, e.name as event_name, e.description as event_description, e.takes_place_date, e.registration_until_date, e.max_participants, e.creator_id, u.first_name, u.last_name FROM invitations i LEFT OUTER JOIN events e ON e.event_id = i.event_id LEFT OUTER JOIN users u ON u.user_id = e.creator_id WHERE i.user_id = ?', userId);
@ -37,5 +37,8 @@ export const getInvitesData = async (useDev: boolean, userId: string): Promise<R
return invites;
} catch (err) {
throw err;
} finally {
// Return connection
await conn.end();
}
};

View File

@ -11,7 +11,7 @@ dotenv.config();
* @return SessionData[] A list containing objects with the session data
*/
export const getSessionData = async (useDev: boolean, userId: string): Promise<SessionData[]> => {
let conn = PartyPlanerDB.getConnection(useDev);
let conn = await PartyPlanerDB.getConnection(useDev);
try {
let rows = await conn.query('SELECT session_id, type, last_login, last_ip FROM sessions WHERE user_id = ? AND valid_until > NOW()', userId);
@ -29,5 +29,8 @@ export const getSessionData = async (useDev: boolean, userId: string): Promise<S
return sessions;
} catch (err) {
throw err;
} finally {
// Return connection
await conn.end();
}
};

View File

@ -15,7 +15,7 @@ dotenv.config();
* @return UserData An object containing the user data
*/
export const getUserData = async (useDev: boolean, userId: string): Promise<UserData> => {
let conn = PartyPlanerDB.getConnection(useDev);
let conn = await PartyPlanerDB.getConnection(useDev);
try {
let rows = await conn.query('SELECT username, email, first_name, last_Name, last_login, email_is_verified, is_premium_user FROM users WHERE user_id = ?', userId);
@ -36,6 +36,9 @@ export const getUserData = async (useDev: boolean, userId: string): Promise<User
return user;
} catch (err) {
throw err;
} finally {
// Return connection
await conn.end();
}
};
@ -45,7 +48,7 @@ export const getUserData = async (useDev: boolean, userId: string): Promise<User
* @return any An object with a list of usernames and emails
*/
export const getExistingUsernamesAndEmails = async (useDev: boolean): Promise<any> => {
let conn = PartyPlanerDB.getConnection(useDev);
let conn = await PartyPlanerDB.getConnection(useDev);
try {
const rows = await conn.query('SELECT username, email FROM users');
@ -63,6 +66,9 @@ export const getExistingUsernamesAndEmails = async (useDev: boolean): Promise<an
};
} catch (err) {
throw err;
} finally {
// Return connection
await conn.end();
}
};
@ -78,7 +84,7 @@ export const getExistingUsernamesAndEmails = async (useDev: boolean): Promise<an
* @param deviceInfo The user agent of the new user
*/
export const registerUser = async (useDev: boolean, username: string, email: string, firstName: string, lastName: string, password: string, ip: string, deviceInfo: string): Promise<Session> => {
let conn = PartyPlanerDB.getConnection(useDev);
let conn = await PartyPlanerDB.getConnection(useDev);
try {
const pwHash = bcrypt.hashSync(password, 10);
const sessionKey = Guid.create().toString();
@ -125,6 +131,9 @@ export const registerUser = async (useDev: boolean, username: string, email: str
};
} catch (err) {
throw err;
} finally {
// Return connection
await conn.end();
}
};
@ -138,7 +147,7 @@ export const registerUser = async (useDev: boolean, username: string, email: str
* @param deviceInfo The user agent of the new user
*/
export const loginUser = async (useDev: boolean, username: string, email: string, password: string, ip: string, deviceInfo: string): Promise<Session> => {
let conn = PartyPlanerDB.getConnection(useDev);
let conn = await PartyPlanerDB.getConnection(useDev);
try {
let query_result;
@ -197,6 +206,9 @@ export const loginUser = async (useDev: boolean, username: string, email: string
} catch (err) {
throw err;
} finally {
// Return connection
await conn.end();
}
};
@ -207,7 +219,7 @@ export const loginUser = async (useDev: boolean, username: string, email: string
* @param email The email to check
*/
export const checkUsernameAndEmail = async (useDev: boolean, username: string, email: string): Promise<Status> => {
let conn = PartyPlanerDB.getConnection(useDev);
let conn = await PartyPlanerDB.getConnection(useDev);
try {
const usernameQuery = 'SELECT username FROM users WHERE username = ?';
const emailQuery = 'SELECT email FROM users WHERE email = ?';
@ -254,11 +266,14 @@ export const checkUsernameAndEmail = async (useDev: boolean, username: string, e
} catch (err) {
throw err;
} finally {
// Return connection
await conn.end();
}
};
export const checkSession = async (useDev: boolean, userId: string, sessionId: string, sessionKey: string): Promise<boolean> => {
let conn = PartyPlanerDB.getConnection(useDev);
let conn = await PartyPlanerDB.getConnection(useDev);
try {
let rows = await conn.query('SELECT session_key_hash FROM sessions WHERE user_id = ? AND session_id = ?', [userId, sessionId]);
@ -270,5 +285,8 @@ export const checkSession = async (useDev: boolean, userId: string, sessionId: s
return bcrypt.compareSync(sessionKey, savedHash);
} catch (err) {
throw err;
} finally {
// Return connection
await conn.end();
}
};

View File

@ -13,7 +13,7 @@ export namespace HighlightMarkerDB {
connectionLimit: 5
});
export function getConnection() {
return pool;
}
export const getConnection = async () => {
return pool.getConnection();
};
}

View File

@ -8,7 +8,7 @@ dotenv.config();
* @param req_body The request body
*/
export const createHighlightEntry = async (req_body: any) => {
let conn = HighlightMarkerDB.getConnection();
let conn = await HighlightMarkerDB.getConnection();
try {
const streamers = await conn.query('SELECT streamer_id FROM streamers WHERE username = ?', req_body.streamer);
let streamer_id: number = -1;
@ -24,5 +24,8 @@ export const createHighlightEntry = async (req_body: any) => {
const rows = await conn.query('INSERT INTO highlights (streamer_id, stream_id, description, stream_timestamp, issuing_user, game) VALUES (?, ?, ?, ?, ?, ?)', params);
} catch (err) {
throw err;
} finally {
// Return connection
await conn.end();
}
};