## Description This is the beta (v3) PR for the v2 PR [here](https://github.com/payloadcms/payload/pull/6857) Addresses #6800, #5108 - [x] I have read and understand the [CONTRIBUTING.md](https://github.com/payloadcms/payload/blob/main/CONTRIBUTING.md) document in this repository. ## Type of change - [x] Bug fix (non-breaking change which fixes an issue) ## Checklist: - [x] I have added tests that prove my fix is effective or that my feature works - [x] Existing test suite passes locally with my changes
63 lines
1.3 KiB
TypeScript
63 lines
1.3 KiB
TypeScript
import type { CreateGlobalVersion, Document, PayloadRequest } from 'payload'
|
|
|
|
import type { MongooseAdapter } from './index.js'
|
|
|
|
import { withSession } from './withSession.js'
|
|
|
|
export const createGlobalVersion: CreateGlobalVersion = async function createGlobalVersion(
|
|
this: MongooseAdapter,
|
|
{ autosave, createdAt, globalSlug, parent, req = {} as PayloadRequest, updatedAt, versionData },
|
|
) {
|
|
const VersionModel = this.versions[globalSlug]
|
|
const options = await withSession(this, req)
|
|
|
|
const [doc] = await VersionModel.create(
|
|
[
|
|
{
|
|
autosave,
|
|
createdAt,
|
|
latest: true,
|
|
parent,
|
|
updatedAt,
|
|
version: versionData,
|
|
},
|
|
],
|
|
options,
|
|
req,
|
|
)
|
|
|
|
await VersionModel.updateMany(
|
|
{
|
|
$and: [
|
|
{
|
|
_id: {
|
|
$ne: doc._id,
|
|
},
|
|
},
|
|
{
|
|
parent: {
|
|
$eq: parent,
|
|
},
|
|
},
|
|
{
|
|
latest: {
|
|
$eq: true,
|
|
},
|
|
},
|
|
],
|
|
},
|
|
{ $unset: { latest: 1 } },
|
|
options,
|
|
)
|
|
|
|
const result: Document = JSON.parse(JSON.stringify(doc))
|
|
const verificationToken = doc._verificationToken
|
|
|
|
// custom id type reset
|
|
result.id = result._id
|
|
if (verificationToken) {
|
|
result._verificationToken = verificationToken
|
|
}
|
|
return result
|
|
}
|