@websy/websy-designs 0.0.116 → 0.0.120

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.
@@ -19,7 +19,11 @@ const sql = {
19
19
  id SERIAL PRIMARY KEY,
20
20
  text text,
21
21
  tags text,
22
- label text
22
+ label text,
23
+ createuser text,
24
+ edituser text,
25
+ createdate timestamp without time zone DEFAULT now(),
26
+ editdate timestamp without time zone DEFAULT now()
23
27
  );
24
28
  `,
25
29
  translations: `
@@ -29,7 +33,11 @@ const sql = {
29
33
  entity_id integer NOT NULL,
30
34
  field_name text,
31
35
  language character varying(28),
32
- text text
36
+ text text,
37
+ createuser text,
38
+ edituser text,
39
+ createdate timestamp without time zone DEFAULT now(),
40
+ editdate timestamp without time zone DEFAULT now()
33
41
  );
34
42
  `
35
43
  }
@@ -93,15 +101,43 @@ class PGHelper {
93
101
  sql += `
94
102
  LEFT JOIN (
95
103
  SELECT entity_id, json_object_agg(field_name, text) as translation
96
- FROM translations
104
+ FROM translations as t
105
+ WHERE table_name = '${entity}' AND language = '${lang}'
106
+ GROUP BY entity_id
107
+ ) t2
108
+ ON ${entity}.${(this.options.entityConfig[entity] && this.options.entityConfig[entity].idColumn) || 'id'} = t2.entity_id
109
+ `
110
+ }
111
+ sql += `
112
+ WHERE ${this.buildWhere(query.where, entity)}
113
+ ${this.buildOrderBy(query)}
114
+ `
115
+ if (query.limit) {
116
+ sql += ` LIMIT ${query.limit}`
117
+ }
118
+ if (query.offset) {
119
+ sql += ` OFFSET ${query.offset}`
120
+ }
121
+ return sql
122
+ }
123
+ buildSelectWithId (entity, id, query, columns, lang) {
124
+ let sql = `
125
+ SELECT ${columns || '*'}
126
+ FROM ${entity}
127
+ `
128
+ if ((process.env.TRANSLATE === true || process.env.TRANSLATE === 'true') && lang !== null) {
129
+ sql += `
130
+ LEFT JOIN (
131
+ SELECT entity_id, json_object_agg(field_name, text) as translation
132
+ FROM translations as t
97
133
  WHERE table_name = '${entity}' AND language = '${lang}'
98
134
  GROUP BY entity_id
99
- ) as translations
100
- ON ${entity}.${(this.options.entityConfig[entity] && this.options.entityConfig[entity].idColumn) || 'id'} = translations.entity_id
135
+ ) t2
136
+ ON ${entity}.${(this.options.entityConfig[entity] && this.options.entityConfig[entity].idColumn) || 'id'} = t2.entity_id
101
137
  `
102
138
  }
103
139
  sql += `
104
- WHERE ${this.buildWhere(query.where)}
140
+ WHERE ${this.buildWhereWithId(entity, id)}
105
141
  ${this.buildOrderBy(query)}
106
142
  `
107
143
  return sql
@@ -119,18 +155,37 @@ class PGHelper {
119
155
  WHERE ${this.buildWhere(where)}
120
156
  `
121
157
  }
