mirror of
https://github.com/darksaid98/new-phone-who-dis.git
synced 2026-12-04 14:38:45 +00:00
refactor(contacts): Refactor contacts to efficient data fetching
This commit is contained in:
1 parent
1940833f56
commit
ecb71cc359
29 files changed
+417
-352
No files matched your search
@@ -1,40 +1,7 @@
|
||||
import { ContactEvents, PreDBContact } from '../../typings/contact';
|
||||
import { sendContactsEvent } from '../utils/messages';
|
||||
import { ContactEvents } from '../../typings/contact';
|
||||
import { RegisterNuiProxy } from './cl_utils';
|
||||
|
||||
onNet(ContactEvents.SEND_CONTACTS, (contacts: any) => {
|
||||
sendContactsEvent(ContactEvents.SEND_CONTACTS, contacts);
|
||||
});
|
||||
|
||||
RegisterNuiCallbackType(ContactEvents.ADD_CONTACT);
|
||||
on(`__cfx_nui:${ContactEvents.ADD_CONTACT}`, (data: PreDBContact, cb: Function) => {
|
||||
emitNet(ContactEvents.ADD_CONTACT, data);
|
||||
cb();
|
||||
});
|
||||
|
||||
onNet(ContactEvents.ADD_CONTACT_SUCCESS, () => {
|
||||
emitNet(ContactEvents.GET_CONTACTS);
|
||||
});
|
||||
|
||||
RegisterNuiCallbackType(ContactEvents.UPDATE_CONTACT);
|
||||
on(`__cfx_nui:${ContactEvents.UPDATE_CONTACT}`, (data: any, cb: Function) => {
|
||||
emitNet(ContactEvents.UPDATE_CONTACT, data);
|
||||
cb();
|
||||
});
|
||||
|
||||
onNet(ContactEvents.UPDATE_CONTACT_SUCCESS, () => {
|
||||
emitNet(ContactEvents.GET_CONTACTS);
|
||||
});
|
||||
|
||||
RegisterNuiCallbackType(ContactEvents.DELETE_CONTACT);
|
||||
on(`__cfx_nui:${ContactEvents.DELETE_CONTACT}`, (data: any, cb: Function) => {
|
||||
emitNet(ContactEvents.DELETE_CONTACT, data);
|
||||
cb();
|
||||
});
|
||||
|
||||
onNet(ContactEvents.DELETE_CONTACT_SUCCESS, () => {
|
||||
emitNet(ContactEvents.GET_CONTACTS);
|
||||
});
|
||||
|
||||
onNet(ContactEvents.ACTION_RESULT, (alert: any) => {
|
||||
sendContactsEvent(ContactEvents.SEND_ALERT, alert);
|
||||
});
|
||||
RegisterNuiProxy(ContactEvents.GET_CONTACTS);
|
||||
RegisterNuiProxy(ContactEvents.ADD_CONTACT);
|
||||
RegisterNuiProxy(ContactEvents.DELETE_CONTACT);
|
||||
RegisterNuiProxy(ContactEvents.UPDATE_CONTACT);
|
||||
@@ -12,7 +12,7 @@ interface ISettingsParams {
|
||||
export default class ClientUtils {
|
||||
private _settings: ISettings;
|
||||
private _defaultSettings: ISettings = {
|
||||
promiseTimeout: 5000,
|
||||
promiseTimeout: 15000,
|
||||
};
|
||||
|
||||
constructor(settings?: ISettingsParams) {
|
||||
|
||||
@@ -1,32 +1,40 @@
|
||||
import { Contact, ContactEvents, PreDBContact } from '../../../typings/contact';
|
||||
import { getSource } from '../utils/miscUtils';
|
||||
import { Contact, ContactDeleteDTO, ContactEvents, PreDBContact } from '../../../typings/contact';
|
||||
import ContactService from './contacts.service';
|
||||
import { contactsLogger } from './contacts.utils';
|
||||
import { onNetPromise } from '../utils/PromiseNetEvents/onNetPromise';
|
||||
|
||||
onNet(ContactEvents.GET_CONTACTS, async (limit: number) => {
|
||||
const src = getSource();
|
||||
ContactService.handleFetchContact(src, limit).catch((e) =>
|
||||
contactsLogger.error(`Error occured in fetch contacts event (${src}), Error: ${e.message}`),
|
||||
);
|
||||
onNetPromise<void, Contact[]>(ContactEvents.GET_CONTACTS, (reqObj, resp) => {
|
||||
ContactService.handleFetchContacts(reqObj, resp).catch((e) => {
|
||||
contactsLogger.error(
|
||||
`Error occured in fetch contacts event (${reqObj.source}), Error: ${e.message}`,
|
||||
);
|
||||
resp({ status: 'error', errorMsg: 'INTERNAL_ERROR' });
|
||||
});
|
||||
});
|
||||
|
||||
onNet(ContactEvents.ADD_CONTACT, async (contact: PreDBContact) => {
|
||||
const src = getSource();
|
||||
ContactService.handleAddContact(src, contact).catch((e) =>
|
||||
contactsLogger.error(`Error occured in fetch contacts event (${src}), Error: ${e.message}`),
|
||||
);
|
||||
onNetPromise<PreDBContact, Contact>(ContactEvents.ADD_CONTACT, (reqObj, resp) => {
|
||||
ContactService.handleAddContact(reqObj, resp).catch((e) => {
|
||||
contactsLogger.error(
|
||||
`Error occured in fetch contacts event (${reqObj.source}), Error: ${e.message}`,
|
||||
);
|
||||
resp({ status: 'error', errorMsg: 'INTERNAL_ERROR' });
|
||||
});
|
||||
});
|
||||
|
||||
onNet(ContactEvents.UPDATE_CONTACT, async (contact: Contact) => {
|
||||
const src = getSource();
|
||||
ContactService.handleUpdateContact(src, contact).catch((e) =>
|
||||
contactsLogger.error(`Error occured in update contact event (${src}), Error: ${e.message}`),
|
||||
);
|
||||
onNetPromise<Contact, void>(ContactEvents.UPDATE_CONTACT, (reqObj, resp) => {
|
||||
ContactService.handleUpdateContact(reqObj, resp).catch((e) => {
|
||||
contactsLogger.error(
|
||||
`Error occured in update contact event (${reqObj.source}), Error: ${e.message}`,
|
||||
);
|
||||
resp({ status: 'error', errorMsg: 'INTERNAL_ERROR' });
|
||||
});
|
||||
});
|
||||
|
||||
onNet(ContactEvents.DELETE_CONTACT, async (contactId: number) => {
|
||||
const src = getSource();
|
||||
ContactService.handleDeleteContact(src, contactId).catch((e) =>
|
||||
contactsLogger.error(`Error occured in delete contact event (${src}), Error: ${e.message}`),
|
||||
);
|
||||
onNetPromise<ContactDeleteDTO, void>(ContactEvents.DELETE_CONTACT, (reqObj, resp) => {
|
||||
ContactService.handleDeleteContact(reqObj, resp).catch((e) => {
|
||||
contactsLogger.error(
|
||||
`Error occured in delete contact event (${reqObj.source}), Error: ${e.message}`,
|
||||
);
|
||||
resp({ status: 'error', errorMsg: 'INTERNAL_ERROR' });
|
||||
});
|
||||
});
|
||||
@@ -1,23 +1,29 @@
|
||||
import { pool } from '../db';
|
||||
import { Contact, PreDBContact } from '../../../typings/contact';
|
||||
import { FetchDefaultLimits } from '../utils/ServerConstants';
|
||||
import { ResultSetHeader } from 'mysql2';
|
||||
|
||||
export class _ContactsDB {
|
||||
async fetchAllContacts(
|
||||
identifier: string,
|
||||
limit = FetchDefaultLimits.CONTACTS_FETCH_LIMIT,
|
||||
): Promise<Contact[]> {
|
||||
const query =
|
||||
'SELECT * FROM npwd_phone_contacts WHERE identifier = ? ORDER BY display ASC';
|
||||
const [results] = await pool.query(query, [identifier, limit]);
|
||||
async fetchAllContacts(identifier: string): Promise<Contact[]> {
|
||||
const query = 'SELECT * FROM npwd_phone_contacts WHERE identifier = ? ORDER BY display ASC';
|
||||
const [results] = await pool.query(query, [identifier]);
|
||||
return <Contact[]>results;
|
||||
}
|
||||
|
||||
async addContact(identifier: string, { display, avatar, number }: PreDBContact): Promise<void> {
|
||||
async addContact(
|
||||
identifier: string,
|
||||
{ display, avatar, number }: PreDBContact,
|
||||
): Promise<Contact> {
|
||||
const query =
|
||||
'INSERT INTO npwd_phone_contacts (identifier, number, display, avatar) VALUES (?, ?, ?, ?)';
|
||||
|
||||
await pool.query(query, [identifier, number, display, avatar]);
|
||||
const [setResult] = await pool.query(query, [identifier, number, display, avatar]);
|
||||
|
||||
return {
|
||||
id: (<ResultSetHeader>setResult).insertId,
|
||||
number,
|
||||
avatar,
|
||||
display,
|
||||
};
|
||||
}
|
||||
|
||||
async updateContact(contact: Contact, identifier: string): Promise<any> {
|
||||
|
||||
@@ -1,8 +0,0 @@
|
||||
import { FxServerRespError } from '../../../typings/phone';
|
||||
import { ContactEvents } from '../../../typings/contact';
|
||||
|
||||
export interface ContactServerResp {
|
||||
data?: unknown;
|
||||
error?: FxServerRespError;
|
||||
action: ContactEvents;
|
||||
}
|
||||
@@ -13,79 +13,57 @@ class _ContactService {
|
||||
contactsLogger.debug('Contacts service started');
|
||||
}
|
||||
|
||||
private responseBuilder(src: number, { data, action, error }: ContactServerResp): void {
|
||||
const serverRespObj: FxServerResponse = {
|
||||
app: 'CONTACTS',
|
||||
error,
|
||||
status: data ? 'success' : 'failure',
|
||||
action,
|
||||
};
|
||||
|
||||
emitNet(ContactEvents.MAIN_CLIENT_LISTER, src, serverRespObj);
|
||||
}
|
||||
|
||||
async handleUpdateContact(src: number, contact: Contact): Promise<void> {
|
||||
const identifier = PlayerService.getIdentifier(src);
|
||||
async handleUpdateContact(
|
||||
reqObj: PromiseRequest<Contact>,
|
||||
resp: PromiseEventResp<void>,
|
||||
): Promise<void> {
|
||||
const identifier = PlayerService.getIdentifier(reqObj.source);
|
||||
try {
|
||||
await this.contactsDB.updateContact(contact, identifier);
|
||||
await this.contactsDB.updateContact(reqObj.data, identifier);
|
||||
|
||||
emitNet(ContactEvents.UPDATE_CONTACT_SUCCESS, src);
|
||||
|
||||
emitNet(ContactEvents.ACTION_RESULT, src, {
|
||||
message: 'CONTACT_UPDATE_SUCCESS',
|
||||
type: 'success',
|
||||
});
|
||||
resp({ status: 'ok' });
|
||||
} catch (e) {
|
||||
contactsLogger.error(`Error in handleUpdateContact (${identifier}), ${e.message}`);
|
||||
emitNet(ContactEvents.ACTION_RESULT, src, {
|
||||
message: 'CONTACT_UPDATE_FAILED',
|
||||
type: 'error',
|
||||
});
|
||||
resp({ status: 'error', errorMsg: 'DB_ERROR' });
|
||||
}
|
||||
}
|
||||
async handleDeleteContact(src: number, contactId: number): Promise<void> {
|
||||
const identifier = PlayerService.getIdentifier(src);
|
||||
async handleDeleteContact(
|
||||
reqObj: PromiseRequest<ContactDeleteDTO>,
|
||||
resp: PromiseEventResp<void>,
|
||||
): Promise<void> {
|
||||
const identifier = PlayerService.getIdentifier(reqObj.source);
|
||||
try {
|
||||
await this.contactsDB.deleteContact(contactId, identifier);
|
||||
emitNet(ContactEvents.DELETE_CONTACT_SUCCESS, src);
|
||||
emitNet(ContactEvents.ACTION_RESULT, src, {
|
||||
message: 'CONTACT_DELETE_SUCCESS',
|
||||
type: 'success',
|
||||
});
|
||||
await this.contactsDB.deleteContact(reqObj.data.id, identifier);
|
||||
resp({ status: 'ok' });
|
||||
} catch (e) {
|
||||
emitNet(ContactEvents.ACTION_RESULT, src, {
|
||||
message: 'CONTACT_DELETE_FAILED',
|
||||
type: 'error',
|
||||
});
|
||||
resp({ status: 'error', errorMsg: 'DB_ERROR' });
|
||||
contactsLogger.error(`Error in handleDeleteContact (${identifier}), ${e.message}`);
|
||||
}
|
||||
}
|
||||
async handleAddContact(src: number, contact: PreDBContact): Promise<void> {
|
||||
const identifier = PlayerService.getIdentifier(src);
|
||||
async handleAddContact(
|
||||
reqObj: PromiseRequest<PreDBContact>,
|
||||
resp: PromiseEventResp<Contact>,
|
||||
): Promise<void> {
|
||||
const identifier = PlayerService.getIdentifier(reqObj.source);
|
||||
try {
|
||||
await this.contactsDB.addContact(identifier, contact);
|
||||
const contact = await this.contactsDB.addContact(identifier, reqObj.data);
|
||||
|
||||
emitNet(ContactEvents.ADD_CONTACT_SUCCESS, src);
|
||||
emitNet(ContactEvents.ACTION_RESULT, src, {
|
||||
message: 'CONTACT_ADD_SUCCESS',
|
||||
type: 'success',
|
||||
});
|
||||
resp({ status: 'ok', data: contact });
|
||||
} catch (e) {
|
||||
contactsLogger.error(`Error in handleAddContact, ${e.message}`);
|
||||
emitNet(ContactEvents.ACTION_RESULT, src, {
|
||||
message: 'CONTACT_ADD_FAILED',
|
||||
type: 'error',
|
||||
});
|
||||
resp({ status: 'error', errorMsg: 'DB_ERROR' });
|
||||
}
|
||||
}
|
||||
async handleFetchContact(src: number, limit?: number): Promise<void> {
|
||||
const identifier = PlayerService.getIdentifier(src);
|
||||
|
||||
async handleFetchContacts(
|
||||
reqObj: PromiseRequest,
|
||||
resp: PromiseEventResp<Contact[]>,
|
||||
): Promise<void> {
|
||||
const identifier = PlayerService.getIdentifier(reqObj.source);
|
||||
try {
|
||||
const contacts = await this.contactsDB.fetchAllContacts(identifier, limit);
|
||||
|
||||
emitNet(ContactEvents.SEND_CONTACTS, src, contacts);
|
||||
const contacts = await this.contactsDB.fetchAllContacts(identifier);
|
||||
resp({ status: 'ok', data: contacts });
|
||||
} catch (e) {
|
||||
resp({ status: 'error', errorMsg: 'DB_ERROR' });
|
||||
contactsLogger.error(`Error in handleFetchContact (${identifier}), ${e.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user