-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
150 lines (112 loc) · 3.97 KB
/
index.js
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
139
140
141
142
143
144
145
146
147
148
149
150
const _ = require('lodash');
const {Testimonial} = require('./lib/testimonial');
const {TESTIMONIAL_RATING} = require('./lib/constants');
const {contactField, rangeField, textAreaField, isContactField} = require('./lib/fields');
module.exports = {
extend: 'apostrophe-pieces',
name: 'cms-testimonial',
label: 'Testimonial',
pluralLabel: 'Testimonials',
targetJoinType: '',
subscriberModule: 'hot-subscriber',
subscriberSourceKey: 'contact-event-source',
subscriberSourceValue: 'Testimonial',
addFields: [
contactField('Name', 'name'),
contactField('Email', 'email', false),
contactField('Phone', 'tel', false),
// set to false when the user chooses to unsubscribe
{
name: 'contact-create-subscriber',
type: 'boolean',
label: 'Subscribe to the blog',
def: false,
contextualOnly: true,
},
textAreaField('testimonial-content', 'Testimonial', true),
rangeField(TESTIMONIAL_RATING, 'Rating', true),
{
name: '_target',
type: 'joinByOne',
filters: {
projection: {
title: 1,
},
},
},
],
arrangeFields: [
{
label: 'Testimonial',
name: 'testimonial',
fields: ['_target', 'title', 'testimonial-content', TESTIMONIAL_RATING],
},
{
label: 'Contact',
name: 'contact',
fields: ['contact-name', 'contact-email', 'contact-phone', 'contact-create-subscriber'],
},
{
label: 'Admin',
name: 'admin',
fields: ['slug', 'published', 'tags'],
},
],
afterConstruct (self) {
self.setSubmitSchema();
},
construct (self, options) {
_.find(options.addFields, _.matchesProperty('name', '_target')).withType = options.targetJoinType;
_.find(options.addFields, _.matchesProperty('name', 'published')).def = false;
/**
* Fetch a Testimonial with all detail relating to the supplied targetId
*/
self.forTarget = (req, targetId, opt, callback) => {
self.find(req, {targetId})
.toArray((err, data) => {
if (err || !data) {
return callback(err);
}
callback(null, new Testimonial(data));
});
};
/**
* Integration hook - when a new testimonial has been created and a subscription should be created
* for the user at the same time, implement this method in your project.
*/
self.createSubscriber = function (req, piece, opt, callback) {
setImmediate(callback);
};
self.setSubmitSchema = function () {
const contactFieldNames = _.map(self.schema, 'name').filter(isUserEditable);
self.submitSchema = self.apos.schemas.subset(self.schema, [...contactFieldNames]);
self.udpateSchema = self.apos.schemas.subset(self.schema, ['slug', ...contactFieldNames]);
};
self.afterInsert = _.wrap(self.afterInsert, (superFn, req, piece, insertOptions, callback) => {
superFn(req, piece, insertOptions, (err, data) => {
if (!err && piece['contact-create-subscriber']) {
return self.createSubscriber(req, _.reduce(piece, contactFieldsCollector, {}), insertOptions, callback);
}
setImmediate(callback);
});
});
self.beforeInsert = (req, piece, insertOptions, callback) => {
piece.slug = self.apos.utils.generateId();
callback();
};
self.beforeSave = (req, piece, options, callback) => {
// TODO: option to update the linked piece with the new
// TODO: rating if published state is changing
setImmediate(callback);
};
}
};
function isUserEditable (fieldName) {
return /^(contact|testimonial)-/.test(fieldName);
}
function contactFieldsCollector (collector, value, name) {
if (isContactField(name)) {
collector[name] = value;
}
return collector;
}