Files
payload/src/versions/saveGlobalVersion.ts
Dan Ribbens bab34d82f5 feat: add i18n to admin panel (#1326)
Co-authored-by: shikhantmaungs <shinkhantmaungs@gmail.com>
Co-authored-by: Thomas Ghysels <info@thomasg.be>
Co-authored-by: Kokutse Djoguenou <kokutse@Kokutses-MacBook-Pro.local>
Co-authored-by: Christian Gil <47041342+ChrisGV04@users.noreply.github.com>
Co-authored-by: Łukasz Rabiec <lukaszrabiec@gmail.com>
Co-authored-by: Jenny <jennifer.eberlei@gmail.com>
Co-authored-by: Hung Vu <hunghvu2017@gmail.com>
Co-authored-by: Shin Khant Maung <101539335+shinkhantmaungs@users.noreply.github.com>
Co-authored-by: Carlo Brualdi <carlo.brualdi@gmail.com>
Co-authored-by: Ariel Tonglet <ariel.tonglet@gmail.com>
Co-authored-by: Roman Ryzhikov <general+github@ya.ru>
Co-authored-by: maekoya <maekoya@stromatolite.jp>
Co-authored-by: Emilia Trollros <3m1l1a@emiliatrollros.se>
Co-authored-by: Kokutse J Djoguenou <90865585+Julesdj@users.noreply.github.com>
Co-authored-by: Mitch Dries <mitch.dries@gmail.com>

BREAKING CHANGE: If you assigned labels to collections, globals or block names, you need to update your config! Your GraphQL schema and generated Typescript interfaces may have changed. Payload no longer uses labels for code based naming. To prevent breaking changes to your GraphQL API and typescript types in your project, you can assign the below properties to match what Payload previously generated for you from labels.

On Collections
Use `graphQL.singularName`, `graphQL.pluralName` for GraphQL schema names.
Use `typescript.interface` for typescript generation name.

On Globals
Use `graphQL.name` for GraphQL Schema name.
Use `typescript.interface` for typescript generation name.

On Blocks (within Block fields)
Use `graphQL.singularName` for graphQL schema names.
2022-11-18 07:36:30 -05:00

95 lines
2.2 KiB
TypeScript

import { Payload } from '..';
import { enforceMaxVersions } from './enforceMaxVersions';
import { PayloadRequest } from '../express/types';
import { SanitizedGlobalConfig } from '../globals/config/types';
import sanitizeInternalFields from '../utilities/sanitizeInternalFields';
import { afterRead } from '../fields/hooks/afterRead';
type Args = {
payload: Payload
config?: SanitizedGlobalConfig
req: PayloadRequest
docWithLocales: any
}
export const saveGlobalVersion = async ({
payload,
config,
req,
docWithLocales,
}: Args): Promise<void> => {
const VersionModel = payload.versions[config.slug];
let version = docWithLocales;
if (config.versions?.drafts) {
const latestVersion = await VersionModel.findOne({
updatedAt: {
$gt: docWithLocales.updatedAt,
},
},
{},
{
lean: true,
leanWithId: true,
sort: {
updatedAt: 'desc',
},
});
if (latestVersion) {
// If the latest version is a draft, no need to re-save it
// Example: when "promoting" a draft to published, the draft already exists.
// Instead, return null
if (latestVersion?.version?._status === 'draft') {
return null;
}
version = latestVersion.version;
version = JSON.parse(JSON.stringify(version));
version = sanitizeInternalFields(version);
}
}
version = await afterRead({
depth: 0,
doc: version,
entityConfig: config,
flattenLocales: false,
overrideAccess: true,
req,
showHiddenFields: true,
});
if (version._id) delete version._id;
let createdVersion;
try {
createdVersion = await VersionModel.create({
version,
autosave: false,
});
} catch (err) {
payload.logger.error(`There was an error while saving a version for the Global ${config.slug}.`);
payload.logger.error(err);
}
if (config.versions.max) {
enforceMaxVersions({
payload: this,
Model: VersionModel,
slug: config.slug,
entityType: 'global',
max: config.versions.max,
});
}
if (createdVersion) {
createdVersion = JSON.parse(JSON.stringify(createdVersion));
createdVersion = sanitizeInternalFields(createdVersion);
}
return createdVersion;
};