From 5e225ca7360bc2913a36eb83410c6851f3516e84 Mon Sep 17 00:00:00 2001 From: Taso Date: Tue, 8 Jun 2021 16:33:36 -0400 Subject: [PATCH] tweak(marketplace): Tweak marketplace to match current fetching std --- .../apps/contacts/components/ContactsApp.tsx | 6 +- .../MarketplaceList/ListingActions.tsx | 23 +++--- .../MarketplaceListContainer.tsx | 10 +-- phone/src/apps/marketplace/hooks/state.ts | 8 ++- .../hooks/useMarketplaceActions.ts | 28 ++++++++ .../hooks/useMarketplaceService.ts | 40 +++++------ phone/src/locale/ba.json | 2 +- phone/src/locale/en.json | 2 +- phone/src/locale/es.json | 2 +- phone/src/locale/fr.json | 2 +- phone/src/locale/hu.json | 2 +- phone/src/locale/no.json | 2 +- phone/src/locale/pt.json | 2 +- phone/src/locale/ptbr.json | 2 +- phone/src/locale/sv.json | 2 +- phone/src/locale/tr.json | 2 +- phone/src/os/apps/config/apps.tsx | 11 ++- resources/client/cl_main.ts | 6 -- resources/client/cl_marketplace.ts | 14 +++- .../marketplace/marketplace.controller.ts | 29 +++----- .../server/marketplace/marketplace.service.ts | 70 +++++++++---------- resources/utils/apps.ts | 2 +- typings/marketplace.ts | 17 ++--- 23 files changed, 148 insertions(+), 136 deletions(-) create mode 100644 phone/src/apps/marketplace/hooks/useMarketplaceActions.ts diff --git a/phone/src/apps/contacts/components/ContactsApp.tsx b/phone/src/apps/contacts/components/ContactsApp.tsx index 9d76932..a48d7b4 100644 --- a/phone/src/apps/contacts/components/ContactsApp.tsx +++ b/phone/src/apps/contacts/components/ContactsApp.tsx @@ -3,13 +3,15 @@ import { AppWrapper } from '../../../ui/components'; import { AppTitle } from '../../../ui/components/AppTitle'; import { AppContent } from '../../../ui/components/AppContent'; import { useApp } from '../../../os/apps/hooks/useApps'; -import InjectDebugData from '../../../os/debug/InjectDebugData'; import { Route } from 'react-router-dom'; import ContactsInfoPage from './views/ContactInfo'; import { ContactPage } from './views/ContactsPage'; import { ContactsThemeProvider } from '../providers/ContactsThemeProvider'; -import { ContactEvents } from '../../../../../typings/contact'; import { LoadingSpinner } from '../../../ui/components/LoadingSpinner'; +import { makeStyles } from '@material-ui/core/styles'; +import { useHistory } from 'react-router'; +import { Fab, Theme } from '@material-ui/core'; +import PersonAddIcon from '@material-ui/icons/PersonAdd'; const useStyles = makeStyles((theme: Theme) => ({ absolute: { diff --git a/phone/src/apps/marketplace/components/MarketplaceList/ListingActions.tsx b/phone/src/apps/marketplace/components/MarketplaceList/ListingActions.tsx index b37bb05..ef2058e 100644 --- a/phone/src/apps/marketplace/components/MarketplaceList/ListingActions.tsx +++ b/phone/src/apps/marketplace/components/MarketplaceList/ListingActions.tsx @@ -1,11 +1,7 @@ import { Box, Tooltip, Button } from '@material-ui/core'; import { makeStyles, Theme } from '@material-ui/core/styles'; import React from 'react'; -import { - MarketplaceActionResp, - MarketplaceEvents, - MarketplaceListing, -} from '../../../../../../typings/marketplace'; +import { MarketplaceEvents, MarketplaceListing } from '../../../../../../typings/marketplace'; import { useSimcard } from '../../../../os/simcard/hooks/useSimcard'; import DeleteIcon from '@material-ui/icons/Delete'; import ReportIcon from '@material-ui/icons/Report'; @@ -17,6 +13,7 @@ import { useHistory } from 'react-router'; import { CallEvents } from '../../../../../../typings/call'; import { fetchNui } from '../../../../utils/fetchNui'; import { useSnackbar } from '../../../../ui/hooks/useSnackbar'; +import { ServerPromiseResp } from '../../../../../../typings/common'; const useStyles = makeStyles((theme: Theme) => ({ icon: { @@ -33,12 +30,12 @@ export const ListingActions: React.FC = ({ children, ...list const { addAlert } = useSnackbar(); const handleDeleteListing = () => { - fetchNui(MarketplaceEvents.DELETE_LISTING, { + fetchNui(MarketplaceEvents.DELETE_LISTING, { id: listing.id, - }).then(({ err, errMsg }) => { - if (err) { + }).then((resp) => { + if (resp.status !== 'ok') { return addAlert({ - message: t(errMsg), + message: t('APPS_MARKETPLACE_DELETE_LISTING_FAILED'), type: 'error', }); } @@ -51,12 +48,12 @@ export const ListingActions: React.FC = ({ children, ...list }; const handleReportListing = () => { - fetchNui(MarketplaceEvents.REPORT_LISTING, { + fetchNui(MarketplaceEvents.REPORT_LISTING, { listingId: listing.id, - }).then(({ err, errMsg }) => { - if (err) { + }).then((resp) => { + if (resp.status !== 'ok') { return addAlert({ - message: t(errMsg), + message: t('APPS_MARKETPLACE_REPORT_LISTING_FAILED'), type: 'error', }); } diff --git a/phone/src/apps/marketplace/components/MarketplaceList/MarketplaceListContainer.tsx b/phone/src/apps/marketplace/components/MarketplaceList/MarketplaceListContainer.tsx index a64af09..3f3d681 100644 --- a/phone/src/apps/marketplace/components/MarketplaceList/MarketplaceListContainer.tsx +++ b/phone/src/apps/marketplace/components/MarketplaceList/MarketplaceListContainer.tsx @@ -1,16 +1,10 @@ import React from 'react'; import { MarketplaceList } from './MarketplaceList'; -import { Box, CircularProgress } from '@material-ui/core'; - -const LoadingListings: React.FC = () => ( - - - -); +import { LoadingSpinner } from '../../../../ui/components/LoadingSpinner'; export const MarketplaceListContainer: React.FC = () => { return ( - }> + }> ); diff --git a/phone/src/apps/marketplace/hooks/state.ts b/phone/src/apps/marketplace/hooks/state.ts index c5b2148..82ba787 100644 --- a/phone/src/apps/marketplace/hooks/state.ts +++ b/phone/src/apps/marketplace/hooks/state.ts @@ -1,10 +1,11 @@ -import { atom, selector, useRecoilValue, useSetRecoilState } from 'recoil'; +import { atom, selector, useRecoilState, useRecoilValue, useSetRecoilState } from 'recoil'; import { MarketplaceEvents, MarketplaceFetchResp, MarketplaceListing, } from '../../../../../typings/marketplace'; import { fetchNui } from '../../../utils/fetchNui'; +import { isEnvBrowser } from '../../../utils/misc'; const defaultData: MarketplaceListing[] = [ { @@ -36,9 +37,11 @@ const listingState = atom({ try { return (await fetchNui(MarketplaceEvents.FETCH_LISTING)).data; } catch (e) { - if (process.env.NODE_ENV === 'development' && !process.env.REACT_APP_IN_GAME) { + if (isEnvBrowser()) { return defaultData; } + console.error(e); + return []; } }, }), @@ -46,3 +49,4 @@ const listingState = atom({ export const useListingValue = () => useRecoilValue(listingState); export const useSetListings = () => useSetRecoilState(listingState); +export const useListings = () => useRecoilState(listingState); diff --git a/phone/src/apps/marketplace/hooks/useMarketplaceActions.ts b/phone/src/apps/marketplace/hooks/useMarketplaceActions.ts new file mode 100644 index 0000000..762c100 --- /dev/null +++ b/phone/src/apps/marketplace/hooks/useMarketplaceActions.ts @@ -0,0 +1,28 @@ +import { useCallback } from 'react'; +import { useSetListings } from './state'; +import { MarketplaceListing } from '../../../../../typings/marketplace'; + +interface MarketplaceActionValues { + deleteListing: (id: number) => void; + addListing: (listing: MarketplaceListing) => void; +} + +export const useMarketplaceActions = (): MarketplaceActionValues => { + const setListings = useSetListings(); + + const deleteListing = useCallback( + (id: number) => { + setListings((curListings) => [...curListings].filter((listing) => listing.id !== id)); + }, + [setListings], + ); + + const addListing = useCallback( + (listing: MarketplaceListing) => { + setListings((curListings) => [...curListings, listing]); + }, + [setListings], + ); + + return { deleteListing, addListing }; +}; diff --git a/phone/src/apps/marketplace/hooks/useMarketplaceService.ts b/phone/src/apps/marketplace/hooks/useMarketplaceService.ts index 5c1f987..83d16f6 100644 --- a/phone/src/apps/marketplace/hooks/useMarketplaceService.ts +++ b/phone/src/apps/marketplace/hooks/useMarketplaceService.ts @@ -1,31 +1,31 @@ import { useNuiEvent } from 'fivem-nui-react-lib'; import { - MarketPlaceBroadcastData, + MarketplaceBroadcastAddDTO, MarketplaceDeleteDTO, MarketplaceEvents, - MarketplaceListing, } from '../../../../../typings/marketplace'; -import { useSetListings } from './state'; +import { useMarketplaceActions } from './useMarketplaceActions'; export const useMarketplaceService = () => { - const setListings = useSetListings(); + const { addListing, deleteListing } = useMarketplaceActions(); - useNuiEvent( - 'SELLOUT', - MarketplaceEvents.BROADCAST_EVENT, - ({ type, listing }) => { - if (type === 'ADD') { - const typeCastListing = listing as MarketplaceListing; - return setListings((curState) => [...curState, typeCastListing]); - } + const addListingHandler = (resp: MarketplaceBroadcastAddDTO) => { + addListing(resp.listing); + }; - if (type === 'DELETE') { - const deleteDTO = listing as MarketplaceDeleteDTO; - return setListings((currVal) => { - const arrayPos = currVal.map((item) => item.id).indexOf(deleteDTO.id); - return [...currVal].splice(arrayPos); - }); - } - }, + const deleteListingHandler = (listing: MarketplaceDeleteDTO) => { + deleteListing(listing.id); + }; + + useNuiEvent( + 'MARKETPLACE', + MarketplaceEvents.BROADCAST_ADD, + addListingHandler, + ); + + useNuiEvent( + 'MARKETPLACE', + MarketplaceEvents.BROADCAST_DELETE, + deleteListingHandler, ); }; diff --git a/phone/src/locale/ba.json b/phone/src/locale/ba.json index 03b83e8..8c9a2de 100644 --- a/phone/src/locale/ba.json +++ b/phone/src/locale/ba.json @@ -14,7 +14,7 @@ "APPS_NOTES": "Bilješke", "APPS_EXAMPLE": "Primjerna Aplikacija", "APPS_TWITTER": "Twitter", - "APPS_SELLOUT": "Market", + "APPS_MARKETPLACE": "Market", "APPS_CAMERA": "Camera", "APPS_DIALER_INCOMING_CALL_TITLE": "Dolazi telefonski poziv", diff --git a/phone/src/locale/en.json b/phone/src/locale/en.json index a59abbf..9b28455 100644 --- a/phone/src/locale/en.json +++ b/phone/src/locale/en.json @@ -59,7 +59,7 @@ "APPS_NOTES": "Notes", "APPS_EXAMPLE": "Example app", "APPS_TWITTER": "Twitter", - "APPS_SELLOUT": "Marketplace", + "APPS_MARKETPLACE": "Marketplace", "APPS_CAMERA": "Camera", "APPS_CAMERA_FAILED_TO_TAKE_PHOTO": "Photo could not be saved :(", diff --git a/phone/src/locale/es.json b/phone/src/locale/es.json index e584923..088e1a4 100644 --- a/phone/src/locale/es.json +++ b/phone/src/locale/es.json @@ -58,7 +58,7 @@ "APPS_NOTES": "Notas", "APPS_EXAMPLE": "App de Ejemplo", "APPS_TWITTER": "Twitter", - "APPS_SELLOUT": "Mercado", + "APPS_MARKETPLACE": "Mercado", "APPS_CAMERA": "Camara", "APPS_DIALER_INCOMING_CALL_TITLE": "Llamada entrante", diff --git a/phone/src/locale/fr.json b/phone/src/locale/fr.json index 9926a80..66d11e2 100644 --- a/phone/src/locale/fr.json +++ b/phone/src/locale/fr.json @@ -59,7 +59,7 @@ "APPS_NOTES": "Notes", "APPS_EXAMPLE": "App test", "APPS_TWITTER": "Twitter", - "APPS_SELLOUT": "Marché", + "APPS_MARKETPLACE": "Marché", "APPS_CAMERA": "Caméra", "APPS_DIALER_INCOMING_CALL_TITLE": "Appel entrant", diff --git a/phone/src/locale/hu.json b/phone/src/locale/hu.json index 7a8b26d..b49092f 100644 --- a/phone/src/locale/hu.json +++ b/phone/src/locale/hu.json @@ -59,7 +59,7 @@ "APPS_NOTES": "Jegyzetek", "APPS_EXAMPLE": "Péla app", "APPS_TWITTER": "Twitter", - "APPS_SELLOUT": "Pia", + "APPS_MARKETPLACE": "Pia", "APPS_CAMERA": "Kamera", "APPS_CAMERA_FAILED_TO_TAKE_PHOTO": "Nem lehet elmenteni a képet :(", diff --git a/phone/src/locale/no.json b/phone/src/locale/no.json index c4ca458..66bdc35 100644 --- a/phone/src/locale/no.json +++ b/phone/src/locale/no.json @@ -52,7 +52,7 @@ "APPS_NOTES": "Notater", "APPS_EXAMPLE": "Example app", "APPS_TWITTER": "Twitter", - "APPS_SELLOUT": "Marked", + "APPS_MARKETPLACE": "Marked", "APPS_CAMERA": "Kamera", "APPS_DIALER_INCOMING_CALL_TITLE": "Innkommende Anrop", diff --git a/phone/src/locale/pt.json b/phone/src/locale/pt.json index 923ce7a..9dfa94f 100644 --- a/phone/src/locale/pt.json +++ b/phone/src/locale/pt.json @@ -14,7 +14,7 @@ "APPS_NOTES": "Notas", "APPS_EXAMPLE": "Aplicativo exemplo", "APPS_TWITTER": "Twitter", - "APPS_SELLOUT": "Market", + "APPS_MARKETPLACE": "Market", "APPS_CAMERA": "Camera", "APPS_DIALER_INCOMING_CALL_TITLE": "Incoming Phone Call", diff --git a/phone/src/locale/ptbr.json b/phone/src/locale/ptbr.json index fb5cd2a..c62a49f 100644 --- a/phone/src/locale/ptbr.json +++ b/phone/src/locale/ptbr.json @@ -59,7 +59,7 @@ "APPS_NOTES": "Notas", "APPS_EXAMPLE": "Aplicativo de exemplo", "APPS_TWITTER": "Twitter", - "APPS_SELLOUT": "Mercado", + "APPS_MARKETPLACE": "Mercado", "APPS_CAMERA": "Camera", "APPS_CAMERA_FAILED_TO_TAKE_PHOTO": "Não foi possível salvar a foto :(", diff --git a/phone/src/locale/sv.json b/phone/src/locale/sv.json index ed8947d..84bf501 100644 --- a/phone/src/locale/sv.json +++ b/phone/src/locale/sv.json @@ -14,7 +14,7 @@ "APPS_NOTES": "Anteckningar", "APPS_EXAMPLE": "Exempel app", "APPS_TWITTER": "Twitter", - "APPS_SELLOUT": "Market", + "APPS_MARKETPLACE": "Market", "APPS_CAMERA": "Camera", "APPS_DIALER_INCOMING_CALL_TITLE": "Incoming Phone Call", diff --git a/phone/src/locale/tr.json b/phone/src/locale/tr.json index 239ff34..9e91a81 100644 --- a/phone/src/locale/tr.json +++ b/phone/src/locale/tr.json @@ -14,7 +14,7 @@ "APPS_NOTES": "Notlar", "APPS_EXAMPLE": "Örnek uygulama", "APPS_TWITTER": "Twitter", - "APPS_SELLOUT": "Market", + "APPS_MARKETPLACE": "Market", "APPS_CAMERA": "Camera", "APPS_DIALER_INCOMING_CALL_TITLE": "Incoming Phone Call", diff --git a/phone/src/os/apps/config/apps.tsx b/phone/src/os/apps/config/apps.tsx index bda8970..e83121f 100644 --- a/phone/src/os/apps/config/apps.tsx +++ b/phone/src/os/apps/config/apps.tsx @@ -127,13 +127,18 @@ export const APPS: IAppConfig[] = [ Route: () => , }, { - id: 'SELLOUT', - nameLocale: 'APPS_SELLOUT', + id: 'MARKETPLACE', + nameLocale: 'APPS_MARKETPLACE', backgroundColor: SELLOUT_APP_PRIMARY_COLOR, color: SELLOUT_APP_ICON_COLOR, path: '/marketplace', Route: () => ( - + ), }, { diff --git a/resources/client/cl_main.ts b/resources/client/cl_main.ts index c88ed79..c850aec 100644 --- a/resources/client/cl_main.ts +++ b/resources/client/cl_main.ts @@ -143,12 +143,6 @@ on(`__cfx_nui:${PhoneEvents.UI_IS_READY}`, (_data: any, cb: Function) => { * NUI Service Callback Registration * * * * * * * * * * * * * */ -RegisterNuiCallbackType(PhoneEvents.OPEN_APP_LISTINGS); -on(`__cfx_nui:${PhoneEvents.OPEN_APP_LISTINGS}`, (data: any, cb: Function) => { - emitNet(MarketplaceEvents.FETCH_LISTING); - cb(); -}); - RegisterNuiCallbackType(PhoneEvents.OPEN_APP_NOTES); on(`__cfx_nui:${PhoneEvents.OPEN_APP_NOTES}`, (data: any, cb: Function) => { emitNet(NotesEvents.FETCH_ALL_NOTES); diff --git a/resources/client/cl_marketplace.ts b/resources/client/cl_marketplace.ts index 3faabcb..048b1c7 100644 --- a/resources/client/cl_marketplace.ts +++ b/resources/client/cl_marketplace.ts @@ -1,4 +1,8 @@ -import { MarketplaceEvents, MarketPlaceBroadcastData } from '../../typings/marketplace'; +import { + MarketplaceBroadcastAddDTO, + MarketplaceDeleteDTO, + MarketplaceEvents, +} from '../../typings/marketplace'; import { RegisterNuiProxy } from './cl_utils'; import { sendMarketplaceEvent } from '../utils/messages'; @@ -8,6 +12,10 @@ RegisterNuiProxy(MarketplaceEvents.ADD_LISTING); RegisterNuiProxy(MarketplaceEvents.DELETE_LISTING); RegisterNuiProxy(MarketplaceEvents.REPORT_LISTING); -onNet(MarketplaceEvents.BROADCAST_EVENT, (broadcastEvent: MarketPlaceBroadcastData) => { - sendMarketplaceEvent(MarketplaceEvents.BROADCAST_EVENT, broadcastEvent); +onNet(MarketplaceEvents.BROADCAST_ADD, (broadcastEvent: MarketplaceBroadcastAddDTO) => { + sendMarketplaceEvent(MarketplaceEvents.BROADCAST_ADD, broadcastEvent); +}); + +onNet(MarketplaceEvents.BROADCAST_DELETE, (broadcastEvent: MarketplaceDeleteDTO) => { + sendMarketplaceEvent(MarketplaceEvents.BROADCAST_DELETE, broadcastEvent); }); diff --git a/resources/server/marketplace/marketplace.controller.ts b/resources/server/marketplace/marketplace.controller.ts index c3ac565..7dd7059 100644 --- a/resources/server/marketplace/marketplace.controller.ts +++ b/resources/server/marketplace/marketplace.controller.ts @@ -1,40 +1,29 @@ import { MarketplaceEvents, + MarketplaceListing, 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'; -onNetPromise(MarketplaceEvents.FETCH_LISTING, async (reqObj, resp) => { - MarketplaceService.handleFetchListings(resp, reqObj.source).catch((e) => { +onNetPromise(MarketplaceEvents.FETCH_LISTING, async (reqObj, resp) => { + MarketplaceService.handleFetchListings(reqObj, resp).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' }); + resp({ status: 'error', errorMsg: 'INTERNAL_ERROR' }); }); }); -onNetPromise(MarketplaceEvents.ADD_LISTING, async (reqObj, resp) => { - const player = PlayerService.getPlayer(reqObj.source); - MarketplaceService.handleAddListing( - { - src: reqObj.source, - name: player.getName(), - username: player.username, - number: player.getPhoneNumber(), - listing: reqObj.data, - identifier: player.getIdentifier(), - }, - resp, - ).catch((e) => { +onNetPromise(MarketplaceEvents.ADD_LISTING, async (reqObj, resp) => { + MarketplaceService.handleAddListing(reqObj, 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' }); + resp({ status: 'error', errorMsg: 'INTERNAL_ERROR' }); }); }); @@ -44,7 +33,7 @@ onNetPromise(MarketplaceEvents.DELETE_LISTING, async (reqO selloutLogger.error( `Error occurred in delete listing event (${reqObj.source}), Error: ${e.message}`, ); - resp({ error: true, errorMsg: 'Error occurred in delete listing event' }); + resp({ status: 'error', errorMsg: 'INTERNAL_ERROR' }); }); }); @@ -54,6 +43,6 @@ onNetPromise(MarketplaceEvents.REPORT_LISTING, async (reqO selloutLogger.error( `Error occurred in report listing event (${reqObj.source}), Error: ${e.message}`, ); - resp({ error: true, errorMsg: 'Error occured in report listing event' }); + resp({ status: 'error', errorMsg: 'INTERNAL_ERROR' }); }); }); diff --git a/resources/server/marketplace/marketplace.service.ts b/resources/server/marketplace/marketplace.service.ts index b1907c4..0e03149 100644 --- a/resources/server/marketplace/marketplace.service.ts +++ b/resources/server/marketplace/marketplace.service.ts @@ -10,16 +10,7 @@ import { } 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; -} +import { PromiseEventResp, PromiseRequest } from '../utils/PromiseNetEvents/promise.types'; class _MarketplaceService { private readonly marketplaceDB: _MarketplaceDB; @@ -30,60 +21,65 @@ class _MarketplaceService { } async handleAddListing( - { listing, src, identifier, number, name, username }: HandleAddListingObj, - resp: PromiseEventReturnFunc, + reqObj: PromiseRequest, + resp: PromiseEventResp, ): Promise { selloutLogger.debug('Handling add listing, listing:'); - selloutLogger.debug(listing); + selloutLogger.debug(reqObj.data); + + const player = PlayerService.getPlayer(reqObj.source); try { const listingId = await this.marketplaceDB.addListing( - identifier, - username, - name, - number, - listing, + player.getIdentifier(), + player.username, + player.getName(), + player.getPhoneNumber(), + reqObj.data, ); // Respond with success to original source resp({ status: 'ok' }); const returnObj: MarketplaceListing = { id: listingId, - identifier, - name, - number, - url: listing.url, - username, - description: listing.description, - title: listing.title, + identifier: player.getIdentifier(), + name: player.getName(), + number: player.getPhoneNumber(), + url: reqObj.data.url, + username: player.username, + description: reqObj.data.description, + title: reqObj.data.title, }; // Broadcast to everyone we are adding a listing - emitNet(MarketplaceEvents.BROADCAST_EVENT, -1, { type: 'ADD', listing: returnObj }); + emitNet(MarketplaceEvents.BROADCAST_ADD, -1, { type: 'ADD', listing: returnObj }); } catch (e) { selloutLogger.error(`Failed to add listing ${e.message}`, { - source: src, + source: reqObj.source, }); - resp({ err: true, errMsg: 'Failed to create listing' }); + resp({ status: 'error', errorMsg: 'DB_ERROR' }); } } - async handleFetchListings(resp: PromiseEventReturnFunc, src: number) { + async handleFetchListings( + req: PromiseRequest, + resp: PromiseEventResp, + ) { try { const listings = await this.marketplaceDB.fetchListings(); resp({ data: listings, status: 'ok' }); } catch (e) { selloutLogger.error(`Failed to fetch listings, ${e.message}`, { - source: src, + source: req.source, }); - resp({ err: true, errMsg: 'Failed to fetch listings' }); + resp({ status: 'error', errorMsg: 'DB_ERROR' }); } } async handleDeleteListing( reqObj: PromiseRequest, - resp: PromiseEventReturnFunc, + resp: PromiseEventResp, ): Promise { const identifier = PlayerService.getIdentifier(reqObj.source); try { @@ -95,17 +91,17 @@ class _MarketplaceService { id: reqObj.data.id, }; - emitNet(MarketplaceEvents.BROADCAST_EVENT, -1, { type: 'DELETE', listing: returnObj }); + emitNet(MarketplaceEvents.BROADCAST_DELETE, -1, returnObj); } catch (e) { notesLogger.error(`Error in handleDeleteListing, ${e.message}`); - resp({ error: true, errMsg: 'Error in deleting listing' }); + resp({ status: 'error', errorMsg: 'DB_ERROR' }); } } async handleReportListing( reqObj: PromiseRequest, - resp: PromiseEventReturnFunc, + resp: PromiseEventResp, ): Promise { try { const rListing = await this.marketplaceDB.getListing(reqObj.data.id); @@ -118,7 +114,7 @@ class _MarketplaceService { if (reportExists) { selloutLogger.error(`This listing has already been reported`); - resp({ error: true, errorMsg: 'Listing already exists' }); + resp({ status: 'error', errorMsg: 'REPORT_EXISTS' }); } await this.marketplaceDB.reportListing(rListing.id, rListing.name); @@ -127,7 +123,7 @@ class _MarketplaceService { selloutLogger.error(`Failed to report listing ${e.message}`, { source: reqObj.source, }); - resp({ error: true, errorMsg: 'Failed to report listing' }); + resp({ status: 'error', errorMsg: 'DB_ERROR' }); } } } diff --git a/resources/utils/apps.ts b/resources/utils/apps.ts index dbc326d..2f5295e 100644 --- a/resources/utils/apps.ts +++ b/resources/utils/apps.ts @@ -3,7 +3,7 @@ export default { MATCH: 'MATCH', MESSAGES: 'MESSAGES', NOTES: 'NOTES', - MARKETPLACE: 'SELLOUT', + MARKETPLACE: 'MARKETPLACE', CONTACTS: 'CONTACTS', CAMERA: 'CAMERA', }; diff --git a/typings/marketplace.ts b/typings/marketplace.ts index 4b41197..f70064b 100644 --- a/typings/marketplace.ts +++ b/typings/marketplace.ts @@ -33,7 +33,12 @@ export enum MarketplaceEvents { REPORT_LISTING_SUCCESS = 'npwd:reportListingSuccess', REPORT_LISTING_SUCESS = 'npwd:reportListingFailed', SEND_ALERT = 'nwpd:sendListingAlert', - BROADCAST_EVENT = 'npwd:sendMarketplaceBroadcast', + BROADCAST_ADD = 'npwd:sendMarketplaceBroadcastAdd', + BROADCAST_DELETE = 'npwd:sendMarketplaceBroadcastDelete', +} + +export interface MarketplaceBroadcastAddDTO { + listing: MarketplaceListing; } export interface MarketplaceDeleteDTO { @@ -43,13 +48,3 @@ export interface MarketplaceDeleteDTO { export interface MarketplaceReportDTO { id: number; } - -export type MarketPlaceBroadcastData = - | { - type: 'ADD'; - listing: MarketplaceListing; - } - | { - type: 'DELETE'; - listing: MarketplaceDeleteDTO; - };