-
Notifications
You must be signed in to change notification settings - Fork 48
/
perspective.js
201 lines (190 loc) · 5.43 KB
/
perspective.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
/**
* Copyright (c) 2016, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or
* https://opensource.org/licenses/BSD-3-Clause
*/
/**
* db/model/perspective.js
*/
'use strict'; // eslint-disable-line strict
const common = require('../helpers/common');
const MissingRequiredFieldError = require('../dbErrors')
.MissingRequiredFieldError;
const constants = require('../constants');
const assoc = {};
const eventName = 'refocus.internal.realtime.perspective.namespace.initialize';
module.exports = function perspective(seq, dataTypes) {
const Perspective = seq.define('Perspective', {
id: {
type: dataTypes.UUID,
primaryKey: true,
defaultValue: dataTypes.UUIDV4,
},
isDeleted: {
type: dataTypes.BIGINT,
defaultValue: 0,
allowNull: false,
},
name: {
type: dataTypes.STRING,
allowNull: false,
},
rootSubject: {
type: dataTypes.STRING(constants.fieldlen.longish),
allowNull: false,
},
aspectFilterType: {
type: dataTypes.ENUM('INCLUDE', 'EXCLUDE'),
defaultValue: 'INCLUDE',
allowNull: false,
},
aspectFilter: {
type: dataTypes.ARRAY(dataTypes.STRING(constants.fieldlen.normalName)),
allowNull: true,
},
aspectTagFilterType: {
type: dataTypes.ENUM('INCLUDE', 'EXCLUDE'),
defaultValue: 'INCLUDE',
allowNull: false,
},
aspectTagFilter: {
type: dataTypes.ARRAY(dataTypes.STRING(constants.fieldlen.normalName)),
allowNull: true,
},
subjectTagFilterType: {
type: dataTypes.ENUM('INCLUDE', 'EXCLUDE'),
defaultValue: 'INCLUDE',
allowNull: false,
},
subjectTagFilter: {
type: dataTypes.ARRAY(dataTypes.STRING(constants.fieldlen.normalName)),
allowNull: true,
},
statusFilterType: {
type: dataTypes.ENUM('INCLUDE', 'EXCLUDE'),
defaultValue: 'INCLUDE',
allowNull: false,
},
statusFilter: {
type: dataTypes.ARRAY(dataTypes.STRING),
allowNull: true,
},
}, {
classMethods: {
getPerspectiveAssociations() {
return assoc;
},
postImport(models) {
assoc.createdBy = Perspective.belongsTo(models.User, {
foreignKey: 'createdBy',
});
assoc.lens = Perspective.belongsTo(models.Lens, {
as: 'lens',
foreignKey: {
name: 'lensId',
allowNull: false,
},
});
assoc.writers = Perspective.belongsToMany(models.User, {
as: 'writers',
through: 'PerspectiveWriters',
foreignKey: 'perspectiveId',
});
Perspective.addScope('defaultScope', {
include: [
// assoc.createdBy,
{
association: assoc.lens,
attributes: [
'helpEmail',
'helpUrl',
'id',
'name',
'thumbnailUrl',
'version',
],
},
],
order: ['Perspective.name'],
}, {
override: true,
});
Perspective.addScope('withoutLensAssociation', {
include: [],
order: ['Perspective.name'],
});
},
},
hooks: {
beforeDestroy(inst /* , opts */) {
return common.setIsDeleted(seq.Promise, inst);
},
/**
* Publishes the created prespective to the redis channel, to initialize
* a socketio namespace if required
*
* @param {Perspective} inst - The newly-created instance
*/
afterCreate(inst /* , opts */) {
const changedKeys = Object.keys(inst._changed);
const ignoreAttributes = ['isDeleted'];
return common.publishChange(inst, eventName, changedKeys,
ignoreAttributes);
},
/**
* Publishes the updated prespective to the redis channel, to initialize
* a socketio namespace if required
*
* @param {Perspective} inst - The updated instance
*/
afterUpdate(inst /* , opts */) {
const changedKeys = Object.keys(inst._changed);
const ignoreAttributes = ['isDeleted'];
return common.publishChange(inst, eventName, changedKeys,
ignoreAttributes);
},
/*
* TODO: socketio namespace object is garbage collected when there are
* no references to it. We still have to check, if deleting the namespace
* object manually in the afterDelete hook will help.
*/
},
indexes: [
{
name: 'PerspectiveUniqueLowercaseNameIsDeleted',
unique: true,
fields: [
seq.fn('lower', seq.col('name')),
'isDeleted',
],
},
],
instanceMethods: {
isWritableBy(who) {
return new seq.Promise((resolve /* , reject */) =>
this.getWriters()
.then((writers) => {
if (!writers.length) {
resolve(true);
}
const found = writers.filter((w) =>
w.name === who || w.id === who);
resolve(found.length === 1);
}));
}, // isWritableBy
},
paranoid: true,
validate: {
lensIdNotNull() {
if (!this.lensId) {
const err = new MissingRequiredFieldError();
err.fields = ['lensId'];
throw err;
}
}, // lensIdNotNull
},
});
return Perspective;
};