<commit_before>/**
 * @module src/Controller/ModelController/API/APIModelController
 * @flow
 */
import lodash from 'lodash';
import { Registry, Column, Utils } from '@admin-interface/core';

import ErrorResponse from '../../../Utils/ErrorResponse/ErrorResponse';
import { getLinkModelSingle, getLinkModelList } from '../../../Utils/View/LinkType/ModelView';
import { getLinkApiModelDelete } from '../../../Utils/View/LinkType/ModelAPI';

/**
 * Get list item
 * @async
 * @function
 * @param {express$Request} req
 * @param {express$Response} res
 * @param {express$NextFunction} next
 * @returns {Promise.<*>}
 */
export async function getApiList(req: express$Request, res: express$Response, next: express$NextFunction) {
    const modelKey: string = req.params.model_key;

    const Model = Registry.getRepository('Model').get(modelKey);
    if (Model) {
        const Columns: Array<Column> = Model.getColumns();

        const refModel: string    = req.query.refModel;
        const refModelKey: string = req.query.refModelKey;

        const dataTableColumns: Array<DataTableColumnType> = req.query.columns;
        const dataTableOrder: Array<DataTableOrderType>    = req.query.order;


        // Reference
        const byReference: DataTableByReference = {
            table: '',
            value: refModelKey
        };
        if (refModel) {
            const refModelObject = Registry.getRepository('Model').get(refModel);
            if (refModelObject) {
                byReference.table = refModelObject.getModel().tableName;
            }
        }

        // Search string
        const search: Array<DataTableSearch> = dataTableColumns
            .filter(e => e.search.value)
            .map(e => ({ column: +e.data, value: e.search.value }));

        // Order params
        const orderBy: DataTableOrderType = dataTableOrder[ 0 ];
        const order: Array<Array<any>>    = Utils.formatOrder(
            Columns[ orderBy.column ],
            orderBy.dir.toLocaleUpperCase()
        );

        // Pagination params
        const query: DataTablePagination = {
            limit:  +req.query.length || 10,
            offset: +req.query.start || 0
        };

        try {
            // Reference where
            const whereByReference  = Utils.getReferenceWhere(Model, byReference);
            // Get items
            const { rows, count }   = await Model.getModel().findAndCountAll(
                Utils.formatQueryModelList(Model, order, query, search, whereByReference)
            );
            // Records total
            const recordsTotal      = await Model.getModel().count(whereByReference ? {
                where: whereByReference
            } : null);
            const itemsArrayPromise = rows.map(_item => {
                const item   = _item.get();
                const fields = Columns.map(async (column: Column) => {
                    // reference
                    if (column.getReferenceKey()) {
                        return item[ column.getReference() ][ column.getReferenceKey() ];
                    }
                    // value
                    if (!column.isEmptyValue()) {
                        const value = await column.getValue(item);
                        return value;
                    }
                    // model field
                    return item[ column.getField() ];
                });

                // Add data for action buttons
                if (Model.isShowActions()) {
                    fields.push(getLinkModelSingle(Model.getKey(), item[ Model.getPrimaryKey() ]));
                    fields.push(getLinkApiModelDelete(Model.getKey(), item[ Model.getPrimaryKey() ]));
                }

                return fields;
            });

            const itemsArray = await Promise.all(itemsArrayPromise.map(item => Promise.all(item)));
<commit_msg>check for exist property from reference<commit_after>/**
 * @module src/Controller/ModelController/API/APIModelController
 * @flow
 */
import lodash from 'lodash';
import { Registry, Column, Utils } from '@admin-interface/core';

import ErrorResponse from '../../../Utils/ErrorResponse/ErrorResponse';
import { getLinkModelSingle, getLinkModelList } from '../../../Utils/View/LinkType/ModelView';
import { getLinkApiModelDelete } from '../../../Utils/View/LinkType/ModelAPI';

/**
 * Get list item
 * @async
 * @function
 * @param {express$Request} req
 * @param {express$Response} res
 * @param {express$NextFunction} next
 * @returns {Promise.<*>}
 */
export async function getApiList(req: express$Request, res: express$Response, next: express$NextFunction) {
    const modelKey: string = req.params.model_key;

    const Model = Registry.getRepository('Model').get(modelKey);
    if (Model) {
        const Columns: Array<Column> = Model.getColumns();

        const refModel: string    = req.query.refModel;
        const refModelKey: string = req.query.refModelKey;

        const dataTableColumns: Array<DataTableColumnType> = req.query.columns;
        const dataTableOrder: Array<DataTableOrderType>    = req.query.order;


        // Reference
        const byReference: DataTableByReference = {
            table: '',
            value: refModelKey
        };
        if (refModel) {
            const refModelObject = Registry.getRepository('Model').get(refModel);
            if (refModelObject) {
                byReference.table = refModelObject.getModel().tableName;
            }
        }

        // Search string
        const search: Array<DataTableSearch> = dataTableColumns
            .filter(e => e.search.value)
            .map(e => ({ column: +e.data, value: e.search.value }));

        // Order params
        const orderBy: DataTableOrderType = dataTableOrder[ 0 ];
        const order: Array<Array<any>>    = Utils.formatOrder(
            Columns[ orderBy.column ],
            orderBy.dir.toLocaleUpperCase()
        );

        // Pagination params
        const query: DataTablePagination = {
            limit:  +req.query.length || 10,
            offset: +req.query.start || 0
        };

        try {
            // Reference where
            const whereByReference  = Utils.getReferenceWhere(Model, byReference);
            // Get items
            const { rows, count }   = await Model.getModel().findAndCountAll(
                Utils.formatQueryModelList(Model, order, query, search, whereByReference)
            );
            // Records total
            const recordsTotal      = await Model.getModel().count(whereByReference ? {
                where: whereByReference
            } : null);
            const itemsArrayPromise = rows.map(_item => {
                const item   = _item.get();
                const fields = Columns.map(async (column: Column) => {
                    // reference
                    if (column.getReferenceKey() && item[ column.getReference() ]) {
                        return item[ column.getReference() ][ column.getReferenceKey() ];
                    }
                    // value
                    if (!column.isEmptyValue()) {
                        const value = await column.getValue(item);
                        return value;
                    }
                    // model field
                    return item[ column.getField() ];
                });

                // Add data for action buttons
                if (Model.isShowActions()) {
                    fields.push(getLinkModelSingle(Model.getKey(), item[ Model.getPrimaryKey() ]));
                    fields.push(getLinkApiModelDelete(Model.getKey(), item[ Model.getPrimaryKey() ]));
                }

                return fields;
            });

            const itemsArray = await Promise.all(itemsArrayPromise.map(item => Promise.all(item)));

{'fraction_non_alphanumeric': 0.11667308432807084, 'fraction_numerical': 0.0010268258246694903, 'mean_word_length': 1.8720973092517508, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6441969', 'n_tokens_mistral': 2016, 'n_tokens_neox': 1939, 'n_words': 437}