tests in typescript
This commit is contained in:
@@ -1,13 +1,13 @@
|
||||
import mysql from "mysql";
|
||||
import { logger } from "../system/Logger";
|
||||
import MYSQL from "./config.json";
|
||||
import logger from "../system/Logger";
|
||||
require("dotenv").config();
|
||||
|
||||
// todo remove export
|
||||
export const db = mysql.createConnection({
|
||||
host: MYSQL.host,
|
||||
user: MYSQL.user,
|
||||
password: MYSQL.pwd,
|
||||
database: MYSQL.database,
|
||||
host: process.env.HOST_DB,
|
||||
user: process.env.USER_DB,
|
||||
password: process.env.PASSWORD_DB,
|
||||
database: process.env.NAME_DB,
|
||||
});
|
||||
|
||||
db.connect(function (err) {
|
||||
@@ -18,7 +18,7 @@ db.connect(function (err) {
|
||||
}
|
||||
});
|
||||
|
||||
export function execQueryAsync(query, values) {
|
||||
export function execQueryAsync(query: string, values: any[]): Promise<any> {
|
||||
return new Promise((resolve, reject) => {
|
||||
db.query(query, values, (err, results, fields) => {
|
||||
if (err) {
|
||||
@@ -30,7 +30,7 @@ export function execQueryAsync(query, values) {
|
||||
});
|
||||
}
|
||||
|
||||
export function execQueryAsyncWithId(query, values) {
|
||||
export function execQueryAsyncWithId(query: string, values: any[]): Promise<number> {
|
||||
return new Promise((resolve, reject) => {
|
||||
db.query(query, values, (err, results, fields) => {
|
||||
if (err) {
|
||||
@@ -42,12 +42,12 @@ export function execQueryAsyncWithId(query, values) {
|
||||
});
|
||||
}
|
||||
|
||||
export function execQuery(query, values) {
|
||||
export function execQuery(query: string, values: any[]) {
|
||||
db.query(query, values, (err, results, fields) => {
|
||||
if (err) {
|
||||
logger.error(err);
|
||||
throw (err);
|
||||
throw err;
|
||||
}
|
||||
return results;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { execQueryAsync, execQueryAsyncWithId } from "./db";
|
||||
|
||||
export async function getAddresseId(email, name) {
|
||||
export async function getAddresseId(email: string, name?: string): Promise<number> {
|
||||
console.log("get address id")
|
||||
const localpart = email.split("@")[0];
|
||||
const domain = email.split("@")[1];
|
||||
const query = `INSERT INTO address
|
||||
@@ -10,19 +11,19 @@ export async function getAddresseId(email, name) {
|
||||
return await execQueryAsyncWithId(query, values);
|
||||
}
|
||||
|
||||
export async function getFieldId(field) {
|
||||
export async function getFieldId(field: string): Promise<number> {
|
||||
const query = `INSERT INTO field_name (field_name) VALUES (?) ON DUPLICATE KEY UPDATE field_id=LAST_INSERT_ID(field_id)`;
|
||||
const values = [field];
|
||||
return await execQueryAsyncWithId(query, values);
|
||||
}
|
||||
|
||||
export async function findRoomByOwner(ownerId) {
|
||||
export async function findRoomByOwner(ownerId: number): Promise<{ room_id: number }[]> {
|
||||
const query = `SELECT room_id FROM app_room WHERE owner_id = ?`;
|
||||
const values = [ownerId];
|
||||
return await execQueryAsync(query, values);
|
||||
}
|
||||
|
||||
export async function getUserIdOfMailbox(boxId) {
|
||||
export async function getUserIdOfMailbox(boxId: number): Promise<{ user_id: number }[]> {
|
||||
const query = `
|
||||
SELECT app_account.user_id
|
||||
FROM mailbox
|
||||
|
||||
@@ -2,7 +2,7 @@ import { transformEmojis } from "../utils/string";
|
||||
import { db, execQueryAsync, execQueryAsyncWithId, execQuery } from "./db";
|
||||
import { queryFromId, queryToId, queryCcId } from "./utils/addressQueries";
|
||||
|
||||
export async function getAllMembers(messageId) {
|
||||
export async function getAllMembers(messageId: number) {
|
||||
const query = `
|
||||
SELECT GROUP_CONCAT(address.address_id) AS is
|
||||
FROM address
|
||||
@@ -15,13 +15,18 @@ export async function getAllMembers(messageId) {
|
||||
return await execQueryAsync(query, values);
|
||||
}
|
||||
|
||||
export async function registerMember(roomId, memberId) {
|
||||
export async function registerMember(roomId: number, memberId: number) {
|
||||
const query = `INSERT IGNORE INTO app_room_member (room_id, member_id) VALUES (?, ?)`;
|
||||
const values = [roomId, memberId];
|
||||
return await execQueryAsync(query, values);
|
||||
}
|
||||
|
||||
export async function createRoom(roomName, ownerId, messageId, roomType) {
|
||||
export async function createRoom(
|
||||
roomName: string | null | undefined,
|
||||
ownerId: number,
|
||||
messageId: number,
|
||||
roomType: number,
|
||||
) {
|
||||
if (!roomName) roomName = "No room name";
|
||||
roomName = transformEmojis(roomName);
|
||||
const query = `INSERT IGNORE INTO app_room (room_name, owner_id, message_id, room_type) VALUES (?, ?, ?, ?)`;
|
||||
@@ -29,7 +34,14 @@ export async function createRoom(roomName, ownerId, messageId, roomType) {
|
||||
return await execQueryAsyncWithId(query, values);
|
||||
}
|
||||
|
||||
export async function registerMessageInRoom(messageId, roomId, isSeen, idate) {
|
||||
// todo date not good
|
||||
export async function registerMessageInRoom(
|
||||
messageId: number,
|
||||
roomId: number,
|
||||
isSeen: boolean,
|
||||
idate: string | undefined | null,
|
||||
) {
|
||||
if (!idate) idate = new Date().toString();
|
||||
const query = `INSERT IGNORE INTO app_room_message (message_id, room_id) VALUES (?, ?)`;
|
||||
const values = [messageId, roomId];
|
||||
await execQueryAsync(query, values);
|
||||
@@ -40,17 +52,17 @@ export async function registerMessageInRoom(messageId, roomId, isSeen, idate) {
|
||||
// }
|
||||
}
|
||||
|
||||
export function updateLastUpdateRoom(roomId, idate) {
|
||||
export function updateLastUpdateRoom(roomId: number, idate: string) {
|
||||
const query = `UPDATE app_room SET lastUpdate = ? WHERE room_id = ?`;
|
||||
const values = [idate, roomId];
|
||||
execQuery(query, values);
|
||||
}
|
||||
|
||||
export function incrementNotSeenRoom(roomId) {
|
||||
export function incrementNotSeenRoom(roomId: number) {
|
||||
// todo
|
||||
}
|
||||
|
||||
export async function getRoomInfo(messageID) {
|
||||
export async function getRoomInfo(messageID: string): Promise<{ room_id: number; root_id: number }[]> {
|
||||
const query = `
|
||||
SELECT
|
||||
app_room.room_id
|
||||
@@ -65,13 +77,13 @@ export async function getRoomInfo(messageID) {
|
||||
return await execQueryAsync(query, values);
|
||||
}
|
||||
|
||||
export async function registerThread(roomId, parentId, rootId) {
|
||||
export async function registerThread(roomId: number, parentId: number, rootId: number) {
|
||||
const query = `INSERT IGNORE INTO app_thread (room_id, parent_id, root_id) VALUES (?, ?, ?)`;
|
||||
const values = [roomId, parentId, rootId];
|
||||
return await execQueryAsync(query, values);
|
||||
}
|
||||
|
||||
export async function isRoomGroup(roomId) {
|
||||
export async function isRoomGroup(roomId: number): Promise<boolean> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const query = `SELECT isGroup FROM app_room WHERE room_id = '${roomId}'`;
|
||||
db.query(query, (err, results, fields) => {
|
||||
@@ -81,13 +93,14 @@ export async function isRoomGroup(roomId) {
|
||||
});
|
||||
}
|
||||
|
||||
export async function findRoomsFromMessage(messageId) {
|
||||
export async function findRoomsFromMessage(messageID: string) {
|
||||
// todo find message in room not started
|
||||
const query = `SELECT room_id FROM app_room_message WHERE message_id = ? ORDER BY room_id`;
|
||||
const values = [messageId];
|
||||
const values = [messageID];
|
||||
return await execQueryAsync(query, values);
|
||||
}
|
||||
|
||||
export async function hasSameMembersAsParent(messageId, messageID) {
|
||||
export async function hasSameMembersAsParent(messageId: number, messageID: string) {
|
||||
const query1 = `
|
||||
SELECT
|
||||
GROUP_CONCAT(fromT.address_id) AS fromA,
|
||||
@@ -129,4 +142,4 @@ export async function hasSameMembersAsParent(messageId, messageID) {
|
||||
addressesMsg1.length == addressesMsg2.length &&
|
||||
addressesMsg1.reduce((a, b) => a && addressesMsg2.includes(b), true)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
const queryAddress = (type) => `
|
||||
const queryAddress = (type: string): string => `
|
||||
LEFT JOIN (
|
||||
SELECT address_field.address_id, address_field.message_id
|
||||
FROM address_field
|
||||
@@ -9,12 +9,6 @@ const queryAddress = (type) => `
|
||||
)
|
||||
`;
|
||||
|
||||
const queryFromId = queryAddress("from");
|
||||
const queryToId = queryAddress("to");
|
||||
const queryCcId = queryAddress("cc");
|
||||
|
||||
module.exports = {
|
||||
queryFromId,
|
||||
queryToId,
|
||||
queryCcId
|
||||
}
|
||||
export const queryFromId = queryAddress("from");
|
||||
export const queryToId = queryAddress("to");
|
||||
export const queryCcId = queryAddress("cc");
|
||||
Reference in New Issue
Block a user