@live-change/user-service 0.2.16 → 0.2.21

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.
package/index.js CHANGED
@@ -5,6 +5,8 @@ const { User, AutheticatedUser } = require('./model.js')
5
5
  require('./authenticator.js')
6
6
  require('./userProperty.js')
7
7
  require('./userItem.js')
8
+ require('./sessionOrUserProperty.js')
9
+ require('./sessionOrUserItem.js')
8
10
 
9
11
  const Session = definition.foreignModel('session', 'Session')
10
12
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@live-change/user-service",
3
- "version": "0.2.16",
3
+ "version": "0.2.21",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -21,8 +21,9 @@
21
21
  "url": "https://www.viamage.com/"
22
22
  },
23
23
  "dependencies": {
24
- "@live-change/framework": "^0.5.7",
25
- "@live-change/relations-plugin": "^0.1.10"
24
+ "@live-change/framework": "^0.5.10",
25
+ "@live-change/relations-plugin": "^0.1.10",
26
+ "pluralize": "8.0.0"
26
27
  },
27
- "gitHead": "98a41ae61218a0ec58d4861b6409d8d9facde37f"
28
+ "gitHead": "1b2e03434af1766f4056fdd0ecf7ae55c9a26dd7"
28
29
  }
@@ -0,0 +1,192 @@
1
+ const definition = require("./definition.js")
2
+ const App = require("@live-change/framework")
3
+ const { PropertyDefinition, ViewDefinition, IndexDefinition, ActionDefinition, EventDefinition } = App
4
+ const { User } = require("./model.js")
5
+
6
+ const pluralize = require('pluralize')
7
+
8
+ definition.processor(function(service, app) {
9
+
10
+ for(let modelName in service.models) {
11
+ const model = service.models[modelName]
12
+ if(model.sessionOrUserItem) {
13
+ if (model.properties.owner) throw new Error('user property already exists!!!')
14
+ const originalModelProperties = { ...model.properties }
15
+ const modelProperties = Object.keys(model.properties)
16
+ const defaults = App.utils.generateDefault(model.properties)
17
+ const modelPropertyName = modelName.slice(0, 1).toLowerCase() + modelName.slice(1)
18
+
19
+ function modelRuntime() {
20
+ return service._runtime.models[modelName]
21
+ }
22
+
23
+ const config = model.sessionOrUserItem
24
+ const writeableProperties = modelProperties || config.writableProperties
25
+
26
+ //console.log("USER ITEM", model)
27
+
28
+ model.itemOfAny = {
29
+ ...config
30
+ }
31
+
32
+ if(config.ownerReadAccess) {
33
+ const viewName = 'my' + pluralize(modelName)
34
+ service.views[viewName] = new ViewDefinition({
35
+ name: viewName,
36
+ access(params, context) {
37
+ return config.ownerReadAccess ? config.ownerReadAccess(params, context) : true
38
+ },
39
+ properties: App.rangeProperties,
40
+ daoPath(range, { client, context }) {
41
+ const owner = client.user ? ['user_User', client.user] : ['session_Session', client.session]
42
+ const path = modelRuntime().indexRangePath('byOwner', owner, range )
43
+ return path
44
+ }
45
+ })
46
+ for(const sortField of config.sortBy || []) {
47
+ const sortFieldUc = sortField.slice(0, 1).toUpperCase() + sortField.slice(1)
48
+ const viewName = 'mySessionOrUser' + pluralize(modelName) + 'By' + sortFieldUc
49
+ service.views[viewName] = new ViewDefinition({
50
+ name: viewName,
51
+ access(params, context) {
52
+ if(!context.client.user) return false
53
+ return config.userReadAccess ? config.userReadAccess(params, context) : true
54
+ },
55
+ properties: App.rangeProperties,
56
+ daoPath(range, { client, context }) {
57
+ const owner = client.user ? ['user_User', client.user] : ['session_Session', client.session]
58
+ return modelRuntime().sortedIndexRangePath('byOwner' + sortFieldUc, owner, range)
59
+ }
60
+ })
61
+ }
62
+ }
63
+
64
+ if(config.ownerCreateAccess || config.ownerWriteAccess) {
65
+ const eventName = 'ownerOwned' + modelName + 'Created'
66
+ const actionName = 'createMy' + modelName
67
+ service.actions[actionName] = new ActionDefinition({
68
+ name: actionName,
69
+ access: config.ownerCreateAccess || config.ownerWriteAccess,
70
+ properties: {
71
+ ...originalModelProperties,
72
+ [modelPropertyName]: {
73
+ type: model,
74
+ validation: ['localId']
75
+ }
76
+ },
77
+ queuedBy: (command) => command.client.user ? 'u:'+command.client.user : 's:'+command.client.session,
78
+ waitForEvents: true,
79
+ async execute(properties, { client, service }, emit) {
80
+ const id = properties[modelPropertyName] || app.generateUid()
81
+ const entity = await modelRuntime().get(id)
82
+ if(entity) throw 'exists'
83
+ const identifiers = client.user ? {
84
+ ownerType: 'user_User',
85
+ owner: client.user,
86
+ } : {
87
+ ownerType: 'session_Session',
88
+ owner: client.session,
89
+ }
90
+ emit({
91
+ type: eventName,
92
+ [modelPropertyName]: id,
93
+ identifiers,
94
+ data: properties
95
+ })
96
+ return id
97
+ }
98
+ })
99
+ }
100
+ if(config.ownerUpdateAccess || config.ownerWriteAccess) {
101
+ const eventName = 'ownerOwned' + modelName + 'Updated'
102
+ const actionName = 'updateMy' + modelName
103
+ service.actions[actionName] = new ActionDefinition({
104
+ name: actionName,
105
+ access: config.ownerUpdateAccess || config.ownerWriteAccess,
106
+ properties: {
107
+ ...originalModelProperties,
108
+ [modelPropertyName]: {
109
+ type: model,
110
+ validation: ['nonEmpty']
111
+ }
112
+ },
113
+ skipValidation: true,
114
+ queuedBy: (command) => command.client.user ? 'u:'+command.client.user : 's:'+command.client.session,
115
+ waitForEvents: true,
116
+ async execute(properties, { client, service }, emit) {
117
+ const entity = await modelRuntime().get(properties[modelPropertyName])
118
+ if(!entity) throw 'not_found'
119
+ if(entity.ownerType == 'user_User') {
120
+ if(entity.owner != client.user) throw 'not_authorized'
121
+ }
122
+ if(entity.ownerType == 'session_Session') {
123
+ if(entity.owner != client.session) throw 'not_authorized'
124
+ }
125
+ let updateObject = {}
126
+ for(const propertyName of writeableProperties) {
127
+ if(properties.hasOwnProperty(propertyName)) {
128
+ updateObject[propertyName] = properties[propertyName]
129
+ }
130
+ }
131
+ const merged = App.utils.mergeDeep({}, entity, updateObject)
132
+ await App.validation.validate(merged, validators, { source: action, action, service, app, client })
133
+ const identifiers = client.user ? {
134
+ ownerType: 'user_User',
135
+ owner: client.user,
136
+ } : {
137
+ ownerType: 'session_Session',
138
+ owner: client.session,
139
+ }
140
+ emit({
141
+ type: eventName,
142
+ [modelPropertyName]: entity.id,
143
+ identifiers,
144
+ data: properties
145
+ })
146
+ }
147
+ })
148
+ const action = service.actions[actionName]
149
+ const validators = App.validation.getValidators(action, service, action)
150
+ }
151
+ if(config.userDeleteAccess || config.userWriteAccess) {
152
+ const eventName = 'userOwned' + modelName + 'Deleted'
153
+ const actionName = 'deleteMyUser' + modelName
154
+ service.actions[actionName] = new ActionDefinition({
155
+ name: actionName,
156
+ access: config.userDeleteAccess || config.userWriteAccess,
157
+ properties: {
158
+ [modelPropertyName]: {
159
+ type: model,
160
+ validation: ['nonEmpty']
161
+ }
162
+ },
163
+ queuedBy: (command) => command.client.user ? 'u:'+command.client.user : 's:'+command.client.session,
164
+ waitForEvents: true,
165
+ async execute(properties, { client, service }, emit) {
166
+ const entity = await modelRuntime().get(properties[modelPropertyName])
167
+ if(!entity) throw 'not_found'
168
+ if(entity.ownerType == 'user_User') {
169
+ if(entity.owner != client.user) throw 'not_authorized'
170
+ }
171
+ if(entity.ownerType == 'session_Session') {
172
+ if(entity.owner != client.session) throw 'not_authorized'
173
+ }
174
+ const identifiers = client.user ? {
175
+ ownerType: 'user_User',
176
+ owner: client.user,
177
+ } : {
178
+ ownerType: 'session_Session',
179
+ owner: client.session,
180
+ }
181
+ emit({
182
+ type: eventName,
183
+ [modelPropertyName]: entity.id,
184
+ identifiers
185
+ })
186
+ }
187
+ })
188
+ }
189
+ }
190
+ }
191
+
192
+ })
@@ -0,0 +1,177 @@
1
+ const definition = require("./definition.js")
2
+ const App = require("@live-change/framework")
3
+ const { PropertyDefinition, ViewDefinition, IndexDefinition, ActionDefinition, EventDefinition } = App
4
+ const { User } = require("./model.js")
5
+
6
+ definition.processor(function(service, app) {
7
+
8
+ for(let modelName in service.models) {
9
+ const model = service.models[modelName]
10
+
11
+ if(model.sessionOrUserProperty) {
12
+ console.log("MODEL " + modelName + " IS SESSION OR USER PROPERTY, CONFIG:", model.userProperty)
13
+ if (model.properties.owner) throw new Error('owner property already exists!!!')
14
+
15
+ const originalModelProperties = { ...model.properties }
16
+ const modelProperties = Object.keys(model.properties)
17
+ const defaults = App.utils.generateDefault(model.properties)
18
+
19
+ function modelRuntime() {
20
+ return service._runtime.models[modelName]
21
+ }
22
+
23
+ const config = model.sessionOrUserProperty
24
+ const writeableProperties = modelProperties || config.writableProperties
25
+
26
+ model.propertyOfAny = {
27
+ ...config
28
+ }
29
+
30
+ if(config.ownerReadAccess) {
31
+ const viewName = 'my' + modelName
32
+ service.views[viewName] = new ViewDefinition({
33
+ name: viewName,
34
+ access(params, context) {
35
+ return config.ownerReadAccess ? config.ownerReadAccess(params, context) : true
36
+ },
37
+ daoPath(params, { client, context }) {
38
+ const owner = client.user ? ['user_User', client.user] : ['session_Session', client.session]
39
+ const id = owner.map(p => JSON.stringify(p)).join(':')
40
+ return modelRuntime().path(id)
41
+ }
42
+ })
43
+ }
44
+
45
+ if(config.ownerViews) {
46
+ for(const view of config.userViews) {
47
+ const viewName = view.name || ('my' + (view.prefix || '') + modelName + (view.suffix || ''))
48
+ service.views[viewName] = new ViewDefinition({
49
+ name: viewName,
50
+ access(params, context) {
51
+ return view.access ? view.access(params, context) : true
52
+ },
53
+ daoPath(params, { client, context }) {
54
+ const owner = client.user ? ['user_User', client.user] : ['session_Session', client.session]
55
+ const id = owner.map(p => JSON.stringify(p)).join(':')
56
+ return view.fields
57
+ ? modelRuntime().limitedPath(id, view.fields)
58
+ : modelRuntime().path(id)
59
+ }
60
+ })
61
+ }
62
+ }
63
+
64
+ if(config.ownerSetAccess || config.ownerWriteAccess) {
65
+ const eventName = 'ownerOwned' + modelName + 'Set'
66
+ const actionName = 'setMy' + modelName
67
+ service.actions[actionName] = new ActionDefinition({
68
+ name: actionName,
69
+ properties: {
70
+ ...originalModelProperties
71
+ },
72
+ access: config.ownerSetAccess || config.ownerWriteAccess,
73
+ skipValidation: true,
74
+ queuedBy: (command) => command.client.user ? 'u:'+command.client.user : 's:'+command.client.session,
75
+ waitForEvents: true,
76
+ async execute(properties, {client, service}, emit) {
77
+ let newObject = {}
78
+ for(const propertyName of writeableProperties) {
79
+ if(properties.hasOwnProperty(propertyName)) {
80
+ newObject[propertyName] = properties[propertyName]
81
+ }
82
+ }
83
+ const data = App.utils.mergeDeep({}, defaults, newObject)
84
+ await App.validation.validate(data, validators, { source: action, action, service, app, client })
85
+ const identifiers = client.user ? {
86
+ ownerType: 'user_User',
87
+ owner: client.user,
88
+ } : {
89
+ ownerType: 'session_Session',
90
+ owner: client.session,
91
+ }
92
+ emit({
93
+ type: eventName,
94
+ identifiers,
95
+ data
96
+ })
97
+ }
98
+ })
99
+ const action = service.actions[actionName]
100
+ const validators = App.validation.getValidators(action, service, action)
101
+ }
102
+
103
+ if(config.ownerUpdateAccess || config.ownerWriteAccess) {
104
+ const eventName = 'ownerOwned' + modelName + 'Updated'
105
+ const actionName = 'updateMy' + modelName
106
+ service.actions[actionName] = new ActionDefinition({
107
+ name: actionName,
108
+ properties: {
109
+ ...originalModelProperties
110
+ },
111
+ access: config.ownerUpdateAccess || config.ownerWriteAccess,
112
+ skipValidation: true,
113
+ queuedBy: (command) => command.client.user ? 'u:'+command.client.user : 's:'+command.client.session,
114
+ waitForEvents: true,
115
+ async execute(properties, { client, service }, emit) {
116
+ const owner = client.user ? ['user_User', client.user] : ['session_Session', client.session]
117
+ const id = owner.map(p => JSON.stringify(p)).join(':')
118
+ const entity = await modelRuntime().get(id)
119
+ if(!entity) throw 'not_found'
120
+ let updateObject = {}
121
+ for(const propertyName of writeableProperties) {
122
+ if(properties.hasOwnProperty(propertyName)) {
123
+ updateObject[propertyName] = properties[propertyName]
124
+ }
125
+ }
126
+ const merged = App.utils.mergeDeep({}, entity, updateObject)
127
+ await App.validation.validate(merged, validators, { source: action, action, service, app, client })
128
+ const identifiers = client.user ? {
129
+ ownerType: 'user_User',
130
+ owner: client.user,
131
+ } : {
132
+ ownerType: 'session_Session',
133
+ owner: client.session,
134
+ }
135
+ emit({
136
+ type: eventName,
137
+ identifiers,
138
+ data: properties || {}
139
+ })
140
+ }
141
+ })
142
+ const action = service.actions[actionName]
143
+ const validators = App.validation.getValidators(action, service, action)
144
+ }
145
+
146
+ if(config.ownerResetAccess || config.ownerWriteAccess) {
147
+ const eventName = 'ownerOwned' + modelName + 'Reset'
148
+ const actionName = 'resetMy' + modelName
149
+ service.actions[actionName] = new ActionDefinition({
150
+ name: actionName,
151
+ access: config.ownerResetAccess || config.ownerWriteAccess,
152
+ queuedBy: (command) => command.client.user ? 'u:'+command.client.user : 's:'+command.client.session,
153
+ waitForEvents: true,
154
+ async execute(properties, {client, service}, emit) {
155
+ const owner = client.user ? ['user_User', client.user] : ['session_Session', client.session]
156
+ const id = owner.map(p => JSON.stringify(p)).join(':')
157
+ const entity = await modelRuntime().get(id)
158
+ if (!entity) throw 'not_found'
159
+ const identifiers = client.user ? {
160
+ ownerType: 'user_User',
161
+ owner: client.user,
162
+ } : {
163
+ ownerType: 'session_Session',
164
+ owner: client.session,
165
+ }
166
+ emit({
167
+ type: eventName,
168
+ identifiers
169
+ })
170
+ }
171
+ })
172
+ }
173
+
174
+ }
175
+ }
176
+
177
+ })
package/userItem.js CHANGED
@@ -3,6 +3,8 @@ const App = require("@live-change/framework")
3
3
  const { PropertyDefinition, ViewDefinition, IndexDefinition, ActionDefinition, EventDefinition } = App
