identity-admin 1.0.0 → 1.1.0

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.
@@ -1,106 +0,0 @@
1
- import { __ } from "i18n";
2
- import { IResourceFile } from "../types/IResourceFile";
3
- import ResourcesHelper from "./ResourceHelper";
4
-
5
- var pluralize = require('pluralize')
6
-
7
- export default class ActionsGenerator {
8
-
9
- public static generateActions (actionsCheck: any, resource: IResourceFile) {
10
-
11
- const actions = this.getActions(actionsCheck? JSON.parse(JSON.stringify(resource.properties.actions)): undefined)//resource.properties.actions? ResourcesHelper.getActions(JSON.parse(JSON.stringify(resource.properties.actions))): ResourcesHelper.getActions(undefined)
12
-
13
- for (const key in actions) {
14
-
15
- if (key === 'extras') {
16
- actions.extras = actions.extras? this.addExtraActions(JSON.parse(JSON.stringify(resource.properties.actions!.extras))): actions.extras
17
- continue
18
- }
19
-
20
- const translation = ResourcesHelper.checkActionTranslation(resource, key)
21
- actions[key] = this.appendActionValues(key, actions[key], translation)
22
- }
23
-
24
- return actions
25
-
26
- }
27
-
28
- private static addExtraActions (extraActions: any) {
29
-
30
- var extraActionsObject : {[key: string]: any} = {
31
- record: {},
32
- resource: {}
33
- }
34
-
35
- extraActions.forEach((action: any) => {
36
- action.keys = action.isVisible? Object.keys(action.isVisible): []
37
-
38
- action.actionType? extraActionsObject[action.actionType][action.key] = action: ''
39
- });
40
-
41
- return extraActionsObject
42
- }
43
-
44
- private static getActions (actions: any) {
45
-
46
- var actionObject : {[key: string]: any} = {
47
- show: { isAccessible: true },
48
- new: { isAccessible: true },
49
- edit: { isAccessible: true },
50
- delete: { isAccessible: true },
51
- bulkDelete: { isAccessible: false },
52
- }
53
-
54
- if (!actions || Object.keys(actions).length === 0) {
55
-
56
- return actionObject
57
- }
58
-
59
- for (const key in actions) {
60
-
61
- if (actions[key].hasOwnProperty('isAccessible') || key === 'extras') {
62
-
63
- actionObject[key] = actions[key]
64
- }
65
- }
66
-
67
- return actionObject
68
- }
69
-
70
- private static appendActionValues(action: string, actionObject: any, translation: string|undefined): any {
71
-
72
- switch (action) {
73
-
74
- case 'new':
75
- actionObject.value = translation? translation: __("actions_new")
76
- break;
77
-
78
- case 'edit':
79
- actionObject.value = translation? translation: __("actions_edit")
80
- break;
81
-
82
- case 'delete':
83
- actionObject.value = translation? translation: __("actions_delete")
84
- actionObject.confirmationMessageTitle = __("actions_delete_confirmMessage_title")
85
- actionObject.confirmationMessageBody = __("actions_delete_confirmMessage_body")
86
- actionObject.confirmDeleteOption = __("actions_delete_options_yes")
87
- actionObject.cancelDeleteOption = __("actions_delete_options_no")
88
- break;
89
-
90
- case 'bulkDelete':
91
- actionObject.value = translation? translation: __("actions_delete")
92
- actionObject.confirmationMessageTitle = __("actions_bulkDelete_confirmMessage_title")
93
- actionObject.confirmationMessageBody = __("actions_bulkDelete_confirmMessage_body")
94
- actionObject.confirmDeleteOption = __("actions_delete_options_yes")
95
- actionObject.cancelDeleteOption = __("actions_delete_options_no")
96
- break;
97
-
98
- }
99
-
100
- return actionObject
101
- }
102
-
103
-
104
-
105
- }
106
-
@@ -1,90 +0,0 @@
1
- import StringUtils from "../utils/StringUtils";
2
- import { __ } from "i18n";
3
- import mongoose, { Schema } from "mongoose";
4
- import SchemaGenerator from "./SchemaGenerator";
5
- import ResourcesHelper from "./ResourceHelper";
6
- import ActionsGenerator from "./ActionsGenerator";
7
- import { IResourceFile } from "../types/IResourceFile";
8
-
9
- const SetupParent = {
10
- name: 'Setup',
11
- icon: 'PermDataSetting'
12
- };
13
-
14
- export default class ResourceGenerator {
15
-
16
- public static generate(resource: IResourceFile): any {
17
-
18
- const modelName = resource.properties.modelName
19
- const modifiedResource = JSON.parse(JSON.stringify(resource))
20
- const schema = resource.properties.resource.schema.paths
21
-
22
- const title = ResourcesHelper.getSchemaTitle(schema, resource)
23
- const recordFields = ResourcesHelper.manageFields(schema, title, resource.properties.hiddenProperties)
24
-
25
- const modelCheck = resource.properties.model? JSON.parse(JSON.stringify(resource.properties.model)): undefined
26
- const actionsCheck = resource.properties.actions
27
-
28
- const modelAndPopulatedString = SchemaGenerator.generateSchema(schema, modelCheck, modelName, resource)
29
- modifiedResource.properties.model = modelAndPopulatedString[0]
30
- modifiedResource.properties.populatedString = modelAndPopulatedString[1]
31
-
32
- const modifiedProperties = ResourcesHelper.addVirtualFields(JSON.parse(JSON.stringify(recordFields)), modelCheck)
33
-
34
- modifiedResource.properties.path = ResourcesHelper.getModelPath(modelName)
35
-
36
- const modifiedListProperties = modifiedProperties[0]
37
- const modifiedShowProperties = modifiedProperties[1]
38
- const modifiedFormProperties = modifiedProperties[2]
39
-
40
- if (!resource.properties.parent) {
41
- modifiedResource.properties.parent = SetupParent
42
- }
43
-
44
- if (!resource.properties.name) {
45
- modifiedResource.properties.name = StringUtils.convertCamelCaseToWord(modelName)
46
- }
47
-
48
- if (!resource.showProperties) {
49
- modifiedResource.showProperties = modifiedShowProperties
50
- }
51
-
52
- if (!resource.listProperties) {
53
- modifiedResource.listProperties = ResourcesHelper.exchangeFirstFieldWithTitle(JSON.parse(JSON.stringify(modifiedListProperties)), title)
54
- }
55
-
56
- if (!resource.formProperties) {
57
- modifiedResource.formProperties = ResourcesHelper.removeUnWantedFieldsFromCreateOrUpdate(JSON.parse(JSON.stringify(modifiedFormProperties)))
58
- }
59
-
60
- modifiedResource.properties.title = title
61
-
62
- if (!resource.properties.defaultOrderBy) {
63
- modifiedResource.properties.defaultOrderBy = title
64
- }
65
-
66
- if (!resource.properties.defaultOrder) {
67
- modifiedResource.properties.defaultOrder = 'asc'
68
- }
69
-
70
- if (!resource.properties.defaultrowsPerPage) {
71
- modifiedResource.properties.defaultrowsPerPage = 10
72
- }
73
-
74
- modifiedResource.listProperties = ResourcesHelper.prepareProperties(resource.listProperties? resource.listProperties: modifiedResource.listProperties, modelName, modifiedResource.properties.model, resource)
75
- modifiedResource.showProperties = ResourcesHelper.prepareProperties(resource.showProperties? resource.showProperties: modifiedResource.showProperties, modelName, modifiedResource.properties.model, resource)
76
- modifiedResource.properties.filters = ResourcesHelper.getFilters(resource.properties.filters? JSON.parse(JSON.stringify(resource.properties.filters)): undefined)
77
-
78
- if (modifiedResource.properties.filters && modifiedResource.properties.filters.scopes && modifiedResource.properties.filters.scopes.isAccessible) {
79
-
80
- const options = resource.properties.filters!.scopes!.options
81
- modifiedResource.properties.filters.scopes.options = ResourcesHelper.setScopeFilterOptions(modelName, options!)
82
- }
83
-
84
- const actions = ActionsGenerator.generateActions(actionsCheck, resource)
85
- modifiedResource.properties.actions = actions
86
-
87
- return modifiedResource
88
- }
89
-
90
- }
@@ -1,391 +0,0 @@
1
- import { Virtuals } from "../types/helpers";
2
- import StringUtils from "../utils/StringUtils";
3
- import { __ } from "i18n";
4
- import { IResourceFile } from "../types/IResourceFile";
5
- import { IDashboardRepository } from "../repositories/Repository";
6
-
7
-
8
- var pluralize = require('pluralize')
9
-
10
- export default class ResourcesHelper {
11
-
12
- public static getSchemaTitle (schema : any, resource: any) {
13
-
14
- if (resource.properties.title) {
15
- return resource.properties.title
16
- }
17
-
18
- return this.getTitle(schema)
19
- }
20
-
21
- public static prepareProperties (properties: string[], modelName: string, model: any, resource: IResourceFile) {
22
-
23
- var preparedProperties : {}[] = []
24
-
25
- properties.forEach(key => {
26
-
27
- if (model[key]) {
28
-
29
- const refCheck = model[key].type === 'ref'
30
- var path = undefined
31
-
32
- if (refCheck) {
33
- path = model[key].path
34
- }
35
-
36
- const translation = this.checkResourceTranslation(resource, key)
37
-
38
- const propertyObject = {
39
- key,
40
- value: translation? translation: StringUtils.checkRefId(key)? StringUtils.convertCamelCaseToWord(key.slice(0, -2)): StringUtils.convertCamelCaseToWord(key),
41
- path: path
42
- }
43
-
44
- preparedProperties.push(propertyObject)
45
-
46
- }
47
-
48
- });
49
-
50
- return preparedProperties
51
- }
52
-
53
- public static getFieldType (schemaField: any) {
54
-
55
- if (schemaField.instance === 'password') {
56
- return 'password'
57
- }
58
-
59
- if (schemaField.instance === 'Embedded') {
60
- return 'nestedSchema'
61
- }
62
-
63
- if (schemaField.options.ref) {
64
- return 'ref'
65
- }
66
-
67
- else if (schemaField.enumValues && schemaField.enumValues.length !== 0) {
68
- return 'enum'
69
- }
70
-
71
- else {
72
- return schemaField.instance
73
- }
74
- }
75
-
76
-
77
- public static checkResourceTranslation (resource: IResourceFile, key: string) {
78
-
79
- var translation = undefined
80
-
81
- if (!resource.properties.keysTranslations || !resource.properties.keysTranslations[key]) {
82
- return translation
83
- }
84
-
85
- translation = __(resource.properties.keysTranslations[key])
86
- return translation
87
-
88
- }
89
-
90
-
91
- public static checkActionTranslation (resource: IResourceFile, action: string) {
92
-
93
- var translation = undefined
94
-
95
- if (!resource.properties.actionsTranslations || !resource.properties.actionsTranslations[action]) {
96
- return translation
97
- }
98
-
99
- translation = __(resource.properties.actionsTranslations[action])
100
- return translation
101
-
102
- }
103
-
104
- public static setScopeFilterOptions (modelName: string, options: string[]) {
105
-
106
- var arrayOfOptions : any = []
107
-
108
- options.forEach((key: string) => {
109
-
110
- const valueOfOption = StringUtils.lowerCaseFirstLetter(modelName) + '_filter_scope_' + key
111
-
112
- const optionObject = {
113
- key,
114
- value: __(valueOfOption)
115
- }
116
-
117
- arrayOfOptions.push(optionObject)
118
- });
119
- return arrayOfOptions
120
- }
121
-
122
- public static getFilters (filters: any) {
123
-
124
- var filterObject = {
125
- scopes: {},
126
- searchBar: {}
127
- }
128
-
129
- if (!filters || Object.keys(filters).length === 0) {
130
-
131
- filterObject = {
132
-
133
- scopes: {
134
- isAccessible: false
135
- },
136
- searchBar: {
137
- isAccessible: true
138
- }
139
- }
140
-
141
- return filterObject
142
- }
143
-
144
- if (!filters.searchBar || !filters.searchBar.hasOwnProperty('isAccessible')) {
145
-
146
- filterObject.searchBar = {
147
- isAccessible: true
148
- }
149
- }
150
-
151
- else {
152
- filterObject.searchBar = filters.searchBar
153
- }
154
-
155
- if (!filters.scopes || !filters.scopes.hasOwnProperty('isAccessible')) {
156
-
157
- filterObject.scopes = {
158
- isAccessible: false
159
- }
160
- }
161
-
162
- else {
163
- filterObject.scopes = filters.scopes
164
- }
165
-
166
- return filterObject
167
- }
168
-
169
- public static async addExtraFields (showProperties: any, model: any, record: any, filterBy: string, repo: IDashboardRepository) {
170
-
171
- for (var i = 0; i < showProperties.length; i++) {
172
-
173
- // if (!model.hasOwnProperty(showProperties[i].key)) {
174
-
175
- // const modelName = pluralize.singular(showProperties[i].key)
176
- // const repository: any = this.getResourceSymbol(modelName)
177
- // filterBy = filterBy + "Id"
178
-
179
- // const filterQuery: {[key:string]: any} = {}
180
- // filterQuery[filterBy] = record._id
181
-
182
- // const extraRecords = await repository.findMany({
183
- // filter: filterQuery
184
- // });
185
-
186
- // record[showProperties[i].key] = extraRecords
187
- // }
188
-
189
- const key = showProperties[i].key
190
-
191
- if ((model[key].type === 'ref' && model.virtuals && model.virtuals[key]) || (model[key].type === 'Array' && model.virtuals && model.virtuals[key] && model[key].arrayType === 'ref')) {
192
-
193
- const modelName = pluralize.singular(showProperties[i].key)
194
- const repository: any = repo;
195
-
196
- filterBy = filterBy + "Id"
197
-
198
- const filterQuery: {[key:string]: any} = {}
199
- filterQuery[filterBy] = record._id
200
-
201
- const extraRecords = await repository.findMany({
202
- filter: filterQuery
203
- });
204
-
205
- record[showProperties[i].key] = extraRecords
206
- }
207
-
208
- };
209
-
210
- return record
211
- }
212
-
213
- // public static getResourceRepository(resourceName: string) {
214
-
215
- // const resource = StringUtils.upperCaseFirstLetter(resourceName);
216
- // const repositorySymbol = Symbol.for(`I${resource}Repository`)
217
- // const repository = container.get(repositorySymbol)
218
-
219
- // return repository;
220
- // }
221
-
222
- public static getTitle(schema: any) {
223
-
224
- if (schema['title']) {
225
- return 'title'
226
- }
227
-
228
- if (schema['name']) {
229
- return 'name'
230
- }
231
-
232
- if (schema['email']) {
233
- return 'email'
234
- }
235
-
236
- return '_id'
237
- }
238
-
239
- public static exchangeFirstFieldWithTitle(arrayOfFields: any, title: string) {
240
-
241
- if (arrayOfFields[0] !== title) {
242
-
243
- for (var i = 1; i < arrayOfFields.length; i++) {
244
-
245
- const field = arrayOfFields[i]
246
-
247
- if (field === title) {
248
-
249
- const temp = arrayOfFields[0]
250
- arrayOfFields[0] = title
251
- arrayOfFields[i] = temp
252
-
253
- break;
254
- }
255
- }
256
- }
257
-
258
- return arrayOfFields
259
- }
260
-
261
- public static addVirtualFields(arrayOfFields: string[], model: any) {
262
-
263
- var showProperties = JSON.parse(JSON.stringify(arrayOfFields))
264
- var listProperties = JSON.parse(JSON.stringify(arrayOfFields))
265
- var formProperties = JSON.parse(JSON.stringify(arrayOfFields))
266
-
267
- if (!model || !model.virtuals || !(Object.keys(model.virtuals))) {
268
- return [listProperties, showProperties, formProperties]
269
- }
270
- const virtualFields = model.virtuals
271
-
272
- for (const extraField in virtualFields) {
273
-
274
- if (virtualFields[extraField].showIn) {
275
-
276
- switch (virtualFields[extraField].showIn) {
277
-
278
- case Virtuals.SHOW:
279
- showProperties.push(extraField)
280
- break;
281
-
282
- case Virtuals.LIST:
283
- listProperties.push(extraField)
284
- break;
285
-
286
- case Virtuals.FORM:
287
- formProperties.push(extraField)
288
- break;
289
- }
290
- }
291
-
292
- else {
293
-
294
- showProperties.push(extraField)
295
- listProperties.push(extraField)
296
- formProperties.push(extraField)
297
- }
298
- }
299
-
300
- return [listProperties, showProperties, formProperties]
301
- }
302
-
303
- public static manageFields(schema: any, title: string, hiddenProperties: any) {
304
-
305
- var arrayOfFields: any[] = []
306
-
307
- if (!hiddenProperties || !hiddenProperties.includes('_id')) {
308
- arrayOfFields.push('_id')
309
- }
310
-
311
- if (title !== '_id' && (!hiddenProperties || !hiddenProperties.includes(title))) {
312
- arrayOfFields.push(title)
313
- }
314
-
315
- const fields = Object.keys(schema)
316
-
317
- for (var i = 0; i < fields.length; i++) {
318
-
319
- const fieldKey = fields[i]
320
-
321
- if (hiddenProperties && hiddenProperties.includes(fieldKey)) {
322
- continue
323
- }
324
-
325
- const unwantedField = this.checkUnwantedFields(fieldKey, title)
326
-
327
- if (unwantedField){
328
- continue
329
- }
330
-
331
- arrayOfFields.push(fieldKey)
332
- }
333
-
334
- return arrayOfFields;
335
- }
336
-
337
- public static checkUnwantedFields(fieldKey: string, title: string) {
338
-
339
- if (fieldKey === '__v' || fieldKey === '_id' || fieldKey === title || fieldKey === 'salt' || fieldKey === 'hash' || fieldKey === 'forgotPasswordToken'){
340
- return true
341
- }
342
-
343
- return false
344
- }
345
-
346
- public static getModelPath(modelName: string) {
347
-
348
- const modelPath = pluralize(StringUtils.lowerCaseFirstLetter(modelName))
349
-
350
- return modelPath
351
- }
352
-
353
- public static removeUnWantedFieldsFromCreateOrUpdate(arrayOfFields: string[]) {
354
-
355
- var modifiedArrayOfFields: string [] = []
356
-
357
- arrayOfFields.forEach(field => {
358
-
359
- if (field !== '_id' && field !== 'createdAt' && field !== 'updatedAt') {
360
- modifiedArrayOfFields.push(field)
361
- }
362
-
363
- });
364
-
365
- return modifiedArrayOfFields
366
- }
367
-
368
- // public static checkIndexBeforeHandler(resource: any) {
369
-
370
- // const crudOperations = resource.properties.crudOperations
371
-
372
- // if (crudOperations && crudOperations.index && crudOperations.index.before) {
373
- // return true
374
- // }
375
-
376
- // return false
377
- // }
378
-
379
- public static checkCreateBeforeHandler(resource: any) {
380
-
381
- const crudOperations = resource.properties.crudOperations
382
-
383
- if (crudOperations && crudOperations.create && crudOperations.create.before) {
384
- return true
385
- }
386
-
387
- return false
388
- }
389
-
390
- }
391
-
@@ -1,120 +0,0 @@
1
- import StringUtils from "../utils/StringUtils";
2
- import { __ } from "i18n";
3
- import ResourcesHelper from "./ResourceHelper";
4
- import { IResourceFile } from "../types/IResourceFile";
5
-
6
- var pluralize = require('pluralize')
7
-
8
- export default class SchemaGenerator {
9
-
10
- public static generateSchema (schema: any, model: any, modelName: string, resource: IResourceFile) {
11
-
12
- const schemaFields = Object.keys(schema)
13
- var populatedString: string = ''
14
-
15
- if (!model) {
16
- model = {}
17
- }
18
-
19
- schemaFields.forEach(field => {
20
-
21
- const schemaField = schema[field]
22
- const refCheck = schemaField.options.ref
23
- const translation = ResourcesHelper.checkResourceTranslation(resource, field)
24
- const fieldType = ResourcesHelper.getFieldType(schemaField)
25
-
26
- model[field] = model[field]? model[field]: {}
27
-
28
- if (fieldType === 'ref') {
29
- populatedString = StringUtils.getPopulatedString(populatedString, field)
30
- model[field].path = StringUtils.lowerCaseFirstLetter(refCheck)
31
- }
32
-
33
- if (fieldType === 'nestedSchema') {
34
-
35
- const modelAndPopulatedString = this.setNestedSchema(schema[field].schema.paths, modelName, model, field, populatedString, resource)
36
- model = modelAndPopulatedString[0]
37
- populatedString = modelAndPopulatedString[1]
38
-
39
- }
40
-
41
- else if (fieldType === 'Array') {
42
-
43
- model[field].required = schema[field].caster.isRequired
44
-
45
- if (schema[field].schema) {
46
- const modelAndPopulatedString = this.setNestedSchema(schema[field].schema.paths, modelName, model, field, populatedString, resource)
47
- model = modelAndPopulatedString[0]
48
- populatedString = modelAndPopulatedString[1]
49
- model[field].arrayType = 'nestedSchema'
50
- }
51
-
52
- else if (schema[field].caster.options.ref) {
53
-
54
- populatedString = StringUtils.getPopulatedString(populatedString, field)
55
- model[field].path = StringUtils.lowerCaseFirstLetter(schema[field].caster.options.ref)
56
- model[field].apiRoute = 'admin/' + StringUtils.lowerCaseFirstLetter(pluralize(schema[field].caster.options.ref))
57
- model[field].arrayType = 'ref'
58
- }
59
-
60
- else {
61
- model[field].arrayType = schema[field].caster.instance
62
- }
63
- }
64
-
65
- model[field].type = model[field].type? model[field].type: fieldType
66
- model[field].defaultValue = schema[field].defaultValue? schema[field].defaultValue: undefined
67
- model[field].required = model[field].required? model[field].required: schemaField.isRequired? schemaField.isRequired: false
68
- model[field].apiRoute = model[field].apiRoute? model[field].apiRoute: schemaField.options.ref? 'admin/' + StringUtils.lowerCaseFirstLetter(pluralize(schemaField.options.ref)): undefined
69
- model[field].enumValues = model[field].type === 'enum'? schemaField.enumValues: undefined
70
- model[field].value = model[field].value? model[field].value: translation? translation: refCheck? StringUtils.checkRefId(field)? StringUtils.convertCamelCaseToWord(field.slice(0, -2)): StringUtils.convertCamelCaseToWord(field) : StringUtils.convertCamelCaseToWord(field)
71
-
72
- });
73
-
74
- if (model.virtuals) {
75
-
76
- for (const extraField in model.virtuals) {
77
-
78
- model[extraField] = model[extraField]? model[extraField]: {}
79
- model[extraField].type = model.virtuals[extraField].type
80
- model[extraField].arrayType = model.virtuals[extraField].arrayType? model.virtuals[extraField].arrayType: undefined
81
- model[extraField].required = model.virtuals[extraField].required
82
- model[extraField].value = ResourcesHelper.checkResourceTranslation(resource, extraField)? ResourcesHelper.checkResourceTranslation(resource, extraField): StringUtils.convertCamelCaseToWord(extraField)
83
- model[extraField].path = StringUtils.getRefPath(extraField)
84
- }
85
-
86
- }
87
-
88
- return [model, populatedString]
89
-
90
- }
91
-
92
- private static setNestedSchema(paths: any, modelName: string, model: any, field: string, populatedString: string, resource: IResourceFile) {
93
-
94
- var nestedSchema : {[key:string]: any} = {}
95
- var nestedSchemaKeys = []
96
- const nestedModelodelAndPopulatedString = this.generateSchema(paths, nestedSchema, modelName, resource)
97
-
98
- model[field].schema = nestedModelodelAndPopulatedString[0]
99
- model[field].populatedString = nestedModelodelAndPopulatedString[1]
100
-
101
- const populatedStringArray = nestedModelodelAndPopulatedString[1].split(' ')
102
-
103
- populatedStringArray.forEach((nestedSchemaPopulatedString: any) => {
104
-
105
- if (nestedSchemaPopulatedString) {
106
- populatedString = StringUtils.getPopulatedString(populatedString, field + '.' + nestedSchemaPopulatedString)
107
- }
108
-
109
- });
110
-
111
- for (const key in model[field].schema) {
112
- nestedSchemaKeys.push(key)
113
- }
114
-
115
- model[field].keys = nestedSchemaKeys
116
-
117
- return [model, populatedString]
118
- }
119
-
120
- }