'use strict';

const linz = require('../../../../');
const cellRenderers = require('./');
const utils = require('../../../utils');

const defaultRenderer = (val, record, fieldName, model, callback) => {
    if (val === undefined) {
        return callback(null);
    }

    if (Array.isArray(val)) {
        if (!val.length) {
            return callback(null);
        }

        // check if this field is a document array
        if (Array.isArray(model.schema.tree[fieldName])) {
            return cellRenderers.documentArray(
                val,
                record,
                fieldName,
                model,
                callback
            );
        }

        return cellRenderers.array(val, record, fieldName, model, callback);
    }

    if (val instanceof Date) {
        return cellRenderers.date(val, record, fieldName, model, callback);
    }

    if (val !== '' && utils.isBoolean(val)) {
        return cellRenderers.boolean(val, record, fieldName, model, callback);
    }

    console.log('teh val', val);

    // check if field is a reference document
    if (
        model.schema.tree[fieldName] &&
        model.schema.tree[fieldName].ref &&
        linz.api.model.getObjectIdFromRefField(val) instanceof
            linz.mongoose.Types.ObjectId
    ) {
        return cellRenderers.referenceName(
            val,
            record,
            fieldName,
            model,
            callback
        );
    }

    return callback(null, val);
};

module.exports = defaultRenderer;

'use strict';

const linz = require('../../../../');
const cellRenderers = require('./');
const utils = require('../../../utils');

const defaultRenderer = (val, record, fieldName, model, callback) => {
    if (val === undefined) {
        return callback(null);
    }

    if (Array.isArray(val)) {
        if (!val.length) {
            return callback(null);
        }

        // check if this field is a document array
        if (Array.isArray(model.schema.tree[fieldName])) {
            return cellRenderers.documentArray(
                val,
                record,
                fieldName,
                model,
                callback
            );
        }

        return cellRenderers.array(val, record, fieldName, model, callback);
    }

    if (val instanceof Date) {
        return cellRenderers.date(val, record, fieldName, model, callback);
    }

    if (val !== '' && utils.isBoolean(val)) {
        return cellRenderers.boolean(val, record, fieldName, model, callback);
    }

    // check if field is a reference document
    if (
        model.schema.tree &&
        model.schema.tree[fieldName] &&
        model.schema.tree[fieldName].ref &&
        linz.api.model.getObjectIdFromRefField(val) instanceof
            linz.mongoose.Types.ObjectId
    ) {
        return cellRenderers.referenceName(
            val,
            record,
            fieldName,
            model,
            callback
        );
    }

    return callback(null, val);
};

module.exports = defaultRenderer;

remove comment