const { buildDatetime } = require("../../../middlewares/general/datatime"); const { getBlog } = require("../../blogs/middlewares/query"); const { getUserWithId } = require("../../users/middlewares/query"); exports.repairNotifications = async function(data){ const result = new Array(); for (let index = 0; index < data.length; index++) { const notification = data[index]; const datas = JSON.parse(notification.data); const rslt = new Object({ id: data[index].id, read: data[index].read, code: notification.notificationCode.code, datetime: new Object() }); rslt.blog = await getBlog({blogId: datas.blog}).then(res => { if(res){ return { href: `/blog/${res.id}` }; }else { return { href: `/blog/deleted` }; } }); rslt.user = await getUserWithId({userId: datas.user}).then(res => { return { name: `${res.first_name} ${res.second_name}`, username: res.username, href: `/profile/${res.username}` }; }); rslt.datetime.createdAt = buildDatetime({date: notification.createdAt}); result.push(rslt); }; return result; };