UNPKG

payload-plugin-scheduler

Version:
176 lines 10.6 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (g && (g = 0, op[0] && (_ = 0)), _) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.SafeRelationship = void 0; /** * Wrapper around the default `RelationshipField` that ensures "related" documents are published before the primary document */ var SafeRelationship = function (props) { var validate = function (value, options) { return __awaiter(void 0, void 0, void 0, function () { var validateRes, config, data, payload, req, relationsTo, values, relatedDraftCollections, relatedDocs, invalidRelatedDocs, publishDate; return __generator(this, function (_a) { switch (_a.label) { case 0: if (!props.validate) return [3 /*break*/, 2]; return [4 /*yield*/, props.validate(value, options)]; case 1: validateRes = _a.sent(); if (validateRes !== true) { return [2 /*return*/, validateRes]; } _a.label = 2; case 2: config = options.config, data = options.data, payload = options.payload, req = options.req; // can't run on the client if (!payload) { return [2 /*return*/, true]; } // abort if the field is empty if (!value || Array.isArray(value) && value.length === 0) { return [2 /*return*/, true]; } // abort if the current document is an unscheduled draft if ((data === null || data === void 0 ? void 0 : data._status) === 'draft' && !(data === null || data === void 0 ? void 0 : data.publish_date)) { return [2 /*return*/, true]; } relationsTo = Array.isArray(props.relationTo) ? props.relationTo : [props.relationTo]; values = Array.isArray(value) ? value : [value]; relatedDraftCollections = {}; relationsTo.forEach(function (name) { var _a; var collection = config.collections.find(function (_a) { var slug = _a.slug; return slug === name; }); if ((_a = collection === null || collection === void 0 ? void 0 : collection.versions) === null || _a === void 0 ? void 0 : _a.drafts) { relatedDraftCollections[collection.slug] = collection.admin.useAsTitle; } }); return [4 /*yield*/, values.reduce(function (accPromise, v) { return __awaiter(void 0, void 0, void 0, function () { var acc, collection, id, doc, error_1; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, accPromise]; case 1: acc = _a.sent(); collection = props.relationTo; id = v; // handle polymorphic relationships if (typeof v === 'object') { collection = v.relationTo; id = v.value; } // ignore related docs w/o drafts if (!relatedDraftCollections[collection]) { return [2 /*return*/, acc]; } _a.label = 2; case 2: _a.trys.push([2, 4, , 5]); return [4 /*yield*/, payload.findByID({ id: id, collection: collection, req: req, })]; case 3: doc = _a.sent(); // Only add the document if it's in draft status if (doc && doc._status === 'draft') { acc.push(__assign(__assign({}, doc), { collection: collection })); } return [3 /*break*/, 5]; case 4: error_1 = _a.sent(); payload.logger.error(error_1, "[SafeRelationship] ".concat(collection, ":").concat(id)); return [3 /*break*/, 5]; case 5: return [2 /*return*/, acc]; } }); }); }, Promise.resolve([]))]; case 3: relatedDocs = _a.sent(); invalidRelatedDocs = []; if ((data === null || data === void 0 ? void 0 : data._status) === 'published' || !(data === null || data === void 0 ? void 0 : data._status)) { // for published docs, or docs w/o drafts, we assume they are currently being published publishDate = new Date(); } else if ((data === null || data === void 0 ? void 0 : data._status) === 'draft' && (data === null || data === void 0 ? void 0 : data.publish_date)) { // for scheduled documents, use the set publish_date publishDate = new Date(data.publish_date); } // loop over the related documents and find any stragglers relatedDocs .forEach(function (rd) { var docPubDate = rd.publish_date ? new Date(rd.publish_date) : null; // this check is necessary otherwise new Date(null) => 1/1/70 if (!docPubDate || docPubDate >= publishDate) { invalidRelatedDocs.push({ collection: rd.collection, title: rd[relatedDraftCollections[rd.collection]] }); } }); if (invalidRelatedDocs.length > 0) { return [2 /*return*/, "The following docs must be published before this one: ".concat(invalidRelatedDocs.map(function (_a) { var collection = _a.collection, title = _a.title; return "".concat(title, " (").concat(collection, ")"); }).join(', '))]; } return [2 /*return*/, true]; } }); }); }; // eslint-disable-next-line @typescript-eslint/consistent-type-assertions return __assign(__assign({ type: 'relationship' }, props), { validate: validate }); }; exports.SafeRelationship = SafeRelationship; //# sourceMappingURL=index.js.map