maestro-server/analytics-front

View on GitHub
app/core/libs/in_maker.js

Summary

Maintainability
A
0 mins
Test Coverage
B
83%
"use strict";

const _ = require('lodash');
const {ObjectId} = require('mongodb');
const NotFoundError = require('core/errors/factoryError')('NotFoundError');


const intObject = (str) => {
  if(ObjectId.isValid(str)) {
    return ObjectId(str);
  }
  throw new NotFoundError(`Indentity ${str} not exist`);
};

module.exports = (_id, key = '$in') => {
  if(_.has(_id, key)) {
    return _id;
  }

  return _.isArray(_id)
  ? {[key]: _.map(_id, e=>intObject(e))}
  : intObject(_id);
};