-
Notifications
You must be signed in to change notification settings - Fork 21
/
resource.js
203 lines (177 loc) · 6.89 KB
/
resource.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
const _ = require('lodash');
const P = require('bluebird');
const moment = require('moment');
const semver = require('semver');
const JSONAPISerializer = require('jsonapi-serializer').Serializer;
const SmartFieldsValuesInjector = require('../services/smart-fields-values-injector');
const Schemas = require('../generators/schemas');
const logger = require('../services/logger');
function ResourceSerializer(Implementation, model, records, integrator, opts, meta,
fieldsSearched, searchValue, fieldsPerModel) {
var modelName = Implementation.getModelName(model);
var schema = Schemas.schemas[modelName];
const needsDateOnlyFormating = Implementation.getLianaName() === 'forest-express-sequelize' &&
semver.lt(Implementation.getOrmVersion(), '4.0.0');
var reservedWords = ['meta'];
var fieldInfoDateonly = [];
var fieldInfoPoint = [];
function getFieldsNames(fields) {
return fields.map(function (field) {
if (reservedWords.indexOf(field.field) > -1) {
return field.field + ':namespace' + field.field;
} else {
return field.field;
}
});
}
function detectFieldWithSpecialFormat(field, fieldReference) {
if (field.type === 'Dateonly' && needsDateOnlyFormating) {
fieldInfoDateonly.push({ name: field.field, association: fieldReference });
}
if (field.type === 'Point') {
fieldInfoPoint.push({ name: field.field, association: fieldReference });
}
}
this.perform = function () {
var typeForAttributes = {};
function getAttributesFor(dest, fields) {
_.map(fields, function (field) {
detectFieldWithSpecialFormat(field);
if (field.integration) {
if (integrator) {
integrator.defineSerializationOption(model, schema, dest, field);
}
} else {
var fieldName = field.field;
if (reservedWords.indexOf(fieldName) > -1) {
fieldName = 'namespace' + fieldName;
}
if (_.isPlainObject(field.type)) {
dest[fieldName] = {
attributes: getFieldsNames(field.type.fields)
};
getAttributesFor(dest[field.field], field.type.fields);
} else if (field.reference) {
var referenceType = field.reference.split('.')[0];
var referenceSchema = Schemas.schemas[referenceType];
typeForAttributes[field.field] = referenceType;
if (!referenceSchema) {
logger.error('Cannot find the \'' + referenceType +
'\' reference field for \'' + schema.name + '\' collection.');
return;
}
var fieldReference = referenceSchema.idField;
if (_.isArray(field.type) && !fieldReference && referenceSchema.isVirtual) {
if (_.find(referenceSchema.fields, function (field) { return field.field === 'id'; })) {
fieldReference = 'id';
} else {
logger.warn('Cannot find the \'idField\' attribute in your \'' +
referenceSchema.name + '\' Smart Collection declaration.');
}
}
_.each(referenceSchema.fields, function (field) {
detectFieldWithSpecialFormat(field, fieldName);
});
dest[fieldName] = {
ref: fieldReference,
attributes: getFieldsNames(referenceSchema.fields),
relationshipLinks: {
related: function (dataSet) {
return {
href: '/forest/' + Implementation.getModelName(model) +
'/' + dataSet[schema.idField] + '/relationships/' +
field.field,
};
}
}
};
if (_.isArray(field.type)) {
dest[fieldName].ignoreRelationshipData = true;
dest[fieldName].included = false;
}
}
}
});
}
function formatFields(record) {
var offsetServer = moment().utcOffset() / 60;
_.each(fieldInfoDateonly, function (fieldInfo) {
var dateonly;
if (fieldInfo.association && record[fieldInfo.association] && fieldInfo.name &&
record[fieldInfo.association][fieldInfo.name]) {
dateonly = moment.utc(record[fieldInfo.association][fieldInfo.name])
.add(offsetServer, 'h');
record[fieldInfo.association][fieldInfo.name] = dateonly.format();
}
if (fieldInfo.name && record[fieldInfo.name]) {
dateonly = moment.utc(record[fieldInfo.name]).add(offsetServer, 'h');
record[fieldInfo.name] = dateonly.format();
}
});
_.each(fieldInfoPoint, function (fieldInfo) {
if (fieldInfo.association && record[fieldInfo.association] && fieldInfo.name &&
record[fieldInfo.association][fieldInfo.name]) {
record[fieldInfo.association][fieldInfo.name] =
record[fieldInfo.association][fieldInfo.name].coordinates;
}
if (fieldInfo.name && record[fieldInfo.name]) {
record[fieldInfo.name] = record[fieldInfo.name].coordinates;
}
});
}
var serializationOptions = {
id: schema.idField,
attributes: getFieldsNames(schema.fields),
keyForAttribute: function (key) { return key; },
typeForAttribute: function (attribute) {
return typeForAttributes[attribute] || attribute;
},
meta: meta
};
getAttributesFor(serializationOptions, schema.fields);
// NOTICE: Format Dateonly field types before serialization.
if (_.isArray(records)) {
_.each(records, function (record) {
formatFields(record);
});
} else {
formatFields(records);
}
return new P(function (resolve) {
if (_.isArray(records)) {
var smartFieldsValuesInjector;
resolve(P
.map(records, function (record) {
smartFieldsValuesInjector =
new SmartFieldsValuesInjector(record, modelName, fieldsPerModel);
return smartFieldsValuesInjector.perform();
})
.then(function(result) {
if (fieldsSearched && smartFieldsValuesInjector) {
fieldsSearched = fieldsSearched.concat(smartFieldsValuesInjector.getFieldsForHighlightedSearch());
}
return result;
}));
} else {
resolve(new SmartFieldsValuesInjector(records, modelName, fieldsPerModel).perform());
}
})
.then(function () {
var decorators = null;
if (searchValue) {
decorators = Implementation.RecordsDecorator.decorateForSearch(
records,
fieldsSearched,
searchValue
);
if (decorators) {
serializationOptions.meta = { decorators };
}
}
})
.then(function () {
return new JSONAPISerializer(schema.name, records, serializationOptions);
});
};
}
module.exports = ResourceSerializer;