mirror of
https://github.com/darksaid98/new-phone-who-dis.git
synced 2026-12-05 06:28:51 +00:00
refactor(server/marketplace): Refactor to use promise events
This commit is contained in:
1 parent
4afa9c691b
commit
3df2f88759
9 files changed
+160
-134
No files matched your search
@@ -1,9 +1,25 @@
|
||||
import PlayerService from '../players/player.service';
|
||||
import { selloutLogger } from './marketplace.utils';
|
||||
import MarketplaceDB, { _MarketplaceDB } from './marketplace.db';
|
||||
import { MarketplaceEvents, MarketplaceListing } from '../../../typings/marketplace';
|
||||
import {
|
||||
MarketplaceDeleteDTO,
|
||||
MarketplaceEvents,
|
||||
MarketplaceListing,
|
||||
MarketplaceListingBase,
|
||||
MarketplaceReportDTO,
|
||||
} from '../../../typings/marketplace';
|
||||
import { notesLogger } from '../notes/notes.utils';
|
||||
import { reportListingToDiscord } from '../discord';
|
||||
import { PromiseEventReturnFunc, PromiseRequest } from '../utils/PromiseNetEvents/promise.types';
|
||||
|
||||
interface HandleAddListingObj {
|
||||
src: number;
|
||||
identifier: string;
|
||||
username: string;
|
||||
name: string;
|
||||
number: any;
|
||||
listing: MarketplaceListingBase;
|
||||
}
|
||||
|
||||
class _MarketplaceService {
|
||||
private readonly marketplaceDB: _MarketplaceDB;
|
||||
@@ -14,93 +30,104 @@ class _MarketplaceService {
|
||||
}
|
||||
|
||||
async handleAddListing(
|
||||
src: number,
|
||||
identifier: string,
|
||||
username: string,
|
||||
name: string,
|
||||
number: any,
|
||||
listing: MarketplaceListing,
|
||||
{ listing, src, identifier, number, name, username }: HandleAddListingObj,
|
||||
resp: PromiseEventReturnFunc,
|
||||
): Promise<void> {
|
||||
selloutLogger.debug('Handling add listing, listing:');
|
||||
selloutLogger.debug;
|
||||
selloutLogger.debug(listing);
|
||||
|
||||
try {
|
||||
await this.marketplaceDB.addListing(identifier, username, name, number, listing);
|
||||
const listingId = await this.marketplaceDB.addListing(
|
||||
identifier,
|
||||
username,
|
||||
name,
|
||||
number,
|
||||
listing,
|
||||
);
|
||||
// Respond with success to original source
|
||||
resp({ status: 'ok' });
|
||||
|
||||
emitNet(MarketplaceEvents.ADD_LISTING_SUCCESS, src);
|
||||
emitNet(MarketplaceEvents.ACTION_RESULT, src, {
|
||||
message: 'MARKETPLACE_CREATE_LISTING_SUCCESS',
|
||||
type: 'success',
|
||||
});
|
||||
const returnObj: MarketplaceListing = {
|
||||
id: listingId,
|
||||
identifier,
|
||||
name,
|
||||
number,
|
||||
url: listing.url,
|
||||
username,
|
||||
description: listing.description,
|
||||
title: listing.title,
|
||||
};
|
||||
// Broadcast to everyone we are adding a listing
|
||||
emitNet(MarketplaceEvents.BROADCAST_EVENT, -1, { type: 'ADD', listing: returnObj });
|
||||
} catch (e) {
|
||||
selloutLogger.error(`Failed to add listing ${e.message}`, {
|
||||
source: src,
|
||||
});
|
||||
|
||||
emitNet(MarketplaceEvents.ACTION_RESULT, src, {
|
||||
message: 'MARKETPLACE_CREATE_LISTING_FAILED',
|
||||
type: 'error',
|
||||
});
|
||||
resp({ err: true, errMsg: 'Failed to create listing' });
|
||||
}
|
||||
}
|
||||
|
||||
async handleFetchListings(src: number) {
|
||||
async handleFetchListings(resp: PromiseEventReturnFunc, src: number) {
|
||||
try {
|
||||
const listings = await this.marketplaceDB.fetchListings();
|
||||
|
||||
emitNet(MarketplaceEvents.SEND_LISTING, src, listings);
|
||||
resp({ data: listings, status: 'ok' });
|
||||
} catch (e) {
|
||||
selloutLogger.error(`Failed to fetch listings, ${e.message}`, {
|
||||
source: src,
|
||||
});
|
||||
resp({ err: true, errMsg: 'Failed to fetch listings' });
|
||||
}
|
||||
}
|
||||
|
||||
async handleDeleteListing(src: number, listingId: number): Promise<void> {
|
||||
const identifier = PlayerService.getIdentifier(src);
|
||||
async handleDeleteListing(
|
||||
reqObj: PromiseRequest<MarketplaceDeleteDTO>,
|
||||
resp: PromiseEventReturnFunc,
|
||||
): Promise<void> {
|
||||
const identifier = PlayerService.getIdentifier(reqObj.source);
|
||||
try {
|
||||
await this.marketplaceDB.deleteListing(listingId, identifier);
|
||||
await this.marketplaceDB.deleteListing(reqObj.data.id, identifier);
|
||||
|
||||
emitNet(MarketplaceEvents.DELETE_LISTING_SUCCESS, src);
|
||||
emitNet(MarketplaceEvents.ACTION_RESULT, src, {
|
||||
message: 'MARKETPLACE_DELETE_LISTING_SUCCESS',
|
||||
type: 'success',
|
||||
});
|
||||
resp({ status: 'ok' });
|
||||
|
||||
const returnObj: MarketplaceDeleteDTO = {
|
||||
id: reqObj.data.id,
|
||||
};
|
||||
|
||||
emitNet(MarketplaceEvents.BROADCAST_EVENT, -1, { type: 'DELETE', listing: returnObj });
|
||||
} catch (e) {
|
||||
notesLogger.error(`Error in handleDeleteListing, ${e.message}`);
|
||||
|
||||
emitNet(MarketplaceEvents.ACTION_RESULT, src, {
|
||||
message: 'MARKETPLACE_DELETE_LISTING_FAILED',
|
||||
type: 'error',
|
||||
});
|
||||
resp({ error: true, errMsg: 'Error in deleting listing' });
|
||||
}
|
||||
}
|
||||
|
||||
async handleReportListing(src: number, listing: MarketplaceListing): Promise<void> {
|
||||
async handleReportListing(
|
||||
reqObj: PromiseRequest<MarketplaceReportDTO>,
|
||||
resp: PromiseEventReturnFunc,
|
||||
): Promise<void> {
|
||||
try {
|
||||
const rListing = await this.marketplaceDB.getListing(listing.id);
|
||||
const reportExists = await this.marketplaceDB.doesReportExist(listing.id, listing.name);
|
||||
const rListing = await this.marketplaceDB.getListing(reqObj.data.id);
|
||||
const reportExists = await this.marketplaceDB.doesReportExist(
|
||||
reqObj.data.id,
|
||||
rListing.number,
|
||||
);
|
||||
|
||||
const reportingPlayer = GetPlayerName(reqObj.source.toString());
|
||||
|
||||
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',
|
||||
});
|
||||
resp({ error: true, errorMsg: 'Listing already exists' });
|
||||
}
|
||||
|
||||
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,
|
||||
source: reqObj.source,
|
||||
});
|
||||
resp({ error: true, errorMsg: 'Failed to report listing' });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user