@replyke/express
Version:
Replyke: Build interactive apps with social features like comments, votes, feeds, user lists, notifications, and more.
48 lines (47 loc) • 1.84 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const models_1 = require("../../../models");
exports.default = async (req, res) => {
try {
const { page = 1, limit = 5 } = req.query;
const loggedInUserId = req.userId;
const projectId = req.project.id;
// Convert 'limit' and 'page' to numbers and validate them.
let limitAsNumber = Number(limit);
if (isNaN(limitAsNumber)) {
res.status(400).json({
error: "Invalid request: limit must be a number",
code: "app-notification/invalid-limit",
});
return;
}
limitAsNumber = Math.min(limitAsNumber, 50);
const pageAsNumber = Number(page);
if (isNaN(pageAsNumber) || pageAsNumber < 1 || pageAsNumber % 1 !== 0) {
res.status(400).json({
error: "Invalid request: 'page' must be a whole number greater than 0",
code: "app-notification/invalid-page",
});
return;
}
// Perform the query on the Entity model with pagination, sorting, and filtering
const appNotifications = await models_1.AppNotification.findAll({
where: {
projectId,
userId: loggedInUserId,
},
order: [["createdAt", "DESC"]],
limit: limitAsNumber,
offset: (pageAsNumber - 1) * limitAsNumber, // Skip count for pagination
});
res.status(200).json(appNotifications.map((i) => i.toJSON()));
}
catch (err) {
console.error("Error fetching app notifications:", err);
res.status(500).json({
error: "Internal server error.",
code: "app-notification/server-error",
details: err.message,
});
}
};