2019-10-13 20:21:04 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const Controller = require('../controller');
|
|
|
|
|
2020-02-27 21:26:18 +01:00
|
|
|
const { contextSchema, nameSchema } = require('./context-schema');
|
|
|
|
const NameExistsError = require('../../error/name-exists-error');
|
|
|
|
const { handleErrors } = require('./util');
|
|
|
|
const extractUser = require('../../extract-user');
|
|
|
|
|
|
|
|
const {
|
|
|
|
CONTEXT_FIELD_CREATED,
|
|
|
|
CONTEXT_FIELD_UPDATED,
|
|
|
|
CONTEXT_FIELD_DELETED,
|
|
|
|
} = require('../../event-type');
|
|
|
|
|
|
|
|
const {
|
|
|
|
CREATE_CONTEXT_FIELD,
|
|
|
|
UPDATE_CONTEXT_FIELD,
|
|
|
|
DELETE_CONTEXT_FIELD,
|
|
|
|
} = require('../../permissions');
|
2019-10-13 20:21:04 +02:00
|
|
|
|
|
|
|
class ContextController extends Controller {
|
|
|
|
constructor(config) {
|
|
|
|
super(config);
|
2020-02-27 21:26:18 +01:00
|
|
|
this.logger = config.getLogger('/admin-api/feature.js');
|
|
|
|
this.eventStore = config.stores.eventStore;
|
|
|
|
this.store = config.stores.contextFieldStore;
|
|
|
|
|
2019-10-13 20:21:04 +02:00
|
|
|
this.get('/', this.getContextFields);
|
2020-02-27 21:26:18 +01:00
|
|
|
this.post('/', this.createContextField, CREATE_CONTEXT_FIELD);
|
|
|
|
this.get('/:contextField', this.getContextField);
|
|
|
|
this.put(
|
|
|
|
'/:contextField',
|
|
|
|
this.updateContextField,
|
2020-04-14 22:29:11 +02:00
|
|
|
UPDATE_CONTEXT_FIELD,
|
2020-02-27 21:26:18 +01:00
|
|
|
);
|
|
|
|
this.delete(
|
|
|
|
'/:contextField',
|
|
|
|
this.deleteContextField,
|
2020-04-14 22:29:11 +02:00
|
|
|
DELETE_CONTEXT_FIELD,
|
2020-02-27 21:26:18 +01:00
|
|
|
);
|
|
|
|
this.post('/validate', this.validate);
|
2019-10-13 20:21:04 +02:00
|
|
|
}
|
|
|
|
|
2020-02-27 21:26:18 +01:00
|
|
|
async getContextFields(req, res) {
|
|
|
|
const fields = await this.store.getAll();
|
2019-10-13 20:21:04 +02:00
|
|
|
res.status(200)
|
2020-02-27 21:26:18 +01:00
|
|
|
.json(fields)
|
2019-10-13 20:21:04 +02:00
|
|
|
.end();
|
|
|
|
}
|
2020-02-27 21:26:18 +01:00
|
|
|
|
|
|
|
async getContextField(req, res) {
|
|
|
|
try {
|
|
|
|
const name = req.params.contextField;
|
|
|
|
const contextField = await this.store.get(name);
|
|
|
|
res.json(contextField).end();
|
|
|
|
} catch (err) {
|
|
|
|
res.status(404).json({ error: 'Could not find context field' });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async createContextField(req, res) {
|
2020-04-14 22:29:11 +02:00
|
|
|
const { name } = req.body;
|
2020-02-27 21:26:18 +01:00
|
|
|
const userName = extractUser(req);
|
|
|
|
|
|
|
|
try {
|
|
|
|
await this.validateUniqueName(name);
|
|
|
|
const value = await contextSchema.validateAsync(req.body);
|
|
|
|
await this.eventStore.store({
|
|
|
|
type: CONTEXT_FIELD_CREATED,
|
|
|
|
createdBy: userName,
|
|
|
|
data: value,
|
|
|
|
});
|
|
|
|
res.status(201).end();
|
|
|
|
} catch (error) {
|
|
|
|
handleErrors(res, this.logger, error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async updateContextField(req, res) {
|
|
|
|
const name = req.params.contextField;
|
|
|
|
const userName = extractUser(req);
|
|
|
|
const updatedContextField = req.body;
|
|
|
|
|
|
|
|
updatedContextField.name = name;
|
|
|
|
|
|
|
|
try {
|
|
|
|
await this.store.get(name);
|
|
|
|
|
|
|
|
await contextSchema.validateAsync(updatedContextField);
|
|
|
|
await this.eventStore.store({
|
|
|
|
type: CONTEXT_FIELD_UPDATED,
|
|
|
|
createdBy: userName,
|
|
|
|
data: updatedContextField,
|
|
|
|
});
|
|
|
|
res.status(200).end();
|
|
|
|
} catch (error) {
|
|
|
|
handleErrors(res, this.logger, error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async deleteContextField(req, res) {
|
|
|
|
const name = req.params.contextField;
|
|
|
|
|
|
|
|
try {
|
|
|
|
await this.store.get(name);
|
|
|
|
await this.eventStore.store({
|
|
|
|
type: CONTEXT_FIELD_DELETED,
|
|
|
|
createdBy: extractUser(req),
|
|
|
|
data: { name },
|
|
|
|
});
|
|
|
|
res.status(200).end();
|
|
|
|
} catch (error) {
|
|
|
|
handleErrors(res, this.logger, error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async validateUniqueName(name) {
|
|
|
|
let msg;
|
|
|
|
try {
|
|
|
|
await this.store.get(name);
|
|
|
|
msg = 'A context field with that name already exist';
|
|
|
|
} catch (error) {
|
|
|
|
// No conflict, everything ok!
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Interntional throw here!
|
|
|
|
throw new NameExistsError(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
async validate(req, res) {
|
2020-04-14 22:29:11 +02:00
|
|
|
const { name } = req.body;
|
2020-02-27 21:26:18 +01:00
|
|
|
|
|
|
|
try {
|
|
|
|
await nameSchema.validateAsync({ name });
|
|
|
|
await this.validateUniqueName(name);
|
|
|
|
res.status(200).end();
|
|
|
|
} catch (error) {
|
|
|
|
handleErrors(res, this.logger, error);
|
|
|
|
}
|
|
|
|
}
|
2019-10-13 20:21:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = ContextController;
|