4
4
  const { User } = require("./model.js")
5
5
 
6
+ const pluralize = require('pluralize')
7
+
6
8
  definition.processor(function(service, app) {
7
9
 
8
10
  for(let modelName in service.models) {
@@ -29,7 +31,7 @@ definition.processor(function(service, app) {
29
31
  }
30
32
 
31
33
  if(config.userReadAccess) {
32
- const viewName = 'myUser' + modelName + 's'
34
+ const viewName = 'myUser' + pluralize(modelName)
33
35
  service.views[viewName] = new ViewDefinition({
34
36
  name: viewName,
35
37
  access(params, context) {
@@ -44,7 +46,7 @@ definition.processor(function(service, app) {
44
46
  })
45
47
  for(const sortField of config.sortBy || []) {
46
48
  const sortFieldUc = sortField.slice(0, 1).toUpperCase() + sortField.slice(1)
47
- const viewName = 'myUser' + modelName + 'sBy' + sortFieldUc
49
+ const viewName = 'myUser' + pluralize(modelName) + 'By' + sortFieldUc
48
50
  service.views[viewName] = new ViewDefinition({
49
51
  name: viewName,
50
52
  access(params, context) {
@@ -72,7 +74,7 @@ definition.processor(function(service, app) {
72
74
  validation: ['localId']
73
75
  }
74
76
  },
75
- //queuedBy: (command) => command.client.user,
77
+ queuedBy: (command) => command.client.user,
76
78
  waitForEvents: true,
77
79
  async execute(properties, { client, service }, emit) {
78
80
  const id = properties[modelPropertyName] || app.generateUid()
package/userProperty.js CHANGED
@@ -96,7 +96,7 @@ definition.processor(function(service, app) {
96
96
 
97
97
  if(config.userUpdateAccess || config.userWriteAccess) {
98
98
  const eventName = 'userOwned' + modelName + 'Updated'
99
- const actionName = 'updateMyuser' + modelName
99
+ const actionName = 'updateMyUser' + modelName
100
100
  service.actions[actionName] = new ActionDefinition({
101
101
  name: actionName,
102
102
  properties: {
@@ -132,14 +132,14 @@ definition.processor(function(service, app) {
132
132
 
133
133
  if(config.userResetAccess || config.userWriteAccess) {
134
134
  const eventName = 'userOwned' + modelName + 'Reset'
135
- const actionName = 'resetMyuser' + modelName
135
+ const actionName = 'resetMyUser' + modelName
136
136
  service.actions[actionName] = new ActionDefinition({
137
137
  name: actionName,
138
138
  access: config.userResetAccess || config.userWriteAccess,
139
139
  queuedBy: (command) => command.client.user,
140
140
  waitForEvents: true,
141
141
  async execute(properties, {client, service}, emit) {
142
- const entity = await modelRuntime().indexObjectGet('byuser', client.user)
142
+ const entity = await modelRuntime().indexObjectGet('byUser', client.user)
143
143
  if (!entity) throw 'not_found'
144
144
  emit({
145
145
  type: eventName,