feat(resources): refactor marketplace

This commit is contained in:
Christopher authored and Taso committed 2021-04-19 14:14:26 -04:00
1 parent 4803753cfa
commit cd4f7e73a6
5 files changed
+218 -163

No files matched your search

@@ -0,0 +1,43 @@
import { MarketplaceEvents, MarketplaceListing } from '../../../typings/marketplace';
import { getSource } from '../utils/miscUtils';
import PlayerService from '../players/player.service';
import { reportListingToDiscord } from '../discord';
import { selloutLogger } from './marketplace.utils';
import MarketplaceService from './marketplace.service';
onNet(MarketplaceEvents.FETCH_LISTING, async () => {
const src = getSource();
MarketplaceService.handleFetchListings(src).catch((e) =>
selloutLogger.error(`Error occurred in fetch listing event (${src}), Error: ${e.message}`),
);
});
onNet(MarketplaceEvents.ADD_LISTING, async (listing: MarketplaceListing) => {
const src = getSource();
const Player = PlayerService.getPlayer(src);
MarketplaceService.handleAddListing(
src,
Player.getFirstName(),
Player.username,
Player.getName(),
Player.getPhoneNumber(),
listing,
).catch((e) =>
selloutLogger.error(`Error occurred in add listing event (${src}), Error: ${e.message}`),
);
});
onNet(MarketplaceEvents.DELETE_LISTING, async (listingId: number) => {
const src = getSource();
MarketplaceService.handleDeleteListing(src, listingId).catch((e) =>
selloutLogger.error(`Error occurred in delete listing event (${src}), Error: ${e.message}`),
);
});
onNet(MarketplaceEvents.REPORT_LISTING, async (listing: MarketplaceListing) => {
const src = getSource();
MarketplaceService.handleReportListing(src, listing).catch((e) =>
selloutLogger.error(`Error occurred in report listing event (${src}), Error: ${e.message}`),
);
});
@@ -0,0 +1,62 @@
import { pool } from '../db';
import { MarketplaceListing } from '../../../typings/marketplace';
export class _MarketplaceDB {
async addListing(
identifier: string,
username: string,
name: string,
number: any,
listing: MarketplaceListing,
): Promise<void> {
const query =
'INSERT INTO npwd_marketplace_listings (identifier, username, name, number, title, url, description) VALUES (?, ?, ?, ?, ?, ?, ?)';
await pool.query(query, [
identifier,
username,
name,
number,
listing.title,
listing.url,
listing.description,
]);
}
async fetchListings(): Promise<MarketplaceListing[]> {
const query = 'SELECT * FROM npwd_marketplace_listings ORDER BY id DESC';
const [results] = await pool.query(query);
return <MarketplaceListing[]>results;
}
async deleteListing(listingId: number, identifier: string): Promise<void> {
const query = 'DELETE FROM npwd_marketplace_listings WHERE id = ? AND identifier = ?';
await pool.query(query, [listingId, identifier]);
}
async getListing(listingId: number): Promise<MarketplaceListing> {
const query = `SELECT * FROM npwd_marketplace_listings WHERE id = ?`;
const [results] = await pool.query(query, [listingId]);
const listings = <MarketplaceListing[]>results;
return listings[0];
}
async reportListing(listingId: number, profile: string): Promise<void> {
const query = `INSERT INTO npwd_marketplace_reports (listing_id, profile) VALUES (?, ?)`;
await pool.query(query, [listingId, profile]);
}
async doesReportExist(listingId: number, profile: string): Promise<boolean> {
const query = `SELECT * FROM npwd_marketplace_reports WHERE listing_id = ? AND profile = ?`;
const [results] = await pool.query(query, [listingId, profile]);
const result = <any>results;
return result.length > 0;
}
}
const MarketplaceDB = new _MarketplaceDB();
export default MarketplaceDB;
@@ -0,0 +1,110 @@
import PlayerService from '../players/player.service';
import { selloutLogger } from './marketplace.utils';
import MarketplaceDB, { _MarketplaceDB } from './marketplace.db';
import { MarketplaceEvents, MarketplaceListing } from '../../../typings/marketplace';
import { notesLogger } from '../notes/notes.utils';
import { listenerCount } from 'cluster';
import { reportListingToDiscord } from '../discord';
class _MarketplaceService {
private readonly marketplaceDB: _MarketplaceDB;
constructor() {
this.marketplaceDB = MarketplaceDB;
selloutLogger.debug('Marketplace service started');
}
async handleAddListing(
src: number,
identifier: string,
username: string,
name: string,
number: any,
listing: MarketplaceListing,
): Promise<void> {
selloutLogger.debug('Handling add listing, listing:');
selloutLogger.debug;
try {
await this.marketplaceDB.addListing(identifier, username, name, number, listing);
emitNet(MarketplaceEvents.ADD_LISTING_SUCCESS, src);
emitNet(MarketplaceEvents.ACTION_RESULT, src, {
message: 'MARKETPLACE_CREATE_LISTING_SUCCESS',
type: 'success',
});
} catch (e) {
selloutLogger.error(`Failed to add listing ${e.message}`, {
source: src,
});
emitNet(MarketplaceEvents.ACTION_RESULT, src, {
message: 'MARKETPLACE_CREATE_LISTING_FAILED',
type: 'error',
});
}
}
async handleFetchListings(src: number) {
try {
const listings = await this.marketplaceDB.fetchListings();
emitNet(MarketplaceEvents.SEND_LISTING, src, listings);
} catch (e) {
selloutLogger.error(`Failed to fetch listings, ${e.message}`, {
source: src,
});
}
}
async handleDeleteListing(src: number, listingId: number): Promise<void> {
const identifier = PlayerService.getIdentifier(src);
try {
await this.marketplaceDB.deleteListing(listingId, identifier);
emitNet(MarketplaceEvents.DELETE_LISTING_SUCCESS, src);
emitNet(MarketplaceEvents.ACTION_RESULT, src, {
message: 'MARKETPLACE_DELETE_LISTING_SUCCESS',
type: 'success',
});
} catch (e) {
notesLogger.error(`Error in handleDeleteListing, ${e.message}`);
emitNet(MarketplaceEvents.ACTION_RESULT, src, {
message: 'MARKETPLACE_DELETE_LISTING_FAILED',
type: 'error',
});
}
}
async handleReportListing(src: number, listing: MarketplaceListing): Promise<void> {
try {
const rListing = await this.marketplaceDB.getListing(listing.id);
const reportExists = await this.marketplaceDB.doesReportExist(listing.id, listing.name);
const reportingPlayer = GetPlayerName(src.toString());
if (reportExists) {
selloutLogger.error(`This listing has already been reported`);
return emitNet(MarketplaceEvents.ACTION_RESULT, src, {
message: 'MARKETPLACE_REPORT_LISTING_FAILED',
type: 'info',
});
}
await this.marketplaceDB.reportListing(rListing.id, rListing.name);
await reportListingToDiscord(rListing, reportingPlayer);
emitNet(MarketplaceEvents.ACTION_RESULT, src, {
message: 'MARKETPLACE_REPORT_LISTING_SUCCESS',
type: 'success',
});
} catch (e) {
selloutLogger.error(`Failed to report listing ${e.message}`, {
source: src,
});
}
}
}
const MarketplaceService = new _MarketplaceService();
export default MarketplaceService;
@@ -0,0 +1,3 @@
import { mainLogger } from '../sv_logger';
export const selloutLogger = mainLogger.child({ module: 'sellout' });
-163
View File
@@ -1,163 +0,0 @@
import { pool } from './db';
import { mainLogger } from './sv_logger';
import { MarketplaceListing } from '../../typings/marketplace';
import { reportListingToDiscord } from './discord';
import { MarketplaceEvents } from '../../typings/marketplace';
import { getSource } from './utils/miscUtils';
import PlayerService from './players/player.service';
const selloutLogger = mainLogger.child({ module: 'sellout' });
async function fetchAllListings(): Promise<MarketplaceListing[]> {
const query = 'SELECT * FROM npwd_marketplace_listings ORDER BY id DESC';
const [results] = await pool.query(query);
return <MarketplaceListing[]>results;
}
async function addListing(
identifier: string,
username: string,
name: string,
number: any,
listing: MarketplaceListing,
): Promise<void> {
const query =
'INSERT INTO npwd_marketplace_listings (identifier, username, name, number, title, url, description) VALUES (?, ?, ?, ?, ?, ?, ?)';
await pool.query(query, [
identifier,
username,
name,
number,
listing.title,
listing.url,
listing.description,
]);
}
async function deleteListing(listingId: number, identifier: string): Promise<void> {
const query = 'DELETE FROM npwd_marketplace_listings WHERE id = ? AND identifier = ?';
await pool.query(query, [listingId, identifier]);
}
async function getListing(listingId: number): Promise<MarketplaceListing> {
const query = `SELECT * FROM npwd_marketplace_listings WHERE id = ?`;
const [results] = await pool.query(query, [listingId]);
const listings = <MarketplaceListing[]>results;
return listings[0];
}
async function reportListing(listingId: number, profile: string): Promise<void> {
const query = `INSERT INTO npwd_marketplace_reports (listing_id, profile) VALUES (?, ?)`;
await pool.query(query, [listingId, profile]);
}
async function doesReportExist(listingId: number, profile: string): Promise<boolean> {
const query = `SELECT * FROM npwd_marketplace_reports WHERE listing_id = ? AND profile = ?`;
const [results] = await pool.query(query, [listingId, profile]);
const result = <any>results;
return result.length > 0;
}
onNet(MarketplaceEvents.FETCH_LISTING, async () => {
const _source = getSource();
try {
const listings = await fetchAllListings();
emitNet(MarketplaceEvents.SEND_LISTING, _source, listings);
} catch (e) {
selloutLogger.error(`Failed to fetch listings, ${e.message}`, {
source: _source,
});
}
});
onNet(MarketplaceEvents.ADD_LISTING, async (listing: MarketplaceListing) => {
const _source = getSource();
try {
const player = PlayerService.getPlayer(_source);
// This is used as username for the reports
await addListing(
player.getIdentifier(),
player.username,
player.getName(),
player.getPhoneNumber(),
listing,
);
emitNet(MarketplaceEvents.ADD_LISTING_SUCCESS, _source);
emitNet(MarketplaceEvents.ACTION_RESULT, _source, {
message: 'MARKETPLACE_CREATE_LISTING_SUCCESS',
type: 'success',
});
} catch (e) {
selloutLogger.error(`Failed to add listing ${e.message}`, {
source: _source,
});
emitNet(MarketplaceEvents.ACTION_RESULT, _source, {
message: 'MARKETPLACE_CREATE_LISTING_FAILED',
type: 'error',
});
}
});
onNet(MarketplaceEvents.DELETE_LISTING, async (listingId: number) => {
const pSource = getSource();
try {
const identifier = PlayerService.getIdentifier(pSource);
await deleteListing(listingId, identifier);
// fetches the listings again
emitNet(MarketplaceEvents.DELETE_LISTING_SUCCESS, pSource);
emitNet(MarketplaceEvents.ACTION_RESULT, pSource, {
message: 'MARKETPLACE_DELETE_LISTING_SUCCESS',
type: 'success',
});
} catch (e) {
selloutLogger.error(`Failed to delete listing ${e.message}`, {
source: pSource,
});
emitNet(MarketplaceEvents.ACTION_RESULT, pSource, {
message: 'MARKETPLACE_DELETE_LISTING_FAILED',
type: 'error',
});
}
});
onNet(MarketplaceEvents.REPORT_LISTING, async (listing: MarketplaceListing) => {
const pSource = getSource();
try {
const rListing = await getListing(listing.id);
const reportExists = await doesReportExist(listing.id, listing.name);
// gets the player name (steam) of the player that is reporting
const reportingPlayer = GetPlayerName(pSource);
if (reportExists) {
// send an info alert
selloutLogger.error(`This listing has already been reported`);
return emitNet(MarketplaceEvents.ACTION_RESULT, pSource, {
message: 'MARKETPLACE_REPORT_LISTING_FAILED',
type: 'info',
});
}
await reportListing(rListing.id, rListing.name);
await reportListingToDiscord(rListing, reportingPlayer);
emitNet(MarketplaceEvents.ACTION_RESULT, pSource, {
message: 'MARKETPLACE_REPORT_LISTING_SUCCESS',
type: 'success',
});
} catch (e) {
selloutLogger.error(`Failed to report listing ${e.message}`, {
source: pSource,
});
}
});