monkeeShark/src/api/serializers/drive-folder.ts

51 lines
1 KiB
TypeScript
Raw Normal View History

2016-12-28 22:49:51 +00:00
/**
* Module dependencies
*/
import * as mongo from 'mongodb';
import DriveFolder from '../models/drive-folder';
2017-01-02 21:03:19 +00:00
import deepcopy = require('deepcopy');
2016-12-28 22:49:51 +00:00
/**
* Serialize a drive folder
*
2017-03-01 08:37:01 +00:00
* @param {any} folder
* @param {any} options?
* @return {Promise<any>}
2016-12-28 22:49:51 +00:00
*/
const self = (
folder: any,
options?: {
2017-02-16 07:55:01 +00:00
detail: boolean
2016-12-28 22:49:51 +00:00
}
2017-03-01 08:37:01 +00:00
) => new Promise<any>(async (resolve, reject) => {
2017-02-16 07:55:01 +00:00
const opts = Object.assign({
detail: false
}, options);
2016-12-28 22:49:51 +00:00
let _folder: any;
// Populate the folder if 'folder' is ID
if (mongo.ObjectID.prototype.isPrototypeOf(folder)) {
2017-03-01 08:37:01 +00:00
_folder = await DriveFolder.findOne({ _id: folder });
2016-12-28 22:49:51 +00:00
} else if (typeof folder === 'string') {
2017-03-01 08:37:01 +00:00
_folder = await DriveFolder.findOne({ _id: new mongo.ObjectID(folder) });
2016-12-28 22:49:51 +00:00
} else {
_folder = deepcopy(folder);
}
// Rename _id to id
_folder.id = _folder._id;
delete _folder._id;
2017-02-16 07:55:01 +00:00
if (opts.detail && _folder.parent_id) {
2016-12-28 22:49:51 +00:00
// Populate parent folder
_folder.parent = await self(_folder.parent_id, {
2017-02-16 07:55:01 +00:00
detail: true
2016-12-28 22:49:51 +00:00
});
}
resolve(_folder);
});
export default self;