-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgenerateEvent.js
More file actions
114 lines (101 loc) · 3.34 KB
/
Copy pathgenerateEvent.js
File metadata and controls
114 lines (101 loc) · 3.34 KB
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
'use strict'
const fs = require('fs')
const path = require('path');
const yaml = require('js-yaml')
function createEventTemplate(DOMAIN, EVENT,NOTIFICATION) {
const eventTemplate = `
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "${EVENT}.schema.json",
"title": "${EVENT}",
"definitions": {
"${EVENT}": {
"$id": "#${EVENT}",
"description": "${EVENT} generic structure",
"required": ["event"],
"type": "object",
"properties": {
"event": {
"$ref": "./${EVENT}Payload.schema.json#/definitions/${EVENT}Payload"
}
},
"allOf": [
{
"$ref": "../../Common/Event.schema.json#Event"
}
]
}
}
}
`
return eventTemplate
}
function createInformationPayloadTemplate(EVENT,RESOURCE) {
const informationPayloadTemplate = `
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "${EVENT}Payload.schema.json",
"title": "${EVENT}Payload",
"definitions": {
"${EVENT}Payload": {
"$id": "#${EVENT}Payload",
"description": "${EVENT}Payload generic structure",
"required": ["${RESOURCE}"],
"type": "object",
"properties": {
"${RESOURCE}": {
"$ref": "../../Common/InformationRequiredEvent.schema.json#InformationRequiredEvent"
}
}
}
}
}
`
return informationPayloadTemplate
}
function createGenericPayloadTemplate(EVENT, RESOURCE, RESOURCE_SCHEMA_REF) {
RESOURCE_SCHEMA_REF = RESOURCE_SCHEMA_REF.split(path.sep).join(path.posix.sep);
const genericPayloadTemplate = `
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "${EVENT}Payload.schema.json",
"title": "${EVENT}Payload",
"definitions": {
"${EVENT}Payload": {
"$id": "#${EVENT}Payload",
"description": "${EVENT}Payload generic structure",
"required": ["${RESOURCE}"],
"type": "object",
"properties": {
"${RESOURCE}": {
"$ref": "../${RESOURCE_SCHEMA_REF}"
}
}
}
}
}
`
return genericPayloadTemplate
}
function createEvent(resource, resourceSchema, domain, event, notification) {
const notificationShort = lowerCaseLeading(notification)
const eventSchema = createEventTemplate(domain, event, notificationShort)
let payloadSchema
if(notification.startsWith('informationR')) {
payloadSchema = createInformationPayloadTemplate(event, notificationShort)
} else {
resourceSchema = resourceSchema.split('/').splice(-1)[0]
payloadSchema= createGenericPayloadTemplate(event, lowerCaseLeading(resource), resourceSchema)
}
const res = {
event: JSON.parse(eventSchema),
payload: JSON.parse(payloadSchema)
}
return res
}
function lowerCaseLeading(str) {
return str.charAt(0).toLowerCase() + str.slice(1)
}
module.exports = {
createEvent
}