2021-07-22 23:04:56 +00:00
|
|
|
import { Merge } from 'type-fest';
|
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
import { database as db } from '../common/database';
|
2021-03-01 21:32:32 +00:00
|
|
|
import { strings } from '../common/strings';
|
2021-07-22 23:04:56 +00:00
|
|
|
import type { BaseModel } from './index';
|
|
|
|
import * as models from './index';
|
2021-07-26 00:49:34 +00:00
|
|
|
import { BASE_SPACE_ID, isSpaceId } from './space';
|
2016-10-02 20:57:00 +00:00
|
|
|
|
2016-11-22 19:42:10 +00:00
|
|
|
export const name = 'Workspace';
|
2016-10-02 20:57:00 +00:00
|
|
|
export const type = 'Workspace';
|
|
|
|
export const prefix = 'wrk';
|
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
|
|
|
|
2021-06-14 15:18:35 +00:00
|
|
|
interface GenericWorkspace<Scope extends 'design' | 'collection'> {
|
2021-05-12 06:35:00 +00:00
|
|
|
name: string;
|
|
|
|
description: string;
|
|
|
|
certificates?: any;
|
2021-06-14 15:18:35 +00:00
|
|
|
scope: Scope;
|
2021-05-12 06:35:00 +00:00
|
|
|
}
|
2017-07-19 01:55:47 +00:00
|
|
|
|
2021-06-14 15:18:35 +00:00
|
|
|
export type DesignWorkspace = GenericWorkspace<'design'>;
|
|
|
|
export type CollectionWorkspace = GenericWorkspace<'collection'>;
|
|
|
|
export type BaseWorkspace = DesignWorkspace | CollectionWorkspace;
|
|
|
|
|
|
|
|
export type WorkspaceScope = BaseWorkspace['scope'];
|
|
|
|
|
|
|
|
export const WorkspaceScopeKeys = {
|
|
|
|
design: 'design',
|
|
|
|
collection: 'collection',
|
|
|
|
} as const;
|
|
|
|
|
2017-09-25 21:32:58 +00:00
|
|
|
export type Workspace = BaseModel & BaseWorkspace;
|
2017-07-19 01:55:47 +00:00
|
|
|
|
2021-06-16 19:19:00 +00:00
|
|
|
export const isWorkspace = (model: Pick<BaseModel, 'type'>): model is Workspace => (
|
|
|
|
model.type === type
|
|
|
|
);
|
|
|
|
|
|
|
|
export const isDesign = (workspace: Pick<Workspace, 'scope'>): workspace is DesignWorkspace => (
|
|
|
|
workspace.scope === WorkspaceScopeKeys.design
|
|
|
|
);
|
|
|
|
|
|
|
|
export const isCollection = (workspace: Pick<Workspace, 'scope'>): workspace is CollectionWorkspace => (
|
|
|
|
workspace.scope === WorkspaceScopeKeys.collection
|
|
|
|
);
|
|
|
|
|
2021-06-14 15:18:35 +00:00
|
|
|
export const init = (): BaseWorkspace => ({
|
|
|
|
name: `New ${strings.collection.singular}`,
|
|
|
|
description: '',
|
|
|
|
scope: WorkspaceScopeKeys.collection,
|
|
|
|
});
|
2016-09-21 20:32:45 +00:00
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
export async function migrate(doc: Workspace) {
|
2020-04-07 22:00:04 +00:00
|
|
|
doc = await _migrateExtractClientCertificates(doc);
|
|
|
|
doc = await _migrateEnsureName(doc);
|
2021-05-12 06:35:00 +00:00
|
|
|
await models.apiSpec.getOrCreateForParentId(doc._id, {
|
|
|
|
fileName: doc.name,
|
|
|
|
});
|
2021-02-02 03:02:17 +00:00
|
|
|
doc = _migrateScope(doc);
|
2021-07-26 00:49:34 +00:00
|
|
|
doc = _migrateIntoBaseSpace(doc);
|
2020-04-07 22:00:04 +00:00
|
|
|
return doc;
|
2016-11-22 19:42:10 +00:00
|
|
|
}
|
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
export function getById(id?: string) {
|
|
|
|
return db.get<Workspace>(type, id);
|
2016-10-02 20:57:00 +00:00
|
|
|
}
|
2016-09-21 20:32:45 +00:00
|
|
|
|
2021-07-26 00:49:34 +00:00
|
|
|
export function findByParentId(parentId: string) {
|
|
|
|
return db.find<Workspace>(type, { parentId });
|
|
|
|
}
|
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
export async function create(patch: Partial<Workspace> = {}) {
|
2021-05-27 03:59:54 +00:00
|
|
|
expectParentToBeSpace(patch.parentId);
|
2021-05-12 06:35:00 +00:00
|
|
|
return db.docCreate<Workspace>(type, patch);
|
2016-10-02 20:57:00 +00:00
|
|
|
}
|
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
export async function all() {
|
2021-07-06 20:30:50 +00:00
|
|
|
return await db.all<Workspace>(type);
|
2016-10-02 20:57:00 +00:00
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
export function count() {
|
2017-03-03 20:09:08 +00:00
|
|
|
return db.count(type);
|
2016-10-02 20:57:00 +00:00
|
|
|
}
|
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
export function update(workspace: Workspace, patch: Partial<Workspace>) {
|
2021-05-27 03:59:54 +00:00
|
|
|
expectParentToBeSpace(patch.parentId);
|
2016-09-21 20:32:45 +00:00
|
|
|
return db.docUpdate(workspace, patch);
|
2016-10-02 20:57:00 +00:00
|
|
|
}
|
2016-09-21 20:32:45 +00:00
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
export function remove(workspace: Workspace) {
|
2016-09-21 20:32:45 +00:00
|
|
|
return db.remove(workspace);
|
2016-10-02 20:57:00 +00:00
|
|
|
}
|
2017-09-15 11:04:44 +00:00
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
async function _migrateExtractClientCertificates(workspace: Workspace) {
|
|
|
|
const certificates = workspace.certificates || null;
|
|
|
|
|
2017-11-06 20:44:55 +00:00
|
|
|
if (!Array.isArray(certificates)) {
|
2017-10-31 18:05:35 +00:00
|
|
|
// Already migrated
|
|
|
|
return workspace;
|
|
|
|
}
|
|
|
|
|
2017-11-06 20:44:55 +00:00
|
|
|
for (const cert of certificates) {
|
2017-10-31 18:05:35 +00:00
|
|
|
await models.clientCertificate.create({
|
|
|
|
parentId: workspace._id,
|
2017-11-22 14:00:12 +00:00
|
|
|
host: cert.host || '',
|
2017-10-31 18:05:35 +00:00
|
|
|
passphrase: cert.passphrase || null,
|
|
|
|
cert: cert.cert || null,
|
|
|
|
key: cert.key || null,
|
|
|
|
pfx: cert.pfx || null,
|
2018-12-12 17:36:11 +00:00
|
|
|
isPrivate: false,
|
2017-10-31 18:05:35 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
delete workspace.certificates;
|
2017-10-31 18:05:35 +00:00
|
|
|
// This will remove the now-missing `certificates` property
|
2017-11-26 13:42:43 +00:00
|
|
|
// NOTE: Using db.update so we don't change things like modified time
|
|
|
|
await db.update(workspace);
|
2017-10-31 18:05:35 +00:00
|
|
|
return workspace;
|
|
|
|
}
|
2020-04-07 22:00:04 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Ensure workspace has a valid String name. Due to real-world bug reports, we know
|
|
|
|
* this happens (and it causes problems) so this migration will ensure that it is
|
|
|
|
* corrected.
|
|
|
|
*/
|
2021-05-12 06:35:00 +00:00
|
|
|
async function _migrateEnsureName(workspace: Workspace) {
|
2020-04-07 22:00:04 +00:00
|
|
|
if (typeof workspace.name !== 'string') {
|
|
|
|
workspace.name = 'My Workspace';
|
|
|
|
}
|
|
|
|
|
|
|
|
return workspace;
|
|
|
|
}
|
2021-02-02 03:02:17 +00:00
|
|
|
|
2021-06-14 15:18:35 +00:00
|
|
|
// Translate the old value
|
|
|
|
type OldScopeTypes = 'spec' | 'debug' | 'designer' | null;
|
|
|
|
type MigrationWorkspace = Merge<Workspace, { scope: OldScopeTypes | Workspace['scope'] }>;
|
|
|
|
|
2021-02-02 03:02:17 +00:00
|
|
|
/**
|
|
|
|
* Ensure workspace scope is set to a valid entry
|
|
|
|
*/
|
2021-06-14 15:18:35 +00:00
|
|
|
function _migrateScope(workspace: MigrationWorkspace) {
|
|
|
|
switch (workspace.scope) {
|
|
|
|
case WorkspaceScopeKeys.collection:
|
|
|
|
case WorkspaceScopeKeys.design:
|
2021-03-16 23:28:12 +00:00
|
|
|
break;
|
2021-05-12 06:35:00 +00:00
|
|
|
|
2021-06-14 15:18:35 +00:00
|
|
|
case 'designer':
|
|
|
|
case 'spec':
|
2021-03-16 23:28:12 +00:00
|
|
|
workspace.scope = WorkspaceScopeKeys.design;
|
2021-02-02 03:02:17 +00:00
|
|
|
break;
|
2021-05-12 06:35:00 +00:00
|
|
|
|
2021-02-02 03:02:17 +00:00
|
|
|
case 'debug':
|
|
|
|
case null:
|
|
|
|
default:
|
2021-03-02 21:16:48 +00:00
|
|
|
workspace.scope = WorkspaceScopeKeys.collection;
|
2021-02-02 03:02:17 +00:00
|
|
|
break;
|
|
|
|
}
|
2021-06-14 15:18:35 +00:00
|
|
|
return workspace as Workspace;
|
2021-02-02 03:02:17 +00:00
|
|
|
}
|
2021-05-27 03:59:54 +00:00
|
|
|
|
2021-07-26 00:49:34 +00:00
|
|
|
function _migrateIntoBaseSpace(workspace: Workspace) {
|
|
|
|
if (!workspace.parentId) {
|
|
|
|
workspace.parentId = BASE_SPACE_ID;
|
|
|
|
}
|
|
|
|
|
|
|
|
return workspace;
|
|
|
|
}
|
|
|
|
|
2021-06-22 20:45:30 +00:00
|
|
|
export async function ensureChildren({ _id }: Workspace) {
|
|
|
|
await models.environment.getOrCreateForParentId(_id);
|
|
|
|
await models.cookieJar.getOrCreateForParentId(_id);
|
|
|
|
await models.workspaceMeta.getOrCreateByParentId(_id);
|
|
|
|
}
|
|
|
|
|
2021-05-27 03:59:54 +00:00
|
|
|
function expectParentToBeSpace(parentId?: string | null) {
|
|
|
|
if (parentId && !isSpaceId(parentId)) {
|
|
|
|
throw new Error('Expected the parent of a Workspace to be a Space');
|
|
|
|
}
|
|
|
|
}
|