yaml-admin-api 0.0.67 → 0.0.69

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": "yaml-admin-api",
3
- "version": "0.0.67",
3
+ "version": "0.0.69",
4
4
  "license": "MIT",
5
5
  "description": "YAML Admin API package",
6
6
  "type": "commonjs",
@@ -20,7 +20,7 @@ const generateChartApi = async (app, db, yml, api_prefix) => {
20
20
 
21
21
  const auth = withConfig({ db, jwt_secret: yml.login["jwt-secret"] });
22
22
 
23
- const createChartDataTypeDate = async (chart, {from_date}) => {
23
+ const createChartDataTypeDate = async (chart, {from_date, filter}) => {
24
24
  const r = {
25
25
  options: {
26
26
  chart: { id: chart.id },
@@ -92,19 +92,26 @@ const generateChartApi = async (app, db, yml, api_prefix) => {
92
92
  }
93
93
  })
94
94
 
95
- let a = [
96
- { $match: {server_id:148, parent_id:null}},
95
+ let a = []
96
+ if(filter && Object.keys(filter).length > 0)
97
+ a.push({ $match: filter })
98
+
99
+ a.push(...[
97
100
  ...lookup_list,
98
101
  { $match: match},
99
102
  ...group_list,
100
103
  { $sort: { _id: -1 } },
101
- ]
104
+ ])
102
105
 
103
106
  if(limit)
104
107
  a.push({ $limit: limit })
105
108
 
109
+ //debug
110
+ if(yml.debug)
111
+ console.log('chart', chart.label, entity_x, JSON.stringify(a, null, 2))
112
+
106
113
  const list = await db.collection(entity_x).aggregate(a).toArray();
107
-
114
+
108
115
  list.map(m => {
109
116
  if (format)
110
117
  return moment.tz(m._id, timezone).format(format);
@@ -131,7 +138,7 @@ const generateChartApi = async (app, db, yml, api_prefix) => {
131
138
  return r
132
139
  }
133
140
 
134
- const createChartDataTypeField = async (chart) => {
141
+ const createChartDataTypeField = async (chart, {filter}) => {
135
142
  const r = {
136
143
  options: {
137
144
  chart: { id: chart.id },
@@ -154,10 +161,6 @@ const generateChartApi = async (app, db, yml, api_prefix) => {
154
161
  for (const s of y.series) {
155
162
  const { label } = s;
156
163
 
157
- let match_list = []
158
- if(s['if'] && !relation)
159
- match_list.push({ $match: evaluateIfToMatch(s['if'])})
160
-
161
164
  let lookup_list = []
162
165
  if (relation) {
163
166
  if(!relation.chain)
@@ -169,7 +172,7 @@ const generateChartApi = async (app, db, yml, api_prefix) => {
169
172
  from: entity_y,
170
173
  let: { root_x_key: `$${relation.match.x}` },
171
174
  pipeline: [],
172
- as: entity_y,
175
+ as: entity_y,
173
176
  }
174
177
 
175
178
  for(let i=0;i<relation.chain.length;i++) {
@@ -185,7 +188,6 @@ const generateChartApi = async (app, db, yml, api_prefix) => {
185
188
  lookup.pipeline.push({ $unwind: `$${m.entity}` })
186
189
  }
187
190
 
188
-
189
191
  if(s['if'])
190
192
  lookup.pipeline.push({ $match: evaluateIfToMatch(s['if'])})
191
193
 
@@ -226,11 +228,12 @@ const generateChartApi = async (app, db, yml, api_prefix) => {
226
228
  })
227
229
  }
228
230
 
229
- let a = [
230
- { $match: {server_id:148, parent_id:null}},
231
- ...lookup_list,
232
- ]
231
+ let a = []
233
232
 
233
+ if(filter && Object.keys(filter).length > 0)
234
+ a.push({ $match: filter })
235
+
236
+ a.push(...lookup_list)
234
237
  a.push(...group_list)
235
238
 
236
239
  if(sort)
@@ -240,8 +243,8 @@ const generateChartApi = async (app, db, yml, api_prefix) => {
240
243
  a.push({ $limit: limit })
241
244
 
242
245
  //debug
243
- if(chart.debug)
244
- console.log('a', entity_x, JSON.stringify(a, null, 2))
246
+ if(yml.debug)
247
+ console.log('chart', chart.label, entity_x, JSON.stringify(a, null, 2))
245
248
 
246
249
  const list = await db.collection(entity_x).aggregate(a).toArray();
247
250
 
@@ -285,11 +288,25 @@ const generateChartApi = async (app, db, yml, api_prefix) => {
285
288
  try {
286
289
  const { x } = chart;
287
290
  let r
291
+
292
+ let filter = {};
293
+ chart.filter?.forEach(s => {
294
+ let value = req.query[s.name];
295
+ if(value == 'null')
296
+ filter[s.name] = null;
297
+ else if(value == 'not null')
298
+ filter[s.name] = { $ne: null };
299
+ else if(s.type == 'integer')
300
+ filter[s.name] = parseInt(value);
301
+ else
302
+ filter[s.name] = value;
303
+ });
304
+
288
305
  if (x.type == 'date') {
289
306
  let {from_date} = req.query //YYYYMMDD
290
- r = await createChartDataTypeDate(chart, {from_date});
307
+ r = await createChartDataTypeDate(chart, {from_date, filter});
291
308
  } else if(x.type == 'field') {
292
- r = await createChartDataTypeField(chart);
309
+ r = await createChartDataTypeField(chart, {filter});
293
310
  } else {
294
311
  throw new Error('x.type is not date or field');
295
312
  }
@@ -78,6 +78,10 @@ const generateCrud = async ({ app, db, entity_name, yml_entity, yml, options })
78
78
 
79
79
  const parseValueByType = (value, field) => {
80
80
  const { type, reference_entity, reference_match } = field
81
+ if(value == 'not null')
82
+ return { $ne: null }
83
+ else if(value == 'null')
84
+ return null
81
85
  if (type == 'reference') {
82
86
  const referenceEntity = yml.entity[reference_entity]
83
87
  const referenceField = referenceEntity.fields.find(f => f.name == reference_match)
@@ -240,9 +244,6 @@ const generateCrud = async ({ app, db, entity_name, yml_entity, yml, options })
240
244
  m.id = getKeyFromEntity(m)
241
245
  })
242
246
 
243
- if(yml.debug)
244
- console.log('list', entity_name, 'found', count)
245
-
246
247
  await addInfo(db, list)
247
248
  if(options?.listener?.entityListed)
248
249
  await options.listener.entityListed(db, entity_name, list)
package/src/login/auth.js CHANGED
@@ -77,14 +77,15 @@ const withConfig = (config) => {
77
77
  next);
78
78
  }
79
79
  else {
80
- const memberProjection = { projection: { _id: false, name: true, email: true, password: true, super: true, id: true } };
80
+ const memberProjection = { projection: { _id: false } };
81
81
  if (type === 'email') {
82
82
  memberProjection['password'] = true;
83
- let member = await db.collection('admin').findOne({ email: email }, memberProjection)
83
+ let entity = 'admin';
84
+ let member = await db.collection(entity).findOne({ email: email }, memberProjection)
84
85
  if (member != null) {
85
86
  let isPasswordMatch = await comparePassword(password, member.password)
86
87
  if (isPasswordMatch) {
87
- await db.collection('admin').updateOne({ email: email }, { $set: { login_date: new Date() } }, { upsert: false })
88
+ await db.collection(entity).updateOne({ email: email }, { $set: { login_date: new Date() } }, { upsert: false })
88
89
  delete member.password;
89
90
  authenticateSuccess(req, res, member, next);
90
91
  } else