UNPKG

@wepublish/api

Version:
102 lines 4.12 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getActions = void 0; const tslib_1 = require("tslib"); const action_1 = require("../../db/action"); const api_1 = require("../../../../permissions-api/src"); const api_2 = require("../../../../permissions-api/src"); const getActions = (authenticate, article, page, comment, subscription, author, poll, user, event) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { const { roles } = authenticate(); const articles = (0, api_2.hasPermission)(api_1.CanGetArticles, roles) ? article .findMany({ take: 50, include: { draft: true, pending: true, published: true }, orderBy: { createdAt: 'desc' } }) .then(val => val.map((article) => { return { date: article.createdAt, actionType: action_1.ActionType.ArticleCreated, id: article.id }; })) : []; const pages = (0, api_2.hasPermission)(api_1.CanGetPages, roles) ? page .findMany({ take: 50, include: { draft: true, pending: true, published: true }, orderBy: { createdAt: 'desc' } }) .then(val => val.map((page) => { return { date: page.createdAt, actionType: action_1.ActionType.PageCreated, id: page.id }; })) : []; const comments = (0, api_2.hasPermission)(api_1.CanGetComments, roles) ? comment .findMany({ take: 50, include: { revisions: true }, orderBy: { createdAt: 'desc' } }) .then(val => val.map((comment) => { return { date: comment.createdAt, actionType: action_1.ActionType.CommentCreated, id: comment.id }; })) : []; const authors = (0, api_2.hasPermission)(api_1.CanGetAuthors, roles) ? author.findMany({ take: 50, orderBy: { createdAt: 'desc' } }).then(val => val.map((author) => { return { date: author.createdAt, actionType: action_1.ActionType.AuthorCreated, id: author.id }; })) : []; const subscriptions = (0, api_2.hasPermission)(api_1.CanGetSubscriptions, roles) ? subscription.findMany({ take: 50, orderBy: { createdAt: 'desc' } }).then(val => val.map((subscription) => { return { date: subscription.createdAt, actionType: action_1.ActionType.SubscriptionCreated, id: subscription.id }; })) : []; const polls = (0, api_2.hasPermission)(api_1.CanGetPoll, roles) ? poll.findMany({ take: 50, orderBy: { createdAt: 'desc' } }).then(val => val.map((poll) => { return { date: poll.opensAt, actionType: action_1.ActionType.PollStarted, id: poll.id }; })) : []; const users = (0, api_2.hasPermission)(api_1.CanGetUsers, roles) ? user.findMany({ take: 50, orderBy: { createdAt: 'desc' } }).then(val => val.map((user) => { return { date: user.createdAt, actionType: action_1.ActionType.UserCreated, id: user.id }; })) : []; const events = (0, api_2.hasPermission)(api_1.CanGetEvent, roles) ? event.findMany({ take: 50, orderBy: { createdAt: 'desc' } }).then(val => val.map((event) => { return { date: event.createdAt, actionType: action_1.ActionType.EventCreated, id: event.id }; })) : []; const actions = (yield Promise.all([articles, pages, comments, authors, subscriptions, polls, users, events])).flat(); return actions .sort((v1, v2) => v2.date.getTime() - v1.date.getTime()) .slice(0, 50); }); exports.getActions = getActions; //# sourceMappingURL=action.private-queries.js.map