mirror of
https://github.com/darksaid98/new-phone-who-dis.git
synced 2026-12-03 22:16:54 +00:00
refactor(server/contacts): Implement singleton architecture
This commit is contained in:
1 parent
16471ed1df
commit
f8f722c8d2
9 files changed
+200
-136
No files matched your search
@@ -1,4 +1,4 @@
|
||||
import { ContactEvents } from '../../typings/contact';
|
||||
import { ContactEvents, PreDBContact } from '../../typings/contact';
|
||||
import { sendContactsEvent } from '../utils/messages';
|
||||
|
||||
onNet(ContactEvents.SEND_CONTACTS, (contacts: any) => {
|
||||
@@ -6,8 +6,8 @@ onNet(ContactEvents.SEND_CONTACTS, (contacts: any) => {
|
||||
});
|
||||
|
||||
RegisterNuiCallbackType(ContactEvents.ADD_CONTACT);
|
||||
on(`__cfx_nui:${ContactEvents.ADD_CONTACT}`, (data: any, cb: Function) => {
|
||||
emitNet(ContactEvents.ADD_CONTACT, data.number, data.display, data.avatar);
|
||||
on(`__cfx_nui:${ContactEvents.ADD_CONTACT}`, (data: PreDBContact, cb: Function) => {
|
||||
emitNet(ContactEvents.ADD_CONTACT, data);
|
||||
cb();
|
||||
});
|
||||
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
import { Contact, ContactEvents, PreDBContact } from '../../../typings/contact';
|
||||
import { getSource } from '../utils/miscUtils';
|
||||
import ContactService from './contacts.service';
|
||||
import { contactsLogger } from './contacts.utils';
|
||||
|
||||
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}`),
|
||||
);
|
||||
});
|
||||
|
||||
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}`),
|
||||
);
|
||||
});
|
||||
|
||||
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}`),
|
||||
);
|
||||
});
|
||||
|
||||
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}`),
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,42 @@
|
||||
import { pool } from '../db';
|
||||
import { Contact, PreDBContact } from '../../../typings/contact';
|
||||
import { FetchDefaultLimits } from '../utils/ServerConstants';
|
||||
|
||||
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 LIMIT ?';
|
||||
const [results] = await pool.query(query, [identifier, limit]);
|
||||
return <Contact[]>results;
|
||||
}
|
||||
|
||||
async addContact(identifier: string, { display, avatar, number }: PreDBContact): Promise<void> {
|
||||
const query =
|
||||
'INSERT INTO npwd_phone_contacts (identifier, number, display, avatar) VALUES (?, ?, ?, ?)';
|
||||
|
||||
await pool.query(query, [identifier, number, display, avatar]);
|
||||
}
|
||||
|
||||
async updateContact(contact: Contact, identifier: string): Promise<any> {
|
||||
const query =
|
||||
'UPDATE npwd_phone_contacts SET number = ?, display = ?, avatar = ? WHERE id = ? AND identifier = ?';
|
||||
await pool.query(query, [
|
||||
contact.number,
|
||||
contact.display,
|
||||
contact.avatar,
|
||||
contact.id,
|
||||
identifier,
|
||||
]);
|
||||
}
|
||||
|
||||
async deleteContact(contactId: number, identifier: string): Promise<void> {
|
||||
const query = 'DELETE FROM npwd_phone_contacts WHERE id = ? AND identifier = ?';
|
||||
await pool.query(query, [contactId, identifier]);
|
||||
}
|
||||
}
|
||||
|
||||
const ContactsDB = new _ContactsDB();
|
||||
export default ContactsDB;
|
||||
@@ -0,0 +1,8 @@
|
||||
import { FxServerRespError } from '../../../typings/phone';
|
||||
import { ContactEvents } from '../../../typings/contact';
|
||||
|
||||
export interface ContactServerResp {
|
||||
data?: unknown;
|
||||
error?: FxServerRespError;
|
||||
action: ContactEvents;
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
import PlayerService from '../players/player.service';
|
||||
import { contactsLogger } from './contacts.utils';
|
||||
import ContactsDB, { _ContactsDB } from './contacts.db';
|
||||
import { Contact, ContactEvents, PreDBContact } from '../../../typings/contact';
|
||||
import { ErrorStringKeys, FxServerResponse } from '../../../typings/phone';
|
||||
import { ContactServerResp } from './contacts.interfaces';
|
||||
|
||||
class _ContactService {
|
||||
private readonly contactsDB: _ContactsDB;
|
||||
|
||||
constructor() {
|
||||
this.contactsDB = ContactsDB;
|
||||
}
|
||||
|
||||
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);
|
||||
try {
|
||||
await this.contactsDB.updateContact(contact, identifier);
|
||||
this.responseBuilder(src, {
|
||||
action: ContactEvents.UPDATE_CONTACT,
|
||||
});
|
||||
} catch (e) {
|
||||
contactsLogger.error(`Error in handleUpdateContact (${identifier}), ${e.message}`);
|
||||
this.responseBuilder(src, {
|
||||
action: ContactEvents.UPDATE_CONTACT,
|
||||
error: {
|
||||
errorKey: ErrorStringKeys.UPDATE_FAILED,
|
||||
message: `Failed to update contact ${contact.id}`,
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
async handleDeleteContact(src: number, contactId: number): Promise<void> {
|
||||
const identifier = PlayerService.getIdentifier(src);
|
||||
try {
|
||||
await this.contactsDB.deleteContact(contactId, identifier);
|
||||
this.responseBuilder(src, {
|
||||
action: ContactEvents.DELETE_CONTACT,
|
||||
});
|
||||
} catch (e) {
|
||||
this.responseBuilder(src, {
|
||||
action: ContactEvents.DELETE_CONTACT,
|
||||
error: {
|
||||
errorKey: ErrorStringKeys.DELETE_FAILED,
|
||||
message: `Failed to delete contactId ${contactId}`,
|
||||
},
|
||||
});
|
||||
contactsLogger.error(`Error in handleDeleteContact (${identifier}), ${e.message}`);
|
||||
}
|
||||
}
|
||||
async handleAddContact(src: number, contact: PreDBContact): Promise<void> {
|
||||
const identifier = PlayerService.getIdentifier(src);
|
||||
try {
|
||||
await this.contactsDB.addContact(identifier, contact);
|
||||
|
||||
this.responseBuilder(src, {
|
||||
action: ContactEvents.ADD_CONTACT,
|
||||
});
|
||||
} catch (e) {
|
||||
contactsLogger.error(`Error in handleAddContact, ${e.message}`);
|
||||
this.responseBuilder(src, {
|
||||
action: ContactEvents.ADD_CONTACT,
|
||||
error: {
|
||||
errorKey: ErrorStringKeys.ADD_FAILED,
|
||||
message: 'Unable to add contacts',
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
async handleFetchContact(src: number, limit?: number): Promise<void> {
|
||||
const identifier = PlayerService.getIdentifier(src);
|
||||
|
||||
try {
|
||||
const contacts = await this.contactsDB.fetchAllContacts(identifier, limit);
|
||||
|
||||
this.responseBuilder(src, {
|
||||
data: contacts,
|
||||
action: ContactEvents.SEND_CONTACTS,
|
||||
});
|
||||
} catch (e) {
|
||||
this.responseBuilder(src, {
|
||||
action: ContactEvents.SEND_CONTACTS,
|
||||
error: {
|
||||
errorKey: ErrorStringKeys.SERVER_ERROR,
|
||||
message: 'Unable to fetch contacts',
|
||||
},
|
||||
});
|
||||
contactsLogger.error(`Error in handleFetchContact (${identifier}), ${e.message}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const ContactService = new _ContactService();
|
||||
export default ContactService;
|
||||
@@ -0,0 +1,3 @@
|
||||
import { mainLogger } from '../sv_logger';
|
||||
|
||||
export const contactsLogger = mainLogger.child({ module: 'contact' });
|
||||
@@ -1,131 +0,0 @@
|
||||
import { pool } from './db';
|
||||
import { getIdentifier, getSource } from './functions';
|
||||
import { mainLogger } from './sv_logger';
|
||||
import { ContactEvents } from '../../typings/contact';
|
||||
|
||||
const contactsLogger = mainLogger.child({ module: 'contact' });
|
||||
|
||||
interface Contacts {
|
||||
id?: number;
|
||||
display: string;
|
||||
number: string;
|
||||
avatar: string;
|
||||
}
|
||||
|
||||
interface ContactId {
|
||||
id: number;
|
||||
}
|
||||
|
||||
async function fetchAllContacts(identifier: string): Promise<Contacts[]> {
|
||||
const query = 'SELECT * FROM npwd_phone_contacts WHERE identifier = ? ORDER BY display ASC';
|
||||
const [results] = await pool.query(query, [identifier]);
|
||||
const contacts = <Contacts[]>results;
|
||||
return contacts;
|
||||
}
|
||||
|
||||
async function addContact(
|
||||
identifier: string,
|
||||
number: string,
|
||||
display: string,
|
||||
avatar: string,
|
||||
): Promise<any> {
|
||||
const query =
|
||||
'INSERT INTO npwd_phone_contacts (identifier, number, display, avatar) VALUES (?, ?, ?, ?)';
|
||||
|
||||
const [result] = await pool.query(query, [identifier, number, display, avatar]);
|
||||
}
|
||||
|
||||
async function updateContact(contact: Contacts, identifier: string): Promise<any> {
|
||||
const query =
|
||||
'UPDATE npwd_phone_contacts SET number = ?, display = ?, avatar = ? WHERE id = ? AND identifier = ?';
|
||||
await pool.query(query, [
|
||||
contact.number,
|
||||
contact.display,
|
||||
contact.avatar,
|
||||
contact.id,
|
||||
identifier,
|
||||
]);
|
||||
}
|
||||
|
||||
async function deleteContact(contact: ContactId, identifier: string): Promise<any> {
|
||||
const query = 'DELETE FROM npwd_phone_contacts WHERE id = ? AND identifier = ?';
|
||||
await pool.query(query, [contact.id, identifier]);
|
||||
}
|
||||
|
||||
onNet(ContactEvents.GET_CONTACTS, async () => {
|
||||
const _source = getSource();
|
||||
try {
|
||||
const _identifier = getIdentifier(_source);
|
||||
|
||||
const contacts = await fetchAllContacts(_identifier);
|
||||
emitNet(ContactEvents.SEND_CONTACTS, _source, contacts);
|
||||
} catch (e) {
|
||||
contactsLogger.error(`Failed to fetch contacts, ${e.message}`);
|
||||
}
|
||||
});
|
||||
|
||||
onNet(ContactEvents.ADD_CONTACT, async (number: string, display: string, avatar: string) => {
|
||||
const _source = getSource();
|
||||
try {
|
||||
const _identifier = getIdentifier(_source);
|
||||
await addContact(_identifier, number, display, avatar);
|
||||
emitNet(ContactEvents.ADD_CONTACT_SUCCESS, _source);
|
||||
emitNet(ContactEvents.ACTION_RESULT, _source, {
|
||||
message: 'CONTACT_ADD_SUCCESS',
|
||||
type: 'success',
|
||||
});
|
||||
} catch (e) {
|
||||
emitNet(ContactEvents.ACTION_RESULT, _source, {
|
||||
message: 'CONTACT_ADD_FAILED',
|
||||
type: 'error',
|
||||
});
|
||||
contactsLogger.error(`Failed to add contact, ${e.message}`, {
|
||||
source: _source,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
onNet(ContactEvents.UPDATE_CONTACT, async (contact: Contacts) => {
|
||||
const _source = getSource();
|
||||
try {
|
||||
const _identifier = getIdentifier(_source);
|
||||
await updateContact(contact, _identifier);
|
||||
|
||||
emitNet(ContactEvents.UPDATE_CONTACT_SUCCESS, _source);
|
||||
|
||||
emitNet(ContactEvents.ACTION_RESULT, _source, {
|
||||
message: 'CONTACT_UPDATE_SUCCESS',
|
||||
type: 'success',
|
||||
});
|
||||
} catch (e) {
|
||||
const _source = (global as any).source;
|
||||
emitNet(ContactEvents.ACTION_RESULT, _source, {
|
||||
message: 'CONTACT_UPDATE_FAILED',
|
||||
type: 'error',
|
||||
});
|
||||
contactsLogger.error(`Failed to update contact, ${e.message}`, {
|
||||
source: _source,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
onNet(ContactEvents.DELETE_CONTACT, async (contact: ContactId) => {
|
||||
const _source = getSource();
|
||||
try {
|
||||
const _identifier = getIdentifier(_source);
|
||||
await deleteContact(contact, _identifier);
|
||||
emitNet(ContactEvents.DELETE_CONTACT_SUCCESS, _source);
|
||||
emitNet(ContactEvents.ACTION_RESULT, _source, {
|
||||
message: 'CONTACT_DELETE_SUCCESS',
|
||||
type: 'success',
|
||||
});
|
||||
} catch (e) {
|
||||
contactsLogger.error(`Failed to delete contact, ${e.message}`, {
|
||||
source: _source,
|
||||
});
|
||||
emitNet(ContactEvents.ACTION_RESULT, _source, {
|
||||
message: 'CONTACT_DELETE_FAILED',
|
||||
type: 'error',
|
||||
});
|
||||
}
|
||||
});
|
||||
@@ -1,4 +1,5 @@
|
||||
export enum FetchDefaultLimits {
|
||||
NOTES_FETCH_LIMIT = 20,
|
||||
CALLS_FETCH_LIMIT = 50,
|
||||
CONTACTS_FETCH_LIMIT = 20,
|
||||
}
|
||||
+6
-2
@@ -11,13 +11,16 @@ export interface IContactAlert {
|
||||
setAlert: (type: string) => void;
|
||||
}
|
||||
|
||||
export interface Contact {
|
||||
id: number;
|
||||
export interface PreDBContact {
|
||||
display: string;
|
||||
number: string;
|
||||
avatar?: string;
|
||||
}
|
||||
|
||||
export interface Contact extends PreDBContact {
|
||||
id: number;
|
||||
}
|
||||
|
||||
export enum ContactEvents {
|
||||
SEND_CONTACTS = 'npwd:sendContacts',
|
||||
GET_CONTACTS = 'npwd:getContacts',
|
||||
@@ -29,4 +32,5 @@ export enum ContactEvents {
|
||||
UPDATE_CONTACT_SUCCESS = 'npwd:updateContactSuccess',
|
||||
ACTION_RESULT = 'npwd:contactActionResult',
|
||||
SEND_ALERT = 'npwd:contactSetAlert',
|
||||
MAIN_CLIENT_LISTER = 'npwd:contactMainListener',
|
||||
}
|
||||
Reference in new issue
Block a user