122
- buildWhere (input) {
158
+ buildUpdateWithId (entity, id, data) {
159
+ let updates = []
160
+ for (let key in data) {
161
+ if (this.updateIgnores.indexOf(key) === -1) {
162
+ updates.push(`${key} = ${(data[key] === null ? data[key] : `'${data[key]}'`)}`)
163
+ }
164
+ }
165
+ return `
166
+ UPDATE ${entity}
167
+ SET ${updates.join(',')}
168
+ WHERE ${this.buildWhereWithId(entity, id)}
169
+ `
170
+ }
171
+ buildWhere (input, entity) {
123
172
  if (typeof input === 'undefined') {
124
173
  return '1=1'
125
174
  }
126
175
  else {
176
+ try {
177
+ input = decodeURI(input)
178
+ }
179
+ catch (error) {
180
+ // console.log(error)
181
+ }
127
182
  let list = input.split(';').map(d => {
128
183
  let parts = d.split(':')
129
184
  if (parts[1].indexOf('%') !== -1) {
130
- return `${parts[0]} LIKE '${parts[1]}'`
185
+ return `${entity ? entity + '.' : ''}${parts[0]} LIKE '${parts[1]}'`
131
186
  }
132
187
  else {
133
- return `${parts[0]} = '${parts[1]}'`
188
+ return `${entity ? entity + '.' : ''}${parts[0]} = '${parts[1]}'`
134
189
  }
135
190
  })
136
191
  return `
@@ -138,6 +193,14 @@ class PGHelper {
138
193
  `
139
194
  }
140
195
  }
196
+ buildWhereWithId (entity, id) {
197
+ if (typeof id === 'undefined') {
198
+ return '1=1'
199
+ }
200
+ else {
201
+ return `${(this.options.entityConfig[entity] && this.options.entityConfig[entity].idColumn) || 'id'} = ${id}`
202
+ }
203
+ }
141
204
  checkTables () {
142
205
  this.createContentTable().then(() => {
143
206
  this.createTranslationTable()
@@ -210,7 +273,7 @@ class PGHelper {
210
273
  rollback (err, callbackFn) {
211
274
  console.log('Rolling Back')
212
275
  console.log(err)
213
- this.client.query('ROLLBACK', function (err) {
276
+ this.client.query('ROLLBACK', function (rollbackErr) {
214
277
  console.log(err)
215
278
  // done()
216
279
  callbackFn(err)
@@ -5,35 +5,32 @@ const router = express.Router()
5
5
  // const AuthHelper = require('../helpers/auth')
6
6
 
7
7
  function APIRoutes (dbHelper) {
8
+ router.delete('/:entity/:id', (req, res) => {
9
+ const sql = `DELETE FROM ${req.params.entity} WHERE ${dbHelper.buildWhereWithId(req.params.entity, req.params.id)}`
10
+ dbHelper.execute(sql).then(response => res.json(response), err => res.json(err))
11
+ })
8
12
  router.delete('/:entity', (req, res) => {
9
13
  const sql = dbHelper.buildDelete(req.params.entity, req.query.where)
10
14
  dbHelper.execute(sql).then(response => res.json(response), err => res.json(err))
11
15
  })
12
- // router.get('/:entity', AuthHelper.checkPermissions, (req, res) => {
16
+ router.get('/:entity/:id', (req, res) => {
17
+ let lang = null
18
+ if (req.session && req.session.language && req.query.notranslate !== 'true') {
19
+ lang = req.session.language
20
+ }
21
+ const sql = dbHelper.buildSelectWithId(req.params.entity, req.params.id, req.query, req.query.columns, lang)
22
+ dbHelper.execute(sql).then(response => {
23
+ res.json(translate(response))
24
+ }, err => res.json(err))
25
+ })
13
26
  router.get('/:entity', (req, res) => {
14
27
  let lang = null
15
- if (req.session && req.session.language) {
28
+ if (req.session && req.session.language && req.query.notranslate !== 'true') {
16
29
  lang = req.session.language
17
30
  }
18
31
  const sql = dbHelper.buildSelect(req.params.entity, req.query, req.query.columns, lang)
19
32
  dbHelper.execute(sql).then(response => {
20
- if (process.env.TRANSLATE && response.rows) {
21
- for (let r = 0; r < response.rows.length; r++) {
22
- const row = response.rows[r]
23
- for (let key in row) {
24
- try {
25
- if (row.translation) {
26
- row.translation = JSON.parse(row.translation)
27
- row[key] = row.translation[key] || row[key]
28
- }
29
- }
30
- catch (error) {
31
- row[key] = row.translation[key] || row[key]
32
- }
33
- }
34
- }
35
- }
36
- res.json(response)
33
+ res.json(translate(response))
37
34
  }, err => res.json(err))
38
35
  })
39
36
  router.post('/:entity', (req, res) => {
@@ -41,6 +38,13 @@ function APIRoutes (dbHelper) {
41
38
  console.log(req.body)
42
39
  const sql = dbHelper.buildInsert(req.params.entity, req.body)
43
40
  console.log(sql)
41
+ dbHelper.execute(sql).then(response => res.json(response), err => {
42
+ res.statusCode = 404
43
+ res.json({err})
44
+ })
45
+ })
46
+ router.put('/:entity/:id', (req, res) => {
47
+ const sql = dbHelper.buildUpdateWithId(req.params.entity, req.params.id, req.body)
44
48
  dbHelper.execute(sql).then(response => res.json(response), err => res.json(err))
45
49
  })
46
50
  router.put('/:entity', (req, res) => {
@@ -50,4 +54,24 @@ function APIRoutes (dbHelper) {
50
54
  return router
51
55
  }
52
56
 
57
+ function translate (response) {
58
+ if (process.env.TRANSLATE && response.rows) {
59
+ for (let r = 0; r < response.rows.length; r++) {
60
+ const row = response.rows[r]
61
+ for (let key in row) {
62
+ try {
63
+ if (row.translation) {
64
+ row.translation = JSON.parse(row.translation)
65
+ row[key] = row.translation[key] || row[key]
66
+ }
67
+ }
68
+ catch (error) {
69
+ row[key] = row.translation[key] || row[key]
70
+ }
71
+ }
72
+ }
73
+ }
74
+ return response
75
+ }
76
+
53
77
  module.exports = APIRoutes
@@ -7,5 +7,11 @@ module.exports = {
7
7
  text += possible.charAt(Math.floor(Math.random() * possible.length))
8
8
  }
9
9
  return text
10
+ },
11
+ getElementPos: el => {
12
+ const rect = el.getBoundingClientRect()
13
+ const scrollLeft = window.pageXOffset || document.documentElement.scrollLeft
14
+ const scrollTop = window.pageYOffset || document.documentElement.scrollTop
15
+ return { top: rect.top + scrollTop, left: rect.left + scrollLeft }
10
16
  }
11
17
  }
@@ -8,6 +8,20 @@ const expressSession = require('express-session')
8
8
 
9
9
  module.exports = function (options) {
10
10
  return new Promise((resolve, reject) => {
11
+ const unless = function (middleware) {
12
+ let excludedPaths = ['health']
13
+ if (process.env.EXCLUDED_SESSION_PATHS) {
14
+ excludedPaths = excludedPaths.concat(process.env.EXCLUDED_SESSION_PATHS.split(','))
15
+ }
16
+ return function (req, res, next) {
17
+ if (excludedPaths.indexOf(req.path) !== -1) {
18
+ return next()
19
+ }
20
+ else {
21
+ return middleware(req, res, next)
22
+ }
23
+ }
24
+ }
11
25
  const app = express()
12
26
  app.set('trust proxy', 1)
13
27
  process.env.wdRoot = __dirname
@@ -33,6 +47,9 @@ module.exports = function (options) {
33
47
  next()
34
48
  }
35
49
  app.use(allowCrossDomain)
50
+ app.get('/health', (req, res) => {
51
+ res.json('OK')
52
+ })
36
53
  app.get('/environment', (req, res) => {
37
54
  const env = {}
38
55
  for (let key in process.env) {
@@ -44,20 +61,17 @@ module.exports = function (options) {
44
61
  })
45
62
  if (options.useRecaptcha === true) {
46
63
  app.use('/google', require(`./routes/${version}/recaptcha`))
47
- }
48
- console.log('setting pdf route')
64
+ }
49
65
  app.use('/pdf', require(`./routes/${version}/pdf`))
50
66
  if (options.useDB === true) {
51
67
  const dbHelper = require(`./helpers/${version}/${options.dbEngine}Helper`)
52
- dbHelper.init(options.dbOptions).then(() => {
53
- console.log('initializing session')
54
- console.log(dbHelper.pool)
68
+ dbHelper.init(options.dbOptions || {}).then(() => {
69
+ console.log('initializing session')
55
70
  // const store = new DBSession({
56
71
  // pool: dbHelper.pool, // Connection pool, need to make dynamic to accommodate mySql
57
72
  // tableName: 'sessions' // Use another table-name than the default "session" one
58
73
  // })
59
- console.log('setting up session')
60
- console.log(process.env)
74
+ console.log('setting up session')
61
75
  app.set('trust proxy', 1)
62
76
  app.use(cookieParser(process.env.SESSION_SECRET))
63
77
  let cookieConfig = {
@@ -73,28 +87,27 @@ module.exports = function (options) {
73
87
  }
74
88
  if (process.env.COOKIE_SECURE === 'true' || process.env.COOKIE_SECURE === true) {
75
89
  cookieConfig.secure = true
90
+ }
91
+ let DBSession = null
92
+ let store = null
93
+ if (options.useDBStore === true) {
94
+ console.log('using db store')
95
+ if (options.dbEngine === 'pg') {
96
+ DBSession = require('connect-pg-simple')(expressSession)
97
+ store = new DBSession({
98
+ pool: dbHelper.pool, // Connection pool, need to make dynamic to accommodate mySql
99
+ tableName: 'sessions' // Use another table-name than the default "session" one
100
+ })
101
+ }
76
102
  }
77
- console.log(cookieConfig)
78
- app.use(expressSession({
103
+ app.use(unless(expressSession({
79
104
  secret: process.env.SESSION_SECRET,
80
105
  resave: false,
81
106
  saveUninitialized: true,
82
107
  cookie: cookieConfig,
83
- name: process.env.COOKIE_NAME
84
- // store: store
85
- }))
86
- // app.use(sessionHelper.checkSession(dbHelper))
87
- // app.use(function (req, res, next) {
88
- // console.log('WD MIDDLEWARE')
89
- // console.log(req.session)
90
- // let cookies = {}
91
- // if (typeof req.headers.cookie === 'string') {
92
- // cookies = cookie.parse(req.headers.cookie)
93
- // }
94
- // console.log('cookies')
95
- // console.log(cookies)
96
- // next()
97
- // })
108
+ name: process.env.COOKIE_NAME,
109
+ store: store
110
+ })))
98
111
  if (options.uses && Array.isArray(options.uses)) {
99
112
  try {
100
113
  options.uses.forEach(u => app.use(u))