0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

SmartHRのデータを上手に加工する

Last updated at Posted at 2022-02-23

サンプルコード

こちらの記事で取得したデータを、扱いやすいように加工する。
特に部署情報について、階層構造をフラットにし、情報をタテ持ちする。

function get_top_depertment(depertment, prev_depertment = null){
    if(depertment !== null){
        result = get_top_depertment(depertment['parent'], depertment);
        return result;
    }else{
        return prev_depertment;
    }
}

function get_middle_depertment(depertment, prev_depertment = null, prev_prev_depertment = null){
    if(depertment !== null){
        result = get_middle_depertment(depertment['parent'], depertment, prev_depertment);
        return result;
    }else{
        return prev_prev_depertment;
    }
}

// 上層から1,2番目の部署と、与えられた部署名を比較して、一致する部署があればTrueを返す
function is_member_of_x(departments, department_name){
    if(departments !== null){
        for(let dept of departments){
            top_depertment = get_top_depertment(dept);
            if(top_depertment !== null && top_depertment['name'] == department_name){
                return true;
            }else{;}
            middle_depertment = get_middle_depertment(dept)
            if(middle_depertment !== null && middle_depertment['name'] == department_name){
                return true;
            }else{;}
        }
        return false;
    }else{
        return false;
    }
}

function is_none_or_empty(value){
    if(value === null || value == ''){
        return true;
    }else{
        return false;
    }
}

function translate(data){
    let result = []
    for(let item of data){
        let business_last_name  = (('business_last_name'  in item) ? item['business_last_name']  : null);
        let business_first_name = ('business_first_name' in item ? item['business_first_name'] : null);
        let last_name  = ('last_name'  in item ? item['last_name']  : null);
        let first_name = ('first_name' in item ? item['first_name'] : null);
        let departments = ('departments' in item ? item['departments'] : null);
        let position = ('position' in item ? item['position'] : null);
        let email = (item['custom_fields'].length > 0 ? item['custom_fields'][0]['value'] : null);
        result = result.concat({
            'last_name':  (is_none_or_empty(business_last_name) ? last_name : business_last_name),
            'first_name': (is_none_or_empty(business_first_name) ? first_name : business_first_name),
            'email':    email,
            'position': position,
            'is_corporate':  is_member_of_x(departments, 'コーポレート本部'),
            'is_hr':  is_member_of_x(departments, '人事部'),   //Human Resources
            'is_ms':  is_member_of_x(departments, '管理基盤部'),//Management System
            'is_cp':  is_member_of_x(departments, '経営企画部'),//Corporate Planning
        })
    }
    return result
}

解説

is_member_of_x()が読解難易度高いかもしれない。
これは、X部署に所属しているかどうかを、部署情報を渡して判別するメソッド。
(ただし、簡単のため上層から1~2階層に判定をとどめている、拡張も可能ではある)

SmartHRの部署情報は以下のように再帰構造になっている。
部署[,,,parent:部署[,,,parent:部署[,,,parent:部署]]]
そこで、get_top_depertmentでは再帰を使って最上位部署を特定する手法を取っている。

初学者は、「再帰」についてググると、似た構造を見つけられるかもしれない。
きちんと理解したほうが良いが、いったんおまじないとして理解を後回しにしてもそこまで困らない。

次回はこのデータのSpreadsheetへのロードについて解説する。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?