All files / src/lib/services tag-type-service.ts

100% Statements 27/27
57.14% Branches 4/7
100% Functions 8/8
100% Lines 27/27

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 10162x   62x     62x                     62x                           162x 162x 162x       2x       8x             15x     14x 13x 13x         13x       27x 27x 3x       24x       2x 1x       1x 1x                     2x 1x 1x         1x       62x  
import NameExistsError from '../error/name-exists-error';
 
import { tagTypeSchema } from './tag-type-schema';
 
import { IUnleashStores } from '../types/stores';
import {
    TAG_TYPE_CREATED,
    TAG_TYPE_DELETED,
    TAG_TYPE_UPDATED,
} from '../types/events';
 
import { Logger } from '../logger';
import { ITagType, ITagTypeStore } from '../types/stores/tag-type-store';
import { IEventStore } from '../types/stores/event-store';
import { IUnleashConfig } from '../types/option';
 
export default class TagTypeService {
    private tagTypeStore: ITagTypeStore;
 
    private eventStore: IEventStore;
 
    private logger: Logger;
 
    constructor(
        {
            tagTypeStore,
            eventStore,
        }: Pick<IUnleashStores, 'tagTypeStore' | 'eventStore'>,
        { getLogger }: Pick<IUnleashConfig, 'getLogger'>,
    ) {
        this.tagTypeStore = tagTypeStore;
        this.eventStore = eventStore;
        this.logger = getLogger('services/tag-type-service.js');
    }
 
    async getAll(): Promise<ITagType[]> {
        return this.tagTypeStore.getAll();
    }
 
    async getTagType(name: string): Promise<ITagType> {
        return this.tagTypeStore.get(name);
    }
 
    async createTagType(
        newTagType: ITagType,
        userName: string,
    ): Promise<ITagType> {
        const data = (await tagTypeSchema.validateAsync(
            newTagType,
        )) as ITagType;
        await this.validateUnique(data);
        await this.tagTypeStore.createTagType(data);
        await this.eventStore.store({
            type: TAG_TYPE_CREATED,
            createdBy: userName || 'unleash-system',
            data,
        });
        return data;
    }
 
    async validateUnique({ name }: Pick<ITagType, 'name'>): Promise<boolean> {
        const exists = await this.tagTypeStore.exists(name);
        if (exists) {
            throw new NameExistsError(
                `There already exists a tag-type with the name ${name}`,
            );
        }
        return Promise.resolve(true);
    }
 
    async validate(tagType: ITagType): Promise<void> {
        await tagTypeSchema.validateAsync(tagType);
        await this.validateUnique(tagType);
    }
 
    async deleteTagType(name: string, userName: string): Promise<void> {
        await this.tagTypeStore.delete(name);
        await this.eventStore.store({
            type: TAG_TYPE_DELETED,
            createdBy: userName || 'unleash-system',
            data: { name },
        });
    }
 
    async updateTagType(
        updatedTagType: ITagType,
        userName: string,
    ): Promise<ITagType> {
        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;