chore: passing tests
This commit is contained in:
@@ -75,7 +75,7 @@ export default function registerCollections(ctx: Payload): void {
|
||||
disableUnique: true,
|
||||
draftsEnabled: true,
|
||||
options: {
|
||||
timestamps: true,
|
||||
timestamps: false,
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
@@ -229,6 +229,7 @@ async function create(incomingArgs: Arguments): Promise<Document> {
|
||||
docWithLocales: result,
|
||||
autosave,
|
||||
createdAt: result.createdAt,
|
||||
onCreate: true,
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -254,7 +254,7 @@ async function update(incomingArgs: Arguments): Promise<Document> {
|
||||
id,
|
||||
autosave,
|
||||
draft: shouldSaveDraft,
|
||||
createdAt: result.createdAt as string,
|
||||
createdAt: originalDoc.createdAt,
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -30,7 +30,7 @@ export default function initGlobals(ctx: Payload): void {
|
||||
disableUnique: true,
|
||||
draftsEnabled: true,
|
||||
options: {
|
||||
timestamps: true,
|
||||
timestamps: false,
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
@@ -14,6 +14,20 @@ export const buildVersionCollectionFields = (collection: SanitizedCollectionConf
|
||||
type: 'group',
|
||||
fields: collection.fields,
|
||||
},
|
||||
{
|
||||
name: 'createdAt',
|
||||
type: 'date',
|
||||
admin: {
|
||||
disabled: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'updatedAt',
|
||||
type: 'date',
|
||||
admin: {
|
||||
disabled: true,
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
if (collection?.versions?.drafts && collection?.versions?.drafts?.autosave) {
|
||||
|
||||
@@ -8,6 +8,20 @@ export const buildVersionGlobalFields = (global: SanitizedGlobalConfig): Field[]
|
||||
type: 'group',
|
||||
fields: global.fields,
|
||||
},
|
||||
{
|
||||
name: 'createdAt',
|
||||
type: 'date',
|
||||
admin: {
|
||||
disabled: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'updatedAt',
|
||||
type: 'date',
|
||||
admin: {
|
||||
disabled: true,
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
if (global?.versions?.drafts && global?.versions?.drafts?.autosave) {
|
||||
|
||||
@@ -50,13 +50,21 @@ export const queryDrafts = async <T extends TypeWithID>({
|
||||
|
||||
const versionQuery = await VersionModel.buildQuery(versionQueryToBuild, locale);
|
||||
|
||||
// TODO
|
||||
// 1. Before group, we could potentially run $match to reduce matches
|
||||
// 2. Then before group, need to sort by updatedAt so first versions are newest versions
|
||||
// 3. Finally can group
|
||||
// 4. Then need to sort on user-defined sort again, if it differs from default
|
||||
|
||||
const aggregate = VersionModel.aggregate<AggregateVersion<T>>([
|
||||
// Sort so that newest are first
|
||||
{ $sort: { updatedAt: -1 } },
|
||||
// Group by parent ID, and take the first of each
|
||||
{
|
||||
$group: {
|
||||
_id: '$parent',
|
||||
version: { $first: '$version' },
|
||||
updatedAt: { $first: '$updatedAt' },
|
||||
createdAt: { $first: '$createdAt' },
|
||||
},
|
||||
},
|
||||
// Filter based on incoming query
|
||||
{ $match: versionQuery },
|
||||
// Re-sort based on incoming sort
|
||||
{
|
||||
$sort: Object.entries(paginationOptions.sort).reduce((sort, [key, order]) => {
|
||||
return {
|
||||
@@ -65,16 +73,7 @@ export const queryDrafts = async <T extends TypeWithID>({
|
||||
};
|
||||
}, {}),
|
||||
},
|
||||
{
|
||||
$group: {
|
||||
_id: '$parent',
|
||||
versionID: { $first: '$_id' },
|
||||
version: { $first: '$version' },
|
||||
updatedAt: { $first: '$updatedAt' },
|
||||
createdAt: { $first: '$createdAt' },
|
||||
},
|
||||
},
|
||||
{ $match: versionQuery },
|
||||
// Add pagination limit
|
||||
{ $limit: paginationOptions.limit },
|
||||
]);
|
||||
|
||||
|
||||
@@ -15,7 +15,8 @@ type Args = {
|
||||
id?: string | number
|
||||
autosave?: boolean
|
||||
draft?: boolean
|
||||
createdAt?: string
|
||||
createdAt: string
|
||||
onCreate?: boolean
|
||||
}
|
||||
|
||||
export const saveVersion = async ({
|
||||
@@ -27,6 +28,7 @@ export const saveVersion = async ({
|
||||
autosave,
|
||||
draft,
|
||||
createdAt,
|
||||
onCreate = false,
|
||||
}: Args): Promise<Record<string, unknown>> => {
|
||||
let entityConfig;
|
||||
let entityType: 'global' | 'collection';
|
||||
@@ -56,11 +58,14 @@ export const saveVersion = async ({
|
||||
}
|
||||
|
||||
let result;
|
||||
const now = new Date().toISOString();
|
||||
|
||||
try {
|
||||
if (autosave && existingAutosaveVersion?.autosave === true) {
|
||||
const data: Record<string, unknown> = {
|
||||
version: versionData,
|
||||
createdAt,
|
||||
updatedAt: now,
|
||||
};
|
||||
|
||||
if (createdAt) data.updatedAt = createdAt;
|
||||
@@ -77,9 +82,10 @@ export const saveVersion = async ({
|
||||
const data: Record<string, unknown> = {
|
||||
version: versionData,
|
||||
autosave: Boolean(autosave),
|
||||
updatedAt: onCreate ? createdAt : now,
|
||||
createdAt: createdAt || now,
|
||||
};
|
||||
|
||||
if (createdAt) data.createdAt = createdAt;
|
||||
if (collection) data.parent = id;
|
||||
|
||||
result = await VersionModel.create(data);
|
||||
|
||||
@@ -157,6 +157,11 @@ describe('Versions', () => {
|
||||
const versions = await payload.findVersions({
|
||||
collection,
|
||||
locale: 'all',
|
||||
where: {
|
||||
parent: {
|
||||
equals: collectionLocalPostID,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
expect(versions.docs[0].version.title.en).toStrictEqual(newEnglishTitle);
|
||||
|
||||
Reference in New Issue
Block a user