@live-change/user-service 0.2.26 → 0.2.27

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,6 +2,10 @@ const definition = require("./definition.js")
2
2
  const App = require("@live-change/framework")
3
3
  const { PropertyDefinition, ViewDefinition, IndexDefinition, ActionDefinition, EventDefinition } = App
4
4
  const { User } = require("./model.js")
5
+ const { allCombinations } = require("./combinations.js")
6
+ const { createIdentifiersProperties } = require('./utils.js')
7
+
8
+ const pluralize = require('pluralize')
5
9
 
6
10
  definition.processor(function(service, app) {
7
11
 
@@ -10,7 +14,7 @@ definition.processor(function(service, app) {
10
14
 
11
15
  if(model.contactOrUserProperty) {
12
16
  console.log("MODEL " + modelName + " IS SESSION OR USER PROPERTY, CONFIG:", model.userProperty)
13
- if (model.properties.owner) throw new Error('owner property already exists!!!')
17
+ if (model.properties.contactOrUser) throw new Error('owner property already exists!!!')
14
18
 
15
19
  const originalModelProperties = { ...model.properties }
16
20
  const modelProperties = Object.keys(model.properties)
@@ -23,9 +27,16 @@ definition.processor(function(service, app) {
23
27
  const config = model.contactOrUserProperty
24
28
  const writeableProperties = modelProperties || config.writableProperties
25
29
 
30
+ if(model.propertyOf) throw new Error("model " + modelName + " already have owner")
26
31
  if(model.propertyOfAny) throw new Error("model " + modelName + " already have owner")
32
+
33
+ const extendedWith = config.extendedWith
34
+ ? (Array.isArray(config.extendedWith) ? config.extendedWith : [config.extendedWith])
35
+ : []
36
+
27
37
  model.propertyOfAny = {
28
- ...config
38
+ ...config,
39
+ to: ['contactOrUser', ...extendedWith]
29
40
  }
30
41
 
31
42
  service.trigger({
@@ -56,7 +67,7 @@ definition.processor(function(service, app) {
56
67
  const mergeResult = await config.merge(contactProperty, userProperty)
57
68
  if(mergeResult && userProperty) {
58
69
  emit({
59
- type: 'ownerOwned' + modelName + 'Updated',
70
+ type: 'contactOrUserOwned' + modelName + 'Updated',
60
71
  identifiers: {
61
72
  ownerType: 'user_User',
62
73
  owner: user
@@ -65,7 +76,7 @@ definition.processor(function(service, app) {
65
76
  })
66
77
  } else {
67
78
  emit({
68
- type: 'ownerOwned' + modelName + 'Set',
79
+ type: 'contactOrUserOwned' + modelName + 'Set',
69
80
  identifiers: {
70
81
  ownerType: 'user_User',
71
82
  owner: user
@@ -74,7 +85,7 @@ definition.processor(function(service, app) {
74
85
  })
75
86
  }
76
87
  emit({
77
- type: 'ownerOwned' + modelName + 'Reset',
88
+ type: 'contactOrUserOwned' + modelName + 'Reset',
78
89
  identifiers: {
79
90
  ownerType: contactType,
80
91
  owner: contact
@@ -83,7 +94,7 @@ definition.processor(function(service, app) {
83
94
  } else {
84
95
  if(!userProperty) {
85
96
  emit({
86
- type: 'ownerOwned' + modelName + 'Transferred',
97
+ type: 'contactOrUserOwned' + modelName + 'Transferred',
87
98
  from: {
88
99
  ownerType: contactType,
89
100
  owner: contact
@@ -99,21 +110,55 @@ definition.processor(function(service, app) {
99
110
  }
100
111
  })
101
112
 
102
- if(config.ownerReadAccess) {
113
+ if(config.ownerReadAccess) { // single item view
103
114
  const viewName = 'my' + modelName
115
+ const identifiers = createIdentifiersProperties(extendedWith)
104
116
  service.views[viewName] = new ViewDefinition({
105
117
  name: viewName,
118
+ properties: {
119
+ ...identifiers
120
+ },
106
121
  access(params, context) {
107
122
  return context.client.user && (config.ownerReadAccess ? config.ownerReadAccess(params, context) : true)
108
123
  },
109
124
  daoPath(params, { client, context }) {
110
125
  const owner = ['user_User', client.user]
126
+ for(const key of extendedWith) {
127
+ owner.push(params[key+'Type'], params[key])
128
+ }
111
129
  const id = owner.map(p => JSON.stringify(p)).join(':')
112
130
  return modelRuntime().path(id)
113
131
  }
114
132
  })
115
133
  }
116
134
 
135
+ if(config.ownerReadAccess && config.extendedWith) {
136
+ const extendedCombinations = [[]].concat(allCombinations(extendedWith).slice(0, -1))
137
+ for(const combination of extendedCombinations) {
138
+ const propsUpperCase = combination.map(prop => prop[0].toUpperCase() + prop.slice(1))
139
+ const indexName = 'by' + (combination).map(prop => prop[0].toUpperCase() + prop.slice(1))
140
+ const viewName = 'my' + propsUpperCase.join('And') + pluralize(modelName)
141
+ const identifiers = createIdentifiersProperties(combination)
142
+ service.views[viewName] = new ViewDefinition({
143
+ name: viewName,
144
+ properties: {
145
+ ...identifiers,
146
+ ...App.rangeProperties,
147
+ },
148
+ access(params, context) {
149
+ return context.client.user && (config.ownerReadAccess ? config.ownerReadAccess(params, context) : true)
150
+ },
151
+ daoPath(params, { client, context }) {
152
+ const owner = ['user_User', client.user]
153
+ for (const key of combination) {
154
+ owner.push(params[key + 'Type'], params[key])
155
+ }
156
+ return modelRuntime().indexRangePath(indexName, owner, App.extractRange(params) )
157
+ }
158
+ })
159
+ }
160
+ }
161
+
117
162
  if(config.ownerViews) {
118
163
  for(const view of config.userViews) {
119
164
  const viewName = view.name || ('my' + (view.prefix || '') + modelName + (view.suffix || ''))
@@ -133,8 +178,12 @@ definition.processor(function(service, app) {
133
178
  }
134
179
  }
135
180
 
181
+ const eventPrefix = ['contactOrUser',
182
+ ...(extendedWith.map(p => p[0].toUpperCase()+p.slice(1)))
183
+ ].join('And') +'Owned'
184
+
136
185
  if(config.ownerSetAccess || config.ownerWriteAccess) {
137
- const eventName = 'ownerOwned' + modelName + 'Set'
186
+ const eventName = eventPrefix + modelName + 'Set'
138
187
  const actionName = 'setMy' + modelName
139
188
  service.actions[actionName] = new ActionDefinition({
140
189
  name: actionName,
@@ -171,7 +220,7 @@ definition.processor(function(service, app) {
171
220
  }
172
221
 
173
222
  if(config.ownerUpdateAccess || config.ownerWriteAccess) {
174
- const eventName = 'ownerOwned' + modelName + 'Updated'
223
+ const eventName = eventPrefix + modelName + 'Updated'
175
224
  const actionName = 'updateMy' + modelName
176
225
  service.actions[actionName] = new ActionDefinition({
177
226
  name: actionName,
@@ -212,7 +261,7 @@ definition.processor(function(service, app) {
212
261
  }
213
262
 
214
263
  if(config.ownerResetAccess || config.ownerWriteAccess) {
215
- const eventName = 'ownerOwned' + modelName + 'Reset'
264
+ const eventName = eventPrefix + modelName + 'Reset'
216
265
  const actionName = 'resetMy' + modelName
217
266
  service.actions[actionName] = new ActionDefinition({
218
267
  name: actionName,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@live-change/user-service",
3
- "version": "0.2.26",
3
+ "version": "0.2.27",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -25,5 +25,5 @@
25
25
  "@live-change/relations-plugin": "0.6.0",
26
26
  "pluralize": "8.0.0"
27
27
  },
28
- "gitHead": "f3d7b9b6c689b9d87df3cb7f64cd75fc72339b7a"
28
+ "gitHead": "9a82ff0e7a7003d5b4e34ef9aef1ad4d7d8605dd"
29
29
  }
@@ -3,23 +3,10 @@ const App = require("@live-change/framework")
3
3
  const { PropertyDefinition, ViewDefinition, IndexDefinition, ActionDefinition, EventDefinition } = App
4
4
  const { User } = require("./model.js")
5
5
  const { allCombinations } = require("./combinations.js")
6
+ const { createIdentifiersProperties } = require('./utils.js')
6
7
 
7
8
  const pluralize = require('pluralize')
8
9
 
9
- function createIdentifiersProperties(keys) {
10
- const identifiers = {}
11
- if(keys) for(const key of keys) {
12
- identifiers[key] = {
13
- type: String,
14
- validation: ['nonEmpty']
15
- }
16
- identifiers[key + 'Type'] = {
17
- type: String,
18
- validation: ['nonEmpty']
19
- }
20
- }
21
- return identifiers
22
- }
23
10
 
24
11
  definition.processor(function(service, app) {
25
12
 
@@ -94,7 +81,7 @@ definition.processor(function(service, app) {
94
81
  access(params, context) {
95
82
  return config.ownerReadAccess ? config.ownerReadAccess(params, context) : true
96
83
  },
97
- daoPath(params, {client, context}) {
84
+ daoPath(params, { client, context }) {
98
85
  const owner = client.user ? ['user_User', client.user] : ['session_Session', client.session]
99
86
  for (const key of combination) {
100
87
  owner.push(params[key + 'Type'], params[key])
@@ -124,8 +111,12 @@ definition.processor(function(service, app) {
124
111
  }
125
112
  }
126
113
 
114
+ const eventPrefix = ['sessionOrUser',
115
+ ...(extendedWith.map(p => p[0].toUpperCase()+p.slice(1)))
116
+ ].join('And') +'Owned'
117
+
127
118
  if(config.ownerSetAccess || config.ownerWriteAccess) {
128
- const eventName = 'ownerOwned' + modelName + 'Set'
119
+ const eventName = eventPrefix + modelName + 'Set'
129
120
  const actionName = 'setMy' + modelName
130
121
  service.actions[actionName] = new ActionDefinition({
131
122
  name: actionName,
@@ -164,7 +155,7 @@ definition.processor(function(service, app) {
164
155
  }
165
156
 
166
157
  if(config.ownerUpdateAccess || config.ownerWriteAccess) {
167
- const eventName = 'ownerOwned' + modelName + 'Updated'
158
+ const eventName = eventPrefix + modelName + 'Updated'
168
159
  const actionName = 'updateMy' + modelName
169
160
  service.actions[actionName] = new ActionDefinition({
170
161
  name: actionName,
@@ -207,7 +198,7 @@ definition.processor(function(service, app) {
207
198
  }
208
199
 
209
200
  if(config.ownerResetAccess || config.ownerWriteAccess) {
210
- const eventName = 'ownerOwned' + modelName + 'Reset'
201
+ const eventName = eventPrefix + modelName + 'Reset'
211
202
  const actionName = 'resetMy' + modelName
212
203
  service.actions[actionName] = new ActionDefinition({
213
204
  name: actionName,
package/utils.js ADDED
@@ -0,0 +1,16 @@
1
+ function createIdentifiersProperties(keys) {
2
+ const identifiers = {}
3
+ if(keys) for(const key of keys) {
4
+ identifiers[key] = {
5
+ type: String,
6
+ validation: ['nonEmpty']
7
+ }
8
+ identifiers[key + 'Type'] = {
9
+ type: String,
10
+ validation: ['nonEmpty']
11
+ }
12
+ }
13
+ return identifiers
14
+ }
15
+
16
+ module.exports = { createIdentifiersProperties }