Keep _ to make the client work until we remove them on the client

This commit is contained in:
Alexandre Bodin 2021-07-30 21:59:34 +02:00
parent e20e7738b5
commit 50849e68c7
2 changed files with 46 additions and 3 deletions

View File

@ -57,6 +57,21 @@ module.exports = ctx => {
return service;
};
// TODO: remove once the front is migrated
const convertOldQuery = params => {
const obj = {};
Object.keys(params).forEach(key => {
if (key.startsWith('_')) {
obj[key.slice(1)] = params[key];
} else {
obj[key] = params[key];
}
});
return obj;
};
// TODO: move to Controller ?
const transformParamsToQuery = (uid, params = {}) => {
const model = strapi.getModel(uid);
@ -75,10 +90,13 @@ const transformParamsToQuery = (uid, params = {}) => {
fields,
populate,
publicationState,
_q,
_where,
...rest
} = params;
if (params._q) {
query._q = params._q;
if (_q) {
query._q = _q;
}
if (page) {
@ -105,6 +123,12 @@ const transformParamsToQuery = (uid, params = {}) => {
query.where = filters;
}
if (_where) {
query.where = {
$and: [_where].concat(query.where || []),
};
}
if (fields) {
query.select = _.castArray(fields);
}
@ -133,7 +157,12 @@ const transformParamsToQuery = (uid, params = {}) => {
}
}
return query;
const finalQuery = {
...convertOldQuery(rest),
...query,
};
return finalQuery;
};
const pickSelectionParams = pick(['fields', 'populate']);

View File

@ -37,6 +37,20 @@ const wrapParams = async (params = {}, ctx = {}) => {
};
}
// TODO: remove when the _locale is renamed to locale
if (has('_locale', params)) {
if (params['_locale'] === 'all') {
return omit('_locale', params);
}
return {
...omit('_locale', params),
filters: {
$and: [{ locale: params['_locale'] }].concat(params.filters || []),
},
};
}
const entityDefinedById = paramsContain('id', params) && SINGLE_ENTRY_ACTIONS.includes(action);
const entitiesDefinedByIds = paramsContain('id.$in', params) && BULK_ACTIONS.includes(action);