2018-08-03 04:48:21 +08:00
"use strict" ;
const sql = require ( '../../services/sql' ) ;
2020-06-05 23:25:14 +08:00
const log = require ( '../../services/log' ) ;
2018-08-03 04:48:21 +08:00
const attributeService = require ( '../../services/attributes' ) ;
const repository = require ( '../../services/repository' ) ;
const Attribute = require ( '../../entities/attribute' ) ;
2020-06-20 18:31:38 +08:00
function getEffectiveNoteAttributes ( req ) {
const note = repository . getNote ( req . params . noteId ) ;
2018-08-07 19:33:10 +08:00
2020-06-20 18:31:38 +08:00
return note . getAttributes ( ) ;
2018-08-03 04:48:21 +08:00
}
2020-06-20 18:31:38 +08:00
function updateNoteAttribute ( req ) {
2018-08-06 20:43:42 +08:00
const noteId = req . params . noteId ;
const body = req . body ;
let attribute ;
if ( body . attributeId ) {
2020-06-20 18:31:38 +08:00
attribute = repository . getAttribute ( body . attributeId ) ;
2020-01-29 05:37:06 +08:00
if ( attribute . noteId !== noteId ) {
return [ 400 , ` Attribute ${ body . attributeId } is not owned by ${ noteId } ` ] ;
}
if ( body . type !== attribute . type
|| body . name !== attribute . name
|| ( body . type === 'relation' && body . value !== attribute . value ) ) {
if ( body . type !== 'relation' || ! ! body . value . trim ( ) ) {
const newAttribute = attribute . createClone ( body . type , body . name , body . value ) ;
2020-06-20 18:31:38 +08:00
newAttribute . save ( ) ;
2020-01-29 05:37:06 +08:00
}
attribute . isDeleted = true ;
2020-06-20 18:31:38 +08:00
attribute . save ( ) ;
2020-01-29 05:37:06 +08:00
return {
attributeId : attribute . attributeId
} ;
}
2018-08-06 20:43:42 +08:00
}
else {
2018-11-13 06:34:22 +08:00
if ( body . type === 'relation' && ! body . value . trim ( ) ) {
return { } ;
}
2018-08-06 20:43:42 +08:00
attribute = new Attribute ( ) ;
attribute . noteId = noteId ;
attribute . name = body . name ;
attribute . type = body . type ;
}
2018-11-13 06:34:22 +08:00
if ( body . value . trim ( ) ) {
attribute . value = body . value ;
}
else {
// relations should never have empty target
attribute . isDeleted = true ;
}
2018-08-06 20:43:42 +08:00
2020-06-20 18:31:38 +08:00
attribute . save ( ) ;
2018-08-06 23:24:35 +08:00
return {
attributeId : attribute . attributeId
} ;
}
2020-06-20 18:31:38 +08:00
function deleteNoteAttribute ( req ) {
2018-08-07 17:38:00 +08:00
const noteId = req . params . noteId ;
2018-08-06 23:24:35 +08:00
const attributeId = req . params . attributeId ;
2020-06-20 18:31:38 +08:00
const attribute = repository . getAttribute ( attributeId ) ;
2018-08-06 23:24:35 +08:00
if ( attribute ) {
2018-08-07 17:38:00 +08:00
if ( attribute . noteId !== noteId ) {
return [ 400 , ` Attribute ${ attributeId } is not owned by ${ noteId } ` ] ;
}
attribute . isDeleted = true ;
2020-06-20 18:31:38 +08:00
attribute . save ( ) ;
2018-08-06 23:24:35 +08:00
}
2018-08-06 20:43:42 +08:00
}
2020-07-04 16:29:17 +08:00
function updateNoteAttributes ( req ) {
2020-06-05 23:25:14 +08:00
const noteId = req . params . noteId ;
const incomingAttributes = req . body ;
2020-06-20 18:31:38 +08:00
const note = repository . getNote ( noteId ) ;
2020-06-05 23:25:14 +08:00
2020-06-20 18:31:38 +08:00
let existingAttrs = note . getAttributes ( ) ;
2020-06-05 23:25:14 +08:00
let position = 0 ;
for ( const incAttr of incomingAttributes ) {
position += 10 ;
const perfectMatchAttr = existingAttrs . find ( attr =>
attr . type === incAttr . type &&
attr . name === incAttr . name &&
attr . isInheritable === incAttr . isInheritable &&
attr . value === incAttr . value ) ;
if ( perfectMatchAttr ) {
existingAttrs = existingAttrs . filter ( attr => attr . attributeId !== perfectMatchAttr . attributeId ) ;
if ( perfectMatchAttr . position !== position ) {
perfectMatchAttr . position = position ;
2020-06-20 18:31:38 +08:00
perfectMatchAttr . save ( ) ;
2020-06-05 23:25:14 +08:00
}
continue ; // nothing to update
}
if ( incAttr . type === 'relation' ) {
2020-06-20 18:31:38 +08:00
const targetNote = repository . getNote ( incAttr . value ) ;
2020-06-05 23:25:14 +08:00
if ( ! targetNote || targetNote . isDeleted ) {
log . error ( ` Target note of relation ${ JSON . stringify ( incAttr ) } does not exist or is deleted ` ) ;
continue ;
}
}
const matchedAttr = existingAttrs . find ( attr =>
attr . type === incAttr . type &&
attr . name === incAttr . name &&
attr . isInheritable === incAttr . isInheritable ) ;
if ( matchedAttr ) {
matchedAttr . value = incAttr . value ;
matchedAttr . position = position ;
2020-06-20 18:31:38 +08:00
matchedAttr . save ( ) ;
2020-06-05 23:25:14 +08:00
existingAttrs = existingAttrs . filter ( attr => attr . attributeId !== matchedAttr . attributeId ) ;
continue ;
}
// no existing attribute has been matched so we need to create a new one
// type, name and isInheritable are immutable so even if there is an attribute with matching type & name, we need to create a new one and delete the former one
2020-06-20 18:31:38 +08:00
note . addAttribute ( incAttr . type , incAttr . name , incAttr . value , incAttr . isInheritable , position ) ;
2020-06-05 23:25:14 +08:00
}
// all the remaining existing attributes are not defined anymore and should be deleted
for ( const toDeleteAttr of existingAttrs ) {
toDeleteAttr . isDeleted = true ;
2020-06-20 18:31:38 +08:00
toDeleteAttr . save ( ) ;
2020-06-05 23:25:14 +08:00
}
}
2020-06-20 18:31:38 +08:00
function getAttributeNames ( req ) {
2018-08-03 17:11:57 +08:00
const type = req . query . type ;
const query = req . query . query ;
2018-08-03 04:48:21 +08:00
2018-08-03 17:11:57 +08:00
return attributeService . getAttributeNames ( type , query ) ;
2018-08-03 04:48:21 +08:00
}
2020-06-20 18:31:38 +08:00
function getValuesForAttribute ( req ) {
2018-08-03 04:48:21 +08:00
const attributeName = req . params . attributeName ;
2020-06-20 18:31:38 +08:00
return sql . getColumn ( "SELECT DISTINCT value FROM attributes WHERE isDeleted = 0 AND name = ? AND type = 'label' AND value != '' ORDER BY value" , [ attributeName ] ) ;
2018-08-03 04:48:21 +08:00
}
2020-06-20 18:31:38 +08:00
function createRelation ( req ) {
2018-10-30 05:38:51 +08:00
const sourceNoteId = req . params . noteId ;
const targetNoteId = req . params . targetNoteId ;
const name = req . params . name ;
2020-06-20 18:31:38 +08:00
let attribute = repository . getEntity ( ` SELECT * FROM attributes WHERE isDeleted = 0 AND noteId = ? AND type = 'relation' AND name = ? AND value = ? ` , [ sourceNoteId , name , targetNoteId ] ) ;
2018-10-30 05:38:51 +08:00
if ( ! attribute ) {
attribute = new Attribute ( ) ;
attribute . noteId = sourceNoteId ;
attribute . name = name ;
attribute . type = 'relation' ;
attribute . value = targetNoteId ;
2020-06-20 18:31:38 +08:00
attribute . save ( ) ;
2018-10-30 05:38:51 +08:00
}
2018-10-30 17:36:19 +08:00
return attribute ;
}
2020-06-20 18:31:38 +08:00
function deleteRelation ( req ) {
2018-10-30 17:36:19 +08:00
const sourceNoteId = req . params . noteId ;
const targetNoteId = req . params . targetNoteId ;
const name = req . params . name ;
2020-06-20 18:31:38 +08:00
let attribute = repository . getEntity ( ` SELECT * FROM attributes WHERE isDeleted = 0 AND noteId = ? AND type = 'relation' AND name = ? AND value = ? ` , [ sourceNoteId , name , targetNoteId ] ) ;
2018-10-30 17:36:19 +08:00
2018-10-30 18:28:48 +08:00
if ( attribute ) {
attribute . isDeleted = true ;
2020-06-20 18:31:38 +08:00
attribute . save ( ) ;
2018-10-30 17:36:19 +08:00
}
2018-10-30 05:38:51 +08:00
}
2018-08-03 04:48:21 +08:00
module . exports = {
updateNoteAttributes ,
2018-08-06 20:43:42 +08:00
updateNoteAttribute ,
2018-08-06 23:24:35 +08:00
deleteNoteAttribute ,
2018-08-03 17:11:57 +08:00
getAttributeNames ,
2018-08-06 14:59:26 +08:00
getValuesForAttribute ,
2018-10-30 05:38:51 +08:00
getEffectiveNoteAttributes ,
2018-10-30 17:36:19 +08:00
createRelation ,
2020-06-19 04:28:18 +08:00
deleteRelation
2020-05-12 05:57:39 +08:00
} ;