mirror of
https://github.com/darksaid98/new-phone-who-dis.git
synced 2026-12-03 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
@@ -51,21 +51,21 @@ export const ListingActions: React.FC<MarketplaceListing> = ({ children, ...list
|
||||
};
|
||||
|
||||
const handleReportListing = () => {
|
||||
fetchNui<MarketplaceActionResp>(MarketplaceEvents.DELETE_LISTING, listing).then(
|
||||
({ err, errMsg }) => {
|
||||
if (err) {
|
||||
return addAlert({
|
||||
message: t(errMsg),
|
||||
type: 'error',
|
||||
});
|
||||
}
|
||||
|
||||
addAlert({
|
||||
message: t('APPS_MARKETPLACE_REPORT_LISTING_SUCCESS'),
|
||||
type: 'success',
|
||||
fetchNui<MarketplaceActionResp>(MarketplaceEvents.REPORT_LISTING, {
|
||||
listingId: listing.id,
|
||||
}).then(({ err, errMsg }) => {
|
||||
if (err) {
|
||||
return addAlert({
|
||||
message: t(errMsg),
|
||||
type: 'error',
|
||||
});
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
addAlert({
|
||||
message: t('APPS_MARKETPLACE_REPORT_LISTING_SUCCESS'),
|
||||
type: 'success',
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
const handleCall = () => {
|
||||
|
||||
@@ -56,7 +56,7 @@ export const ListingForm = () => {
|
||||
}).then(({ err, errMsg }) => {
|
||||
if (err) {
|
||||
return addAlert({
|
||||
message: t(errMsg),
|
||||
message: t('errMsg'),
|
||||
type: 'error',
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
import { atom, selector, useRecoilValue, useSetRecoilState } from 'recoil';
|
||||
import { MarketplaceEvents, MarketplaceListing } from '../../../../../typings/marketplace';
|
||||
import {
|
||||
MarketplaceEvents,
|
||||
MarketplaceFetchResp,
|
||||
MarketplaceListing,
|
||||
} from '../../../../../typings/marketplace';
|
||||
import { fetchNui } from '../../../utils/fetchNui';
|
||||
|
||||
const defaultData: MarketplaceListing[] = [
|
||||
@@ -30,7 +34,7 @@ const listingState = atom<MarketplaceListing[]>({
|
||||
key: 'defaultListings',
|
||||
get: async () => {
|
||||
try {
|
||||
return await fetchNui<MarketplaceListing[]>(MarketplaceEvents.FETCH_LISTING);
|
||||
return (await fetchNui<MarketplaceFetchResp>(MarketplaceEvents.FETCH_LISTING)).data;
|
||||
} catch (e) {
|
||||
if (process.env.NODE_ENV === 'development' && !process.env.REACT_APP_IN_GAME) {
|
||||
return defaultData;
|
||||
|
||||
@@ -23,7 +23,6 @@ export const useMarketplaceService = () => {
|
||||
const deleteDTO = listing as MarketplaceDeleteDTO;
|
||||
return setListings((currVal) => {
|
||||
const arrayPos = currVal.map((item) => item.id).indexOf(deleteDTO.id);
|
||||
|
||||
return [...currVal].splice(arrayPos);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,47 +1,13 @@
|
||||
import { MarketplaceEvents, MarketplaceListingBroadcast } from '../../typings/marketplace';
|
||||
import { MarketplaceEvents, MarketPlaceBroadcastData } from '../../typings/marketplace';
|
||||
import { RegisterNuiProxy } from './cl_utils';
|
||||
import { sendMarketplaceEvent } from '../utils/messages';
|
||||
|
||||
// onNet(MarketplaceEvents.SEND_LISTING, (listings: MarketplaceListing[]) => {
|
||||
// sendMarketplaceEvent(MarketplaceEvents.SEND_LISTING, listings);
|
||||
// });
|
||||
|
||||
RegisterNuiProxy(MarketplaceEvents.FETCH_LISTING);
|
||||
RegisterNuiProxy(MarketplaceEvents.ADD_LISTING);
|
||||
|
||||
// RegisterNuiCallbackType(MarketplaceEvents.ADD_LISTING);
|
||||
// on(`__cfx_nui:${MarketplaceEvents.ADD_LISTING}`, (data: MarketplaceListing, cb: Function) => {
|
||||
// emitNet(MarketplaceEvents.ADD_LISTING, data);
|
||||
// cb();
|
||||
// });
|
||||
|
||||
RegisterNuiProxy(MarketplaceEvents.DELETE_LISTING);
|
||||
RegisterNuiProxy(MarketplaceEvents.REPORT_LISTING);
|
||||
|
||||
onNet(MarketplaceEvents.BROADCAST_EVENT, (broadcastEvent: MarketplaceListingBroadcast) => {
|
||||
onNet(MarketplaceEvents.BROADCAST_EVENT, (broadcastEvent: MarketPlaceBroadcastData) => {
|
||||
sendMarketplaceEvent(MarketplaceEvents.BROADCAST_EVENT, broadcastEvent);
|
||||
});
|
||||
|
||||
// RegisterNuiCallbackType(MarketplaceEvents.DELETE_LISTING);
|
||||
// on(`__cfx_nui:${MarketplaceEvents.DELETE_LISTING}`, (data: MarketplaceListing, cb: Function) => {
|
||||
// emitNet(MarketplaceEvents.DELETE_LISTING, data.id);
|
||||
// cb();
|
||||
// });
|
||||
|
||||
// RegisterNuiCallbackType(MarketplaceEvents.REPORT_LISTING);
|
||||
// on(`__cfx_nui:${MarketplaceEvents.REPORT_LISTING}`, (data: MarketplaceListing, cb: Function) => {
|
||||
// emitNet(MarketplaceEvents.REPORT_LISTING, data);
|
||||
// cb();
|
||||
// });
|
||||
|
||||
// onNet(MarketplaceEvents.ADD_LISTING_SUCCESS, () => {
|
||||
// emitNet(MarketplaceEvents.FETCH_LISTING);
|
||||
// });
|
||||
|
||||
// onNet(MarketplaceEvents.DELETE_LISTING_SUCCESS, () => {
|
||||
// emitNet(MarketplaceEvents.FETCH_LISTING);
|
||||
// });
|
||||
|
||||
// onNet(MarketplaceEvents.ACTION_RESULT, (alert: IAlertProps) => {
|
||||
// sendMarketplaceEvent(MarketplaceEvents.SEND_ALERT, alert);
|
||||
// });
|
||||
@@ -1,42 +1,59 @@
|
||||
import { MarketplaceEvents, MarketplaceListing } from '../../../typings/marketplace';
|
||||
import { getSource } from '../utils/miscUtils';
|
||||
import {
|
||||
MarketplaceEvents,
|
||||
MarketplaceListingBase,
|
||||
MarketplaceReportDTO,
|
||||
} from '../../../typings/marketplace';
|
||||
import PlayerService from '../players/player.service';
|
||||
import { selloutLogger } from './marketplace.utils';
|
||||
import MarketplaceService from './marketplace.service';
|
||||
import { onNetPromise } from '../utils/PromiseNetEvents/onNetPromise';
|
||||
import { MarketplaceDeleteDTO } from '../../../typings/marketplace';
|
||||
|
||||
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}`),
|
||||
);
|
||||
onNetPromise(MarketplaceEvents.FETCH_LISTING, async (reqObj, resp) => {
|
||||
MarketplaceService.handleFetchListings(resp, reqObj.source).catch((e) => {
|
||||
selloutLogger.error(
|
||||
`Error occurred in fetch listing event (${reqObj.source}), Error: ${e.message}`,
|
||||
);
|
||||
resp({ error: true, errorMsg: '`Error occurred in fetch listing event' });
|
||||
});
|
||||
});
|
||||
|
||||
onNet(MarketplaceEvents.ADD_LISTING, async (listing: MarketplaceListing) => {
|
||||
const src = getSource();
|
||||
const Player = PlayerService.getPlayer(src);
|
||||
|
||||
onNetPromise<MarketplaceListingBase>(MarketplaceEvents.ADD_LISTING, async (reqObj, resp) => {
|
||||
const player = PlayerService.getPlayer(reqObj.source);
|
||||
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}`),
|
||||
);
|
||||
{
|
||||
src: reqObj.source,
|
||||
name: player.getName(),
|
||||
username: player.username,
|
||||
number: player.getPhoneNumber(),
|
||||
listing: reqObj.data,
|
||||
identifier: player.getIdentifier(),
|
||||
},
|
||||
resp,
|
||||
).catch((e) => {
|
||||
selloutLogger.error(
|
||||
`Error occurred in add listing event (${reqObj.source}), Error: ${e.message}`,
|
||||
);
|
||||
resp({ error: true, errorMsg: 'Error occurred in add listing event' });
|
||||
});
|
||||
});
|
||||
|
||||
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}`),
|
||||
);
|
||||
onNetPromise<MarketplaceDeleteDTO>(MarketplaceEvents.DELETE_LISTING, async (reqObj, resp) => {
|
||||
// TODO: Needs a permission check of some sort here eventually
|
||||
MarketplaceService.handleDeleteListing(reqObj, resp).catch((e) => {
|
||||
selloutLogger.error(
|
||||
`Error occurred in delete listing event (${reqObj.source}), Error: ${e.message}`,
|
||||
);
|
||||
resp({ error: true, errorMsg: 'Error occurred in delete listing event' });
|
||||
});
|
||||
});
|
||||
|
||||
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}`),
|
||||
);
|
||||
onNetPromise<MarketplaceReportDTO>(MarketplaceEvents.REPORT_LISTING, async (reqObj, resp) => {
|
||||
// TODO: Needs a permission check of some sort here eventually
|
||||
MarketplaceService.handleReportListing(reqObj, resp).catch((e) => {
|
||||
selloutLogger.error(
|
||||
`Error occurred in report listing event (${reqObj.source}), Error: ${e.message}`,
|
||||
);
|
||||
resp({ error: true, errorMsg: 'Error occured in report listing event' });
|
||||
});
|
||||
});
|
||||
@@ -1,5 +1,6 @@
|
||||
import { pool } from '../db';
|
||||
import { MarketplaceListing } from '../../../typings/marketplace';
|
||||
import { MarketplaceListing, MarketplaceListingBase } from '../../../typings/marketplace';
|
||||
import { ResultSetHeader } from 'mysql2';
|
||||
|
||||
export class _MarketplaceDB {
|
||||
async addListing(
|
||||
@@ -7,12 +8,12 @@ export class _MarketplaceDB {
|
||||
username: string,
|
||||
name: string,
|
||||
number: any,
|
||||
listing: MarketplaceListing,
|
||||
): Promise<void> {
|
||||
listing: MarketplaceListingBase,
|
||||
): Promise<number> {
|
||||
const query =
|
||||
'INSERT INTO npwd_marketplace_listings (identifier, username, name, number, title, url, description) VALUES (?, ?, ?, ?, ?, ?, ?)';
|
||||
|
||||
await pool.query(query, [
|
||||
const [result] = await pool.query(query, [
|
||||
identifier,
|
||||
username,
|
||||
name,
|
||||
@@ -21,6 +22,10 @@ export class _MarketplaceDB {
|
||||
listing.url,
|
||||
listing.description,
|
||||
]);
|
||||
|
||||
const resultCast = result as ResultSetHeader;
|
||||
|
||||
return resultCast.insertId;
|
||||
}
|
||||
|
||||
async fetchListings(): Promise<MarketplaceListing[]> {
|
||||
|
||||
@@ -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' });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,6 +12,10 @@ export interface MarketplaceListingBase {
|
||||
description: string;
|
||||
}
|
||||
|
||||
export interface MarketplaceFetchResp extends MarketplaceActionResp {
|
||||
data: MarketplaceListing[];
|
||||
}
|
||||
|
||||
export interface MarketplaceActionResp {
|
||||
err: boolean;
|
||||
errMsg?: string;
|
||||
@@ -36,6 +40,10 @@ export interface MarketplaceDeleteDTO {
|
||||
id: number;
|
||||
}
|
||||
|
||||
export interface MarketplaceReportDTO {
|
||||
id: number;
|
||||
}
|
||||
|
||||
export type MarketPlaceBroadcastData =
|
||||
| {
|
||||
type: 'ADD';
|
||||
|
||||
Reference in new issue
Block a user