86 lines
1.8 KiB
TypeScript
86 lines
1.8 KiB
TypeScript
import { CollectionConfig } from "payload";
|
|
|
|
const HoldRequests: CollectionConfig = {
|
|
slug: 'holdRequests',
|
|
fields: [
|
|
{
|
|
name: 'copy',
|
|
type: 'relationship',
|
|
relationTo: 'copies',
|
|
filterOptions: ({ data }) => {
|
|
return {
|
|
book: {
|
|
equals: data.book
|
|
},
|
|
}
|
|
}
|
|
},
|
|
{
|
|
name: 'book',
|
|
type: 'relationship',
|
|
relationTo: 'books',
|
|
required: true,
|
|
},
|
|
{
|
|
name: 'repository',
|
|
type: 'relationship',
|
|
relationTo: 'repositories',
|
|
},
|
|
{
|
|
name: 'userRequested',
|
|
type: 'relationship',
|
|
relationTo: 'users',
|
|
required: true,
|
|
},
|
|
{
|
|
name: 'dateRequested',
|
|
type: 'date',
|
|
},
|
|
{
|
|
name: 'isHolding',
|
|
type: 'checkbox',
|
|
hooks: {
|
|
beforeValidate: [({ data, originalDoc }) => {
|
|
if (data?.isHolding && !data.copy) return originalDoc
|
|
}]
|
|
}
|
|
},
|
|
{
|
|
name: 'holdingUntilDate',
|
|
type: 'date',
|
|
},
|
|
{
|
|
name: 'isRejected',
|
|
type: 'checkbox',
|
|
},
|
|
{
|
|
name: 'isCheckedOut',
|
|
type: 'checkbox',
|
|
hooks:
|
|
{
|
|
beforeValidate: [({ data, originalDoc }) => {
|
|
if (originalDoc.isCheckedOut) return originalDoc
|
|
if (data?.isCheckedOut && !originalDoc.copy) return originalDoc
|
|
if (originalDoc.isCheckedOut && !data?.isCheckedOut) return originalDoc
|
|
}],
|
|
afterChange: [({ value, data, req }) => {
|
|
if (value) {
|
|
req.payload.create({
|
|
collection: 'checkouts',
|
|
data: {
|
|
user: data?.userRequested,
|
|
copy: data?.copy,
|
|
}
|
|
})
|
|
|
|
return data
|
|
}
|
|
}]
|
|
}
|
|
|
|
}
|
|
],
|
|
}
|
|
|
|
export default HoldRequests
|