"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.BoardSubscriber = void 0;
const typeorm_1 = require("typeorm");
const uuid_1 = require("uuid");
const board_service_1 = require("@things-factory/board-service");
const operato_license_checker_1 = require("@things-factory/operato-license-checker");
const license_error_1 = require("../errors/license-error");
const board_secret_1 = require("../service/board-secret/board-secret");
let BoardSubscriber = class BoardSubscriber {
    listenTo() {
        return board_service_1.Board;
    }
    /**
     * Called before board insertion.
     */
    async afterInsert(event) {
        var entityManager = event.manager;
        if ((0, operato_license_checker_1.getValidity)()) {
            //target
            //1. check board count is over licensed quantity or not
            var board = event.entity;
            var domain = event.entity.domain;
            var count = (await entityManager
                .createQueryBuilder()
                .select('board')
                .from(board_secret_1.BoardSecret, 'secret')
                .where('domain_id = :domainId', { domainId: domain.id })
                .getCount()) + 1;
            //2. create new token to board and save board-secret
            if ((0, operato_license_checker_1.checkTarget)(count)) {
                var board = event.entity;
                var boardSecret = new board_secret_1.BoardSecret();
                boardSecret.id = (0, uuid_1.v4)();
                boardSecret.targetId = board.id;
                boardSecret.targetType = 'board';
                boardSecret.domain = board.domain;
                var hash = (0, operato_license_checker_1.doSomething)({ secId: boardSecret.id, id: board.id });
                boardSecret.secret = hash;
                await entityManager.save(boardSecret);
            }
            else {
                throw new license_error_1.LicenseError({
                    errorCode: license_error_1.LicenseError.ERROR_CODES.OVER_LIMIT
                });
            }
        }
    }
    /**
     * Called after entity is removed.
     */
    async afterRemove(event) {
        var entityManager = event.manager;
        await entityManager.query(`delete 
             from board_secrets
            where target_id not in (select id from boards)`);
    }
};
BoardSubscriber = __decorate([
    (0, typeorm_1.EventSubscriber)()
], BoardSubscriber);
exports.BoardSubscriber = BoardSubscriber;
//# sourceMappingURL=board-subscriber.js.map