Source

gom/cnRelation.js

//
// $Id$
//

import { RestServices, RestResponse } from "../tools/RestServices.js"
import { Context } from "./../infohub/Context.js"
import { ApiResponse } from "../tools/ApiResponse.js"

/**
 *  
 *  @category COLNEO gom <br>
 *    <font size="6pt">Generic Object Model</font>
 * 
 *  @classdesc Base Class for nodes/objects on COLNEO infohub. <br>
    Uses the infohub GOM (Generic Object Model)
      
 */
export class cnRelation {

  #_data = null

  constructor() {
  }

  /**
   * 
   * @param {*} obj_as_json 
   * @returns {void}
   */
  setFromJson(obj_as_json) {
    this.#_data = obj_as_json
  }
   
  /**
   * 
   * @returns {*} The relation data as JSON
   */
  getAsJson() {  
    return this.#_data  
  }

}

export class cnRelationServices {

  #_ctx = null 

   /**
   *   
   * @param {Context} ctx Infohub Context
   * 
   * @since 08.2025, jh
   * 
   */
  constructor( ctx ) {      
    this.#_ctx = ctx  
  }

  /**
   * 
   * @param {*} left_nodeid 
   * @param {*} relation_data 
   * @param {*} cb 
   * @returns {Promise<ApiResponse>} Returns the created relation
   */
  async createRelation( left_nodeid , relation_data, cb) {

    // console.log( '### <createRelation()> ' )
    
    // 
    //  relation_data
    //  {
    //   "name": "string",
    //   "description": "string",
    //   "weight": 0,
    //   "right_objects": [
    //     "string"
    //   ]
    // }

    // /{scope}/{node}/relations

    const url = `${this.#_ctx.getServiceUrl(Context.SERVICE.HUB)}/${this.#_ctx.getScope()}/${left_nodeid}/relations`;

    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    /* RestResponse */ const resp = await RestServices.makeApiCall(this.#_ctx.getToken(), RestServices.METHODS.POST, url, relation_data)
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    // resp.dump( "NEW RELATION" )

    if (resp.status < 300) {

      let obj = new cnRelation()
      obj.setFromJson(resp.data)

      return cb(new ApiResponse(resp.status, obj))

    } else {

      return cb(resp)

    }

  }
  
  async saveRelation( cnRel, left_nodeid , cb ) {
    
    // create object/node ...

    let object_data = cnRel.getAsJson()

    const url = `${this.#_ctx.getServiceUrl(Context.SERVICE.HUB)}/${this.#_ctx.getScope()}/${left_nodeid}/relations`;
    
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////    
    /* RestResponse */ const resp = await RestServices.makeApiCall(this.#_ctx.getToken(), RestServices.METHODS.PUT, url, object_data)
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    // resp.dump("RETURN UPDATE OBJECT")

    if (resp.status < 300) {

      let obj = new cnRelation()
      obj.setFromJson(resp.data)

      return cb(new ApiResponse(resp.status, obj))

    } else {

      return cb(resp)

    }

  }

  async patchRelation( rel_data , relation_id , cb ) {
        
    const url = `${this.#_ctx.getServiceUrl(Context.SERVICE.HUB)}/${this.#_ctx.getScope()}/relations/${relation_id}`;
    
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////    
    /* RestResponse */ const resp = await RestServices.makeApiCall(this.#_ctx.getToken(), RestServices.METHODS.PATCH, url, rel_data)
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    resp.dump("RETURN UPDATE RELATION")

    if (resp.status < 300) {

      let obj = new cnRelation()
      obj.setFromJson(resp.data)

      return cb(new ApiResponse(resp.status, obj))

    } else {

      return cb(resp)

    }

  }

  /**
   * 
   * @param {*} relation_sid 
   * @param {*} cb 
   * @returns {Promise<RestResponse>} Returns the response from the delete operation
   */
  async deleteRelation(relation_sid, cb) {

    console.log( `### <deleteRelation( ${relation_sid} )> `)

    // /{scope}/relations/{relation}
    
    const url = `${this.#_ctx.getServiceUrl(Context.SERVICE.HUB)}/${this.#_ctx.getScope()}/relations/${relation_sid}`;

    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    /* RestResponse */ const resp = await RestServices.makeApiCall(this.#_ctx.getToken(), RestServices.METHODS.DELETE, url)
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    return cb(resp)

  }  

  /**
   * 
   * @param {*} left_nodeid 
   * @param {*} query 
   * @param {*} cb 
   * @returns {Promise<ApiResponse>} Returns array of relations
   */
  async getRelationListByNode( left_nodeid , query, cb) {

    let q = ''
    let c = '?'
     
    if (query['members']) {
      let plist = query['members'].join(',')
      q += c + 'members=' + encodeURIComponent(plist)
      c = '&'
    }    
     
    if (query['filter']) {
      q += c + 'filter=' + encodeURIComponent(filter + query['filter'])
      c = '&'
    }

    const url = `${this.#_ctx.getServiceUrl(Context.SERVICE.HUB)}/${this.#_ctx.getScope()}/${left_nodeid}/relations${q}`;

    console.log(`GET : ${url}`)
    
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////        
    /* RestResponse */ const resp = await RestServices.makeApiCall(this.#_ctx.getToken(), RestServices.METHODS.GET, url)
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    // console.log( 'REL REL ' )
    // console.log( JSON.stringify(resp.data) )

    if (resp.status < 300) {

      // resp.dump( "REL LIST" )

      let array = []

      for (const obj_json of resp.data) {

        let obj = new cnRelation()
        obj.setFromJson(obj_json)

        array.push(obj)

      }

      return cb(new ApiResponse(resp.status, array))

    } else {

      return cb(resp)

    }

  }

  /**
   * 
   * @param {*} query 
   * @param {*} cb 
   * @returns {Promise<ApiResponse>} Returns array of relations
   */
  async getRelationList( query, cb) {

    let q = ''
    let c = '?'
     
    if (query['members']) {
      let plist = query['members'].join(',')
      q += c + 'members=' + encodeURIComponent(plist)
      c = '&'
    }

    if (query['filter']) {
      q += c + 'filter=' + encodeURIComponent(filter + query['filter'])
      c = '&'
    }

    const url = `${this.#_ctx.getServiceUrl(Context.SERVICE.HUB)}/${this.#_ctx.getScope()}/relations${q}`;

    console.log(`GET : ${url}`)
    
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////        
    /* RestResponse */ const resp = await RestServices.makeApiCall(this.#_ctx.getToken(), RestServices.METHODS.GET, url)
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    if (resp.status < 300) {

      let array = []

      for (const obj_json of resp.data) {

        let obj = new cnRelation()
        obj.setFromJson(obj_json)

        array.push(obj)

      }

      return cb(new ApiResponse(resp.status, array))

    } else {

      return cb(resp)

    }

  }

}