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

53 lines
1 KiB
TypeScript
Raw Normal View History

2016-12-28 22:49:51 +00:00
'use strict';
/**
* 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
*
* @param {Object} folder
* @param {Object} options?
* @return {Promise<Object>}
*/
const self = (
folder: any,
options?: {
2017-02-16 07:55:01 +00:00
detail: boolean
2016-12-28 22:49:51 +00:00
}
) => new Promise<Object>(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)) {
_folder = await DriveFolder.findOne({_id: folder});
} else if (typeof folder === 'string') {
_folder = await DriveFolder.findOne({_id: new mongo.ObjectID(folder)});
} 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;