Some checks failed
Build & Deploy / 🔍 Prepare (push) Successful in 20s
Build & Deploy / 🧪 QA (push) Failing after 34s
Build & Deploy / 🏗️ Build (push) Has started running
Build & Deploy / 🚀 Deploy (push) Has been cancelled
Build & Deploy / 🧪 Smoke Test (push) Has been cancelled
Build & Deploy / ⚡ Lighthouse (push) Has been cancelled
Build & Deploy / 🔔 Notify (push) Has been cancelled
170 lines
6.0 KiB
Plaintext
170 lines
6.0 KiB
Plaintext
import { executeAccess } from '../../auth/executeAccess.js';
|
|
import { NotFound } from '../../errors/index.js';
|
|
import { afterChange } from '../../fields/hooks/afterChange/index.js';
|
|
import { afterRead } from '../../fields/hooks/afterRead/index.js';
|
|
import { commitTransaction } from '../../utilities/commitTransaction.js';
|
|
import { initTransaction } from '../../utilities/initTransaction.js';
|
|
import { killTransaction } from '../../utilities/killTransaction.js';
|
|
export const restoreVersionOperation = async (args)=>{
|
|
const { id, depth, draft, globalConfig, overrideAccess, populate, showHiddenFields } = args;
|
|
const req = args.req;
|
|
const { fallbackLocale, locale, payload } = req;
|
|
try {
|
|
const shouldCommit = await initTransaction(req);
|
|
// /////////////////////////////////////
|
|
// beforeOperation - Global
|
|
// /////////////////////////////////////
|
|
if (globalConfig.hooks?.beforeOperation?.length) {
|
|
for (const hook of globalConfig.hooks.beforeOperation){
|
|
args = await hook({
|
|
args,
|
|
context: req.context,
|
|
global: globalConfig,
|
|
operation: 'restoreVersion',
|
|
overrideAccess,
|
|
req
|
|
}) || args;
|
|
}
|
|
}
|
|
// /////////////////////////////////////
|
|
// Access
|
|
// /////////////////////////////////////
|
|
if (!overrideAccess) {
|
|
await executeAccess({
|
|
req
|
|
}, globalConfig.access.update);
|
|
}
|
|
// /////////////////////////////////////
|
|
// Retrieve original raw version
|
|
// /////////////////////////////////////
|
|
const { docs: versionDocs } = await payload.db.findGlobalVersions({
|
|
global: globalConfig.slug,
|
|
limit: 1,
|
|
req,
|
|
where: {
|
|
id: {
|
|
equals: id
|
|
}
|
|
}
|
|
});
|
|
if (!versionDocs || versionDocs.length === 0) {
|
|
throw new NotFound(req.t);
|
|
}
|
|
const rawVersion = versionDocs[0];
|
|
// Patch globalType onto version doc
|
|
rawVersion.version.globalType = globalConfig.slug;
|
|
// Overwrite draft status if draft is true
|
|
if (draft) {
|
|
rawVersion.version._status = 'draft';
|
|
}
|
|
// /////////////////////////////////////
|
|
// fetch previousDoc
|
|
// /////////////////////////////////////
|
|
const previousDoc = await payload.findGlobal({
|
|
slug: globalConfig.slug,
|
|
depth,
|
|
req
|
|
});
|
|
// /////////////////////////////////////
|
|
// Update global
|
|
// /////////////////////////////////////
|
|
const global = await payload.db.findGlobal({
|
|
slug: globalConfig.slug,
|
|
req
|
|
});
|
|
let result = rawVersion.version;
|
|
if (global) {
|
|
// Ensure updatedAt date is always updated
|
|
result.updatedAt = new Date().toISOString();
|
|
result = await payload.db.updateGlobal({
|
|
slug: globalConfig.slug,
|
|
data: result,
|
|
req
|
|
});
|
|
const now = new Date().toISOString();
|
|
result = await payload.db.createGlobalVersion({
|
|
autosave: false,
|
|
createdAt: result.createdAt ? new Date(result.createdAt).toISOString() : now,
|
|
globalSlug: globalConfig.slug,
|
|
req,
|
|
updatedAt: draft ? now : new Date(result.updatedAt).toISOString(),
|
|
versionData: result
|
|
});
|
|
} else {
|
|
result = await payload.db.createGlobal({
|
|
slug: globalConfig.slug,
|
|
data: result,
|
|
req
|
|
});
|
|
}
|
|
// /////////////////////////////////////
|
|
// afterRead - Fields
|
|
// /////////////////////////////////////
|
|
result = await afterRead({
|
|
collection: null,
|
|
context: req.context,
|
|
depth: depth,
|
|
doc: result,
|
|
draft: undefined,
|
|
fallbackLocale: fallbackLocale,
|
|
global: globalConfig,
|
|
locale: locale,
|
|
overrideAccess: overrideAccess,
|
|
populate,
|
|
req,
|
|
showHiddenFields: showHiddenFields
|
|
});
|
|
// /////////////////////////////////////
|
|
// afterRead - Global
|
|
// /////////////////////////////////////
|
|
if (globalConfig.hooks?.afterRead?.length) {
|
|
for (const hook of globalConfig.hooks.afterRead){
|
|
result = await hook({
|
|
context: req.context,
|
|
doc: result,
|
|
global: globalConfig,
|
|
overrideAccess,
|
|
req
|
|
}) || result;
|
|
}
|
|
}
|
|
// /////////////////////////////////////
|
|
// afterChange - Fields
|
|
// /////////////////////////////////////
|
|
result = await afterChange({
|
|
collection: null,
|
|
context: req.context,
|
|
data: result,
|
|
doc: result,
|
|
global: globalConfig,
|
|
operation: 'update',
|
|
previousDoc,
|
|
req
|
|
});
|
|
// /////////////////////////////////////
|
|
// afterChange - Global
|
|
// /////////////////////////////////////
|
|
if (globalConfig.hooks?.afterChange?.length) {
|
|
for (const hook of globalConfig.hooks.afterChange){
|
|
result = await hook({
|
|
context: req.context,
|
|
data: result,
|
|
doc: result,
|
|
global: globalConfig,
|
|
overrideAccess,
|
|
previousDoc,
|
|
req
|
|
}) || result;
|
|
}
|
|
}
|
|
if (shouldCommit) {
|
|
await commitTransaction(req);
|
|
}
|
|
return result;
|
|
} catch (error) {
|
|
await killTransaction(req);
|
|
throw error;
|
|
}
|
|
};
|
|
|
|
//# sourceMappingURL=restoreVersion.js.map |