This repository has been archived by the owner on Dec 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
index.ts
138 lines (129 loc) · 3.53 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import type { CollectionConfig } from 'payload/types'
import type { PluginConfig } from '../../types'
import createCharge from './hooks/createCharge'
import sendEmail from './hooks/sendEmail'
// all settings can be overridden by the config
export const generateSubmissionCollection = (formConfig: PluginConfig): CollectionConfig => {
const newConfig: CollectionConfig = {
...(formConfig?.formSubmissionOverrides || {}),
slug: formConfig?.formSubmissionOverrides?.slug || 'form-submissions',
access: {
create: () => true,
update: () => false,
read: ({ req: { user } }) => !!user, // logged-in users,
...(formConfig?.formSubmissionOverrides?.access || {}),
},
admin: {
...(formConfig?.formSubmissionOverrides?.admin || {}),
enableRichTextRelationship: false,
},
hooks: {
beforeChange: [
data => createCharge(data, formConfig),
data => sendEmail(data, formConfig),
...(formConfig?.formSubmissionOverrides?.hooks?.beforeChange || []),
],
...(formConfig?.formSubmissionOverrides?.hooks || {}),
},
fields: [
{
name: 'form',
type: 'relationship',
relationTo: formConfig?.formOverrides?.slug || 'forms',
required: true,
admin: {
readOnly: true,
},
},
{
name: 'submissionData',
type: 'array',
admin: {
readOnly: true,
},
fields: [
{
name: 'field',
type: 'text',
required: true,
},
{
name: 'value',
type: 'text',
required: true,
validate: (value: unknown) => {
// TODO:
// create a validation function that dynamically
// relies on the field type and its options as configured.
// How to access sibling data from this field?
// Need the `name` of the field in order to validate it.
// Might not be possible to use this validation function.
// Instead, might need to do all validation in a `beforeValidate` collection hook.
if (typeof value !== 'undefined') {
return true
}
return 'This field is required.'
},
},
],
},
...(formConfig?.formSubmissionOverrides?.fields || []),
],
}
const paymentFieldConfig = formConfig?.fields?.payment
if (paymentFieldConfig) {
newConfig.fields.push({
name: 'payment',
type: 'group',
admin: {
readOnly: true,
},
fields: [
{
name: 'field',
label: 'Field',
type: 'text',
},
{
name: 'status',
label: 'Status',
type: 'text',
},
{
name: 'amount',
type: 'number',
admin: {
description: 'Amount in cents',
},
},
{
name: 'paymentProcessor',
type: 'text',
},
{
name: 'creditCard',
label: 'Credit Card',
type: 'group',
fields: [
{
name: 'token',
label: 'token',
type: 'text',
},
{
name: 'brand',
label: 'Brand',
type: 'text',
},
{
name: 'number',
label: 'Number',
type: 'text',
},
],
},
],
})
}
return newConfig
}