96 lines
1.9 KiB
TypeScript
96 lines
1.9 KiB
TypeScript
import { Copy } from "@/payload-types";
|
|
import { CollectionBeforeValidateHook, CollectionConfig } from "payload";
|
|
|
|
const beforeValidate: CollectionBeforeValidateHook = async ({ data, req }) => {
|
|
const doc = data as Copy
|
|
if (doc.label) return doc;
|
|
|
|
let bookName;
|
|
if (doc.book) {
|
|
const relatedBook = await req.payload.findByID({
|
|
req,
|
|
collection: 'books',
|
|
id: doc.book as number,
|
|
depth: 2,
|
|
})
|
|
bookName = relatedBook.title
|
|
} else return doc
|
|
|
|
let repositoryName;
|
|
if (doc.repository) {
|
|
const relatedRepo = await req.payload.findByID({
|
|
req,
|
|
collection: 'repositories',
|
|
id: doc.repository as number,
|
|
depth: 2,
|
|
})
|
|
repositoryName = relatedRepo.abbreviation || relatedRepo.name
|
|
} else return doc
|
|
|
|
return { ...doc, label: `[${repositoryName}] ${bookName}` }
|
|
}
|
|
|
|
export const Copies: CollectionConfig = {
|
|
slug: 'copies',
|
|
admin: {
|
|
useAsTitle: 'label',
|
|
pagination: {
|
|
limits: [10, 20],
|
|
},
|
|
},
|
|
fields: [
|
|
{
|
|
name: 'label',
|
|
type: 'text',
|
|
|
|
},
|
|
{
|
|
name: 'condition',
|
|
label: 'Condition out of 5',
|
|
type: 'number',
|
|
min: 1,
|
|
max: 5,
|
|
},
|
|
{
|
|
name: 'book',
|
|
type: 'relationship',
|
|
relationTo: 'books',
|
|
hasMany: false,
|
|
},
|
|
{
|
|
name: 'repository',
|
|
type: 'relationship',
|
|
relationTo: 'repositories',
|
|
hasMany: false,
|
|
},
|
|
{
|
|
name: 'notes',
|
|
type: 'richText'
|
|
},
|
|
{
|
|
name: 'holdRequests',
|
|
type: 'join',
|
|
collection: 'holdRequests',
|
|
on: 'copy',
|
|
where: {
|
|
or: [
|
|
{
|
|
isCheckedOut: {
|
|
equals: false
|
|
}
|
|
},
|
|
{
|
|
isCheckedOut: {
|
|
equals: null
|
|
}
|
|
}
|
|
]
|
|
}
|
|
}
|
|
],
|
|
hooks: {
|
|
beforeValidate: [beforeValidate],
|
|
//beforeRead: [beforeRead],
|
|
},
|
|
}
|