diff --git a/phone/src/apps/marketplace/components/MarketplaceList/ListingActions.tsx b/phone/src/apps/marketplace/components/MarketplaceList/ListingActions.tsx index baccfb4..b37bb05 100644 --- a/phone/src/apps/marketplace/components/MarketplaceList/ListingActions.tsx +++ b/phone/src/apps/marketplace/components/MarketplaceList/ListingActions.tsx @@ -51,21 +51,21 @@ export const ListingActions: React.FC = ({ children, ...list }; const handleReportListing = () => { - fetchNui(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(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 = () => { diff --git a/phone/src/apps/marketplace/components/form/ListingForm.tsx b/phone/src/apps/marketplace/components/form/ListingForm.tsx index 5997802..293b153 100644 --- a/phone/src/apps/marketplace/components/form/ListingForm.tsx +++ b/phone/src/apps/marketplace/components/form/ListingForm.tsx @@ -56,7 +56,7 @@ export const ListingForm = () => { }).then(({ err, errMsg }) => { if (err) { return addAlert({ - message: t(errMsg), + message: t('errMsg'), type: 'error', }); } diff --git a/phone/src/apps/marketplace/hooks/state.ts b/phone/src/apps/marketplace/hooks/state.ts index 2e336ab..c5b2148 100644 --- a/phone/src/apps/marketplace/hooks/state.ts +++ b/phone/src/apps/marketplace/hooks/state.ts @@ -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({ key: 'defaultListings', get: async () => { try { - return await fetchNui(MarketplaceEvents.FETCH_LISTING); + return (await fetchNui(MarketplaceEvents.FETCH_LISTING)).data; } catch (e) { if (process.env.NODE_ENV === 'development' && !process.env.REACT_APP_IN_GAME) { return defaultData; diff --git a/phone/src/apps/marketplace/hooks/useMarketplaceService.ts b/phone/src/apps/marketplace/hooks/useMarketplaceService.ts index ba59fa0..5c1f987 100644 --- a/phone/src/apps/marketplace/hooks/useMarketplaceService.ts +++ b/phone/src/apps/marketplace/hooks/useMarketplaceService.ts @@ -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); }); } diff --git a/resources/client/cl_marketplace.ts b/resources/client/cl_marketplace.ts index 72fe64a..3faabcb 100644 --- a/resources/client/cl_marketplace.ts +++ b/resources/client/cl_marketplace.ts @@ -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); -// }); diff --git a/resources/server/marketplace/marketplace.controller.ts b/resources/server/marketplace/marketplace.controller.ts index c6dce16..c3ac565 100644 --- a/resources/server/marketplace/marketplace.controller.ts +++ b/resources/server/marketplace/marketplace.controller.ts @@ -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(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(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(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' }); + }); }); diff --git a/resources/server/marketplace/marketplace.db.ts b/resources/server/marketplace/marketplace.db.ts index f449fbe..8888b12 100644 --- a/resources/server/marketplace/marketplace.db.ts +++ b/resources/server/marketplace/marketplace.db.ts @@ -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 { + listing: MarketplaceListingBase, + ): Promise { 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 { diff --git a/resources/server/marketplace/marketplace.service.ts b/resources/server/marketplace/marketplace.service.ts index d9d5a7b..b1907c4 100644 --- a/resources/server/marketplace/marketplace.service.ts +++ b/resources/server/marketplace/marketplace.service.ts @@ -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 { 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 { - const identifier = PlayerService.getIdentifier(src); + async handleDeleteListing( + reqObj: PromiseRequest, + resp: PromiseEventReturnFunc, + ): Promise { + 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 { + async handleReportListing( + reqObj: PromiseRequest, + resp: PromiseEventReturnFunc, + ): Promise { 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' }); } } } diff --git a/typings/marketplace.ts b/typings/marketplace.ts index ea6805a..4b41197 100644 --- a/typings/marketplace.ts +++ b/typings/marketplace.ts @@ -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';