fix sort on admin user

This commit is contained in:
Pierre Noël 2021-09-27 16:06:43 +02:00
parent 412a834f98
commit 95d9b19588
4 changed files with 27 additions and 31 deletions

View File

@ -87,7 +87,7 @@ describe('User Controller', () => {
await userController.findOne(ctx);
expect(findOne).toHaveBeenCalledWith({ id: user.id });
expect(findOne).toHaveBeenCalledWith(user.id);
expect(sanitizeUser).toHaveBeenCalledWith(user);
expect(ctx.body).toStrictEqual({ data: user });
});
@ -108,7 +108,7 @@ describe('User Controller', () => {
await userController.findOne(ctx);
expect(findOne).toHaveBeenCalledWith({ id: fakeId });
expect(findOne).toHaveBeenCalledWith(fakeId);
expect(notFound).toHaveBeenCalledWith('User does not exist');
});
});

View File

@ -59,7 +59,7 @@ module.exports = {
async findOne(ctx) {
const { id } = ctx.params;
const user = await getService('user').findOne({ id });
const user = await getService('user').findOne(id);
if (!user) {
return ctx.notFound('User does not exist');

View File

@ -383,16 +383,18 @@ describe('User', () => {
const defaults = { page: 1, pageSize: 100 };
beforeEach(() => {
const fetchPage = jest.fn(({ page = defaults.page, pageSize = defaults.pageSize } = {}) => {
return {
results: Array.from({ length: pageSize }).map((_, i) => i + (page - 1) * pageSize),
pagination: { page, pageSize, total: page * pageSize, pageCount: page },
};
});
const findPage = jest.fn(
(uid, { page = defaults.page, pageSize = defaults.pageSize } = {}) => {
return {
results: Array.from({ length: pageSize }).map((_, i) => i + (page - 1) * pageSize),
pagination: { page, pageSize, total: page * pageSize, pageCount: page },
};
}
);
global.strapi = {
query() {
return { findPage: fetchPage };
entityService: {
findPage,
},
};
});
@ -428,7 +430,7 @@ describe('User', () => {
const user = { firstname: 'Kai', lastname: 'Doe', email: 'kaidoe@email.com' };
beforeEach(() => {
const findOne = jest.fn(({ where: { id } }) =>
const findOne = jest.fn((uid, id) =>
Promise.resolve(
{
1: user,
@ -437,23 +439,23 @@ describe('User', () => {
);
global.strapi = {
query() {
return { findOne };
entityService: {
findOne,
},
};
});
test('Finds and returns a user by its ID', async () => {
const input = { id: 1 };
const res = await userService.findOne(input);
const id = 1;
const res = await userService.findOne(id);
expect(res).not.toBeNull();
expect(res).toMatchObject(user);
});
test('Fails to find a user with provided params', async () => {
const input = { id: 27 };
const res = await userService.findOne(input);
const id = 27;
const res = await userService.findOne(id);
expect(res).toBeNull();
});

View File

@ -1,6 +1,7 @@
'use strict';
const _ = require('lodash');
const { defaults } = require('lodash/fp');
const { stringIncludes } = require('@strapi/utils');
const { createUser, hasSuperAdminRole } = require('../domain/user');
const { password: passwordValidator } = require('../validation/common-validators');
@ -103,7 +104,7 @@ const updateById = async (id, attributes) => {
* @param {string} password - new password
*/
const resetPasswordByEmail = async (email, password) => {
const user = await findOne({ email });
const user = await strapi.query('admin::user').findOne({ where: { email }, populate: ['roles'] });
if (!user) {
throw new Error(`User not found for email: ${email}`);
@ -125,7 +126,7 @@ const resetPasswordByEmail = async (email, password) => {
* @param {int|string} userId user's id to look for
*/
const isLastSuperAdminUser = async userId => {
const user = await findOne({ id: userId }, ['roles']);
const user = await findOne(userId, ['roles']);
const superAdminRole = await getService('role').getSuperAdminWithUsersCount();
return superAdminRole.usersCount === 1 && hasSuperAdminRole(user);
@ -180,8 +181,8 @@ const register = async ({ registrationToken, userInfo }) => {
/**
* Find one user
*/
const findOne = async (where = {}, populate = ['roles']) => {
return strapi.query('admin::user').findOne({ where, populate });
const findOne = async (id, populate = ['roles']) => {
return strapi.entityService.findOne('admin::user', id, { populate });
};
/** Find many users (paginated)
@ -189,15 +190,8 @@ const findOne = async (where = {}, populate = ['roles']) => {
* @returns {Promise<user>}
*/
const findPage = async (query = {}) => {
const { page = 1, pageSize = 100, populate = ['roles'] } = query;
return strapi.query('admin::user').findPage({
where: query.filters,
_q: query._q,
populate,
page,
pageSize,
});
const enrichedQuery = defaults({ populate: ['roles'] }, query);
return strapi.entityService.findPage('admin::user', enrichedQuery);
};
/** Delete a user