Skip to content

Commit

Permalink
Add node exports
Browse files Browse the repository at this point in the history
  • Loading branch information
phalestrivir committed Nov 13, 2024
1 parent 31cbb4e commit 56feb67
Show file tree
Hide file tree
Showing 4 changed files with 3,336 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/ops/NodeOps.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,19 @@ describe('NodeOps', () => {
process.env.FRODO_RECORD_PHASE === '1')
) {

describe('createNodeExportTemplate()', () => {
test('0: Method is implemented', async () => {
expect(NodeOps.createNodeExportTemplate).toBeDefined();
});

test('1: Create Node Export Template', async () => {
const response = NodeOps.createNodeExportTemplate({ state });
expect(response).toMatchSnapshot({
meta: expect.any(Object),
});
});
});

describe('readNodeTypes()', () => {
test('0: Method is implemented', async () => {
expect(NodeOps.readNodeTypes).toBeDefined();
Expand Down Expand Up @@ -127,6 +140,19 @@ describe('NodeOps', () => {
//TODO: Create tests
});

describe('exportNodes()', () => {
test('0: Method is implemented', async () => {
expect(NodeOps.exportNodes).toBeDefined();
});

test('1: Export nodes', async () => {
const response = await NodeOps.exportNodes({ state });
expect(response).toMatchSnapshot({
meta: expect.any(Object),
});
});
});

describe('createNode()', () => {
test('0: Method is implemented', async () => {
expect(NodeOps.createNode).toBeDefined();
Expand Down
76 changes: 76 additions & 0 deletions src/ops/NodeOps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@ import Constants from '../shared/Constants';
import { State } from '../shared/State';
import {
createProgressIndicator,
debugMessage,
printError,
stopProgressIndicator,
updateProgressIndicator,
} from '../utils/Console';
import { getMetadata } from '../utils/ExportImportUtils';
import { FrodoError } from './FrodoError';
import { ExportMetaData } from './OpsTypes';

export type Node = {
/**
Expand All @@ -44,6 +47,11 @@ export type Node = {
* @returns {Promise<NodeSkeleton>} a promise that resolves to a node object
*/
readNode(nodeId: string, nodeType: string): Promise<NodeSkeleton>;
/**
* Export all nodes
* @returns {Promise<NodeExportInterface>} a promise that resolves to an array of node objects
*/
exportNodes(): Promise<NodeExportInterface>;
/**
* Create node by type
* @param {string} nodeType node type
Expand Down Expand Up @@ -136,6 +144,9 @@ export default (state: State): Node => {
async readNode(nodeId: string, nodeType: string): Promise<NodeSkeleton> {
return readNode({ nodeId, nodeType, state });
},
async exportNodes(): Promise<NodeExportInterface> {
return exportNodes({ state });
},
async createNode(
nodeType: string,
nodeData: NodeSkeleton
Expand Down Expand Up @@ -181,6 +192,26 @@ export default (state: State): Node => {
};
};

export interface NodeExportInterface {
meta?: ExportMetaData;
node: Record<string, NodeSkeleton>;
}

/**
* Create an empty node export template
* @returns {NodeExportInterface} an empty node export template
*/
export function createNodeExportTemplate({
state,
}: {
state: State;
}): NodeExportInterface {
return {
meta: getMetadata({ state }),
node: {},
};
}

export type NodeClassificationType =
| 'standard'
| 'custom'
Expand Down Expand Up @@ -276,6 +307,51 @@ export async function readNode({
}
}

/**
* Export all nodes
* @returns {Promise<NodeExportInterface>} a promise that resolves to an array of node objects
*/
export async function exportNodes({
state,
}: {
state: State;
}): Promise<NodeExportInterface> {
let indicatorId: string;
try {
debugMessage({ message: `NodeOps.exportNodes: start`, state });
const exportData = createNodeExportTemplate({ state });
const nodes = await readNodes({ state });
indicatorId = createProgressIndicator({
total: nodes.length,
message: 'Exporting nodes...',
state,
});
for (const node of nodes) {
updateProgressIndicator({
id: indicatorId,
message: `Exporting node ${node._id}`,
state,
});
exportData.node[node._id] = node;
}
stopProgressIndicator({
id: indicatorId,
message: `Exported ${nodes.length} nodes.`,
state,
});
debugMessage({ message: `NodeOps.exportNodes: end`, state });
return exportData;
} catch (error) {
stopProgressIndicator({
id: indicatorId,
message: `Error exporting nodes.`,
status: 'fail',
state,
});
throw new FrodoError(`Error reading nodes`, error);
}
}

/**
* Create node
* @param {string} nodeId node uuid
Expand Down

Large diffs are not rendered by default.

Loading

0 comments on commit 56feb67

Please sign in to comment.