UNPKG

content-generator

Version:

Easy template generator to manage and save templates by key and languagecode.

30 lines (21 loc) 629 B
'use strict'; const mdbhandler = require('mongodb-handler'); const ObjectID = require('mongodb').ObjectID; const processenv = require('processenv'); const collection = processenv('CTM_COLLECTION') || 'CM_Templates'; const getTemplateById = (id, callback) => { if (!id) { const errMsg = 'Function is called without id'; return callback(errMsg); } if (/[a-f0-9]{24}/.test(id)) { id = new ObjectID(id.toString()); } mdbhandler.fetch({ collection, doc: { _id: id }}, (err, res) => { if (err) { return callback(err); } callback(null, res[0]); }); }; module.exports = getTemplateById;