LoginSignup
6
7

More than 5 years have passed since last update.

Kintone API で組織情報一覧を取得してツリー構造化する

Last updated at Posted at 2018-07-21

組織情報の取得

cybozu.com 共通設定で設定されている、組織情報を一覧で取得するには以下のように書けば良い。JSONで取得できる。

async function getOrganizationInfo() {
    try {
        const response = await kintone.api('/v1/organizations', 'GET', {});
        return response.organizations;
    } catch (error) {
        throw error;
    }
}

レスポンスの中身は以下のようなオブジェクトとなる。ちなみに一番親にあたる組織の場合、'parentCode === null' となる。

{
    "organizations": [
        {
        "id": 5,
        "name": "org1",
        "code": "org1",
        "parentCode": "org3",
        "localName": "ln1",
        "localNameLocale": "en",
        "description": "desc1",
        },
        {
        "id": 6,
        "name": "org2",
        "code": "org2",
        "parentCode": "org3",
        "localName": "ln2",
        "localNameLocale": "zh",
        "description": "desc2",
        },
        {
        "id": 7,
        "name": "org3",
        "code": "org3",
        "parentCode": null,
        "localName": "ln3",
        "localNameLocale": "zh",
        "description": "desc3",
        }
    ]
}

参考:KintoneAPI公式ページ

組織階層をツリー構造のデータに格納する

以前に私が書いた記事、「Javascript でツリー階層をデータで持たせる」 で使ったアイディアを応用して組織情報をツリー構造のクラスインスタンスに持たせるようにすれば諸々カスタマイズの際に融通が効く。

クラスの準備


class Group {

    constructor(code, name) {
        this.code = code;
        this.name = name;
        this.parent = null;
        this.children = [];
    }

    addChild(child) {
        this.children.push(child);
        child.parent = this;
    }

    addChildren(childrenArray) {
        for (let i = 0; i < childrenArray.length; i++) {
            this.addChild(childrenArray[i]);
        }
    }

}

組織情報のツリー化

async function createOrganizationTree() {

    try {

        // KintoneAPIで組織情報の取得
        const groups = await getOrganizationInfo();

        // APIのレスポンスから Groupクラスインスタンスを生成、parentCodeとセットにする
        // この時点では親子関係は構築されていない
        const arrayTmp = groups.map(eachGroup => {
            return {
            groupInstance : new Group(eachGroup.code, eachGroup.name), 
            parentCode : eachGroup.parentCode
            };
        });

        // parentCode の値が指定のものになっているGroupクラスインスタンスの配列を取得
        const searchInstanceByParentCode = function(code) {
            const returnArray = [];
            for (let i = 0; i < arrayTmp.length; i++) {
                const instance = arrayTmp[i].groupInstance;
                const parentCode = arrayTmp[i].parentCode;
                if (parentCode === code) {
                    returnArray.push(instance);
                }
             }
             return returnArray;
        }

        // 仮想のROOTを作ってすべてを配下におく
        const ROOT = new Group(null, 'ROOT');

        // 親子関係の構築
        for (let i = 0; i < arrayTmp.length; i++) {

            const eachGroup = arrayTmp[i].groupInstance;
            const childrenGroups = searchInstanceByParentCode(eachGroup.code);
            eachGroup.addChildren(childrenGroups);

            // 最上層組織はROOT直下に
            if (arrayTmp[i].parentCode === null) {
                ROOT.addChild(eachGroup);
            }
        }

        console.log(ROOT);
        return ROOT;

    } catch (e) {
        throw e;
    }

}

以上の処理をKintoneの何かしらのイベント処理の際に呼び出せば、ROOT配下にすべての組織がツリー構造になって格納される。

6
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
7