|
@@ -1,7 +1,7 @@
|
|
|
const _ = require('lodash')
|
|
const _ = require('lodash')
|
|
|
const defaults = require('./defaults')
|
|
const defaults = require('./defaults')
|
|
|
const { Op } = require('sequelize')
|
|
const { Op } = require('sequelize')
|
|
|
-const { diffBy } = require('../util')
|
|
|
|
|
|
|
+const { diffBy, sanitize } = require('../util')
|
|
|
const { sequelize } = require('../database')
|
|
const { sequelize } = require('../database')
|
|
|
|
|
|
|
|
const crudController = (opts) => {
|
|
const crudController = (opts) => {
|
|
@@ -34,14 +34,14 @@ const crudController = (opts) => {
|
|
|
[field.fieldName]: { [Op.like]: `%${req.query.q}%` }
|
|
[field.fieldName]: { [Op.like]: `%${req.query.q}%` }
|
|
|
}))
|
|
}))
|
|
|
const where = { ...subset, [Op.or]: or }
|
|
const where = { ...subset, [Op.or]: or }
|
|
|
- const data = (await Type.findAll({ where })).map(d => d.sanitize ? d.sanitize() : d)
|
|
|
|
|
|
|
+ const data = await sanitize(req, await Type.findAll({ where }))
|
|
|
res.status(200).send(data)
|
|
res.status(200).send(data)
|
|
|
} else if (res.query && res.query.ids) {
|
|
} else if (res.query && res.query.ids) {
|
|
|
const ids = res.query.ids.split(',')
|
|
const ids = res.query.ids.split(',')
|
|
|
- const data = (await Type.findAll({where: { ...(await subset(req)), id: { [Op.in]: ids }}})).map(d => d.sanitize ? d.sanitize() : d)
|
|
|
|
|
|
|
+ const data = await sanitize(req, await Type.findAll({where: { ...(await subset(req)), id: { [Op.in]: ids }}}))
|
|
|
res.status(200).send(data)
|
|
res.status(200).send(data)
|
|
|
} else {
|
|
} else {
|
|
|
- const data = (await Type.findAll({where: { ...(await subset(req)) }})).map(d => d.sanitize ? d.sanitize() : d)
|
|
|
|
|
|
|
+ const data = await sanitize(req, await Type.findAll({where: { ...(await subset(req)) }}))
|
|
|
res.status(200).send(data)
|
|
res.status(200).send(data)
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -82,7 +82,7 @@ const crudController = (opts) => {
|
|
|
const data = (await Type.create(record, {transaction}))
|
|
const data = (await Type.create(record, {transaction}))
|
|
|
await setAssociations(record, data, transaction)
|
|
await setAssociations(record, data, transaction)
|
|
|
await transaction.commit()
|
|
await transaction.commit()
|
|
|
- res.status(200).send(data && data.sanitize ? data.sanitize() : data)
|
|
|
|
|
|
|
+ res.status(200).send(await sanitize(req, data))
|
|
|
} catch (err) {
|
|
} catch (err) {
|
|
|
await transaction.rollback()
|
|
await transaction.rollback()
|
|
|
throw err
|
|
throw err
|
|
@@ -90,7 +90,7 @@ const crudController = (opts) => {
|
|
|
}
|
|
}
|
|
|
const read = async (req, res) => {
|
|
const read = async (req, res) => {
|
|
|
const data = (await Type.findOne({where: {id: req.params[opts.routeParam]}}))
|
|
const data = (await Type.findOne({where: {id: req.params[opts.routeParam]}}))
|
|
|
- const json = data && (data.sanitize ? data.sanitize() : data.toJSON())
|
|
|
|
|
|
|
+ const json = await sanitize(req, data)
|
|
|
await getAssociations(data, json)
|
|
await getAssociations(data, json)
|
|
|
res.status(200).send(json)
|
|
res.status(200).send(json)
|
|
|
}
|
|
}
|
|
@@ -100,7 +100,7 @@ const crudController = (opts) => {
|
|
|
const record = _.omit(req.body, _.keys(await subset(req)))
|
|
const record = _.omit(req.body, _.keys(await subset(req)))
|
|
|
const updated = (await Type.update(record, { where: { id: req.params[opts.routeParam] }, transaction }))
|
|
const updated = (await Type.update(record, { where: { id: req.params[opts.routeParam] }, transaction }))
|
|
|
const data = (await Type.findOne({where: { id: req.params[opts.routeParam] }}))
|
|
const data = (await Type.findOne({where: { id: req.params[opts.routeParam] }}))
|
|
|
- const json = data && (data.sanitize ? data.sanitize() : data.toJSON())
|
|
|
|
|
|
|
+ const json = await sanitize(req, data)
|
|
|
|
|
|
|
|
await setAssociations(record, data, transaction)
|
|
await setAssociations(record, data, transaction)
|
|
|
await transaction.commit()
|
|
await transaction.commit()
|
|
@@ -124,14 +124,14 @@ const crudController = (opts) => {
|
|
|
deletedAt: { [Op.ne]: null }
|
|
deletedAt: { [Op.ne]: null }
|
|
|
}
|
|
}
|
|
|
}))
|
|
}))
|
|
|
- res.status(200).send(data && data.sanitize ? data.sanitize() : data)
|
|
|
|
|
|
|
+ res.status(200).send(await sanitize(req, data))
|
|
|
}
|
|
}
|
|
|
const undelete = async (req, res) => {
|
|
const undelete = async (req, res) => {
|
|
|
const data = (await Type.update({ deletedAt: null }, {
|
|
const data = (await Type.update({ deletedAt: null }, {
|
|
|
paranoid: false,
|
|
paranoid: false,
|
|
|
where: { id: req.params[opts.routeParam] }
|
|
where: { id: req.params[opts.routeParam] }
|
|
|
}))
|
|
}))
|
|
|
- res.status(200).send(data && data.sanitize ? data.sanitize() : data)
|
|
|
|
|
|
|
+ res.status(200).send(await sanitize(req, data))
|
|
|
}
|
|
}
|
|
|
// TODO: Create, Read, Update, Delete
|
|
// TODO: Create, Read, Update, Delete
|
|
|
|
|
|