const { db, execQueryAsync, execQueryAsyncWithId } = require("./db.js"); const DEBUG = require("../utils/debug").DEBUG; async function registerMailbox(userId, pwd, xoauth, xoauth2, host, port, tls) { const query = ` INSERT INTO app_account (user_id, account_pwd, xoauth, xoauth2, host, port, tls) VALUES (?, ?, ?, ?, ?, ?, ?) `; const values = [userId, pwd, xoauth, xoauth2, host, port, tls]; return await execQueryAsyncWithId(query, values); } async function getMailboxes() { const query = ` SELECT app_account.account_id AS id, address.email FROM app_account INNER JOIN address WHERE address.address_id = app_account.user_id `; const values = []; return await execQueryAsync(query, values); } async function getRooms(mailboxId) { const query = ` SELECT app_room.room_id AS id, app_room.room_name AS roomName, address.email AS user, app_room.owner_id AS userId, app_room.notSeen, mailbox_message.mailbox_id AS mailboxId FROM app_room INNER JOIN message INNER JOIN mailbox_message INNER JOIN address WHERE message.message_id = app_room.message_id AND mailbox_message.mailbox_id = 1 AND mailbox_message.message_id = message.message_id AND address.address_id = app_room.owner_id `; // todo mailboxId const values = []; return await execQueryAsync(query, values); } async function getMessages(roomId) { // todo attachements name const query = ` SELECT address_field.address_id, bodypart.text, header_field.value FROM bodypart INNER JOIN header_field INNER JOIN address_field INNER JOIN field_name WHERE ( header_field.field_id = field_name.field_id OR address_field.field_id = field_name.field_id ) AND ( field_name.field_name = 'html' OR field_name.field_name = 'text' OR field_name.field_name = 'textAsHtml' OR field_name.field_name = 'to' OR field_name.field_name = 'cc' OR field_name.field_name = 'subject' ) `; // todo roomID const values = []; return await execQueryAsync(query, values); } module.exports = { registerMailbox, getMailboxes, getRooms, getMessages };