feat: builds global publishVersion
This commit is contained in:
@@ -148,7 +148,7 @@ async function find<T extends TypeWithID = any>(incomingArgs: Arguments): Promis
|
||||
docs: await Promise.all(result.docs.map(async (doc) => replaceWithDraftIfAvailable({
|
||||
accessResult,
|
||||
payload: this,
|
||||
collection: collectionConfig,
|
||||
entity: collectionConfig,
|
||||
doc,
|
||||
locale,
|
||||
}))),
|
||||
|
||||
@@ -141,7 +141,7 @@ async function findByID<T extends TypeWithID = any>(this: Payload, incomingArgs:
|
||||
if (collectionConfig.versions?.drafts && draftEnabled) {
|
||||
result = await replaceWithDraftIfAvailable({
|
||||
payload: this,
|
||||
collection: collectionConfig,
|
||||
entity: collectionConfig,
|
||||
doc: result,
|
||||
accessResult,
|
||||
locale,
|
||||
|
||||
@@ -41,7 +41,7 @@ async function publishVersion<T extends TypeWithID = any>(this: Payload, args: A
|
||||
}
|
||||
|
||||
// /////////////////////////////////////
|
||||
// Retrieve original raw version to get parent ID
|
||||
// Retrieve original raw version
|
||||
// /////////////////////////////////////
|
||||
|
||||
const VersionModel = this.versions[collectionConfig.slug];
|
||||
|
||||
@@ -1,26 +1,28 @@
|
||||
import executeAccess from '../../auth/executeAccess';
|
||||
import sanitizeInternalFields from '../../utilities/sanitizeInternalFields';
|
||||
import replaceWithDraftIfAvailable from '../../versions/drafts/replaceWithDraftIfAvailable';
|
||||
|
||||
async function findOne(args) {
|
||||
const { globals: { Model } } = this;
|
||||
|
||||
const {
|
||||
globalConfig,
|
||||
locale,
|
||||
req,
|
||||
slug,
|
||||
depth,
|
||||
showHiddenFields,
|
||||
draft = false,
|
||||
draft: draftEnabled = false,
|
||||
} = args;
|
||||
|
||||
// /////////////////////////////////////
|
||||
// 1. Retrieve and execute access
|
||||
// Retrieve and execute access
|
||||
// /////////////////////////////////////
|
||||
|
||||
await executeAccess({ req }, globalConfig.access.read);
|
||||
const accessResult = await executeAccess({ req }, globalConfig.access.read);
|
||||
|
||||
// /////////////////////////////////////
|
||||
// 2. Perform database operation
|
||||
// Perform database operation
|
||||
// /////////////////////////////////////
|
||||
|
||||
let doc = await Model.findOne({ globalType: slug }).lean();
|
||||
@@ -36,6 +38,20 @@ async function findOne(args) {
|
||||
doc = JSON.parse(doc);
|
||||
doc = sanitizeInternalFields(doc);
|
||||
|
||||
// /////////////////////////////////////
|
||||
// Replace document with draft if available
|
||||
// /////////////////////////////////////
|
||||
|
||||
if (globalConfig.versions?.drafts && draftEnabled) {
|
||||
doc = await replaceWithDraftIfAvailable({
|
||||
payload: this,
|
||||
entity: globalConfig,
|
||||
doc,
|
||||
locale,
|
||||
accessResult,
|
||||
});
|
||||
}
|
||||
|
||||
// /////////////////////////////////////
|
||||
// 3. Execute before collection hook
|
||||
// /////////////////////////////////////
|
||||
|
||||
@@ -119,7 +119,7 @@ async function findVersionByID<T extends TypeWithVersion<T> = any>(args: Argumen
|
||||
});
|
||||
|
||||
// /////////////////////////////////////
|
||||
// afterRead - Collection
|
||||
// afterRead - Global
|
||||
// /////////////////////////////////////
|
||||
|
||||
await globalConfig.hooks.afterRead.reduce(async (priorHook, hook) => {
|
||||
|
||||
@@ -127,7 +127,7 @@ async function findVersions<T extends TypeWithVersion<T> = any>(args: Arguments)
|
||||
};
|
||||
|
||||
// /////////////////////////////////////
|
||||
// afterRead - Collection
|
||||
// afterRead - Global
|
||||
// /////////////////////////////////////
|
||||
|
||||
result = {
|
||||
|
||||
@@ -3,15 +3,15 @@ import { PayloadRequest } from '../../express/types';
|
||||
import executeAccess from '../../auth/executeAccess';
|
||||
import sanitizeInternalFields from '../../utilities/sanitizeInternalFields';
|
||||
import { PaginatedDocs } from '../../mongoose/types';
|
||||
import { hasWhereAccessResult } from '../../auth/types';
|
||||
import flattenWhereConstraints from '../../utilities/flattenWhereConstraints';
|
||||
import { buildSortParam } from '../../mongoose/buildSortParam';
|
||||
import { TypeWithVersion } from '../../versions/types';
|
||||
import { SanitizedGlobalConfig } from '../config/types';
|
||||
import { Payload } from '../..';
|
||||
import { NotFound } from '../../errors';
|
||||
|
||||
export type Arguments = {
|
||||
globalConfig: SanitizedGlobalConfig
|
||||
where?: Where
|
||||
id: string
|
||||
page?: number
|
||||
limit?: number
|
||||
sort?: string
|
||||
@@ -23,138 +23,122 @@ export type Arguments = {
|
||||
|
||||
// TODO: finish
|
||||
|
||||
async function publishVersion<T extends TypeWithVersion<T> = any>(args: Arguments): Promise<PaginatedDocs<T>> {
|
||||
async function publishVersion<T extends TypeWithVersion<T> = any>(this: Payload, args: Arguments): Promise<PaginatedDocs<T>> {
|
||||
const { globals: { Model } } = this;
|
||||
|
||||
const {
|
||||
where,
|
||||
page,
|
||||
limit,
|
||||
id,
|
||||
depth,
|
||||
globalConfig,
|
||||
req,
|
||||
req: {
|
||||
locale,
|
||||
},
|
||||
overrideAccess,
|
||||
showHiddenFields,
|
||||
} = args;
|
||||
|
||||
const VersionsModel = this.versions[globalConfig.slug];
|
||||
|
||||
// /////////////////////////////////////
|
||||
// Access
|
||||
// /////////////////////////////////////
|
||||
|
||||
const queryToBuild: { where?: Where} = {};
|
||||
let useEstimatedCount = false;
|
||||
|
||||
if (where) {
|
||||
let and = [];
|
||||
|
||||
if (Array.isArray(where.and)) and = where.and;
|
||||
if (Array.isArray(where.AND)) and = where.AND;
|
||||
|
||||
queryToBuild.where = {
|
||||
...where,
|
||||
and: [
|
||||
...and,
|
||||
],
|
||||
};
|
||||
|
||||
const constraints = flattenWhereConstraints(queryToBuild);
|
||||
|
||||
useEstimatedCount = constraints.some((prop) => Object.keys(prop).some((key) => key === 'near'));
|
||||
}
|
||||
|
||||
if (!overrideAccess) {
|
||||
const accessResults = await executeAccess({ req }, globalConfig.access.readVersions);
|
||||
|
||||
if (hasWhereAccessResult(accessResults)) {
|
||||
if (!where) {
|
||||
queryToBuild.where = {
|
||||
and: [
|
||||
accessResults,
|
||||
],
|
||||
};
|
||||
} else {
|
||||
(queryToBuild.where.and as Where[]).push(accessResults);
|
||||
}
|
||||
}
|
||||
await executeAccess({ req }, globalConfig.access.update);
|
||||
}
|
||||
|
||||
const query = await VersionsModel.buildQuery(queryToBuild, locale);
|
||||
|
||||
// /////////////////////////////////////
|
||||
// Find
|
||||
// Retrieve original raw version
|
||||
// /////////////////////////////////////
|
||||
|
||||
const [sortProperty, sortOrder] = buildSortParam(args.sort, true);
|
||||
const VersionModel = this.versions[globalConfig.slug];
|
||||
|
||||
const optionsToExecute = {
|
||||
page: page || 1,
|
||||
limit: limit || 10,
|
||||
sort: {
|
||||
[sortProperty]: sortOrder,
|
||||
},
|
||||
lean: true,
|
||||
leanWithId: true,
|
||||
useEstimatedCount,
|
||||
};
|
||||
let rawVersion = await VersionModel.findOne({
|
||||
_id: id,
|
||||
});
|
||||
|
||||
const paginatedDocs = await VersionsModel.paginate(query, optionsToExecute);
|
||||
if (!rawVersion) {
|
||||
throw new NotFound();
|
||||
}
|
||||
|
||||
rawVersion = rawVersion.toJSON({ virtuals: true });
|
||||
|
||||
// /////////////////////////////////////
|
||||
// Update global
|
||||
// /////////////////////////////////////
|
||||
|
||||
const global = await Model.findOne({ globalType: globalConfig.slug });
|
||||
|
||||
let result;
|
||||
|
||||
if (global) {
|
||||
result = await Model.findOneAndUpdate(
|
||||
{ globalType: globalConfig.slug },
|
||||
rawVersion.version,
|
||||
{ new: true },
|
||||
);
|
||||
} else {
|
||||
result = await Model.create(result);
|
||||
}
|
||||
|
||||
result = result.toJSON({ virtuals: true });
|
||||
|
||||
// custom id type reset
|
||||
result.id = result._id;
|
||||
result = JSON.stringify(result);
|
||||
result = JSON.parse(result);
|
||||
result = sanitizeInternalFields(result);
|
||||
|
||||
// /////////////////////////////////////
|
||||
// afterRead - Fields
|
||||
// /////////////////////////////////////
|
||||
|
||||
let result = {
|
||||
...paginatedDocs,
|
||||
docs: await Promise.all(paginatedDocs.docs.map(async (data) => ({
|
||||
...data,
|
||||
version: await this.performFieldOperations(
|
||||
globalConfig,
|
||||
{
|
||||
depth,
|
||||
data: data.version,
|
||||
req,
|
||||
id: data.version.id,
|
||||
hook: 'afterRead',
|
||||
operation: 'read',
|
||||
overrideAccess,
|
||||
flattenLocales: true,
|
||||
showHiddenFields,
|
||||
isVersion: true,
|
||||
},
|
||||
),
|
||||
}))),
|
||||
};
|
||||
result = await this.performFieldOperations(globalConfig, {
|
||||
data: result,
|
||||
hook: 'afterRead',
|
||||
operation: 'read',
|
||||
req,
|
||||
depth,
|
||||
showHiddenFields,
|
||||
flattenLocales: true,
|
||||
overrideAccess,
|
||||
});
|
||||
|
||||
// /////////////////////////////////////
|
||||
// afterRead - Collection
|
||||
// afterRead - Global
|
||||
// /////////////////////////////////////
|
||||
|
||||
result = {
|
||||
...result,
|
||||
docs: await Promise.all(result.docs.map(async (doc) => {
|
||||
const docRef = doc;
|
||||
await globalConfig.hooks.afterChange.reduce(async (priorHook, hook) => {
|
||||
await priorHook;
|
||||
|
||||
await globalConfig.hooks.afterRead.reduce(async (priorHook, hook) => {
|
||||
await priorHook;
|
||||
|
||||
docRef.version = await hook({ req, query, doc: doc.version }) || doc.version;
|
||||
}, Promise.resolve());
|
||||
|
||||
return docRef;
|
||||
})),
|
||||
};
|
||||
result = await hook({
|
||||
doc: result,
|
||||
req,
|
||||
}) || result;
|
||||
}, Promise.resolve());
|
||||
|
||||
// /////////////////////////////////////
|
||||
// Return results
|
||||
// afterChange - Fields
|
||||
// /////////////////////////////////////
|
||||
|
||||
result = {
|
||||
...result,
|
||||
docs: result.docs.map((doc) => sanitizeInternalFields<T>(doc)),
|
||||
};
|
||||
result = await this.performFieldOperations(globalConfig, {
|
||||
data: result,
|
||||
hook: 'afterChange',
|
||||
operation: 'update',
|
||||
req,
|
||||
depth,
|
||||
overrideAccess,
|
||||
showHiddenFields,
|
||||
});
|
||||
|
||||
// /////////////////////////////////////
|
||||
// afterChange - Global
|
||||
// /////////////////////////////////////
|
||||
|
||||
await globalConfig.hooks.afterChange.reduce(async (priorHook, hook) => {
|
||||
await priorHook;
|
||||
|
||||
result = await hook({
|
||||
doc: result,
|
||||
req,
|
||||
}) || result;
|
||||
}, Promise.resolve());
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -186,6 +186,33 @@ async function update<T extends TypeWithID = any>(this: Payload, args): Promise<
|
||||
}) || global;
|
||||
}, Promise.resolve());
|
||||
|
||||
// /////////////////////////////////////
|
||||
// afterChange - Fields
|
||||
// /////////////////////////////////////
|
||||
|
||||
global = await this.performFieldOperations(globalConfig, {
|
||||
data: global,
|
||||
hook: 'afterChange',
|
||||
operation: 'update',
|
||||
req,
|
||||
depth,
|
||||
overrideAccess,
|
||||
showHiddenFields,
|
||||
});
|
||||
|
||||
// /////////////////////////////////////
|
||||
// afterChange - Collection
|
||||
// /////////////////////////////////////
|
||||
|
||||
await globalConfig.hooks.afterChange.reduce(async (priorHook, hook) => {
|
||||
await priorHook;
|
||||
|
||||
global = await hook({
|
||||
doc: global,
|
||||
req,
|
||||
}) || result;
|
||||
}, Promise.resolve());
|
||||
|
||||
// /////////////////////////////////////
|
||||
// Return results
|
||||
// /////////////////////////////////////
|
||||
|
||||
@@ -6,10 +6,11 @@ import { CollectionModel, SanitizedCollectionConfig, TypeWithID } from '../../co
|
||||
import flattenWhereConstraints from '../../utilities/flattenWhereConstraints';
|
||||
import sanitizeInternalFields from '../../utilities/sanitizeInternalFields';
|
||||
import { appendVersionToQueryKey } from './appendVersionToQueryKey';
|
||||
import { SanitizedGlobalConfig } from '../../globals/config/types';
|
||||
|
||||
type Arguments<T> = {
|
||||
payload: Payload
|
||||
collection: SanitizedCollectionConfig
|
||||
entity: SanitizedCollectionConfig | SanitizedGlobalConfig
|
||||
doc: T
|
||||
locale: string
|
||||
accessResult: AccessResult
|
||||
@@ -17,13 +18,13 @@ type Arguments<T> = {
|
||||
|
||||
const replaceWithDraftIfAvailable = async <T extends TypeWithID>({
|
||||
payload,
|
||||
collection,
|
||||
entity,
|
||||
doc,
|
||||
locale,
|
||||
accessResult,
|
||||
}: Arguments<T>): Promise<T> => {
|
||||
if (docHasTimestamps(doc)) {
|
||||
const VersionModel = payload.versions[collection.slug] as CollectionModel;
|
||||
const VersionModel = payload.versions[entity.slug] as CollectionModel;
|
||||
|
||||
let useEstimatedCount = false;
|
||||
const queryToBuild: { where: Where } = {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import path from 'path';
|
||||
import MiniCSSExtractPlugin from 'mini-css-extract-plugin';
|
||||
import TerserJSPlugin from 'terser-webpack-plugin';
|
||||
import OptimizeCSSAssetsPlugin from 'optimize-css-assets-webpack-plugin';
|
||||
import OptimizeCSSAssetsPlugin from 'css-minimizer-webpack-plugin';
|
||||
|
||||
export default {
|
||||
entry: {
|
||||
|
||||
Reference in New Issue
Block a user