import { ObjectId } from "mongodb";
import { Mails } from "./mongodb"
import DBMail from "../../types/db/DBMail";

export const getMailById = async (id: ObjectId) => {
    return await (await Mails()).findOne({
        _id: id
    }) as DBMail;
}

export const getMailsByTo = async (to: ObjectId) => {
    return await (await Mails()).find({
        to
    }).toArray() as DBMail[];
}

export const checkForUnreadMails = async (to: ObjectId) => {
    return (await (await Mails()).find({
        to,
        read: false
    }).toArray()).length > 0;
}

export const sendMail = async (from: ObjectId | null, to: ObjectId, date: Date, subject: string, body: string) => {
    await (await Mails()).insertOne({
        from,
        to,
        date,
        subject,
        body,
        read: false
    });
}