rudeshark.net/src/api/serializers/drive-folder.ts

53 lines
1.0 KiB
TypeScript
Raw Normal View History

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