* chore: add jsdocs for limit * moar jsdocs * Replace find in deleteByID * (check this:) add missing locale * move findByID * Make findByID return only one document * _id => id * _id => id * Improve version types * Improve sortOrder types * move version stuff over * version stuff * fix: sort types * fix params * fix: findVersionByID generic * fix: type error for versions * remove unused value * fix: Document import * add todo * feat: updateOne to mongoose * remove unnecessary Model * more update stuff * remove unnecessary imports * remove unnecessary function arguments * fix: auth db update calls * fix: bad updateByID which made tests fail * fix: update returned docs to fix tests * fix: update from version using mongoose directly even though the Model does not exist * feat: implement deleteOne * fix: assign verificationToken only when it exists - fixes test * migrate saveVersion partly * feat: make dev:generate-graphql-schema work even without specifying extra argument * fix: this.db can be null * chore: use destructured locale where possible * chore: improve variable name * fix: SanitizedConfig type * feat: findGlobal database adapter * fix: flaky e2e test * chore: improve incorrect comment * chore: undo diffs * fix: id types * fix: id typing
95 lines
2.3 KiB
TypeScript
95 lines
2.3 KiB
TypeScript
import { Payload } from '../../payload';
|
|
import { docHasTimestamps, PayloadRequest, Where } from '../../types';
|
|
import { hasWhereAccessResult } from '../../auth';
|
|
import { AccessResult } from '../../config/types';
|
|
import { SanitizedCollectionConfig, TypeWithID } from '../../collections/config/types';
|
|
import sanitizeInternalFields from '../../utilities/sanitizeInternalFields';
|
|
import { appendVersionToQueryKey } from './appendVersionToQueryKey';
|
|
import { SanitizedGlobalConfig } from '../../globals/config/types';
|
|
import { combineQueries } from '../../database/combineQueries';
|
|
import { FindVersionArgs } from '../../database/types';
|
|
|
|
type Arguments<T> = {
|
|
entity: SanitizedCollectionConfig | SanitizedGlobalConfig
|
|
entityType: 'collection' | 'global'
|
|
doc: T
|
|
req: PayloadRequest
|
|
overrideAccess: boolean
|
|
accessResult: AccessResult
|
|
}
|
|
|
|
const replaceWithDraftIfAvailable = async <T extends TypeWithID>({
|
|
entity,
|
|
entityType,
|
|
doc,
|
|
req,
|
|
accessResult,
|
|
}: Arguments<T>): Promise<T> => {
|
|
const queryToBuild: Where = {
|
|
and: [
|
|
{
|
|
'version._status': {
|
|
equals: 'draft',
|
|
},
|
|
},
|
|
],
|
|
};
|
|
|
|
if (entityType === 'collection') {
|
|
queryToBuild.and.push({
|
|
parent: {
|
|
equals: doc.id,
|
|
},
|
|
});
|
|
}
|
|
|
|
if (docHasTimestamps(doc)) {
|
|
queryToBuild.and.push({
|
|
updatedAt: {
|
|
greater_than: doc.updatedAt,
|
|
},
|
|
});
|
|
}
|
|
|
|
let versionAccessResult;
|
|
|
|
if (hasWhereAccessResult(accessResult)) {
|
|
versionAccessResult = appendVersionToQueryKey(accessResult);
|
|
}
|
|
|
|
|
|
const findVersionArgs: FindVersionArgs = {
|
|
locale: req.locale,
|
|
where: combineQueries(queryToBuild, versionAccessResult),
|
|
collection: entity.slug,
|
|
limit: 1,
|
|
sortProperty: 'updatedAt',
|
|
sortOrder: 'desc',
|
|
|
|
};
|
|
|
|
const { docs: versionDocs } = await req.payload.db.findVersions<T>(findVersionArgs);
|
|
|
|
let draft = versionDocs[0];
|
|
|
|
|
|
if (!draft) {
|
|
return doc;
|
|
}
|
|
|
|
draft = JSON.parse(JSON.stringify(draft));
|
|
draft = sanitizeInternalFields(draft);
|
|
|
|
// Disregard all other draft content at this point,
|
|
// Only interested in the version itself.
|
|
// Operations will handle firing hooks, etc.
|
|
return {
|
|
id: doc.id,
|
|
...draft.version,
|
|
createdAt: draft.createdAt,
|
|
updatedAt: draft.updatedAt,
|
|
};
|
|
};
|
|
|
|
export default replaceWithDraftIfAvailable;
|