All checks were successful
Jenkins Production Deployment
Co-authored-by: Patrick Mueller <patrick@mueller-patrick.tech> Reviewed-on: #15 Co-authored-by: Patrick Müller <patrick@plutodev.de> Co-committed-by: Patrick Müller <patrick@plutodev.de>
175 lines
5.3 KiB
TypeScript
175 lines
5.3 KiB
TypeScript
import * as dotenv from 'dotenv';
|
|
import {Vendor} from './vendor.interface';
|
|
import {Vendors} from './vendors.interface';
|
|
import {BetterzonDB} from '../Betterzon.db';
|
|
|
|
dotenv.config();
|
|
|
|
/**
|
|
* Data Model Interfaces
|
|
*/
|
|
|
|
|
|
/**
|
|
* Service Methods
|
|
*/
|
|
|
|
/**
|
|
* Fetches and returns all known vendors
|
|
*/
|
|
export const findAll = async (): Promise<Vendors> => {
|
|
let conn = 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');
|
|
for (let row in rows) {
|
|
if (row !== 'meta') {
|
|
let vendor: Vendor = {
|
|
city: '',
|
|
country_code: '',
|
|
name: '',
|
|
phone: '',
|
|
streetname: '',
|
|
vendor_id: 0,
|
|
website: '',
|
|
zip_code: ''
|
|
};
|
|
const sqlVendor = rows[row];
|
|
|
|
vendor.vendor_id = sqlVendor.vendor_id;
|
|
vendor.name = sqlVendor.name;
|
|
vendor.streetname = sqlVendor.streetname;
|
|
vendor.zip_code = sqlVendor.zip_code;
|
|
vendor.city = sqlVendor.city;
|
|
vendor.country_code = sqlVendor.country_code;
|
|
vendor.phone = sqlVendor.phone;
|
|
vendor.website = sqlVendor.website;
|
|
vendorRows.push(vendor);
|
|
}
|
|
}
|
|
|
|
} catch (err) {
|
|
throw err;
|
|
}
|
|
|
|
return vendorRows;
|
|
};
|
|
|
|
/**
|
|
* Fetches and returns the vendor with the specified id
|
|
* @param id The id of the vendor to fetch
|
|
*/
|
|
export const find = async (id: number): Promise<Vendor> => {
|
|
let conn = 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);
|
|
for (let row in rows) {
|
|
if (row !== 'meta') {
|
|
vendor = rows[row];
|
|
}
|
|
}
|
|
|
|
} catch (err) {
|
|
throw err;
|
|
}
|
|
|
|
return vendor;
|
|
};
|
|
|
|
/**
|
|
* Fetches and returns all vendors that match the search term
|
|
* @param term the term to match
|
|
*/
|
|
export const findBySearchTerm = async (term: string): Promise<Vendors> => {
|
|
let conn = BetterzonDB.getConnection();
|
|
let vendorRows = [];
|
|
try {
|
|
term = '%' + term + '%';
|
|
const rows = await conn.query('SELECT vendor_id, name, streetname, zip_code, city, country_code, phone, website FROM vendors WHERE name LIKE ? AND isActive = true', term);
|
|
for (let row in rows) {
|
|
if (row !== 'meta') {
|
|
vendorRows.push(rows[row]);
|
|
}
|
|
}
|
|
|
|
} catch (err) {
|
|
throw err;
|
|
}
|
|
|
|
return vendorRows;
|
|
};
|
|
|
|
/**
|
|
* Get all vendors that have the given user as admin
|
|
* @param user The user to return the managed shops for
|
|
*/
|
|
export const getManagedShops = async (user_id: number): Promise<Vendors> => {
|
|
let conn = 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);
|
|
for (let row in rows) {
|
|
if (row !== 'meta') {
|
|
vendorRows.push(rows[row]);
|
|
}
|
|
}
|
|
|
|
} catch (err) {
|
|
throw err;
|
|
}
|
|
|
|
return vendorRows;
|
|
};
|
|
|
|
/**
|
|
* Deactivates a product listing for a specific vendor
|
|
* @param user_id The user id of the issuing user
|
|
* @param vendor_id The vendor id of the vendor to deactivate the listing for
|
|
* @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();
|
|
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]);
|
|
if (user_vendor_rows.length !== 1) {
|
|
return false;
|
|
}
|
|
|
|
const status = await conn.query('UPDATE prices SET active_listing = false WHERE vendor_id = ? and product_id = ?', [vendor_id, product_id]);
|
|
|
|
return status.affectedRows > 0;
|
|
} catch (err) {
|
|
throw err;
|
|
}
|
|
|
|
return false;
|
|
};
|
|
|
|
/**
|
|
* Set the specified shop to either active or not active
|
|
* @param user_id The user id of the issuing user
|
|
* @param vendor_id The vendor id of the shop to update
|
|
* @param isActive The new active state
|
|
*/
|
|
export const setShopStatus = async (user_id: number, vendor_id: number, isActive: boolean): Promise<Boolean> => {
|
|
let conn = 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]);
|
|
if (user_vendor_rows.length !== 1) {
|
|
return false;
|
|
}
|
|
|
|
// Update the vendor state
|
|
const status = await conn.query('UPDATE vendors SET isActive = ? WHERE vendor_id = ?', [isActive, vendor_id]);
|
|
|
|
return status.affectedRows > 0;
|
|
} catch (err) {
|
|
throw err;
|
|
}
|
|
|
|
return false;
|
|
};
|