@kravc/dos 1.11.19 → 1.11.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.
@@ -3,4 +3,16 @@
3
3
  const Read = require('../src/operations/Read')
4
4
  const Profile = require('./Profile')
5
5
 
6
- module.exports = Read(Profile)
6
+ class ReadProfile extends Read(Profile) {
7
+ static get query() {
8
+ return {
9
+ id: {
10
+ description: 'Profile ID',
11
+ required: true,
12
+ example: 'PRO_1'
13
+ }
14
+ }
15
+ }
16
+ }
17
+
18
+ module.exports = ReadProfile
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kravc/dos",
3
- "version": "1.11.19",
3
+ "version": "1.11.21",
4
4
  "description": "Convention-based, easy-to-use library for building API-driven serverless services.",
5
5
  "keywords": [
6
6
  "Service",
package/src/Document.js CHANGED
@@ -9,6 +9,8 @@ const DocumentNotFoundError = require('./errors/DocumentNotFoundError')
9
9
  const { get, omit, capitalize, cloneDeep } = require('lodash')
10
10
 
11
11
  const STORE = {}
12
+ const SYSTEM = 'SYSTEM'
13
+ const IDENTITY_SUBJECT_PATH = 'identity.sub'
12
14
 
13
15
  class Document extends Component {
14
16
  static get idKey() {
@@ -33,10 +35,10 @@ class Document extends Component {
33
35
  return this._schema
34
36
  }
35
37
 
36
- static set schema(schema) {
38
+ static get _defaultSchemaProperties() {
37
39
  const documentTitle = getComponentTitle(this, false)
38
40
 
39
- this._schema = schema.extend({
41
+ return {
40
42
  id: {
41
43
  description: capitalize(documentTitle) + ' ID',
42
44
  required: true
@@ -56,8 +58,11 @@ class Document extends Component {
56
58
  updatedBy: {
57
59
  description: `ID of a user who updated ${documentTitle}`
58
60
  }
59
- }, this.id)
61
+ }
62
+ }
60
63
 
64
+ static set schema(schema) {
65
+ this._schema = schema.extend(this._defaultSchemaProperties, this.id)
61
66
  this._bodySchema = schema
62
67
  }
63
68
 
@@ -69,6 +74,13 @@ class Document extends Component {
69
74
  parameters.partition = this.getPartition(context, parameters)
70
75
  }
71
76
 
77
+ static _extendWithCreateStamps(mutation) {
78
+ const timestamp = new Date().toJSON()
79
+ mutation.createdAt = timestamp
80
+ mutation.updatedAt = timestamp
81
+ mutation.createdBy = get(context, IDENTITY_SUBJECT_PATH, SYSTEM)
82
+ }
83
+
72
84
  static async create(context, query, mutation) {
73
85
  /* NOTE: existing document in the context allows to return document without
74
86
  duplicate been created */
@@ -89,15 +101,7 @@ class Document extends Component {
89
101
  const { validator } = context
90
102
  mutation = validator.normalize(mutation, this.id)
91
103
 
92
- const identitySubjectId = get(context, 'identity.sub')
93
-
94
- if (identitySubjectId) {
95
- mutation.createdBy = identitySubjectId
96
- }
97
-
98
- const timestamp = new Date().toJSON()
99
- mutation.createdAt = timestamp
100
- mutation.updatedAt = timestamp
104
+ this._extendWithCreateStamps(mutation)
101
105
 
102
106
  if (this.beforeCreate) {
103
107
  await this.beforeCreate(context, query, mutation)
@@ -189,17 +193,16 @@ class Document extends Component {
189
193
  return this._index(query)
190
194
  }
191
195
 
196
+ static _extendWithUpdateStamps(mutation) {
197
+ const timestamp = new Date().toJSON()
198
+ mutation.updatedAt = timestamp
199
+ mutation.updatedBy = get(context, IDENTITY_SUBJECT_PATH, SYSTEM)
200
+ }
201
+
192
202
  static async update(context, query, mutation, originalDocument = null) {
193
203
  mutation = omit(mutation, [ this.idKey, 'createdAt', 'createdBy' ])
194
204
 
195
- const identitySubjectId = get(context, 'identity.sub')
196
-
197
- if (identitySubjectId) {
198
- mutation.updatedBy = identitySubjectId
199
- }
200
-
201
- const timestamp = new Date().toJSON()
202
- mutation.updatedAt = timestamp
205
+ this._extendWithUpdateStamps(mutation)
203
206
 
204
207
  if (this.beforeUpdate) {
205
208
  await this.beforeUpdate(context, query, mutation)
@@ -58,14 +58,14 @@ describe('Document', () => {
58
58
  expect(profile.attributes.name).to.eql('Olga')
59
59
  })
60
60
 
61
- it('creates document without identity in context', async () => {
61
+ it('creates document without identity in context as SYSTEM', async () => {
62
62
  const context = { validator }
63
63
  const profile = await Profile.create(context, { name: 'Oleg' })
64
64
 
65
65
  expect(profile.id).to.exist
66
66
  expect(profile.attributes.name).to.eql('Oleg')
67
67
  expect(profile.attributes.createdAt).to.exist
68
- expect(profile.attributes.createdBy).to.not.exist
68
+ expect(profile.attributes.createdBy).to.eql('SYSTEM')
69
69
  })
70
70
 
71
71
  it('returns exiting document from the context', async () => {
@@ -115,14 +115,14 @@ describe('Document', () => {
115
115
  expect(profile.attributes.updatedBy).to.exist
116
116
  })
117
117
 
118
- it('updates document without identity in context', async () => {
118
+ it('updates document without identity in context as SYSTEM', async () => {
119
119
  const context = { validator }
120
120
  const { id } = await Profile.create(context, { name: 'Gustav' })
121
121
 
122
122
  const profile = await Profile.update(context, { id }, { name: 'Jack' })
123
123
 
124
124
  expect(profile.attributes.updatedAt).to.exist
125
- expect(profile.attributes.updatedBy).to.not.exist
125
+ expect(profile.attributes.updatedBy).to.eql('SYSTEM')
126
126
  })
127
127
 
128
128
  it('throws "DocumentNotFoundError" if document not found', async () => {
@@ -80,7 +80,11 @@ const createSpec = (operations, schemasMap, url) => {
80
80
 
81
81
  for (const name in query) {
82
82
  const queryParameter = { in: 'query', name, type: 'string', ...query[name] }
83
- delete queryParameter.example
83
+
84
+ if (queryParameter.example) {
85
+ queryParameter['x-example'] = queryParameter.example
86
+ delete queryParameter.example
87
+ }
84
88
 
85
89
  parameters.push(queryParameter)
86
90
  }