2017-01-02 21:49:44 +03:00
|
|
|
/* eslint-disable max-statements-per-line */
|
2022-03-08 14:32:01 +03:00
|
|
|
import {Response} from 'miragejs';
|
2021-04-08 14:06:27 +03:00
|
|
|
import {isArray} from '@ember/array';
|
2017-01-02 21:49:44 +03:00
|
|
|
|
|
|
|
export function paginatedResponse(modelName) {
|
|
|
|
return function (schema, request) {
|
|
|
|
let page = +request.queryParams.page || 1;
|
2018-11-07 20:07:29 +03:00
|
|
|
let limit = request.queryParams.limit;
|
2018-03-13 14:17:29 +03:00
|
|
|
let collection = schema[modelName].all();
|
2017-01-02 21:49:44 +03:00
|
|
|
|
2018-11-07 20:07:29 +03:00
|
|
|
if (limit !== 'all') {
|
|
|
|
limit = +request.queryParams.limit || 15;
|
|
|
|
}
|
|
|
|
|
2018-03-13 14:17:29 +03:00
|
|
|
return paginateModelCollection(modelName, collection, page, limit);
|
2017-01-25 23:05:28 +03:00
|
|
|
};
|
|
|
|
}
|
2017-01-02 21:49:44 +03:00
|
|
|
|
2018-03-13 14:17:29 +03:00
|
|
|
export function paginateModelCollection(modelName, collection, page, limit) {
|
2017-01-25 23:05:28 +03:00
|
|
|
let pages, next, prev, models;
|
2017-01-02 21:49:44 +03:00
|
|
|
|
2017-01-25 23:05:28 +03:00
|
|
|
if (limit === 'all') {
|
|
|
|
pages = 1;
|
|
|
|
} else {
|
|
|
|
limit = +limit;
|
2017-01-02 21:49:44 +03:00
|
|
|
|
2017-01-25 23:05:28 +03:00
|
|
|
let start = (page - 1) * limit;
|
|
|
|
let end = start + limit;
|
2017-01-02 21:49:44 +03:00
|
|
|
|
2018-03-13 14:17:29 +03:00
|
|
|
pages = Math.ceil(collection.models.length / limit);
|
|
|
|
models = collection.models.slice(start, end);
|
2017-01-25 23:05:28 +03:00
|
|
|
|
|
|
|
if (start > 0) {
|
|
|
|
prev = page - 1;
|
|
|
|
}
|
|
|
|
|
2018-03-13 14:17:29 +03:00
|
|
|
if (end < collection.models.length) {
|
2017-01-25 23:05:28 +03:00
|
|
|
next = page + 1;
|
2017-01-02 21:49:44 +03:00
|
|
|
}
|
2017-01-25 23:05:28 +03:00
|
|
|
}
|
2017-01-02 21:49:44 +03:00
|
|
|
|
2018-03-13 14:17:29 +03:00
|
|
|
collection.meta = {
|
|
|
|
pagination: {
|
|
|
|
page,
|
|
|
|
limit,
|
|
|
|
pages,
|
|
|
|
total: collection.models.length,
|
|
|
|
next: next || null,
|
|
|
|
prev: prev || null
|
|
|
|
}
|
2017-01-02 21:49:44 +03:00
|
|
|
};
|
2018-03-13 14:17:29 +03:00
|
|
|
|
|
|
|
if (models) {
|
|
|
|
collection.models = models;
|
|
|
|
}
|
|
|
|
|
|
|
|
return collection;
|
2017-01-02 21:49:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
export function maintenanceResponse() {
|
|
|
|
return new Response(503, {}, {
|
|
|
|
errors: [{
|
2019-03-25 14:29:14 +03:00
|
|
|
type: 'Maintenance'
|
2017-01-02 21:49:44 +03:00
|
|
|
}]
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function versionMismatchResponse() {
|
|
|
|
return new Response(400, {}, {
|
|
|
|
errors: [{
|
2019-03-25 14:29:14 +03:00
|
|
|
type: 'VersionMismatchError'
|
2017-01-02 21:49:44 +03:00
|
|
|
}]
|
|
|
|
});
|
|
|
|
}
|
2021-04-08 14:06:27 +03:00
|
|
|
|
|
|
|
function normalizeBooleanParams(arr) {
|
|
|
|
if (!isArray(arr)) {
|
|
|
|
return arr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return arr.map((i) => {
|
|
|
|
if (i === 'true') {
|
|
|
|
return true;
|
|
|
|
} else if (i === 'false') {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function normalizeStringParams(arr) {
|
|
|
|
if (!isArray(arr)) {
|
|
|
|
return arr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return arr.map((i) => {
|
|
|
|
if (!i.replace) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
return i.replace(/^['"]|['"]$/g, '');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: use GQL to parse filter string?
|
2021-05-10 23:02:54 +03:00
|
|
|
export function extractFilterParam(param, filter = '') {
|
2021-04-08 14:06:27 +03:00
|
|
|
let filterRegex = new RegExp(`${param}:(.*?)(?:\\+|$)`);
|
|
|
|
let match;
|
|
|
|
|
|
|
|
let [, result] = filter.match(filterRegex) || [];
|
|
|
|
|
|
|
|
if (!result) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result.startsWith('[')) {
|
|
|
|
match = result.replace(/^\[|\]$/g, '').split(',');
|
|
|
|
} else {
|
|
|
|
match = [result];
|
|
|
|
}
|
|
|
|
|
|
|
|
return normalizeBooleanParams(normalizeStringParams(match));
|
|
|
|
}
|