Skip to content
This repository has been archived by the owner on Dec 16, 2021. It is now read-only.

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
marefati110 committed Sep 21, 2020
1 parent 2dc0734 commit 58255cf
Showing 1 changed file with 37 additions and 27 deletions.
64 changes: 37 additions & 27 deletions services/core/middleware/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 };

0 comments on commit 58255cf

Please sign in to comment.