const { param, validationResult } = require('express-validator')
const { _sr } = require('../plugins/serverResponses')
const { checkValidations } = require('../plugins/controllersHelperFunctions')
const Notification = require('../models/GPS.Notif')


module.exports.setSeen = [
    [
        param('id').notEmpty().isMongoId().withMessage('ایدی را وارد یا تصحیح کنید')
    ],
    checkValidations(validationResult),
    async (req, res) => {
        const notif = await Notification.findById(req.params.id)
        if (!notif || notif.userID !== req.userID) {
            res.status(404).json({ msg: _sr.fa.not_found.item_id })
        } else {
            notif.read = true;
            notif.save()
        }
    }
]

module.exports.setSeenAll = [
    async (req, res) => {
        await Notification.updateMany(
            { userID: req.userID },
            { seen: true }
        )
    }
]