2022-10-31 15:01:39 +03:00
|
|
|
import {paginatedResponse, withPermissionsCheck} from '../utils';
|
|
|
|
|
|
|
|
const ALLOWED_WRITE_ROLES = [
|
|
|
|
'Owner',
|
|
|
|
'Administrator'
|
|
|
|
];
|
|
|
|
const ALLOWED_READ_ROLES = [
|
|
|
|
'Owner',
|
|
|
|
'Administrator',
|
|
|
|
'Editor',
|
|
|
|
'Author'
|
|
|
|
];
|
2022-05-11 20:11:54 +03:00
|
|
|
|
|
|
|
export default function mockTiers(server) {
|
2022-10-31 15:01:39 +03:00
|
|
|
// CREATE
|
|
|
|
server.post('/tiers/', withPermissionsCheck(ALLOWED_WRITE_ROLES, function ({tiers}) {
|
|
|
|
const attrs = this.normalizedRequestAttrs();
|
|
|
|
return tiers.create(attrs);
|
|
|
|
}));
|
2022-05-11 20:11:54 +03:00
|
|
|
|
2022-10-31 15:01:39 +03:00
|
|
|
// READ
|
|
|
|
server.get('/tiers/', withPermissionsCheck(ALLOWED_READ_ROLES, paginatedResponse('tiers')));
|
2022-05-11 20:11:54 +03:00
|
|
|
|
2022-10-31 15:01:39 +03:00
|
|
|
server.get('/tiers/:id/', withPermissionsCheck(ALLOWED_READ_ROLES, function ({tiers}, {params}) {
|
2022-05-11 20:11:54 +03:00
|
|
|
let {id} = params;
|
|
|
|
let tier = tiers.find(id);
|
|
|
|
|
|
|
|
return tier || new Response(404, {}, {
|
|
|
|
errors: [{
|
|
|
|
type: 'NotFoundError',
|
|
|
|
message: 'Tier not found.'
|
|
|
|
}]
|
|
|
|
});
|
2022-10-31 15:01:39 +03:00
|
|
|
}));
|
2022-05-11 20:11:54 +03:00
|
|
|
|
2022-10-31 15:01:39 +03:00
|
|
|
// UPDATE
|
|
|
|
server.put('/tiers/:id/', withPermissionsCheck(ALLOWED_WRITE_ROLES, function ({tiers}, {params}) {
|
2022-05-11 20:11:54 +03:00
|
|
|
const attrs = this.normalizedRequestAttrs();
|
|
|
|
const tier = tiers.find(params.id);
|
|
|
|
|
|
|
|
tier.update(attrs);
|
|
|
|
|
|
|
|
return tier.save();
|
2022-10-31 15:01:39 +03:00
|
|
|
}));
|
2022-05-11 20:11:54 +03:00
|
|
|
|
2022-10-31 15:01:39 +03:00
|
|
|
// DELETE
|
|
|
|
server.del('/tiers/:id/', withPermissionsCheck(ALLOWED_WRITE_ROLES, function (schema, request) {
|
|
|
|
const id = request.params.id;
|
|
|
|
schema.tiers.find(id).destroy();
|
|
|
|
}));
|
2022-05-11 20:11:54 +03:00
|
|
|
}
|