2017-09-18 16:25:51 +00:00
|
|
|
// @flow
|
2016-11-10 05:56:23 +00:00
|
|
|
import * as db from '../common/database';
|
2018-06-25 17:42:50 +00:00
|
|
|
import type { BaseModel } from './index';
|
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;
|
2019-04-18 00:50:03 +00:00
|
|
|
export const canSync = true;
|
2016-11-22 19:42:10 +00:00
|
|
|
|
2017-09-18 16:25:51 +00:00
|
|
|
type BaseRequestGroup = {
|
|
|
|
name: string,
|
|
|
|
description: string,
|
|
|
|
environment: Object,
|
2019-05-29 20:10:48 +00:00
|
|
|
environmentPropertyOrder: Object | null,
|
2018-12-12 17:36:11 +00:00
|
|
|
metaSortKey: number,
|
2017-09-18 16:25:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export type RequestGroup = BaseModel & BaseRequestGroup;
|
|
|
|
|
2020-04-26 20:33:39 +00:00
|
|
|
export function init(): BaseRequestGroup {
|
2016-11-10 01:15:27 +00:00
|
|
|
return {
|
2016-10-02 20:57:00 +00:00
|
|
|
name: 'New Folder',
|
2017-06-01 13:54:46 +00:00
|
|
|
description: '',
|
2016-10-02 20:57:00 +00:00
|
|
|
environment: {},
|
2019-05-29 20:10:48 +00:00
|
|
|
environmentPropertyOrder: null,
|
2018-12-12 17:36:11 +00:00
|
|
|
metaSortKey: -1 * Date.now(),
|
2017-03-03 20:09:08 +00:00
|
|
|
};
|
2016-10-02 20:57:00 +00:00
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
export function migrate(doc: RequestGroup) {
|
2016-11-22 19:42:10 +00:00
|
|
|
return doc;
|
|
|
|
}
|
|
|
|
|
2020-03-18 18:58:57 +00:00
|
|
|
export function create(patch: $Shape<RequestGroup> = {}): Promise<RequestGroup> {
|
2016-09-21 20:32:45 +00:00
|
|
|
if (!patch.parentId) {
|
2018-10-17 16:42:33 +00:00
|
|
|
throw new Error('New RequestGroup missing `parentId`: ' + JSON.stringify(patch));
|
2016-09-21 20:32:45 +00:00
|
|
|
}
|
|
|
|
|
2016-10-02 20:57:00 +00:00
|
|
|
return db.docCreate(type, patch);
|
|
|
|
}
|
2016-09-21 20:32:45 +00:00
|
|
|
|
2020-03-18 18:58:57 +00:00
|
|
|
export function update(
|
|
|
|
requestGroup: RequestGroup,
|
|
|
|
patch: $Shape<RequestGroup> = {},
|
|
|
|
): Promise<RequestGroup> {
|
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
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
export function getById(id: string): Promise<RequestGroup | null> {
|
2016-10-02 20:57:00 +00:00
|
|
|
return db.get(type, id);
|
|
|
|
}
|
2016-09-21 20:32:45 +00:00
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
export function findByParentId(parentId: string): Promise<Array<RequestGroup>> {
|
|
|
|
return db.find(type, { parentId });
|
2016-10-02 20:57:00 +00:00
|
|
|
}
|
2016-09-21 20:32:45 +00:00
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
export function remove(requestGroup: RequestGroup): Promise<void> {
|
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
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
export function all(): Promise<Array<RequestGroup>> {
|
2016-10-02 20:57:00 +00:00
|
|
|
return db.all(type);
|
|
|
|
}
|
2016-09-21 20:32:45 +00:00
|
|
|
|
2019-12-11 17:48:56 +00:00
|
|
|
export async function duplicate(
|
|
|
|
requestGroup: RequestGroup,
|
2020-03-18 18:58:57 +00:00
|
|
|
patch: $Shape<RequestGroup> = {},
|
2019-12-11 17:48:56 +00:00
|
|
|
): Promise<RequestGroup> {
|
|
|
|
if (!patch.name) {
|
|
|
|
patch.name = `${requestGroup.name} (Copy)`;
|
|
|
|
}
|
2017-02-27 22:54:56 +00:00
|
|
|
|
|
|
|
// Get sort key of next request
|
2018-06-25 17:42:50 +00:00
|
|
|
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;
|
|
|
|
|
2019-12-11 17:48:56 +00:00
|
|
|
return db.duplicate(requestGroup, { metaSortKey, ...patch });
|
2016-10-02 20:57:00 +00:00
|
|
|
}
|