@live-change/user-service 0.2.26 → 0.2.29

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/LICENSE.md CHANGED
@@ -1,4 +1,4 @@
1
- Copyright 2019-2020 Michał Łaszczewski
1
+ Copyright 2019-2022 Michał Łaszczewski
2
2
 
3
3
  Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
4
4
 
@@ -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,11 +27,21 @@ 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
 
42
+ const transferEventName = ['contactOrUser', ...(extendedWith.map(e => e[0].toUpperCase() + e.slice(1)))]
43
+ .join('And') + 'Owned' + modelName + 'Transferred'
44
+
31
45
  service.trigger({
32
46
  name: 'contactConnected',
33
47
  properties: {
@@ -47,8 +61,14 @@ definition.processor(function(service, app) {
47
61
  async execute({ contactType, contact, user }, { service }, emit) {
48
62
  const contactPath = [contactType, contact]
49
63
  const contactPropertyId = contactPath.map(p => JSON.stringify(p)).join(':')
50
- const contactProperty = await modelRuntime().get(contactPropertyId)
51
- if(contactProperty) {
64
+ const range = {
65
+ gte: contactPropertyId + '', // '' because it can be not-extended
66
+ lte: contactPropertyId + ':\xFF'
67
+ }
68
+ const contactProperties = await modelRuntime().rangeGet(range)
69
+ /// TODO: list based merge method
70
+ for(const contactProperty of contactProperties) {
71
+ console.log("CONTACT PROPERTY FOUND!", contactProperty, "MERGE =", config.merge)
52
72
  const userPath = ['user_User', user]
53
73
  const userPropertyId = userPath.map(p => JSON.stringify(p)).join(':')
54
74
  const userProperty = await modelRuntime().get(userPropertyId)
@@ -56,41 +76,48 @@ definition.processor(function(service, app) {
56
76
  const mergeResult = await config.merge(contactProperty, userProperty)
57
77
  if(mergeResult && userProperty) {
58
78
  emit({
59
- type: 'ownerOwned' + modelName + 'Updated',
79
+ type: 'contactOrUserOwned' + modelName + 'Updated',
60
80
  identifiers: {
61
- ownerType: 'user_User',
62
- owner: user
81
+ contactOrUserType: 'user_User',
82
+ contactOrUser: user
63
83
  },
64
84
  data: mergeResult
65
85
  })
66
86
  } else {
67
87
  emit({
68
- type: 'ownerOwned' + modelName + 'Set',
88
+ type: 'contactOrUserOwned' + modelName + 'Set',
69
89
  identifiers: {
70
- ownerType: 'user_User',
71
- owner: user
90
+ contactOrUserType: 'user_User',
91
+ contactOrUser: user
72
92
  },
73
93
  data: mergeResult
74
94
  })
75
95
  }
76
96
  emit({
77
- type: 'ownerOwned' + modelName + 'Reset',
97
+ type: 'contactOrUserOwned' + modelName + 'Reset',
78
98
  identifiers: {
79
- ownerType: contactType,
80
- owner: contact
99
+ contactOrUserType: contactType,
100
+ contactOrUser: contact
81
101
  }
82
102
  })
83
103
  } else {
84
104
  if(!userProperty) {
105
+ const extendedIdentifiers = {}
106
+ for(const key of extendedWith) {
107
+ extendedIdentifiers[key+'Type'] = contactProperty[key+'Type']
108
+ extendedIdentifiers[key] = contactProperty[key]
109
+ }
85
110
  emit({
86
- type: 'ownerOwned' + modelName + 'Transferred',
111
+ type: transferEventName,
87
112
  from: {
88
- ownerType: contactType,
89
- owner: contact
113
+ contactOrUserType: contactType,
114
+ contactOrUser: contact,
115
+ ...extendedIdentifiers
90
116
  },
91
117
  to: {
92
- ownerType: 'user_User',
93
- owner: user
118
+ contactOrUserType: 'user_User',
119
+ contactOrUser: user,
120
+ ...extendedIdentifiers
94
121
  }
95
122
  })
96
123
  }
@@ -99,21 +126,55 @@ definition.processor(function(service, app) {
99
126
  }
100
127
  })
101
128
 
102
- if(config.ownerReadAccess) {
129
+ if(config.ownerReadAccess) { // single item view
103
130
  const viewName = 'my' + modelName
131
+ const identifiers = createIdentifiersProperties(extendedWith)
104
132
  service.views[viewName] = new ViewDefinition({
105
133
  name: viewName,
134
+ properties: {
135
+ ...identifiers
136
+ },
106
137
  access(params, context) {
107
138
  return context.client.user && (config.ownerReadAccess ? config.ownerReadAccess(params, context) : true)
108
139
  },
109
140
  daoPath(params, { client, context }) {
110
141
  const owner = ['user_User', client.user]
142
+ for(const key of extendedWith) {
143
+ owner.push(params[key+'Type'], params[key])
144
+ }
111
145
  const id = owner.map(p => JSON.stringify(p)).join(':')
112
146
  return modelRuntime().path(id)
113
147
  }
114
148
  })
115
149
  }
116
150
 
151
+ if(config.ownerReadAccess && config.extendedWith) {
152
+ const extendedCombinations = [[]].concat(allCombinations(extendedWith).slice(0, -1))
153
+ for(const combination of extendedCombinations) {
154
+ const propsUpperCase = combination.map(prop => prop[0].toUpperCase() + prop.slice(1))
155
+ const indexName = 'by' + (combination).map(prop => prop[0].toUpperCase() + prop.slice(1))
156
+ const viewName = 'my' + propsUpperCase.join('And') + pluralize(modelName)
157
+ const identifiers = createIdentifiersProperties(combination)
158
+ service.views[viewName] = new ViewDefinition({
159
+ name: viewName,
160
+ properties: {
161
+ ...identifiers,
162
+ ...App.rangeProperties,
163
+ },
164
+ access(params, context) {
165
+ return context.client.user && (config.ownerReadAccess ? config.ownerReadAccess(params, context) : true)
166
+ },
167
+ daoPath(params, { client, context }) {
168
+ const owner = ['user_User', client.user]
169
+ for (const key of combination) {
170
+ owner.push(params[key + 'Type'], params[key])
171
+ }
172
+ return modelRuntime().indexRangePath(indexName, owner, App.extractRange(params) )
173
+ }
174
+ })
175
+ }
176
+ }
177
+
117
178
  if(config.ownerViews) {
118
179
  for(const view of config.userViews) {
119
180
  const viewName = view.name || ('my' + (view.prefix || '') + modelName + (view.suffix || ''))
@@ -133,8 +194,12 @@ definition.processor(function(service, app) {
133
194
  }
134
195
  }
135
196
 
197
+ const eventPrefix = ['contactOrUser',
198
+ ...(extendedWith.map(p => p[0].toUpperCase()+p.slice(1)))
199
+ ].join('And') +'Owned'
200
+
136
201
  if(config.ownerSetAccess || config.ownerWriteAccess) {
137
- const eventName = 'ownerOwned' + modelName + 'Set'
202
+ const eventName = eventPrefix + modelName + 'Set'
138
203
  const actionName = 'setMy' + modelName
139
204
  service.actions[actionName] = new ActionDefinition({
140
205
  name: actionName,
@@ -171,7 +236,7 @@ definition.processor(function(service, app) {
171
236
  }
172
237
 
173
238
  if(config.ownerUpdateAccess || config.ownerWriteAccess) {
174
- const eventName = 'ownerOwned' + modelName + 'Updated'
239
+ const eventName = eventPrefix + modelName + 'Updated'
175
240
  const actionName = 'updateMy' + modelName
176
241
  service.actions[actionName] = new ActionDefinition({
177
242
  name: actionName,
@@ -212,7 +277,7 @@ definition.processor(function(service, app) {
212
277
  }
213
278
 
214
279
  if(config.ownerResetAccess || config.ownerWriteAccess) {
215
- const eventName = 'ownerOwned' + modelName + 'Reset'
280
+ const eventName = eventPrefix + modelName + 'Reset'
216
281
  const actionName = 'resetMy' + modelName
217
282
  service.actions[actionName] = new ActionDefinition({
218
283
  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.29",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -21,9 +21,9 @@
21
21
  "url": "https://www.viamage.com/"
22
22
  },
23
23
  "dependencies": {
24
- "@live-change/framework": "0.6.0",
25
- "@live-change/relations-plugin": "0.6.0",
24
+ "@live-change/framework": "0.6.3",
25
+ "@live-change/relations-plugin": "0.6.3",
26
26
  "pluralize": "8.0.0"
27
27
  },
28
- "gitHead": "f3d7b9b6c689b9d87df3cb7f64cd75fc72339b7a"
28
+ "gitHead": "37d229ac05adf5e045ae3dcc826c6945d5dc3670"
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 }
package/README.md DELETED
@@ -1 +0,0 @@
1
- # user-service