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));
|
|
|
|
}
|
2022-10-31 15:01:39 +03:00
|
|
|
|
|
|
|
export function hasInvalidPermissions(allowedRoles) {
|
|
|
|
const {schema, request} = this;
|
|
|
|
|
|
|
|
// always allow dev requests through - the logged in user will be real so
|
|
|
|
// we can't check against it in the mocked db
|
|
|
|
if (!request.requestHeaders['X-Test-User']) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const invalidPermsResponse = new Response(403, {}, {
|
|
|
|
errors: [{
|
|
|
|
type: 'NoPermissionError',
|
|
|
|
message: 'You do not have permission to perform this action'
|
|
|
|
}]
|
|
|
|
});
|
|
|
|
|
|
|
|
const user = schema.users.find(request.requestHeaders['X-Test-User']);
|
|
|
|
const adminRoles = user.roles.filter(role => allowedRoles.includes(role.name));
|
|
|
|
|
|
|
|
if (adminRoles.length === 0) {
|
|
|
|
return invalidPermsResponse;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function withPermissionsCheck(allowedRoles, fn) {
|
|
|
|
return function () {
|
|
|
|
const boundPermsCheck = hasInvalidPermissions.bind(this);
|
|
|
|
const boundFn = fn.bind(this);
|
|
|
|
return boundPermsCheck(allowedRoles) || boundFn(...arguments);
|
|
|
|
};
|
|
|
|
}
|