@opengis/fastify-table
Version:
core-plugins
34 lines (33 loc) • 1.46 kB
JavaScript
import pgClients from "../../../plugins/pg/pgClients.js";
import getSelectVal from "../../../plugins/table/funcs/metaFormat/getSelectVal.js";
const maxLimit = 100;
const q = `select notification_id as id, subject, body, created_at,
author_id, read, link, entity_id, (select avatar from admin.users where uid=a.author_id limit 1) as avatar
from crm.notifications a
where addressee_id=$1 order by created_at desc limit $2 offset $3`;
export default async function userNotifications(req, reply) {
const { pg = pgClients.client, query, user, } = req;
const t1 = Date.now();
if (!user?.uid) {
return reply.status(401).send({ error: "unauthorized", code: 401 });
}
const limit = Math.min(maxLimit, +(query.limit || 5));
const offset = query.page && query.page > 0 ? (query.page - 1) * limit : 0;
const rows = pg && pg.pk?.["crm.notifications"]
? await pg
.query(q, [user.uid, limit, offset])
.then((el) => el.rows || [])
: [];
const values = rows
.map((row) => row.author_id)
.filter((el, idx, arr) => el && arr.indexOf(el) === idx);
if (values.length) {
const vals = await getSelectVal({ name: "core.user_mentioned", values });
rows.forEach((row) => {
Object.assign(row, { author: vals?.[row.author_id] });
});
}
return reply
.status(200)
.send({ time: Date.now() - t1, total: rows.length, rows });
}