@@ -41,56 +41,4 @@ exports.login = function(args, res, next) {
4141 token : token
4242 } ) ) ;
4343 }
44- }
45-
46- exports . storedata = async ( args , res , next ) => {
47- const existingDataId = args . request . value . existingDataId ;
48- const dataType = "aliasdata" ; //args.request.value.dataType;
49- const data = args . request . value . data ;
50- res . setHeader ( 'Content-Type' , 'application/json' ) ;
51-
52- try {
53- const db = await MongoClient . connect ( config . mongodb . database_url ) ;
54- const collection = db . collection ( "aliasdata" ) ; //TODO: change collectionName to variable
55- console . log ( "storing data offchain: " + data ) ;
56-
57- let filter ;
58- const newId = new ObjectID ( ) ;
59- if ( existingDataId ) {
60- try {
61- filter = { _id : ObjectID ( existingDataId ) , dataType : dataType } ;
62- let docs = await collection . find ( filter ) . toArray ( ) ;
63- if ( docs . length > 1 ) {
64- res . writeHead ( 500 ) ;
65- res . end ( JSON . stringify ( { success : false , message : `Data id ${ existingDataId } returned ${ docs . length } matches, too many!` } ) ) ;
66- } else if ( docs . length == 0 ) {
67- console . log ( "No matches found for provided ID, creating new ID." ) ;
68- filter . _id = newId ;
69- }
70- } catch ( e ) { //objectid error, create new id
71- console . log ( "Object ID error create new ID." ) ;
72- filter = { _id : newId , dataType : dataType } ;
73- }
74- } else {
75- filter = { _id : newId , dataType : dataType } ;
76- }
77-
78- try {
79- const result = await mongoUtils . upsertDocument ( collection , filter , { dataType : 'aliasdata' , data : data } ) ;
80- let ret = {
81- storeLocations : [
82- { dataUrl : config . mongodb . offchain_url + result . upsertedId }
83- ]
84- } ;
85- res . end ( JSON . stringify ( ret ) ) ;
86- } catch ( err ) {
87- res . writeHead ( 500 ) ;
88- res . end ( JSON . stringify ( { success : false , message : 'Error inserting data into offchain data storage service. Details: ' + JSON . stringify ( err ) } ) ) ;
89- }
90- } catch ( err ) {
91- res . writeHead ( 500 ) ;
92- res . end ( JSON . stringify ( { success : false , message : 'Error connecting from API to data storage service. Details: ' + JSON . stringify ( err ) } ) ) ;
93- }
94- }
95- ;
96-
44+ } ;
0 commit comments