22/**
33 * Tenant management module
44 */
5-
6- var util = require ( 'util' ) ,
7- database = require ( './database' ) ,
8- logger = require ( './logger' ) ,
9- fs = require ( 'fs' ) ,
10- path = require ( 'path' ) ,
11- ncp = require ( 'ncp' ) ,
12- mkdirp = require ( 'mkdirp' ) ,
13- frameworkhelper = require ( './frameworkhelper' ) ,
14- configuration = require ( './configuration' ) ;
5+ var async = require ( 'async' ) ;
6+ var fs = require ( 'fs-extra' ) ;
7+ var mkdirp = require ( 'mkdirp' ) ;
8+ var ncp = require ( 'ncp' ) ;
9+ var path = require ( 'path' ) ;
10+ var util = require ( 'util' ) ;
11+
12+ var configuration = require ( './configuration' ) ;
13+ var database = require ( './database' ) ;
14+ var frameworkhelper = require ( './frameworkhelper' ) ;
15+ var logger = require ( './logger' ) ;
1516
1617// Constants
1718var MODNAME = 'tenantmanager' ;
@@ -442,12 +443,12 @@ exports = module.exports = {
442443 }
443444
444445 // only execute if we have a single matching record
445- self . retrieveTenant ( search , function ( error , result ) {
446+ self . retrieveTenant ( search , function ( error , tenantRec ) {
446447 if ( error ) {
447448 return callback ( error ) ;
448449 }
449450
450- if ( result ) {
451+ if ( tenantRec ) {
451452 if ( ! update . updatedAt ) {
452453 update . updatedAt = new Date ( ) ;
453454 }
@@ -458,12 +459,12 @@ exports = module.exports = {
458459 }
459460
460461 // Re-fetch the tenant
461- self . retrieveTenant ( search , function ( error , result ) {
462+ self . retrieveTenant ( search , function ( error , tenantRec ) {
462463 if ( error ) {
463464 return callback ( error ) ;
464465 }
465466
466- return callback ( null , result ) ;
467+ return callback ( null , tenantRec ) ;
467468 } ) ;
468469
469470 } ) ;
@@ -489,18 +490,15 @@ exports = module.exports = {
489490 logger . log ( 'error' , err ) ;
490491 return callback ( err ) ;
491492 }
492-
493- // confirm the tenant exists and is there is only one of them
494- self . retrieveTenant ( { _id : tenantId } , function ( error , results ) {
493+ // confirm the tenant exists
494+ self . retrieveTenant ( { _id : tenantId } , function ( error , tenantRec ) {
495495 if ( error ) {
496496 return callback ( error ) ;
497497 }
498-
499- if ( results && results . length === 1 ) {
500- return self . updateTenant ( { '_id' : results [ 0 ] . _id } , { _isDeleted : true } , callback ) ;
498+ if ( ! result ) {
499+ return callback ( new Error ( 'No matching tenant record found' ) ) ;
501500 }
502-
503- return callback ( new Error ( 'No matching tenant record found' ) ) ;
501+ return self . updateTenant ( { '_id' : tenantRec . _id } , { _isDeleted : true } , callback ) ;
504502 } ) ;
505503 } , configuration . getConfig ( 'dbName' ) ) ;
506504 } ,
@@ -515,27 +513,40 @@ exports = module.exports = {
515513
516514 deleteTenant : function ( tenant , callback ) {
517515 var self = this ;
518- database . getDatabase ( function ( err , db ) {
516+ database . getDatabase ( function ( err , db ) {
519517 if ( err ) {
520518 logger . log ( 'error' , err ) ;
521519 return callback ( err ) ;
522520 }
523-
524- // confirm the tenant exists and is there is only one of them
525- self . retrieveTenant ( tenant , function ( error , result ) {
521+ // confirm the tenant exists
522+ self . retrieveTenant ( tenant , function ( error , tenantRec ) {
526523 if ( error ) {
527524 return callback ( error ) ;
528525 }
529-
530- if ( result ) {
531- return db . destroy ( 'tenant' , tenant , callback ) ;
526+ if ( ! tenantRec ) {
527+ return callback ( new Error ( 'No matching tenant record found' ) ) ;
532528 }
533-
534- return callback ( new Error ( 'No matching tenant record found' ) ) ;
529+ async . parallel ( [
530+ function cleanDB ( done ) {
531+ db . destroy ( 'tenant' , tenant , done ) ;
532+ } ,
533+ function removeTemp ( done ) {
534+ self . deleteTenantFilesystem ( tenantRec , done ) ;
535+ }
536+ ] , callback ) ;
535537 } ) ;
536538 } , configuration . getConfig ( 'dbName' ) ) ;
537539 } ,
538540
541+ /**
542+ * Removes the tenant's working folders
543+ * @param {object } tenant - a fully defined tenant object
544+ * @param {function } callback - function of the form function (error)
545+ */
546+ deleteTenantFilesystem : function ( tenant , callback ) {
547+ fs . remove ( path . join ( configuration . tempDir , tenant . _id . toString ( ) ) , callback ) ;
548+ } ,
549+
539550 /**
540551 * returns the database object for a named tenant
541552 *
@@ -544,7 +555,7 @@ exports = module.exports = {
544555 */
545556 getDatabaseConfig : function ( tenantId , next ) {
546557 // just fetch up the tenant from the master db
547- this . retrieveTenant ( { _id : tenantId } , function ( err , tenant ) {
558+ this . retrieveTenant ( { _id : tenantId } , function ( err , tenantRec ) {
548559 if ( err ) {
549560 return next ( err ) ;
550561 }
@@ -554,7 +565,7 @@ exports = module.exports = {
554565 }
555566
556567 // if database is not defined, the caller is expected to deal with item
557- return next ( null , tenant . database ) ;
568+ return next ( null , tenantRec . database ) ;
558569 } ) ;
559570 }
560571} ;
0 commit comments