@ -9,7 +9,7 @@ const {
ViewNames ,
ViewNames ,
} = require ( "../../db/utils" )
} = require ( "../../db/utils" )
const usersController = require ( "./user" )
const usersController = require ( "./user" )
const { cloneDeep } = require ( "lodash " )
const { coerceRowValues } = require ( "../../utilities " )
const TABLE_VIEW_BEGINS_WITH = ` all ${ SEPARATOR } ${ DocumentTypes . TABLE } ${ SEPARATOR } `
const TABLE_VIEW_BEGINS_WITH = ` all ${ SEPARATOR } ${ DocumentTypes . TABLE } ${ SEPARATOR } `
@ -29,11 +29,24 @@ validateJs.extend(validateJs.validators.datetime, {
} ,
} ,
} )
} )
// lots of row functionality too specific to pass to user controller, simply handle the
async function findRow ( db , appId , tableId , rowId ) {
// password deletion here
let row
function removePassword ( tableId , row ) {
if ( tableId === ViewNames . USERS ) {
if ( tableId === ViewNames . USERS ) {
delete row . password
let ctx = {
params : {
userId : rowId ,
} ,
user : {
appId ,
} ,
}
await usersController . find ( ctx )
row = ctx . body
} else {
row = await db . get ( rowId )
}
if ( row . tableId !== tableId ) {
throw "Supplied tableId does not match the rows tableId"
}
}
return row
return row
}
}
@ -224,13 +237,12 @@ exports.fetchTableRows = async function(ctx) {
exports . find = async function ( ctx ) {
exports . find = async function ( ctx ) {
const appId = ctx . user . appId
const appId = ctx . user . appId
const db = new CouchDB ( appId )
const db = new CouchDB ( appId )
let row = await db . get ( ctx . params . rowId )
try {
if ( row . tableId !== ctx . params . tableId ) {
const row = await findRow ( db , appId , ctx . params . tableId , ctx . params . rowId )
ctx . throw ( 400 , "Supplied tableId does not match the rows tableId" )
ctx . body = await linkRows . attachLinkInfo ( appId , row )
return
} catch ( err ) {
ctx . throw ( 400 , err )
}
}
row = removePassword ( ctx . params . tableId , row )
ctx . body = await linkRows . attachLinkInfo ( appId , row )
}
}
exports . destroy = async function ( ctx ) {
exports . destroy = async function ( ctx ) {
@ -296,8 +308,10 @@ exports.fetchEnrichedRow = async function(ctx) {
return
return
}
}
// need table to work out where links go in row
// need table to work out where links go in row
let [ table , row ] = await Promise . all ( [ db . get ( tableId ) , db . get ( rowId ) ] )
let [ table , row ] = await Promise . all ( [
row = removePassword ( tableId , row )
db . get ( tableId ) ,
findRow ( db , appId , tableId , rowId ) ,
] )
// get the link docs
// get the link docs
const linkVals = await linkRows . getLinkDocuments ( {
const linkVals = await linkRows . getLinkDocuments ( {
appId ,
appId ,
@ -327,68 +341,6 @@ exports.fetchEnrichedRow = async function(ctx) {
ctx . status = 200
ctx . status = 200
}
}
function coerceRowValues ( record , table ) {
const row = cloneDeep ( record )
for ( let [ key , value ] of Object . entries ( row ) ) {
const field = table . schema [ key ]
if ( ! field ) continue
// eslint-disable-next-line no-prototype-builtins
if ( TYPE_TRANSFORM_MAP [ field . type ] . hasOwnProperty ( value ) ) {
row [ key ] = TYPE_TRANSFORM_MAP [ field . type ] [ value ]
} else if ( TYPE_TRANSFORM_MAP [ field . type ] . parse ) {
row [ key ] = TYPE_TRANSFORM_MAP [ field . type ] . parse ( value )
}
}
return row
}
const TYPE_TRANSFORM_MAP = {
link : {
"" : [ ] ,
[ null ] : [ ] ,
[ undefined ] : undefined ,
} ,
options : {
"" : "" ,
[ null ] : "" ,
[ undefined ] : undefined ,
} ,
string : {
"" : "" ,
[ null ] : "" ,
[ undefined ] : undefined ,
} ,
longform : {
"" : "" ,
[ null ] : "" ,
[ undefined ] : undefined ,
} ,
number : {
"" : null ,
[ null ] : null ,
[ undefined ] : undefined ,
parse : n => parseFloat ( n ) ,
} ,
datetime : {
"" : null ,
[ undefined ] : undefined ,
[ null ] : null ,
} ,
attachment : {
"" : [ ] ,
[ null ] : [ ] ,
[ undefined ] : undefined ,
} ,
boolean : {
"" : null ,
[ null ] : null ,
[ undefined ] : undefined ,
true : true ,
false : false ,
} ,
}
async function bulkDelete ( ctx ) {
async function bulkDelete ( ctx ) {
const appId = ctx . user . appId
const appId = ctx . user . appId
const { rows } = ctx . request . body
const { rows } = ctx . request . body