diff --git a/services/core/middleware/index.js b/services/core/middleware/index.js index 7fbab39..836c60f 100644 --- a/services/core/middleware/index.js +++ b/services/core/middleware/index.js @@ -8,18 +8,24 @@ const urlIdPattern = /\d*$/, urlIndexPattern = /^\/(\w+)/, adminUrlIndexPattern = /::(.+)\./; -const elasticsearchManager = async (ctx) => { - // validation +function checkRequest(ctx) { + let status = false; if ( - !( - ctx.request && - setting.validMethod.includes(ctx.request.method) && - urlIdPattern.test(ctx.request.url) && - urlIndexPattern.test(ctx.request.url) && - setting.validStatus.includes(ctx.response.status) - ) + ctx.request && + setting.validMethod.includes(ctx.request.method) && + urlIdPattern.test(ctx.request.url) && + urlIndexPattern.test(ctx.request.url) && + setting.validStatus.includes(ctx.response.status) ) - return; + status = true; + + // + return status; +} + +const elasticsearchManager = async (ctx) => { + // validation + if (!checkRequest(ctx)) return; // extract data from url const url = ctx.request.url, body = ctx.response.body, @@ -40,25 +46,29 @@ const elasticsearchManager = async (ctx) => { let data; if (setting.fillByResponse === true) { data = body; - } else if (setting.fillByResponse === false) { - if (withRelated) { - const raw_data = await strapi - .query(index_.service) - .model.query((qb) => { - qb.where('id', '=', id); - }) - .fetch({ - withRelated: withRelated, - }); - data = await raw_data.toJSON(); - } else if (!withRelated) { - data = await strapi.services[index_.service].findOne({ id: id }); - } + // + } else if (setting.fillByResponse === false && withRelated) { + const raw_data = await strapi + .query(index_.service) + .model.query((qb) => { + qb.where('id', '=', id); + }) + .fetch({ + withRelated: withRelated, + }); + data = await raw_data.toJSON(); + + // + } else if (setting.fillByResponse === false && !withRelated) { + data = await strapi.services[index_.service].findOne({ id: id }, []); } + return await createOrUpdate(index, id, data); - } else if (ctx.request.method === 'DELETE') { - return await destroy(index, id); - } else return; + // + } + + // delete data + if (ctx.request.method === 'DELETE') return await destroy(index, id); }; module.exports = { elasticsearchManager };