const statusCodes = require("../utils/statusCodes.js").statusCodes; const express = require("express"); const router = express.Router(); const Ajv = require("ajv"); const addFormats = require("ajv-formats"); const ajv = new Ajv({ allErrors: true }); addFormats(ajv); const schema_mailbox = require("../schemas/mailbox_schema.json"); const { addMailbox } = require("../controllers/addMailbox.js"); const { getMailboxes } = require("../db/api.js"); const validate_mailbox = ajv.compile(schema_mailbox); /** * Return all mailboxes and folders for an user */ router.get("/mailboxes", (req, res) => { getMailboxes().then((data) => { res.status(statusCodes.OK).json(data) }); }); /** * @param {number} mailboxId the id of the mailbox (account_id) from which to fetch the messages, 0 if from all * @param {number} offset the offset of the query * @param {number} limit the number of message to return * @param {string} token the token of the user * @return {object} a list of room and their preview (subject) */ router.get("/{mailboxId}/messages", (req, res) => { const { mailboxId, offset, limit } = req.params; // todo check token // todo use offset const query = ` SELECT app_room.room_id, app_room.room_name, app_room.owner_id, app_room.notSeen, mailbox_message.mailbox_id, address.email 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 `; const values = [mailboxId]; }); /** * Register a new mailbox inside the app */ router.post("/mailbox", async (req, res) => { const valid = validate_mailbox(req.body); if (!valid) { res.status(statusCodes.NOT_ACCEPTABLE).send(validate_mailbox.errors) } else { await addMailbox(req.body, res); } }); module.exports = router;