2017-10-13 12:39:49 +03:00
|
|
|
import {Response} from 'ember-cli-mirage';
|
2018-03-13 14:17:29 +03:00
|
|
|
import {paginateModelCollection} from '../utils';
|
2017-03-08 21:21:35 +03:00
|
|
|
|
2017-01-02 21:49:44 +03:00
|
|
|
export default function mockUsers(server) {
|
|
|
|
// /users/me = Always return the user with ID=1
|
|
|
|
server.get('/users/me/', function ({users}) {
|
2017-10-13 12:39:49 +03:00
|
|
|
let user = users.find(1);
|
|
|
|
|
|
|
|
if (user) {
|
|
|
|
return user;
|
|
|
|
} else {
|
|
|
|
return new Response(404, {}, {errors: [
|
|
|
|
{message: 'Not found', errorType: 'NotFoundError'}
|
|
|
|
]});
|
|
|
|
}
|
2017-01-02 21:49:44 +03:00
|
|
|
});
|
|
|
|
|
2017-03-08 21:21:35 +03:00
|
|
|
server.get('/users/', function ({users}, {queryParams}) {
|
|
|
|
let page = +queryParams.page || 1;
|
|
|
|
|
|
|
|
// NOTE: this is naive and only set up to work with queries that are
|
|
|
|
// actually used - if you use a different filter in the app, add it here!
|
2018-03-13 14:17:29 +03:00
|
|
|
let collection = users.where(function (user) {
|
2017-03-08 21:21:35 +03:00
|
|
|
let statusMatch = true;
|
|
|
|
|
|
|
|
if (queryParams.filter === 'status:-inactive') {
|
|
|
|
statusMatch = user.status !== 'inactive';
|
|
|
|
} else if (queryParams.filter === 'status:inactive') {
|
|
|
|
statusMatch = user.status === 'inactive';
|
|
|
|
} else if (queryParams.status && queryParams.status !== 'all') {
|
|
|
|
statusMatch = user.status === queryParams.status;
|
|
|
|
}
|
|
|
|
|
|
|
|
return statusMatch;
|
|
|
|
});
|
|
|
|
|
2018-03-13 14:17:29 +03:00
|
|
|
return paginateModelCollection('users', collection, page, queryParams.limit);
|
2017-03-08 21:21:35 +03:00
|
|
|
});
|
2017-01-02 21:49:44 +03:00
|
|
|
|
|
|
|
server.get('/users/slug/:slug/', function ({users}, {params, queryParams}) {
|
|
|
|
let user = users.findBy({slug: params.slug});
|
|
|
|
user.postCount = queryParams.include.match(/count\.posts/);
|
|
|
|
return user;
|
|
|
|
});
|
|
|
|
|
|
|
|
server.get('/users/:id', function ({users}, {params, queryParams}) {
|
|
|
|
let user = users.find(params.id);
|
|
|
|
user.postCount = queryParams.include.match(/count\.posts/);
|
|
|
|
return user;
|
|
|
|
});
|
|
|
|
|
|
|
|
server.put('/users/:id/', function ({users}, {params}) {
|
|
|
|
let {id} = params;
|
|
|
|
|
|
|
|
if (id === 'password') {
|
|
|
|
return {
|
|
|
|
password: [{message: 'Password changed successfully.'}]
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
let attrs = this.normalizedRequestAttrs();
|
|
|
|
|
2017-03-08 21:21:35 +03:00
|
|
|
// TODO: why is our custom serializer causing .update to throw
|
|
|
|
// children.update is not a function?
|
|
|
|
// https://github.com/samselikoff/ember-cli-mirage/issues/964
|
|
|
|
delete attrs.roles;
|
|
|
|
|
2017-01-02 21:49:44 +03:00
|
|
|
return users.find(id).update(attrs);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
server.del('/users/:id/');
|
|
|
|
}
|