@websy/websy-designs 1.9.13 → 1.10.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.
@@ -8,7 +8,7 @@ class AuthHelper {
8
8
  loginType: 'email'
9
9
  }
10
10
  this.options = Object.assign({}, DEFAULTS, options)
11
- console.log(this.options)
11
+ // console.log(this.options)
12
12
  }
13
13
  login (req, res) {
14
14
  return new Promise((resolve, reject) => {
@@ -16,25 +16,25 @@ class AuthHelper {
16
16
  return (md5(md5(password) + user.salt)) === user.pepper
17
17
  }
18
18
  // check in mongo if a user with username exists or not
19
- console.log(req.body)
19
+ // console.log(req.body)
20
20
  const email = req.body[this.options.loginType].toLowerCase()
21
21
  const userQuery = `
22
22
  SELECT *
23
23
  FROM users
24
24
  WHERE ${this.options.loginType} = '${email}'
25
25
  `
26
- console.log(userQuery)
26
+ // console.log(userQuery)
27
27
  this.dbHelper.execute(userQuery).then(result => {
28
28
  // Username does not exist, log the error and redirect back
29
29
  if (result.rows.length === 0) {
30
- console.log('No User')
30
+ // console.log('No User')
31
31
  reject(`User not found with ${this.options.loginType} ${email}`)
32
32
  return
33
33
  }
34
34
  const user = result.rows[0]
35
35
  // User exists but wrong password, log the error
36
36
  if (!isValidPassword(user, req.body.password)) {
37
- console.log('Invalid Password')
37
+ // console.log('Invalid Password')
38
38
  reject('Invalid Password')
39
39
  return
40
40
  }
@@ -46,10 +46,10 @@ class AuthHelper {
46
46
  WHERE id = ${user.id}
47
47
  `
48
48
  this.dbHelper.execute(userUpdateQuery).then(result => {
49
- console.log('yes')
49
+ // console.log('yes')
50
50
  resolve(user)
51
51
  }, err => {
52
- console.log('no')
52
+ // console.log('no')
53
53
  reject(err)
54
54
  })
55
55
  // LogonHistory.create({
@@ -121,18 +121,18 @@ class AuthHelper {
121
121
  })
122
122
  }
123
123
  isLoggedIn (req, res, next) {
124
- console.log(req.session)
124
+ // console.log(req.session)
125
125
  if (req.session && req.session.user && req.session.user.isAnonymous !== true) {
126
- console.log('in condition D')
126
+ // console.log('in condition D')
127
127
  next()
128
128
  }
129
129
  else {
130
- console.log('in condition E')
130
+ // console.log('in condition E')
131
131
  res.redirect('/login')
132
132
  }
133
133
  }
134
134
  checkPermissions (req, res, next) {
135
- console.log('hello')
135
+ // console.log('hello')
136
136
  if (process.env.USE_PERMISSIONS === 'true' || process.env.USE_PERMISSIONS === true) {
137
137
  const permissions = {
138
138
  entities: {
@@ -117,11 +117,27 @@ class PGHelper {
117
117
  if (process.env.EDIT_DATE_FIELD) {
118
118
  data[process.env.EDIT_DATE_FIELD] = (new Date()).toISOString()
119
119
  }
120
- return `
120
+ let sql = `
121
121
  INSERT INTO ${entity} (${Object.keys(data).join(',')})
122
- VALUES (${Object.values(data).map(d => (d === null ? `${d}` : `'${d}'`)).join(',')})
122
+ VALUES (
123
+ `
124
+ sql += Object.values(data).map(d => {
125
+ if (d === null) {
126
+ return d
127
+ }
128
+ else {
129
+ if (typeof d === 'string') {
130
+ d = d.replace(/'/g, `''`)
131
+ }
132
+ return `'${d}'`
133
+ // (d === null ? `${d}` : `'${d.replace(/'/)}'`)
134
+ }
135
+ }).join(',')
136
+ sql += `
137
+ )
123
138
  RETURNING ${(this.options.entityConfig[entity] && this.options.entityConfig[entity].idColumn) || 'id'}
124
139
  `
140
+ return sql
125
141
  }
126
142
  buildOrderBy (query) {
127
143
  return `
@@ -178,7 +194,7 @@ class PGHelper {
178
194
  `
179
195
  return sql
180
196
  }
181
- buildUpdate (entity, where, data) {
197
+ buildUpdate (entity, where, data, user) {
182
198
  let updates = []
183
199
  for (let key in data) {
184
200
  if (this.updateIgnores.indexOf(key) === -1) {
@@ -186,7 +202,14 @@ class PGHelper {
186
202
  data[key] = data[key].replace(/''/gm, `'`).replace(/'/gm, `''`).replace(/\\\\/gm, '\\')
187
203
  }
188
204
  updates.push(`${key} = ${(data[key] === null ? data[key] : `'${data[key]}'`)}`)
189
- }
205
+ }
206
+ }
207
+ if (process.env.CREATE_USER_FIELD && process.env.USERID_FIELD && user) {
208
+ let userId = user[process.env.USERID_FIELD || 'id']
209
+ updates.push(`${process.env.EDIT_USER_FIELD} = '${userId}'`)
210
+ }
211
+ if (process.env.EDIT_DATE_FIELD) {
212
+ updates.push(`${process.env.EDIT_DATE_FIELD} = '${(new Date()).toISOString()}'`)
190
213
  }
191
214
  return `
192
215
  UPDATE ${entity}
@@ -194,19 +217,67 @@ class PGHelper {
194
217
  WHERE ${this.buildWhere(where)}
195
218
  `
196
219
  }
197
- buildUpdateWithId (entity, id, data) {
220
+ buildUpdateWithId (entity, id, data, user) {
198
221
  let updates = []
199
222
  for (let key in data) {
200
223
  if (this.updateIgnores.indexOf(key) === -1) {
201
224
  updates.push(`${key} = ${(data[key] === null ? data[key] : `'${data[key]}'`)}`)
202
225
  }
203
226
  }
227
+ if (process.env.CREATE_USER_FIELD && process.env.USERID_FIELD && user) {
228
+ let userId = user[process.env.USERID_FIELD || 'id']
229
+ updates.push(`${process.env.EDIT_USER_FIELD} = '${userId}'`)
230
+ }
231
+ if (process.env.EDIT_DATE_FIELD) {
232
+ updates.push(`${process.env.EDIT_DATE_FIELD} = '${(new Date()).toISOString()}'`)
233
+ }
204
234
  return `
205
235
  UPDATE ${entity}
206
236
  SET ${updates.join(',')}
207
237
  WHERE ${this.buildWhereWithId(entity, id)}
208
238
  `
209
239
  }
240
+ buildUpsert (entity, data, user) {
241
+ // delete data[(this.options.entityConfig[entity] && this.options.entityConfig[entity].idColumn) || 'id']
242
+ // data.create_user = user
243
+ let sql = ``
244
+ data.forEach(row => {
245
+ if (process.env.CREATE_USER_FIELD && process.env.USERID_FIELD && user) {
246
+ row[process.env.CREATE_USER_FIELD] = user[process.env.USERID_FIELD || 'id']
247
+ row[process.env.EDIT_USER_FIELD] = user[process.env.USERID_FIELD || 'id']
248
+ }
249
+ if (process.env.EDIT_DATE_FIELD) {
250
+ row[process.env.EDIT_DATE_FIELD] = (new Date()).toISOString()
251
+ }
252
+ if (typeof row[(this.options.entityConfig[entity] && this.options.entityConfig[entity].idColumn) || 'id'] !== 'undefined') {
253
+ let id = row[(this.options.entityConfig[entity] && this.options.entityConfig[entity].idColumn) || 'id']
254
+ delete row[(this.options.entityConfig[entity] && this.options.entityConfig[entity].idColumn) || 'id']
255
+ let updates = []
256
+ for (let key in row) {
257
+ if (this.updateIgnores.indexOf(key) === -1) {
258
+ if (typeof row[key] === 'string') {
259
+ row[key] = row[key].replace(/''/gm, `'`).replace(/'/gm, `''`).replace(/\\\\/gm, '\\')
260
+ }
261
+ updates.push(`${key} = ${(row[key] === null ? row[key] : `'${row[key]}'`)}`)
262
+ }
263
+ }
264
+ sql += `
265
+ UPDATE ${entity}
266
+ SET ${updates.join(',')}
267
+ WHERE ${this.buildWhereWithId(entity, id)};
268
+ `
269
+ }
270
+ else {
271
+ delete row[(this.options.entityConfig[entity] && this.options.entityConfig[entity].idColumn) || 'id']
272
+ sql += `
273
+ INSERT INTO ${entity} (${Object.keys(row).join(',')})
274
+ VALUES (${Object.values(row).map(d => (d === null ? `${d}` : `'${d}'`)).join(',')})
275
+ RETURNING ${(this.options.entityConfig[entity] && this.options.entityConfig[entity].idColumn) || 'id'};
276
+ `
277
+ }
278
+ })
279
+ return sql
280
+ }
210
281
  buildWhere (input, entity) {
211
282
  if (typeof input === 'undefined') {
212
283
  return '1=1'
@@ -218,8 +289,8 @@ class PGHelper {
218
289
  catch (error) {
219
290
  // console.log(error)
220
291
  }
221
- console.log('where input', input)
222
- console.log('splitter is', this.options.fieldValueSeparator)
292
+ // console.log('where input', input)
293
+ // console.log('splitter is', this.options.fieldValueSeparator)
223
294
  let list = input.split(';').map(d => {
224
295
  let parts = d.split(this.options.fieldValueSeparator)
225
296
  if (parts.length === 2) {