2016-11-10 05:56:23 +00:00
|
|
|
import * as db from '../common/database';
|
2016-10-02 20:57:00 +00:00
|
|
|
|
2016-11-22 19:42:10 +00:00
|
|
|
export const name = 'Folder';
|
2016-10-02 20:57:00 +00:00
|
|
|
export const type = 'RequestGroup';
|
|
|
|
export const prefix = 'fld';
|
2017-03-23 22:10:42 +00:00
|
|
|
export const canDuplicate = true;
|
2016-11-22 19:42:10 +00:00
|
|
|
|
2016-10-02 20:57:00 +00:00
|
|
|
export function init () {
|
2016-11-10 01:15:27 +00:00
|
|
|
return {
|
2016-10-02 20:57:00 +00:00
|
|
|
name: 'New Folder',
|
|
|
|
environment: {},
|
|
|
|
metaSortKey: -1 * Date.now()
|
2017-03-03 20:09:08 +00:00
|
|
|
};
|
2016-10-02 20:57:00 +00:00
|
|
|
}
|
|
|
|
|
2016-11-22 19:42:10 +00:00
|
|
|
export function migrate (doc) {
|
|
|
|
return doc;
|
|
|
|
}
|
|
|
|
|
2016-10-02 20:57:00 +00:00
|
|
|
export function create (patch = {}) {
|
2016-09-21 20:32:45 +00:00
|
|
|
if (!patch.parentId) {
|
|
|
|
throw new Error('New Requests missing `parentId`', patch);
|
|
|
|
}
|
|
|
|
|
2016-10-02 20:57:00 +00:00
|
|
|
return db.docCreate(type, patch);
|
|
|
|
}
|
2016-09-21 20:32:45 +00:00
|
|
|
|
2016-10-02 20:57:00 +00:00
|
|
|
export function update (requestGroup, patch) {
|
2016-09-21 20:32:45 +00:00
|
|
|
return db.docUpdate(requestGroup, patch);
|
2016-10-02 20:57:00 +00:00
|
|
|
}
|
2016-09-21 20:32:45 +00:00
|
|
|
|
2016-10-02 20:57:00 +00:00
|
|
|
export function getById (id) {
|
|
|
|
return db.get(type, id);
|
|
|
|
}
|
2016-09-21 20:32:45 +00:00
|
|
|
|
2016-10-02 20:57:00 +00:00
|
|
|
export function findByParentId (parentId) {
|
2017-03-03 20:09:08 +00:00
|
|
|
return db.find(type, {parentId});
|
2016-10-02 20:57:00 +00:00
|
|
|
}
|
2016-09-21 20:32:45 +00:00
|
|
|
|
2016-10-02 20:57:00 +00:00
|
|
|
export function remove (requestGroup) {
|
2016-09-21 20:32:45 +00:00
|
|
|
return db.remove(requestGroup);
|
2016-10-02 20:57:00 +00:00
|
|
|
}
|
2016-09-21 20:32:45 +00:00
|
|
|
|
2016-10-02 20:57:00 +00:00
|
|
|
export function all () {
|
|
|
|
return db.all(type);
|
|
|
|
}
|
2016-09-21 20:32:45 +00:00
|
|
|
|
2017-02-27 22:54:56 +00:00
|
|
|
export async function duplicate (requestGroup) {
|
2016-09-21 20:32:45 +00:00
|
|
|
const name = `${requestGroup.name} (Copy)`;
|
2017-02-27 22:54:56 +00:00
|
|
|
|
|
|
|
// Get sort key of next request
|
|
|
|
const q = {metaSortKey: {$gt: requestGroup.metaSortKey}};
|
|
|
|
const [nextRequestGroup] = await db.find(type, q, {metaSortKey: 1});
|
2017-03-03 20:09:08 +00:00
|
|
|
const nextSortKey = nextRequestGroup
|
|
|
|
? nextRequestGroup.metaSortKey
|
|
|
|
: requestGroup.metaSortKey + 100;
|
2017-02-27 22:54:56 +00:00
|
|
|
|
|
|
|
// Calculate new sort key
|
|
|
|
const sortKeyIncrement = (nextSortKey - requestGroup.metaSortKey) / 2;
|
|
|
|
const metaSortKey = requestGroup.metaSortKey + sortKeyIncrement;
|
|
|
|
|
|
|
|
return db.duplicate(requestGroup, {name, metaSortKey});
|
2016-10-02 20:57:00 +00:00
|
|
|
}
|