const { Notification } = require("../../models");
const { success, error } = require("../../handlers");
const { Op } = require("sequelize");

const index = async (req, res) => {
  try {
    await Notification.update(
      { status: 1 },
      {
        where: {
          [Op.or]: [
            {
              userId: null,
            },
            {
              userId: req.user?.id,
            },
          ],
        },
      }
    );
    let notifications = await Notification.findAll({
      order: [["id", "desc"]],
      where: {
        [Op.or]: [
          {
            userId: null,
          },
          {
            userId: req.user?.id,
          },
        ],
      },
    });

    return success(res, {
      msg: "Notifications listed successfully",
      data: notifications,
    });
  } catch (err) {
    return error(res, {
      msg: "Something went wrong!!",
    });
  }
};

module.exports = {
  index,
};
