"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.boardSettingsResolver = void 0;
const typeorm_1 = require("typeorm");
const auth_base_1 = require("@things-factory/auth-base");
const board_service_1 = require("@things-factory/board-service");
const setting_base_1 = require("@things-factory/setting-base");
const opa_app_setting_constants_1 = require("../../../opa-app-setting-constants");
exports.boardSettingsResolver = {
    async boardSettings(_, params, context) {
        const { domain } = context.state;
        /***********************************************************************************
         * Temporary solution for dashboard user type
         ***********************************************************************************/
        const user = await (0, typeorm_1.getRepository)(auth_base_1.User)
            .createQueryBuilder('USER')
            .leftJoinAndSelect('USER.roles', 'ROLES')
            .leftJoinAndSelect('ROLES.domain', 'ROLES_DOMAIN')
            .where('USER.id = :userId', { userId: context.state.user.id })
            .andWhere('ROLES_DOMAIN.id = :domainId', { domainId: domain.id })
            .getOne();
        const userType = user.roles.filter(role => role.description)[0].description;
        /**********************************************************************************/
        const HOME_BOARD = `${opa_app_setting_constants_1.HOME_DASHBOARD_PREFIX}:${userType}`;
        // const HOME_BOARD = `${HOME_DASHBOARD_PREFIX}:${context.state.user.userType}`
        const queryBuilder = (0, typeorm_1.getRepository)(setting_base_1.Setting).createQueryBuilder();
        const names = [
            opa_app_setting_constants_1.LOCATION_LABEL_SETTING_KEY,
            opa_app_setting_constants_1.PRODUCT_LABEL_SETTING_KEY,
            opa_app_setting_constants_1.PALLET_LABEL_SETTING_KEY,
            opa_app_setting_constants_1.ITEM_LABEL_SETTING_KEY,
            opa_app_setting_constants_1.REUSABLE_PALLET_LABEL_SETTING_KEY,
            HOME_BOARD,
            opa_app_setting_constants_1.CARTON_LABEL_SETTING_KEY,
            opa_app_setting_constants_1.CARTON_LABEL_BY_SKU_SETTING_KEY,
            opa_app_setting_constants_1.INTENALIZED_AWB_SETTING_KEY,
            opa_app_setting_constants_1.ROUTE_LABEL_SETTING_KEY,
            opa_app_setting_constants_1.TOTE_LABEL_SETTING_KEY
        ];
        var qb = queryBuilder
            // .innerJoin(Board, 'Board', 'Board.id = CAST(Setting.value AS uuid)')
            .innerJoin(board_service_1.Board, 'Board', 'Setting.value = CAST(Board.id AS char(36))')
            // .innerJoin(Board, 'Board', 'Board.id = Setting.value')
            .select([
            'Setting.id as id',
            'Setting.name as name',
            'Setting.value as value',
            'Board.id as boardId',
            'Board.name as boardName',
            'Board.description as boardDescription',
            'Board.thumbnail as boardThumbnail'
        ])
            .where('Setting.domain_id = :domain', { domain: context.state.domain.id });
        if (names && names.length)
            qb.andWhere('Setting.name IN (:...names)', { names });
        var boardSettingList = await qb.getRawMany();
        return boardSettingList.map(boardSetting => {
            var setting = {
                board: {}
            };
            for (let key in boardSetting) {
                if (key.includes('board')) {
                    let originKey = key;
                    key = key.replace('board', '').toLowerCase();
                    setting.board[key] = boardSetting[originKey];
                }
                else {
                    setting[key] = boardSetting[key];
                }
            }
            if (setting.name.indexOf(opa_app_setting_constants_1.HOME_DASHBOARD_PREFIX) == 0) {
                setting.name = opa_app_setting_constants_1.HOME_DASHBOARD_PREFIX;
            }
            return setting;
        });
    }
};
//# sourceMappingURL=board-settings.js.map