Files
new-phone-who-dis/resources/server/sv_contacts.ts
T
Taso 0f61088999 refactor(events): Events are now tied to a common enum typings (#167)
* chore(scripts/twitter): Refactored twitter to use event enums

* chore(scripts/marketplace): Refactored marketplace to use event enums

* chore(scripts/messages): Refactored messages to use event enums

* chore(scripts/contacts): Refactored contacts to use event enums

* chore(scripts/calls): Refactored calls to use event enums

* chore(scripts/notes): Refactored notes to use event enums

* chore(scripts/photos): Refactored photos to use event enums

* chore(scripts/bank): Refactored bank to use event enums

* chore(scripts/phone): Refactored phone to use event enums

* chore(events): Removed old events object

* refactor(phone/nui-events): Attached NUI events to respective enum

* tweak(event-enums): Added npwd prefix to all events

* tweak(phone/serve): Added craco to override CRA without eject

Also bumped react-scripts to 4.0.1, with seemingly no issues

* fix(typings/twitter): Fixed method enums for twitter
2021-03-22 13:37:07 -04:00

132 lines
3.8 KiB
TypeScript

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',
});
}
});