@wepublish/api
Version:
API core for we.publish.
82 lines • 3.82 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getPublishedArticleByIdOrSlug = exports.getPublishedArticles = void 0;
const tslib_1 = require("tslib");
const api_1 = require("../../../../authentication-api/src");
const api_2 = require("../../../../utils-api/src");
const article_queries_1 = require("./article.queries");
const getPublishedArticles = (filter, sortedField, order, cursorId, skip, take, article) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
const data = yield (0, article_queries_1.getArticles)(Object.assign(Object.assign({}, filter), { published: true }), sortedField, order, cursorId, skip, take, article);
return Object.assign(Object.assign({}, data), { nodes: data.nodes.map(({ id, shared, published }) => (Object.assign(Object.assign({ shared }, published), { id }))) });
});
exports.getPublishedArticles = getPublishedArticles;
const getPublishedArticleByIdOrSlug = (id, slug, token, session, verifyJWT, publicArticles, articles, articleClient) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
var _a;
let article = id ? yield publicArticles.load(id) : null;
if (!article && slug) {
const fullArticle = yield articleClient.findFirst({
where: {
OR: [
{
published: {
is: {
slug
}
}
},
{
pending: {
is: {
slug
}
}
}
]
},
include: {
draft: {
include: {
properties: true,
authors: true,
socialMediaAuthors: true
}
},
pending: {
include: {
properties: true,
authors: true,
socialMediaAuthors: true
}
},
published: {
include: {
properties: true,
authors: true,
socialMediaAuthors: true
}
}
}
});
article = fullArticle
? Object.assign(Object.assign({}, ((_a = fullArticle === null || fullArticle === void 0 ? void 0 : fullArticle.published) !== null && _a !== void 0 ? _a : fullArticle.pending)), { id: fullArticle.id, shared: fullArticle === null || fullArticle === void 0 ? void 0 : fullArticle.shared })
: null;
}
if (!article && token) {
try {
const articleId = verifyJWT(token);
const privateArticle = yield articles.load(articleId);
article = (privateArticle === null || privateArticle === void 0 ? void 0 : privateArticle.draft)
? Object.assign(Object.assign({}, privateArticle.draft), { id: privateArticle.id, shared: privateArticle.shared, updatedAt: new Date(), publishedAt: new Date() })
: null;
}
catch (error) {
(0, api_2.logger)('graphql-query').warn(error, 'Error while verifying token with article id.');
}
}
if ((session === null || session === void 0 ? void 0 : session.type) === api_1.AuthSessionType.Token) {
return (article === null || article === void 0 ? void 0 : article.shared) ? article : null;
}
return article;
});
exports.getPublishedArticleByIdOrSlug = getPublishedArticleByIdOrSlug;
//# sourceMappingURL=article.public-queries.js.map