2022-09-23 20:15:08 +03:00
|
|
|
import moment from 'moment-timezone';
|
2022-03-08 14:32:01 +03:00
|
|
|
import {Response} from 'miragejs';
|
2017-08-22 10:53:26 +03:00
|
|
|
import {dasherize} from '@ember/string';
|
2021-04-08 14:06:27 +03:00
|
|
|
import {extractFilterParam, paginateModelCollection} from '../utils';
|
2018-07-20 13:57:53 +03:00
|
|
|
import {isBlank, isEmpty} from '@ember/utils';
|
|
|
|
|
2018-11-08 16:43:58 +03:00
|
|
|
// NOTE: mirage requires Model objects when saving relationships, however the
|
|
|
|
// `attrs` on POST/PUT requests will contain POJOs for authors and tags so we
|
|
|
|
// need to replace them
|
|
|
|
function extractAuthors(postAttrs, users) {
|
|
|
|
return postAttrs.authors.map(author => users.find(author.id));
|
|
|
|
}
|
|
|
|
|
|
|
|
function extractTags(postAttrs, tags) {
|
|
|
|
return postAttrs.tags.map((requestTag) => {
|
|
|
|
let tag = tags.find(requestTag.id);
|
|
|
|
|
|
|
|
if (!tag) {
|
|
|
|
tag = tag.create(requestTag);
|
|
|
|
}
|
|
|
|
|
|
|
|
return tag;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-05-07 00:04:13 +03:00
|
|
|
export function getPosts({posts}, {queryParams}) {
|
|
|
|
let {filter, page, limit} = queryParams;
|
|
|
|
|
|
|
|
page = +page || 1;
|
|
|
|
limit = +limit || 15;
|
|
|
|
|
|
|
|
let statusFilter = extractFilterParam('status', filter);
|
2024-07-29 19:19:28 +03:00
|
|
|
let authorsFilter = extractFilterParam('authors', filter);
|
|
|
|
let visibilityFilter = extractFilterParam('visibility', filter);
|
2024-05-07 00:04:13 +03:00
|
|
|
|
|
|
|
let collection = posts.all().filter((post) => {
|
|
|
|
let matchesStatus = true;
|
2024-07-29 19:19:28 +03:00
|
|
|
let matchesAuthors = true;
|
|
|
|
let matchesVisibility = true;
|
2024-05-07 00:04:13 +03:00
|
|
|
|
|
|
|
if (!isEmpty(statusFilter)) {
|
|
|
|
matchesStatus = statusFilter.includes(post.status);
|
|
|
|
}
|
|
|
|
|
2024-07-29 19:19:28 +03:00
|
|
|
if (!isEmpty(authorsFilter)) {
|
|
|
|
matchesAuthors = authorsFilter.includes(post.authors.models[0].slug);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isEmpty(visibilityFilter)) {
|
|
|
|
matchesVisibility = visibilityFilter.includes(post.visibility);
|
|
|
|
}
|
|
|
|
|
|
|
|
return matchesStatus && matchesAuthors && matchesVisibility;
|
2024-05-07 00:04:13 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
return paginateModelCollection('posts', collection, page, limit);
|
|
|
|
}
|
|
|
|
|
2017-01-02 21:49:44 +03:00
|
|
|
export default function mockPosts(server) {
|
2018-11-08 16:43:58 +03:00
|
|
|
server.post('/posts', function ({posts, users, tags}) {
|
2017-01-02 21:49:44 +03:00
|
|
|
let attrs = this.normalizedRequestAttrs();
|
2018-03-13 14:17:29 +03:00
|
|
|
|
2018-11-08 16:43:58 +03:00
|
|
|
attrs.authors = extractAuthors(attrs, users);
|
|
|
|
attrs.tags = extractTags(attrs, tags);
|
2017-07-10 14:33:05 +03:00
|
|
|
|
2017-01-02 21:49:44 +03:00
|
|
|
if (isBlank(attrs.slug) && !isBlank(attrs.title)) {
|
|
|
|
attrs.slug = dasherize(attrs.title);
|
|
|
|
}
|
|
|
|
|
|
|
|
return posts.create(attrs);
|
|
|
|
});
|
|
|
|
|
2024-05-07 00:04:13 +03:00
|
|
|
server.get('/posts/', getPosts);
|
2017-01-02 21:49:44 +03:00
|
|
|
|
|
|
|
server.get('/posts/:id/', function ({posts}, {params}) {
|
|
|
|
let {id} = params;
|
|
|
|
let post = posts.find(id);
|
|
|
|
|
|
|
|
return post || new Response(404, {}, {
|
|
|
|
errors: [{
|
2019-03-25 14:29:14 +03:00
|
|
|
type: 'NotFoundError',
|
2017-01-02 21:49:44 +03:00
|
|
|
message: 'Post not found.'
|
|
|
|
}]
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-06-01 00:59:12 +03:00
|
|
|
server.put('/posts/:id/', function ({newsletters, posts, users, tags}, {params, queryParams}) {
|
2022-04-06 12:22:00 +03:00
|
|
|
const attrs = this.normalizedRequestAttrs();
|
|
|
|
const post = posts.find(params.id);
|
2017-03-14 06:27:57 +03:00
|
|
|
|
2018-11-08 16:43:58 +03:00
|
|
|
attrs.authors = extractAuthors(attrs, users);
|
|
|
|
attrs.tags = extractTags(attrs, tags);
|
2017-03-14 06:27:57 +03:00
|
|
|
|
2018-03-27 20:06:55 +03:00
|
|
|
attrs.updatedAt = moment.utc().toDate();
|
|
|
|
|
2022-06-01 00:59:12 +03:00
|
|
|
if (queryParams.newsletter) {
|
|
|
|
const newsletter = newsletters.findBy({slug: queryParams.newsletter});
|
2022-04-06 12:22:00 +03:00
|
|
|
post.newsletter = newsletter;
|
|
|
|
post.save();
|
|
|
|
}
|
|
|
|
|
2018-03-13 14:17:29 +03:00
|
|
|
return post.update(attrs);
|
2017-03-14 06:27:57 +03:00
|
|
|
});
|
2017-01-02 21:49:44 +03:00
|
|
|
|
|
|
|
server.del('/posts/:id/');
|
2024-07-16 20:56:15 +03:00
|
|
|
|
|
|
|
server.del('/posts/', function ({posts}, {queryParams}) {
|
|
|
|
let ids = extractFilterParam('id', queryParams.filter);
|
|
|
|
|
|
|
|
posts.find(ids).destroy();
|
|
|
|
});
|
|
|
|
|
2024-07-29 19:19:28 +03:00
|
|
|
server.post('/posts/:id/copy/', function ({posts}, {params}) {
|
|
|
|
let post = posts.find(params.id);
|
|
|
|
let attrs = post.attrs;
|
|
|
|
|
|
|
|
return posts.create(attrs);
|
|
|
|
});
|
|
|
|
|
2024-07-16 20:56:15 +03:00
|
|
|
server.put('/posts/bulk/', function ({tags}, {requestBody}) {
|
|
|
|
const bulk = JSON.parse(requestBody).bulk;
|
|
|
|
const action = bulk.action;
|
|
|
|
// const ids = extractFilterParam('id', queryParams.filter);
|
|
|
|
|
|
|
|
if (action === 'addTag') {
|
|
|
|
// create tag so we have an id from the server
|
|
|
|
const newTags = bulk.meta.tags;
|
|
|
|
|
|
|
|
// check applied tags to see if any new ones should be created
|
|
|
|
newTags.forEach((tag) => {
|
|
|
|
if (!tag.id) {
|
|
|
|
tags.create(tag);
|
|
|
|
}
|
|
|
|
});
|
2024-07-29 19:19:28 +03:00
|
|
|
// TODO: update the actual posts in the mock db if wanting to write tests where we navigate around (refresh model)
|
2024-07-16 20:56:15 +03:00
|
|
|
// const postsToUpdate = posts.find(ids);
|
|
|
|
// getting the posts is fine, but within this we CANNOT manipulate them (???) not even iterate with .forEach
|
|
|
|
}
|
|
|
|
});
|
2017-01-02 21:49:44 +03:00
|
|
|
}
|