mirror of
https://github.com/Unleash/unleash.git
synced 2024-10-18 20:09:08 +02:00
c17a1980a2
This simplifies stores to just be storage interaction, they no longer react to events. Controllers now call services and awaits the result from the call. When the service calls are returned the database is updated. This simplifies testing dramatically, cause you know that your state is updated when returned from a call, rather than hoping the store has picked up the event (which really was a command) and reacted to it. Events are still emitted from eventStore, so other parts of the app can react to events as they're being sent out. As part of the move to services, we now also emit an application-created event when we see a new client application. Fixes: #685 Fixes: #595
77 lines
2.2 KiB
JavaScript
77 lines
2.2 KiB
JavaScript
const NameExistsError = require('../error/name-exists-error');
|
|
const NotFoundError = require('../error/notfound-error');
|
|
const { tagTypeSchema } = require('./tag-type-schema');
|
|
const {
|
|
TAG_TYPE_CREATED,
|
|
TAG_TYPE_DELETED,
|
|
TAG_TYPE_UPDATED,
|
|
} = require('../event-type');
|
|
|
|
class TagTypeService {
|
|
constructor({ tagTypeStore, eventStore }, { getLogger }) {
|
|
this.tagTypeStore = tagTypeStore;
|
|
this.eventStore = eventStore;
|
|
this.logger = getLogger('services/tag-type-service.js');
|
|
}
|
|
|
|
async getAll() {
|
|
return this.tagTypeStore.getAll();
|
|
}
|
|
|
|
async getTagType(name) {
|
|
return this.tagTypeStore.getTagType(name);
|
|
}
|
|
|
|
async createTagType(newTagType, userName) {
|
|
const data = await tagTypeSchema.validateAsync(newTagType);
|
|
await this.validateUnique(newTagType);
|
|
await this.tagTypeStore.createTagType(data);
|
|
await this.eventStore.store({
|
|
type: TAG_TYPE_CREATED,
|
|
createdBy: userName || 'unleash-system',
|
|
data,
|
|
});
|
|
return data;
|
|
}
|
|
|
|
async validateUnique({ name }) {
|
|
try {
|
|
await this.tagTypeStore.getTagType(name);
|
|
} catch (err) {
|
|
if (err instanceof NotFoundError) {
|
|
return;
|
|
}
|
|
}
|
|
throw new NameExistsError(
|
|
`There already exists a tag-type with the name ${name}`,
|
|
);
|
|
}
|
|
|
|
async validate(tagType) {
|
|
await tagTypeSchema.validateAsync(tagType);
|
|
await this.validateUnique(tagType);
|
|
}
|
|
|
|
async deleteTagType(name, userName) {
|
|
await this.tagTypeStore.deleteTagType(name);
|
|
await this.eventStore.store({
|
|
type: TAG_TYPE_DELETED,
|
|
createdBy: userName || 'unleash-system',
|
|
data: { name },
|
|
});
|
|
}
|
|
|
|
async updateTagType(updatedTagType, userName) {
|
|
const data = await tagTypeSchema.validateAsync(updatedTagType);
|
|
await this.tagTypeStore.updateTagType(data);
|
|
await this.eventStore.store({
|
|
type: TAG_TYPE_UPDATED,
|
|
createdBy: userName || 'unleash-system',
|
|
data,
|
|
});
|
|
return data;
|
|
}
|
|
}
|
|
|
|
module.exports = TagTypeService;
|