mirror of
https://github.com/strapi/strapi.git
synced 2025-12-08 04:54:44 +00:00
refacto
Signed-off-by: Pierre Noël <petersg83@gmail.com>
This commit is contained in:
parent
75d007526e
commit
dce8f7c6fa
@ -24,8 +24,6 @@ const RESERVED_ATTRIBUTE_NAMES = [
|
|||||||
'attributes',
|
'attributes',
|
||||||
'relations',
|
'relations',
|
||||||
'changed',
|
'changed',
|
||||||
'page',
|
|
||||||
'pageSize',
|
|
||||||
|
|
||||||
// list found here https://mongoosejs.com/docs/api.html#schema_Schema.reserved
|
// list found here https://mongoosejs.com/docs/api.html#schema_Schema.reserved
|
||||||
'_posts',
|
'_posts',
|
||||||
|
|||||||
@ -35,8 +35,8 @@ const withDefaultPagination = params => {
|
|||||||
const { page = 1, pageSize = 100, ...rest } = params;
|
const { page = 1, pageSize = 100, ...rest } = params;
|
||||||
|
|
||||||
return {
|
return {
|
||||||
page: Math.max(1, parseInt(page)) || 1,
|
page: parseInt(page),
|
||||||
pageSize: Math.max(1, parseInt(pageSize)) || 100,
|
pageSize: parseInt(pageSize),
|
||||||
...rest,
|
...rest,
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|||||||
@ -9,7 +9,7 @@ const {
|
|||||||
pickWritableAttributes,
|
pickWritableAttributes,
|
||||||
} = require('../utils');
|
} = require('../utils');
|
||||||
const { MANY_RELATIONS } = require('../services/constants');
|
const { MANY_RELATIONS } = require('../services/constants');
|
||||||
const { validateBulkDeleteInput } = require('./validation');
|
const { validateBulkDeleteInput, validatePagination } = require('./validation');
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
async find(ctx) {
|
async find(ctx) {
|
||||||
@ -232,6 +232,8 @@ module.exports = {
|
|||||||
const { model, id, targetField } = ctx.params;
|
const { model, id, targetField } = ctx.params;
|
||||||
const { pageSize = 10, page = 1 } = ctx.request.query;
|
const { pageSize = 10, page = 1 } = ctx.request.query;
|
||||||
|
|
||||||
|
validatePagination({ page, pageSize });
|
||||||
|
|
||||||
const contentTypeService = getService('content-types');
|
const contentTypeService = getService('content-types');
|
||||||
const entityManager = getService('entity-manager');
|
const entityManager = getService('entity-manager');
|
||||||
const permissionChecker = getService('permission-checker').create({ userAbility, model });
|
const permissionChecker = getService('permission-checker').create({ userAbility, model });
|
||||||
@ -276,7 +278,7 @@ module.exports = {
|
|||||||
|
|
||||||
ctx.body = {
|
ctx.body = {
|
||||||
pagination: relationList.pagination,
|
pagination: relationList.pagination,
|
||||||
results: relationList.results.map(pick(['id', 'ids', settings.mainField])),
|
results: relationList.results.map(pick(['id', modelDef.primaryKey, settings.mainField])),
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|||||||
@ -88,6 +88,18 @@ const validateUIDField = (contentTypeUID, field) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const validatePagination = ({ page, pageSize }) => {
|
||||||
|
const pageNumber = parseInt(page);
|
||||||
|
const pageSizeNumber = parseInt(pageSize);
|
||||||
|
|
||||||
|
if (isNaN(pageNumber) || pageNumber < 1) {
|
||||||
|
throw strapi.errors.badRequest('invalid pageNumber param');
|
||||||
|
}
|
||||||
|
if (isNaN(pageSizeNumber) || pageSizeNumber < 1) {
|
||||||
|
throw strapi.errors.badRequest('invalid pageSize param');
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
createModelConfigurationSchema,
|
createModelConfigurationSchema,
|
||||||
validateKind,
|
validateKind,
|
||||||
@ -95,4 +107,5 @@ module.exports = {
|
|||||||
validateGenerateUIDInput,
|
validateGenerateUIDInput,
|
||||||
validateCheckUIDAvailabilityInput,
|
validateCheckUIDAvailabilityInput,
|
||||||
validateUIDField,
|
validateUIDField,
|
||||||
|
validatePagination,
|
||||||
};
|
};
|
||||||
|
|||||||
@ -5,6 +5,9 @@ const { registerAndLogin } = require('../../../../test/helpers/auth');
|
|||||||
const createModelsUtils = require('../../../../test/helpers/models');
|
const createModelsUtils = require('../../../../test/helpers/models');
|
||||||
const { createAuthRequest } = require('../../../../test/helpers/request');
|
const { createAuthRequest } = require('../../../../test/helpers/request');
|
||||||
|
|
||||||
|
const toIds = arr => uniq(map(prop('id'))(arr));
|
||||||
|
const getFrom = model => (start, end) => fixtures[model].map(prop('name')).slice(start, end);
|
||||||
|
|
||||||
let rq;
|
let rq;
|
||||||
let modelsUtils;
|
let modelsUtils;
|
||||||
const data = {
|
const data = {
|
||||||
@ -88,8 +91,6 @@ const fixtures = {
|
|||||||
product: () => {
|
product: () => {
|
||||||
const { shop, category } = data;
|
const { shop, category } = data;
|
||||||
|
|
||||||
const getFrom = model => (start, end) => fixtures[model].map(prop('name')).slice(start, end);
|
|
||||||
|
|
||||||
const items = [
|
const items = [
|
||||||
{ name: 'PD.A', categories: getFrom('category')(0, 5), shops: getFrom('shop')(0, 12) },
|
{ name: 'PD.A', categories: getFrom('category')(0, 5), shops: getFrom('shop')(0, 12) },
|
||||||
];
|
];
|
||||||
@ -176,12 +177,11 @@ describe('x-to-many RF Preview', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe('Default Behavior', () => {
|
describe('Default Behavior', () => {
|
||||||
test.each(['shops', 'categories'])('Should returns a preview for the %s field', async field => {
|
test.each(['shops', 'categories'])('Should return a preview for the %s field', async field => {
|
||||||
const product = data.product[0];
|
const product = data.product[0];
|
||||||
|
|
||||||
const { body, statusCode } = await rq.get(`${cmProductUrl}/${product.id}/${field}`);
|
const { body, statusCode } = await rq.get(`${cmProductUrl}/${product.id}/${field}`);
|
||||||
|
|
||||||
const toIds = arr => uniq(map(prop('id'))(arr));
|
|
||||||
const expected = product[field].slice(0, 10);
|
const expected = product[field].slice(0, 10);
|
||||||
|
|
||||||
expect(statusCode).toBe(200);
|
expect(statusCode).toBe(200);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user