properties: mapToObj()

in source/backend/functions/api/src/index.js [605:747]


            properties: mapToObj(R.omit(['id', 'label'], v)),
            parent: v.id === id
        }));

        return wrapResponse(200, linked);
    }

    return wrapResponse(200, []);
}

/**
 * The properties from neptune sometimes come back as an array of one.
 * If they do then strip out the array.
 * @param {*} inputMap
 */
const mapToObj = R.map(v => Array.isArray(v) ? v[0] : v);

/**
 * Load a node from it's id.
 *
 * @param {*} id
 * @param {*} dc
 */
const getNodeFromId = async (id, dc) => {
    const graph = new Graph();
    const g = graph.traversal().withRemote(dc);
    const data = await g.V(id).valueMap(true).next();

    console.log("data = ");
    console.log(util.inspect(data, { depth: 10 }));

    return data.value ?
        {
            id: data.value.id,
            perspectiveBirthDate: data.value.perspectiveBirthDate[0],
            label: data.value.label.replace(/_/g, "::"),
            properties: mapToObj(R.omit(['id', 'label'], data.value)),
        } : undefined
};

const wrapGetNodeFromId = async (event, dc) => {
    return await getNodeFromId(event.data.id, dc);
}

const deleteAllNodes = async (event, dc) => {
    const graph = new Graph();
    const g = graph.traversal().withRemote(dc);
    const results = await g.V().drop().next();
    return wrapResponse(200, { success: true, results: results });
};

const deleteNodes = async (event, dc) => {
    let deleted = [];

    for (let node of event.data.nodes) {
        let response = await deleteNode(node, dc);
        deleted.push(response);
    }

    return wrapResponse(200, { success: true, results: deleted });
};

const softDeleteNodes = async (event, dc) => {
    let deleted = [];

    /*
         Most perspective deletes are soft deletes, but there are two types
         of softDelete (stored in the node as softDeleteType).
         The first type of softDelete are when event.data.softDeleteType = delete.
         This is set when the node has been removed from config / the api. For example
         when an ec2 instance is terminated it will be removed from config.  The second
         type of delete happens when there is a change to a node, i.e. its ip address
         changes.  Here a new node is created, and the old one soft deleted, but the
         softDeleteType parameter is set to update.
    */

    for (let node of event.data.nodes) {
        let response = await softDeleteNode(node, event.data.softDeleteType, dc);
        console.log("Soft deleted:");
        console.log(util.inspect(node, { depth: 10 }));
        deleted.push(response);
    }

    return wrapResponse(200, { success: true, results: deleted });
};

const deleteNode = async (nodeId, dc) => {
    const graph = new Graph();
    const g = graph.traversal().withRemote(dc);
    const results = await g.V(nodeId).drop().next();
    return { success: true, results: results }
};

const softDeleteNode = async (nodeId, softDeleteType = "unknown", dc) => {
    console.log("softDeleteType=", softDeleteType);
    const graph = new Graph();
    const g = graph.traversal().withRemote(dc);
    const results = await addProperties(g.V(nodeId), {
        softDelete: true,
        softDeleteDate: new Date(),
        softDeleteType: softDeleteType }).next();
    return { success: true, results: results }
};

/**
 * Filters the nodes according to account and region.
 *
 * TODO:  This should be done in neptune!
 *
 * THIS IS WHAT the filter looks like
 *
 * {
  "command": "viewAllNodesAndLinks",
  "data": {
      "accountFilter": {
        "XXXXXXXXXXX1": [
            "eu-west-1",
            "us-east-2"
        ],
        "XXXXXXXXXXX2": [
            "us-east-2"
        ]
    }
  }
}
 *
 * @param {*} nodes
 * @param {*} filterParameters
 */
const filterAllNodes = (nodes, filterParameters) => {
    if (filterParameters === undefined) {
        return nodes;
    }

    // If there are no parmeters then return the nodes unfiltered
    if (Object.keys(filterParameters).length === 0) {
        return nodes;
    }

    let fnodes = nodes.filter(node => {
        let allow = false;

        for (let accountId of Object.keys(filterParameters)) {