2021-02-12 10:23:43 +01:00
|
|
|
'use strict';
|
|
|
|
|
2021-04-20 12:32:02 +02:00
|
|
|
import ContextFieldStore from '../db/context-field-store';
|
|
|
|
import EventStore from '../db/event-store';
|
|
|
|
import ProjectStore from '../db/project-store';
|
|
|
|
import { Logger } from '../logger';
|
|
|
|
|
2021-02-12 10:23:43 +01:00
|
|
|
const { contextSchema, nameSchema } = require('./context-schema');
|
|
|
|
const NameExistsError = require('../error/name-exists-error');
|
|
|
|
|
|
|
|
const {
|
|
|
|
CONTEXT_FIELD_CREATED,
|
|
|
|
CONTEXT_FIELD_UPDATED,
|
|
|
|
CONTEXT_FIELD_DELETED,
|
2021-04-29 10:29:39 +02:00
|
|
|
} = require('../types/events');
|
2021-02-12 10:23:43 +01:00
|
|
|
|
|
|
|
class ContextService {
|
2021-04-20 12:32:02 +02:00
|
|
|
private projectStore: ProjectStore;
|
|
|
|
|
|
|
|
private eventStore: EventStore;
|
|
|
|
|
|
|
|
private contextFieldStore: ContextFieldStore;
|
|
|
|
|
|
|
|
private logger: Logger;
|
|
|
|
|
2021-02-12 10:23:43 +01:00
|
|
|
constructor(
|
|
|
|
{ projectStore, eventStore, contextFieldStore },
|
|
|
|
{ getLogger },
|
|
|
|
) {
|
|
|
|
this.projectStore = projectStore;
|
|
|
|
this.eventStore = eventStore;
|
|
|
|
this.contextFieldStore = contextFieldStore;
|
|
|
|
this.logger = getLogger('services/context-service.js');
|
|
|
|
}
|
|
|
|
|
|
|
|
async getAll() {
|
|
|
|
return this.contextFieldStore.getAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
async getContextField(name) {
|
|
|
|
return this.contextFieldStore.get(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
async createContextField(value, userName) {
|
|
|
|
// validations
|
|
|
|
await this.validateUniqueName(value);
|
|
|
|
const contextField = await contextSchema.validateAsync(value);
|
|
|
|
|
|
|
|
// creations
|
|
|
|
await this.contextFieldStore.create(value);
|
|
|
|
await this.eventStore.store({
|
|
|
|
type: CONTEXT_FIELD_CREATED,
|
|
|
|
createdBy: userName,
|
|
|
|
data: contextField,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async updateContextField(updatedContextField, userName) {
|
|
|
|
// validations
|
|
|
|
await this.contextFieldStore.get(updatedContextField.name);
|
|
|
|
const value = await contextSchema.validateAsync(updatedContextField);
|
|
|
|
|
|
|
|
// update
|
|
|
|
await this.contextFieldStore.update(value);
|
|
|
|
await this.eventStore.store({
|
|
|
|
type: CONTEXT_FIELD_UPDATED,
|
|
|
|
createdBy: userName,
|
|
|
|
data: value,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async deleteContextField(name, userName) {
|
|
|
|
// validate existence
|
|
|
|
await this.contextFieldStore.get(name);
|
|
|
|
|
|
|
|
// delete
|
|
|
|
await this.contextFieldStore.delete(name);
|
|
|
|
await this.eventStore.store({
|
|
|
|
type: CONTEXT_FIELD_DELETED,
|
|
|
|
createdBy: userName,
|
|
|
|
data: { name },
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async validateUniqueName({ name }) {
|
|
|
|
let msg;
|
|
|
|
try {
|
|
|
|
await this.contextFieldStore.get(name);
|
|
|
|
msg = 'A context field with that name already exist';
|
|
|
|
} catch (error) {
|
|
|
|
// No conflict, everything ok!
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Intentional throw here!
|
|
|
|
throw new NameExistsError(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
async validateName(name) {
|
|
|
|
await nameSchema.validateAsync({ name });
|
|
|
|
await this.validateUniqueName({ name });
|
|
|
|
}
|
|
|
|
}
|
2021-04-20 12:32:02 +02:00
|
|
|
export default ContextService;
|
2021-02-12 10:23:43 +01:00
|
|
|
module.exports = ContextService;
|