@modular-rest/server 1.6.3 → 1.6.5

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@modular-rest/server",
3
- "version": "1.6.3",
3
+ "version": "1.6.5",
4
4
  "description": "a nodejs module based on KOAJS for developing Rest-APIs in a modular solution.",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -1,21 +1,31 @@
1
- const DataProvider = require('../services/data_provider/service');
1
+ const DataProvider = require("../services/data_provider/service");
2
2
 
3
3
  function createPermissions() {
4
- let model = DataProvider.getCollection('cms', 'permission');
5
-
6
- return new Promise(async (done, reject) => {
4
+ let model = DataProvider.getCollection("cms", "permission");
7
5
 
6
+ return new Promise(async (done, reject) => {
8
7
  // create customer permission
9
- let isAnonymousExisted = await model.countDocuments({ title: 'anonymous' }).exec().catch(reject);
10
- let isCoustomerExisted = await model.countDocuments({ title: 'customer' }).exec().catch(reject);
11
- let isAdministratorExisted = await model.countDocuments({ title: 'administrator' }).exec().catch(reject);
8
+ let isAnonymousExisted = await model
9
+ .countDocuments({ title: "anonymous" })
10
+ .exec()
11
+ .catch(reject);
12
+ let isCoustomerExisted = await model
13
+ .countDocuments({ title: "customer" })
14
+ .exec()
15
+ .catch(reject);
16
+ let isAdministratorExisted = await model
17
+ .countDocuments({ title: "administrator" })
18
+ .exec()
19
+ .catch(reject);
12
20
 
13
21
  if (!isAnonymousExisted) {
14
22
  await new model({
15
23
  anonymous_access: true,
16
24
  isAnonymous: true,
17
- title: 'anonymous',
18
- }).save().catch(reject);
25
+ title: "anonymous",
26
+ })
27
+ .save()
28
+ .catch(reject);
19
29
  }
20
30
 
21
31
  if (!isCoustomerExisted) {
@@ -25,8 +35,10 @@ function createPermissions() {
25
35
  upload_file_access: true,
26
36
  remove_file_access: true,
27
37
  isDefault: true,
28
- title: 'customer',
29
- }).save().catch(reject);
38
+ title: "customer",
39
+ })
40
+ .save()
41
+ .catch(reject);
30
42
  }
31
43
 
32
44
  if (!isAdministratorExisted) {
@@ -36,50 +48,61 @@ function createPermissions() {
36
48
  anonymous_access: true,
37
49
  upload_file_access: true,
38
50
  remove_file_access: true,
39
- title: 'administrator',
40
- }).save().catch(reject);
51
+ title: "administrator",
52
+ })
53
+ .save()
54
+ .catch(reject);
41
55
  }
42
56
 
43
57
  done();
44
58
  });
45
-
46
59
  }
47
60
 
48
- function createAdminUser({ email, password }) {
49
- let permissionModel = DataProvider.getCollection('cms', 'permission');
50
- let authModel = DataProvider.getCollection('cms', 'auth');
61
+ async function createAdminUser({ email, password }) {
62
+ let permissionModel = DataProvider.getCollection("cms", "permission");
63
+ let authModel = DataProvider.getCollection("cms", "auth");
51
64
 
52
- return new Promise(async (done, reject) => {
53
- let isAnonymousExisted = await authModel.countDocuments({ type: 'anonymous' }).exec().catch(reject);
54
- let isAdministratorExisted = await authModel.countDocuments({ type: 'user', email: email }).exec().catch(reject);
65
+ try {
66
+ let isAnonymousExisted = await authModel
67
+ .countDocuments({ type: "anonymous" })
68
+ .exec();
55
69
 
56
- let anonymousPermission = await permissionModel.findOne({ title: 'anonymous' }).exec().catch(reject);
57
- let administratorPermission = await permissionModel.findOne({ title: 'administrator' }).exec().catch(reject);
70
+ let isAdministratorExisted = await authModel
71
+ .countDocuments({ type: "user", email: email })
72
+ .exec();
58
73
 
59
- if (!isAnonymousExisted) {
74
+ let anonymousPermission = await permissionModel
75
+ .findOne({ title: "anonymous" })
76
+ .exec();
77
+
78
+ let administratorPermission = await permissionModel
79
+ .findOne({ title: "administrator" })
80
+ .exec();
81
+
82
+ if (isAnonymousExisted == 0) {
60
83
  await new authModel({
61
84
  permission: anonymousPermission._id,
62
- email: '',
63
- phone: '',
64
- password: '',
65
- type: 'anonymous',
66
- }).save().catch(reject);
85
+ email: "",
86
+ phone: "",
87
+ password: "",
88
+ type: "anonymous",
89
+ }).save();
67
90
  }
68
91
 
69
- if (!isAdministratorExisted) {
92
+ if (isAdministratorExisted == 0) {
70
93
  await new authModel({
71
94
  permission: administratorPermission._id,
72
95
  email: email,
73
96
  password: password,
74
- type: 'user'
75
- }).save().catch(reject);
97
+ type: "user",
98
+ }).save();
76
99
  }
77
-
78
- done();
79
- });
100
+ } catch (e) {
101
+ return Promise.reject(e);
102
+ }
80
103
  }
81
104
 
82
105
  module.exports = {
83
106
  createPermissions,
84
107
  createAdminUser,
85
- };
108
+ };
@@ -22,8 +22,19 @@ module.exports.setup = async ({ keypair, adminUser, uploadDirectory }) => {
22
22
  * Insert permissions and admin user
23
23
  * for the first time
24
24
  */
25
- await DataInsertion.createPermissions();
26
- await DataInsertion.createAdminUser(adminUser);
25
+ await DataInsertion.createPermissions().catch((err) => {
26
+ console.log(
27
+ "Error while creating permissions, it seems data is already inserted.",
28
+ err
29
+ );
30
+ });
31
+
32
+ await DataInsertion.createAdminUser(adminUser).catch((err) => {
33
+ console.log(
34
+ "Error while creating admin user, it seems data is already inserted.",
35
+ err
36
+ );
37
+ });
27
38
 
28
39
  /**
29
40
  * File Service