@bluedot-innovation/models 8.4.0 → 8.4.2
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/CHANGELOG.md +6 -0
- package/__tests__/project/project.data.json +10 -1
- package/__tests__/project/project.spec.js +135 -0
- package/helpers.js +14 -0
- package/models/Project.js +8 -1
- package/package.json +2 -1
- package/schemas/ReportConfiguration.js +46 -0
package/CHANGELOG.md
CHANGED
|
@@ -104,5 +104,14 @@
|
|
|
104
104
|
"hello": {
|
|
105
105
|
"registrationTtl": 86400,
|
|
106
106
|
"ssoLinkingCode": "HGDUS69SDJJ0"
|
|
107
|
-
}
|
|
107
|
+
},
|
|
108
|
+
"reportConfigurations": [
|
|
109
|
+
{
|
|
110
|
+
"type": "hello",
|
|
111
|
+
"frequency": "weekly",
|
|
112
|
+
"reportDay": 1,
|
|
113
|
+
"timezone": "Australia/Melbourne",
|
|
114
|
+
"recipients": "abc@example.com"
|
|
115
|
+
}
|
|
116
|
+
]
|
|
108
117
|
}
|
|
@@ -25,6 +25,7 @@ describe('Project', () => {
|
|
|
25
25
|
assertions('should validate a valid project ', (done) => {
|
|
26
26
|
let Project = new ProjectModel(project)
|
|
27
27
|
Project.validate((err, res) => {
|
|
28
|
+
console.log(err)
|
|
28
29
|
assert.notExists(res)
|
|
29
30
|
assert(err === null, 'err equals null')
|
|
30
31
|
done()
|
|
@@ -728,6 +729,140 @@ describe('Project', () => {
|
|
|
728
729
|
})
|
|
729
730
|
})
|
|
730
731
|
})
|
|
732
|
+
|
|
733
|
+
describe("reportConfigurations validation", () => {
|
|
734
|
+
it('Should successfully create project without reportConfigurations', (done) => {
|
|
735
|
+
delete project.reportConfigurations
|
|
736
|
+
let Project = new ProjectModel(project)
|
|
737
|
+
Project.validate((err, res) => {
|
|
738
|
+
assert.notExists(err)
|
|
739
|
+
done()
|
|
740
|
+
})
|
|
741
|
+
})
|
|
742
|
+
it('Should throw an error when reportConfigurations is null', (done) => {
|
|
743
|
+
project.reportConfigurations = null
|
|
744
|
+
let Project = new ProjectModel(project)
|
|
745
|
+
Project.validate((err, res) => {
|
|
746
|
+
assert.exists(err)
|
|
747
|
+
assert.match(err.message, /Validator failed for path `reportConfigurations` with value `null`/)
|
|
748
|
+
done()
|
|
749
|
+
})
|
|
750
|
+
})
|
|
751
|
+
it('Should throw an error when frequency is undefined', (done) => {
|
|
752
|
+
delete project.reportConfigurations[0].frequency
|
|
753
|
+
let Project = new ProjectModel(project)
|
|
754
|
+
Project.validate((err, res) => {
|
|
755
|
+
assert.exists(err)
|
|
756
|
+
assert.match(err.message, /Path `frequency` is required/)
|
|
757
|
+
done()
|
|
758
|
+
})
|
|
759
|
+
})
|
|
760
|
+
|
|
761
|
+
it('Should throw an error when frequency is invalid', (done) => {
|
|
762
|
+
project.reportConfigurations[0].frequency = 'invalid'
|
|
763
|
+
let Project = new ProjectModel(project)
|
|
764
|
+
Project.validate((err, res) => {
|
|
765
|
+
assert.exists(err)
|
|
766
|
+
assert.match(err.message, /`invalid` is not a valid enum value for path `frequency`/)
|
|
767
|
+
done()
|
|
768
|
+
})
|
|
769
|
+
})
|
|
770
|
+
|
|
771
|
+
it('Should throw an error when frequency is weekly and reportDay is not in range 0-6', (done) => {
|
|
772
|
+
project.reportConfigurations[0].frequency = 'weekly'
|
|
773
|
+
project.reportConfigurations[0].reportDay = 7
|
|
774
|
+
let Project = new ProjectModel(project)
|
|
775
|
+
Project.validate((err, res) => {
|
|
776
|
+
assert.exists(err)
|
|
777
|
+
assert.match(err.message, /reportDay must be an integer number in range of 0-6 for weekly frequency/)
|
|
778
|
+
done()
|
|
779
|
+
})
|
|
780
|
+
})
|
|
781
|
+
it('Should throw an error when frequency is monthly and reportDay is not in range 1-31', (done) => {
|
|
782
|
+
project.reportConfigurations[0].frequency = 'monthly'
|
|
783
|
+
project.reportConfigurations[0].reportDay = 0
|
|
784
|
+
let Project = new ProjectModel(project)
|
|
785
|
+
Project.validate((err, res) => {
|
|
786
|
+
assert.exists(err)
|
|
787
|
+
assert.match(err.message, /reportDay must be an integer number in range of 1-31 for monthly frequency/)
|
|
788
|
+
done()
|
|
789
|
+
})
|
|
790
|
+
})
|
|
791
|
+
it('Should throw an error when frequency is monthly and reportDay is a decimal number', (done) => {
|
|
792
|
+
project.reportConfigurations[0].frequency = 'monthly'
|
|
793
|
+
project.reportConfigurations[0].reportDay = 1.5
|
|
794
|
+
let Project = new ProjectModel(project)
|
|
795
|
+
Project.validate((err, res) => {
|
|
796
|
+
assert.exists(err)
|
|
797
|
+
assert.match(err.message, /reportDay must be an integer number in range of 1-31 for monthly frequency/)
|
|
798
|
+
done()
|
|
799
|
+
})
|
|
800
|
+
})
|
|
801
|
+
it('Should throw an error when reportDay is not a number', (done) => {
|
|
802
|
+
project.reportConfigurations[0].reportDay = "dump"
|
|
803
|
+
let Project = new ProjectModel(project)
|
|
804
|
+
Project.validate((err, res) => {
|
|
805
|
+
assert.exists(err)
|
|
806
|
+
assert.match(err.message, /Cast to Number failed for value "dump" \(type string\) at path "reportDay"/)
|
|
807
|
+
done()
|
|
808
|
+
})
|
|
809
|
+
})
|
|
810
|
+
it('Should throw an error when there are multiple report config with the same type', (done) => {
|
|
811
|
+
project.reportConfigurations.push({
|
|
812
|
+
"type": "hello",
|
|
813
|
+
"frequency": "monthly",
|
|
814
|
+
"reportDay": 1,
|
|
815
|
+
"timezone": "Australia/Sydney",
|
|
816
|
+
"recipients": "xyz@example.com"
|
|
817
|
+
})
|
|
818
|
+
let Project = new ProjectModel(project)
|
|
819
|
+
Project.validate((err, res) => {
|
|
820
|
+
assert.exists(err)
|
|
821
|
+
assert.match(err.message, /Only one report type per project is allowed/)
|
|
822
|
+
done()
|
|
823
|
+
})
|
|
824
|
+
})
|
|
825
|
+
|
|
826
|
+
it('Should be successfully when the frequency is daily and reportDay is undefined', (done) => {
|
|
827
|
+
project.reportConfigurations[0].frequency = "daily"
|
|
828
|
+
delete project.reportConfigurations[0].reportDay
|
|
829
|
+
let Project = new ProjectModel(project)
|
|
830
|
+
Project.validate((err, res) => {
|
|
831
|
+
assert.notExists(err)
|
|
832
|
+
done()
|
|
833
|
+
})
|
|
834
|
+
})
|
|
835
|
+
|
|
836
|
+
it('Should be successfully when the frequency is daily and reportDay is any number', (done) => {
|
|
837
|
+
project.reportConfigurations[0].frequency = "daily"
|
|
838
|
+
project.reportConfigurations[0].reportDay = 100
|
|
839
|
+
let Project = new ProjectModel(project)
|
|
840
|
+
Project.validate((err, res) => {
|
|
841
|
+
assert.notExists(err)
|
|
842
|
+
done()
|
|
843
|
+
})
|
|
844
|
+
})
|
|
845
|
+
|
|
846
|
+
it('Should throw an error when timezone is invalid', (done) => {
|
|
847
|
+
project.reportConfigurations[0].timezone = "Invalid"
|
|
848
|
+
let Project = new ProjectModel(project)
|
|
849
|
+
Project.validate((err, res) => {
|
|
850
|
+
assert.exists(err)
|
|
851
|
+
assert.match(err.message, /Invalid timezone/)
|
|
852
|
+
done()
|
|
853
|
+
})
|
|
854
|
+
})
|
|
855
|
+
|
|
856
|
+
it('Should throw an error when recipients are invalid', (done) => {
|
|
857
|
+
project.reportConfigurations[0].recipients = "abc@example.com, invalid-1, invalid-2@example"
|
|
858
|
+
let Project = new ProjectModel(project)
|
|
859
|
+
Project.validate((err, res) => {
|
|
860
|
+
assert.exists(err)
|
|
861
|
+
assert.match(err.message, /Invalid email address\(es\): invalid-1,invalid-2@example/)
|
|
862
|
+
done()
|
|
863
|
+
})
|
|
864
|
+
})
|
|
865
|
+
})
|
|
731
866
|
})
|
|
732
867
|
|
|
733
868
|
describe('should throw an error when any of the values of the array contains a falsy value', () => {
|
package/helpers.js
CHANGED
|
@@ -253,4 +253,18 @@ helpers.validateSsoLinkingCode = function(code){
|
|
|
253
253
|
return regex.test(code)
|
|
254
254
|
}
|
|
255
255
|
|
|
256
|
+
helpers.validateNoDuplicateReportConfigurations = function(reportConfigArray) {
|
|
257
|
+
const reportConfigByType = reportConfigArray.reduce((result, currentValue) => {
|
|
258
|
+
(result[currentValue['type']] = result[currentValue['type']] || []).push(currentValue);
|
|
259
|
+
return result;
|
|
260
|
+
}, {})
|
|
261
|
+
|
|
262
|
+
for(const type of Object.keys(reportConfigByType)){
|
|
263
|
+
if (reportConfigByType[type].length > 1) {
|
|
264
|
+
return false
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
return true;
|
|
268
|
+
}
|
|
269
|
+
|
|
256
270
|
module.exports = helpers
|
package/models/Project.js
CHANGED
|
@@ -17,12 +17,18 @@ let iosConfig = require('../schemas/IosConfig')
|
|
|
17
17
|
let androidConfig = require('../schemas/AndroidConfig')
|
|
18
18
|
let projectTempoSchema = require('../schemas/ProjectTempo')
|
|
19
19
|
let projectHelloSchema = require('../schemas/ProjectHello')
|
|
20
|
+
let reportConfigurationSchema = require('../schemas/ReportConfiguration')
|
|
20
21
|
|
|
21
22
|
let webhooksValidators = [
|
|
22
23
|
{validator: helpers.validateNotNull},
|
|
23
24
|
{validator: helpers.validateNoDuplicateWebhooks, msg: "Duplicate webhooks found with the same type and url as another. Only one is allowed"}
|
|
24
25
|
];
|
|
25
26
|
|
|
27
|
+
const reportConfigurationsValidators = [
|
|
28
|
+
{validator: helpers.validateNotNull},
|
|
29
|
+
{validator: helpers.validateNoDuplicateReportConfigurations, msg: "Only one report type per project is allowed"}
|
|
30
|
+
]
|
|
31
|
+
|
|
26
32
|
let ProjectSchema = new mongoose.Schema({
|
|
27
33
|
_id: {type: String, default: uuidv4},
|
|
28
34
|
nextRuleUpdateInterval: {
|
|
@@ -50,7 +56,8 @@ let ProjectSchema = new mongoose.Schema({
|
|
|
50
56
|
apiKey: {type: String},
|
|
51
57
|
integrations: IntegrationSchema,
|
|
52
58
|
tempo: projectTempoSchema,
|
|
53
|
-
hello: projectHelloSchema
|
|
59
|
+
hello: projectHelloSchema,
|
|
60
|
+
reportConfigurations: {type: [reportConfigurationSchema], validate: reportConfigurationsValidators}
|
|
54
61
|
},
|
|
55
62
|
{collection: 'project'})
|
|
56
63
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bluedot-innovation/models",
|
|
3
|
-
"version": "8.4.
|
|
3
|
+
"version": "8.4.2",
|
|
4
4
|
"main": "Models.js",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"lint": "./node_modules/.bin/eslint -c .eslintrc.js ./models/*.js ./__tests__/**/*.js ./__tests__/**/**/*.js ./schemas/*.js",
|
|
@@ -37,6 +37,7 @@
|
|
|
37
37
|
"deep-equal": "^1.0.1",
|
|
38
38
|
"fast-deep-equal": "^2.0.1",
|
|
39
39
|
"lodash.clonedeep": "4.5.0",
|
|
40
|
+
"moment-timezone": "^0.5.45",
|
|
40
41
|
"mongoose": "6.12.0",
|
|
41
42
|
"uuid": "^3.1.0"
|
|
42
43
|
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
let mongoose = require('mongoose')
|
|
2
|
+
const moment = require('moment-timezone')
|
|
3
|
+
const helpers = require('../helpers')
|
|
4
|
+
|
|
5
|
+
function validateReportDay(reportDay) {
|
|
6
|
+
if (this.frequency === 'weekly' && (!Number.isInteger(reportDay) || !(reportDay >= 0 && reportDay <= 6))) {
|
|
7
|
+
throw new Error('reportDay must be an integer number in range of 0-6 for weekly frequency')
|
|
8
|
+
} else if (this.frequency === 'monthly' && (!Number.isInteger(reportDay) || !(reportDay >= 1 && reportDay <= 31))) {
|
|
9
|
+
throw new Error('reportDay must be an integer number in range of 1-31 for monthly frequency')
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function validateTimeZone(timezone) {
|
|
14
|
+
const tz = moment.tz.zone(timezone)
|
|
15
|
+
if (!tz) {
|
|
16
|
+
return false
|
|
17
|
+
}
|
|
18
|
+
return true
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function validateRecipients(value) {
|
|
22
|
+
const invalidEmails = []
|
|
23
|
+
const recipients = value.split(',').map((email) => email.trim())
|
|
24
|
+
recipients.forEach((email) => {
|
|
25
|
+
if (!helpers.validateEmail(email)) {
|
|
26
|
+
invalidEmails.push(email)
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
if (invalidEmails.length) {
|
|
31
|
+
throw new Error(`Invalid email address(es): ${invalidEmails.join(",")}`)
|
|
32
|
+
}
|
|
33
|
+
return true
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
let ReportConfigurationSchema = new mongoose.Schema({
|
|
37
|
+
type: {type: String, enum: ['hello'], required: true},
|
|
38
|
+
frequency: {type: String, enum: ['daily', 'weekly', 'monthly'], required: true},
|
|
39
|
+
reportDay: {type: Number, validate: {validator: validateReportDay}},
|
|
40
|
+
timezone: {type: String, required: true, validate: { validator: validateTimeZone, msg: 'Invalid timezone'}},
|
|
41
|
+
recipients: {type: String, required: true, validate: {validator: validateRecipients}}
|
|
42
|
+
}, {
|
|
43
|
+
_id: false
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
module.exports = ReportConfigurationSchema
|