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.6 KiB
Plaintext
170 lines
6.6 KiB
Plaintext
import { transformWhereQuery } from '../utilities/transformWhereQuery.js';
|
|
import { validateWhereQuery } from '../utilities/validateWhereQuery.js';
|
|
import { getAccess } from './access.js';
|
|
import { getConstraints } from './constraints.js';
|
|
import { operations } from './types.js';
|
|
export const queryPresetsCollectionSlug = 'payload-query-presets';
|
|
export const getQueryPresetsConfig = (config)=>({
|
|
slug: queryPresetsCollectionSlug,
|
|
access: getAccess(config),
|
|
admin: {
|
|
defaultColumns: [
|
|
'title',
|
|
'isShared',
|
|
'access',
|
|
'where',
|
|
'columns',
|
|
'groupBy'
|
|
],
|
|
hidden: true,
|
|
useAsTitle: 'title'
|
|
},
|
|
fields: [
|
|
{
|
|
name: 'title',
|
|
type: 'text',
|
|
required: true
|
|
},
|
|
{
|
|
name: 'isShared',
|
|
type: 'checkbox',
|
|
defaultValue: false,
|
|
validate: (isShared, { data })=>{
|
|
const typedData = data;
|
|
// ensure the `isShared` is only true if all constraints are 'onlyMe'
|
|
if (typedData?.access) {
|
|
const someOperationsAreShared = Object.values(typedData.access).some((operation)=>operation.constraint !== 'onlyMe');
|
|
if (!isShared && someOperationsAreShared) {
|
|
return 'If any constraint is not "onlyMe", the preset must be shared';
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
},
|
|
getConstraints(config),
|
|
{
|
|
name: 'where',
|
|
type: 'json',
|
|
admin: {
|
|
components: {
|
|
Cell: '@payloadcms/next/client#QueryPresetsWhereCell',
|
|
Field: '@payloadcms/next/client#QueryPresetsWhereField'
|
|
}
|
|
},
|
|
hooks: {
|
|
beforeValidate: [
|
|
({ data })=>{
|
|
// transform the "where" query here so that the client-side doesn't have to
|
|
if (data?.where) {
|
|
if (validateWhereQuery(data.where)) {
|
|
return data.where;
|
|
} else {
|
|
return transformWhereQuery(data.where);
|
|
}
|
|
}
|
|
return data?.where;
|
|
}
|
|
]
|
|
},
|
|
label: 'Filters'
|
|
},
|
|
{
|
|
name: 'columns',
|
|
type: 'json',
|
|
admin: {
|
|
components: {
|
|
Cell: '@payloadcms/next/client#QueryPresetsColumnsCell',
|
|
Field: '@payloadcms/next/client#QueryPresetsColumnField'
|
|
}
|
|
},
|
|
validate: (value)=>{
|
|
if (value) {
|
|
try {
|
|
JSON.parse(JSON.stringify(value));
|
|
} catch {
|
|
return 'Invalid JSON';
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
},
|
|
{
|
|
name: 'groupBy',
|
|
type: 'text',
|
|
admin: {
|
|
components: {
|
|
Cell: '@payloadcms/next/client#QueryPresetsGroupByCell',
|
|
Field: '@payloadcms/next/client#QueryPresetsGroupByField'
|
|
}
|
|
},
|
|
label: 'Group By'
|
|
},
|
|
{
|
|
name: 'relatedCollection',
|
|
type: 'select',
|
|
admin: {
|
|
hidden: true
|
|
},
|
|
options: config.collections ? config.collections.reduce((acc, collection)=>{
|
|
if (collection.enableQueryPresets) {
|
|
acc.push({
|
|
label: collection.labels?.plural || collection.slug,
|
|
value: collection.slug
|
|
});
|
|
}
|
|
return acc;
|
|
}, []) : [],
|
|
required: true
|
|
},
|
|
{
|
|
name: 'isTemp',
|
|
type: 'checkbox',
|
|
admin: {
|
|
description: "This is a temporary field used to determine if updating the preset would remove the user's access to it. When `true`, this record will be deleted after running the preset's `validate` function.",
|
|
disabled: true,
|
|
hidden: true
|
|
}
|
|
}
|
|
],
|
|
hooks: {
|
|
beforeValidate: [
|
|
({ data, operation })=>{
|
|
// TODO: type this
|
|
const typedData = data;
|
|
if (operation === 'create' || operation === 'update') {
|
|
// Ensure all operations have a constraint
|
|
operations.forEach((operation)=>{
|
|
if (!typedData.access) {
|
|
typedData.access = {};
|
|
}
|
|
if (!typedData.access?.[operation]) {
|
|
typedData[operation] = {};
|
|
}
|
|
// Ensure all operations have a constraint
|
|
if (!typedData.access[operation]?.constraint) {
|
|
typedData.access[operation] = {
|
|
...typedData.access[operation],
|
|
constraint: 'onlyMe'
|
|
};
|
|
}
|
|
});
|
|
// If at least one constraint is not `onlyMe` then `isShared` must be true
|
|
if (typedData?.access) {
|
|
const someOperationsAreShared = Object.values(typedData.access).some(// TODO: remove the `any` here
|
|
(operation)=>operation.constraint !== 'onlyMe');
|
|
typedData.isShared = someOperationsAreShared;
|
|
}
|
|
}
|
|
return typedData;
|
|
}
|
|
]
|
|
},
|
|
labels: {
|
|
plural: 'Presets',
|
|
singular: 'Preset',
|
|
...config.queryPresets?.labels || {}
|
|
},
|
|
lockDocuments: false
|
|
});
|
|
|
|
//# sourceMappingURL=config.js.map |