All files / src/lib/services context-service.ts

100% Statements 29/29
100% Branches 0/0
100% Functions 8/8
100% Lines 29/29

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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125                      60x 60x           60x                                           147x 147x 147x 147x       3x       6x               12x 10x     6x 6x                       6x 6x     3x 3x                 3x     2x 2x                     16x 16x 4x     12x       4x       5x 4x     60x 60x  
import { Logger } from '../logger';
import {
    IContextField,
    IContextFieldDto,
    IContextFieldStore,
} from '../types/stores/context-field-store';
import { IEventStore } from '../types/stores/event-store';
import { IProjectStore } from '../types/stores/project-store';
import { IUnleashStores } from '../types/stores';
import { IUnleashConfig } from '../types/option';
 
const { contextSchema, nameSchema } = require('./context-schema');
const NameExistsError = require('../error/name-exists-error');
 
const {
    CONTEXT_FIELD_CREATED,
    CONTEXT_FIELD_UPDATED,
    CONTEXT_FIELD_DELETED,
} = require('../types/events');
 
class ContextService {
    private projectStore: IProjectStore;
 
    private eventStore: IEventStore;
 
    private contextFieldStore: IContextFieldStore;
 
    private logger: Logger;
 
    constructor(
        {
            projectStore,
            eventStore,
            contextFieldStore,
        }: Pick<
            IUnleashStores,
            'projectStore' | 'eventStore' | 'contextFieldStore'
        >,
        { getLogger }: Pick<IUnleashConfig, 'getLogger'>,
    ) {
        this.projectStore = projectStore;
        this.eventStore = eventStore;
        this.contextFieldStore = contextFieldStore;
        this.logger = getLogger('services/context-service.js');
    }
 
    async getAll(): Promise<IContextField[]> {
        return this.contextFieldStore.getAll();
    }
 
    async getContextField(name: string): Promise<IContextField> {
        return this.contextFieldStore.get(name);
    }
 
    async createContextField(
        value: IContextFieldDto,
        userName: string,
    ): Promise<void> {
        // 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: IContextFieldDto,
        userName: string,
    ): Promise<void> {
        // 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: string, userName: string): Promise<void> {
        // 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,
    }: Pick<IContextFieldDto, 'name'>): Promise<void> {
        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: string): Promise<void> {
        await nameSchema.validateAsync({ name });
        await this.validateUniqueName({ name });
    }
}
export default ContextService;
module.exports = ContextService;