Source

infohub/Properties.js

// $Id$
// @ts-check

'use strict';

import { RestServices } from '../tools/RestServices.js';
import { ApiResponse } from '../tools/ApiResponse.js';
import { Context } from './Context.js';



/**
 *
 *  @category COLNEO infohub
 *
 *  @classdesc Properties on infohub
 *
 */
export class PropertyServices {

    /** @type {Context} */
    #_ctx = new Context();

    /**
     * @since 1.0, 11.2025, ar
     * @param {Context} ctx - Context instance
     *
     */
    constructor(ctx) {
        this.#_ctx = ctx;
    }

    /**
     *
     * POST service_hub/<scope>/projects/<project>/properties
     * @since 27.11.2025, ar
     * @param {Object} proplist
     * = [
     * [
            {
                "parent_id": "string",
                "object_id": "string",
                "object_name": "string",
                "object_type": "string",
                "properties": {
                    "additionalProp1": "string",
                    "additionalProp2": "string",
                    "additionalProp3": "string"
                },
                "properties_comments" : {
                    "additionalProp1": "string"
                }
            }
     * ]
     * @returns {Promise<ApiResponse>}
     */
    async writePropertyList( proplist ) {

        console.log("<writePropertyList()>")

        const target = this.#_ctx.getServiceUrl(Context.SERVICE.HUB) + "/" + this.#_ctx.getScope() + "/projects/" + this.#_ctx.getProjectShortId() + "/properties"

        console.log("POST  : " + target);

        try {
            const response = await fetch(target, {
                method: 'POST',
                headers: {
                    'Authorization': 'Bearer ' + this.#_ctx.getToken(),
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify(proplist)
            });

            if (response.ok) {
                return new ApiResponse( response.status, null, "Eigenschaften in Datenbank geschrieben." );
            } else {
                console.log("status : " + response.status);
                return new ApiResponse( response.status, null, "Eigenschaften NICHT in Datenbank geschrieben." );
            }

        } catch (error) {
            const errorMsg = error instanceof Error ? error.message : String(error);
            console.log("Error: " + errorMsg);
            return new ApiResponse( 500, null, "Fehler beim Schreiben der Eigenschaften: " + errorMsg );
        }

    }
    
    /**
     * 
     *  POST /{scope}/properties/values
     *      
     *  @param {*} proplist =
     *  [
            {
                "shortid": "string",
                "properties": {
                    "additionalProp1": "string",
                    "additionalProp2": "string",
                    "additionalProp3": "string"
                },
                "properties_comments": {
                    "additionalProp1": "string",
                    "additionalProp2": "string",
                    "additionalProp3": "string"
                }
            }
        ]
     */
    async writePropertyValues(proplist) {

        
        const url = this.#_ctx.getServiceUrl(Context.SERVICE.HUB) + "/" + this.#_ctx.getScope() + "/properties"
        console.log("POST  : " + url );
                
        //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        /* RestResponse */ const resp = await RestServices.makeApiCall(this.#_ctx.getToken(), RestServices.METHODS.POST, url, proplist)
        //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        return resp

    }

    /**
     *  Get property list from infohub.
     *  Use query to specify which objects and properties should be taken into account.
     *  For convenience, you can get the member ‘properties’ in the correct format 
     *  from a list of properties by using the method getPropertyListFromTypeIds( typeids ).
     *  
     * 
     *  @param {*} query = {
     *      "project_ids"      : [ '... project shortid... ' , ' ... ' ],
     *      "object_ids"       : [ '... object id ' , ' ... ' ],
     *      "properties"       : [
     *          {
     *          }
     *       ]
     *  }
     * 
     *  @returns {Promise<ApiResponse>}
     * 
     */

    async readPropertyList(query) {

        console.log('### <readPropertyList()>')
        // console.log(JSON.stringify(query))

        // /{scope}/properties/queries

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

        /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        /* RestResponse */ const resp = await RestServices.makeApiCall(this.#_ctx.getToken(), RestServices.METHODS.POST, url, query)
        /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        
        if (resp.status < 300) {
    
            // console.log( resp.dump("QUERY"))

            const query_id = resp.data.shortid

            // console.log( `QUERYID : [${query_id}] `)

            const q = `query_id=${query_id}`            
            // const url = `${this.#_ctx.getServiceUrl(Context.SERVICE.HUB)}/${this.#_ctx.getScope()}/properties?${q}`;
            const url = `${this.#_ctx.getServiceUrl(Context.SERVICE.HUB)}/${this.#_ctx.getScope()}/properties/values?${q}`;
            
            // console.log( `URL = [${url}]` )

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

            if (resp2.status < 300) {

                // console.log(resp2.dump("QUERY2"))
                return resp2
                
            } else {
                return resp2
            }
            
        } else {

            return resp

        }

        // return new ApiResponse( 400 , null, "Unbekannter Fehler.");

    }

    /**
     * 
     * Get list of property types in a format that can be used in 'readPropertyList
     * 
     * @param {Array<string>} typeids 
     * @returns array<dict>
     * 
     */
    getPropertyListFromTypeIds(typeids) {

        let pl = []

        for (const typeid of typeids) {
            const p = {
                "property_type": ` ~eq~ '${typeid}' `,
                "value": "~notnull~ "
            }
            pl.push(p)
        }

        return pl

    }

}