2020-05-13 12:27:46 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const _ = require('lodash');
|
2020-07-15 15:46:59 +02:00
|
|
|
const {
|
|
|
|
validateUserCreationInput,
|
|
|
|
validateUserUpdateInput,
|
|
|
|
validateUsersDeleteInput,
|
|
|
|
} = require('../validation/user');
|
2020-05-13 12:27:46 +02:00
|
|
|
|
2021-06-29 16:27:35 +02:00
|
|
|
const { getService } = require('../utils');
|
|
|
|
|
2020-05-13 12:27:46 +02:00
|
|
|
module.exports = {
|
|
|
|
async create(ctx) {
|
|
|
|
const { body } = ctx.request;
|
|
|
|
|
2020-05-14 11:57:43 +02:00
|
|
|
try {
|
|
|
|
await validateUserCreationInput(body);
|
|
|
|
} catch (err) {
|
2020-05-15 09:34:49 +02:00
|
|
|
return ctx.badRequest('ValidationError', err);
|
2020-05-13 12:27:46 +02:00
|
|
|
}
|
|
|
|
|
2021-02-05 16:35:47 +01:00
|
|
|
const attributes = _.pick(body, [
|
|
|
|
'firstname',
|
|
|
|
'lastname',
|
|
|
|
'email',
|
|
|
|
'roles',
|
|
|
|
'preferedLanguage',
|
|
|
|
]);
|
2020-05-13 12:27:46 +02:00
|
|
|
|
2021-07-28 15:32:21 +02:00
|
|
|
const userAlreadyExists = await getService('user').exists({
|
2020-05-14 11:57:43 +02:00
|
|
|
email: attributes.email,
|
2020-05-13 12:27:46 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
if (userAlreadyExists) {
|
2020-05-14 18:54:52 +02:00
|
|
|
return ctx.badRequest('Email already taken');
|
2020-05-13 12:27:46 +02:00
|
|
|
}
|
|
|
|
|
2021-07-28 15:32:21 +02:00
|
|
|
const createdUser = await getService('user').create(attributes);
|
2020-05-13 12:27:46 +02:00
|
|
|
|
2021-07-28 15:32:21 +02:00
|
|
|
const userInfo = getService('user').sanitizeUser(createdUser);
|
2020-05-18 16:07:37 +02:00
|
|
|
|
2020-05-13 12:27:46 +02:00
|
|
|
// Send 201 created
|
2020-05-18 16:07:37 +02:00
|
|
|
ctx.created({ data: userInfo });
|
2020-05-13 12:27:46 +02:00
|
|
|
},
|
2020-05-19 10:55:52 +02:00
|
|
|
|
|
|
|
async find(ctx) {
|
2021-07-28 15:32:21 +02:00
|
|
|
const userService = getService('user');
|
|
|
|
|
2021-07-28 21:03:32 +02:00
|
|
|
const { results, pagination } = await userService.findPage(ctx.query);
|
2020-05-19 11:06:07 +02:00
|
|
|
|
2020-05-29 11:41:53 +02:00
|
|
|
ctx.body = {
|
2020-05-19 16:10:53 +02:00
|
|
|
data: {
|
2021-07-28 15:32:21 +02:00
|
|
|
results: results.map(user => userService.sanitizeUser(user)),
|
2020-05-19 16:10:53 +02:00
|
|
|
pagination,
|
|
|
|
},
|
2020-05-19 11:06:07 +02:00
|
|
|
};
|
2020-05-19 10:55:52 +02:00
|
|
|
},
|
2020-05-27 16:06:15 +02:00
|
|
|
|
2020-05-28 14:03:48 +02:00
|
|
|
async findOne(ctx) {
|
|
|
|
const { id } = ctx.params;
|
|
|
|
|
2021-07-28 15:32:21 +02:00
|
|
|
const user = await getService('user').findOne({ id });
|
2020-05-28 14:03:48 +02:00
|
|
|
|
|
|
|
if (!user) {
|
2020-06-04 10:25:02 +02:00
|
|
|
return ctx.notFound('User does not exist');
|
2020-05-28 14:03:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx.body = {
|
2021-07-28 15:32:21 +02:00
|
|
|
data: getService('user').sanitizeUser(user),
|
2020-05-28 14:03:48 +02:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2020-05-27 16:06:15 +02:00
|
|
|
async update(ctx) {
|
|
|
|
const { id } = ctx.params;
|
|
|
|
const { body: input } = ctx.request;
|
|
|
|
|
|
|
|
try {
|
|
|
|
await validateUserUpdateInput(input);
|
|
|
|
} catch (err) {
|
|
|
|
return ctx.badRequest('ValidationError', err);
|
|
|
|
}
|
|
|
|
|
2020-06-04 12:34:22 +02:00
|
|
|
if (_.has(input, 'email')) {
|
2021-07-28 15:32:21 +02:00
|
|
|
const uniqueEmailCheck = await getService('user').exists({
|
2021-06-22 17:13:11 +02:00
|
|
|
id: { $ne: id },
|
2020-06-04 12:34:22 +02:00
|
|
|
email: input.email,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (uniqueEmailCheck) {
|
|
|
|
return ctx.badRequest('A user with this email address already exists');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-28 15:32:21 +02:00
|
|
|
const updatedUser = await getService('user').updateById(id, input);
|
2020-05-27 16:06:15 +02:00
|
|
|
|
2020-06-04 10:25:02 +02:00
|
|
|
if (!updatedUser) {
|
2020-06-04 10:48:02 +02:00
|
|
|
return ctx.notFound('User does not exist');
|
2020-05-27 16:06:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx.body = {
|
2021-07-28 15:32:21 +02:00
|
|
|
data: getService('user').sanitizeUser(updatedUser),
|
2020-05-27 16:06:15 +02:00
|
|
|
};
|
|
|
|
},
|
2020-06-18 11:40:50 +02:00
|
|
|
|
2020-07-15 15:46:59 +02:00
|
|
|
async deleteOne(ctx) {
|
2020-06-18 11:40:50 +02:00
|
|
|
const { id } = ctx.params;
|
|
|
|
|
2021-07-28 15:32:21 +02:00
|
|
|
const deletedUser = await getService('user').deleteById(id);
|
2020-06-18 11:40:50 +02:00
|
|
|
|
|
|
|
if (!deletedUser) {
|
|
|
|
return ctx.notFound('User not found');
|
|
|
|
}
|
|
|
|
|
|
|
|
return ctx.deleted({
|
2021-07-28 15:32:21 +02:00
|
|
|
data: getService('user').sanitizeUser(deletedUser),
|
2020-06-18 11:40:50 +02:00
|
|
|
});
|
|
|
|
},
|
2020-07-15 15:46:59 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete several users
|
|
|
|
* @param {KoaContext} ctx - koa context
|
|
|
|
*/
|
|
|
|
async deleteMany(ctx) {
|
|
|
|
const { body } = ctx.request;
|
|
|
|
try {
|
|
|
|
await validateUsersDeleteInput(body);
|
|
|
|
} catch (err) {
|
|
|
|
return ctx.badRequest('ValidationError', err);
|
|
|
|
}
|
|
|
|
|
2021-06-29 16:27:35 +02:00
|
|
|
const users = await getService('user').deleteByIds(body.ids);
|
|
|
|
|
2021-07-28 15:32:21 +02:00
|
|
|
const sanitizedUsers = users.map(getService('user').sanitizeUser);
|
2020-07-15 15:46:59 +02:00
|
|
|
|
|
|
|
return ctx.deleted({
|
|
|
|
data: sanitizedUsers,
|
|
|
|
});
|
|
|
|
},
|
2020-05-13 12:27:46 +02:00
|
|
|
};
|