feat(contacts/server): update and delete contacts

This commit is contained in:
chip committed 2020-11-01 22:50:48 +01:00
1 parent b1835bbd75
commit 0aa26851ae
1 file changed
+53 -5
+53 -5
View File
@@ -1,12 +1,20 @@
import { ESX } from "./server";
import { ESX, getSource } from "./server";
import { pool } from "./db";
import events from "../utils/events";
import { useIdentifier } from './functions'
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 = ?";
const [results] = await pool.query(query, [identifier]);
@@ -26,6 +34,25 @@ async function addContact(
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(events.CONTACTS_GET_CONTACTS, async () => {
try {
const _source = (global as any).source;
@@ -39,16 +66,37 @@ onNet(events.CONTACTS_GET_CONTACTS, async () => {
}
})
onNet(events.CONTACTS_ADD_CONTACT, (number: string, display: string, avatar: string) => {
onNet(events.CONTACTS_ADD_CONTACT, async (number: string, display: string, avatar: string) => {
try {
const _source = (global as any).source;
const xPlayer = ESX.GetPlayerFromId(_source);
const _identifier = xPlayer.getIdentifier()
const _identifier = await useIdentifier()
addContact(_identifier, number, display, avatar);
//emit(events.CONTACTS_GET_CONTACTS);
emitNet(events.CONTACTS_ADD_CONTACT_SUCCESS, _source)
} catch(error) {
console.log("Failed to add contact: ", error);
}
});
onNet(events.CONTACTS_UPDATE_CONTACT, async (contact: Contacts) => {
try {
const _source = (global as any)._source
const _identifier = await useIdentifier()
updateContact(contact, _identifier)
emitNet(events.CONTACTS_UPDATE_CONTACT_SUCCESS, _source)
} catch (error) {
console.log(error)
}
})
onNet(events.CONTACTS_DELETE_CONTACT, async (contact: ContactId) => {
console.log("ID", contact)
try {
const _identifier = await useIdentifier()
deleteContact(contact, _identifier)
emitNet(events.CONTACTS_DELETE_CONTACT_SUCCESS, getSource())
} catch (error) {
console.log("DELETE CONTACT ERROR", error)
}
})