"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "PrinterGroupService", {
    enumerable: true,
    get: function() {
        return PrinterGroupService;
    }
});
const _baseservice = require("./base.service");
const _printergroupentity = require("../../entities/printer-group.entity");
const _printergroupdto = require("../interfaces/printer-group.dto");
const _groupentity = require("../../entities/group.entity");
const _classvalidator = require("class-validator");
const _runtimeexceptions = require("../../exceptions/runtime.exceptions");
class PrinterGroupService extends (0, _baseservice.BaseService)(_printergroupentity.PrinterGroup, _printergroupdto.PrinterGroupDto) {
    groupRepository;
    constructor({ typeormService }){
        super({
            typeormService
        });
        this.groupRepository = typeormService.getDataSource().getRepository(_groupentity.Group);
    }
    async listGroups() {
        const groups = await this.groupRepository.find();
        const groupListing = {};
        for (const group of groups){
            groupListing[group.id] = {
                id: group.id,
                name: group.name,
                printers: []
            };
        }
        for (const group of groups){
            groupListing[group.id].printers = await this.repository.findBy({
                groupId: group.id
            });
        }
        return Object.values(groupListing);
    }
    async getGroup(groupId) {
        const group = await this.groupRepository.findOneBy({
            id: groupId
        });
        if (!group) {
            throw new _runtimeexceptions.NotFoundException("Group does not exist");
        }
        return group;
    }
    async getGroupWithPrinters(groupId) {
        const group = await this.getGroup(groupId);
        const printerGroups = await this.repository.findBy({
            groupId: group.id
        });
        return {
            id: group.id,
            name: group.name,
            printers: printerGroups
        };
    }
    async createGroup(dto) {
        if (dto.id) {
            delete dto.id;
        }
        await (0, _classvalidator.validate)(dto);
        const entity = this.groupRepository.create(dto);
        await (0, _classvalidator.validate)(entity);
        const group = await this.groupRepository.save(entity);
        return await this.getGroupWithPrinters(group.id);
    }
    async updateGroupName(groupId, name) {
        const entity = await this.getGroup(groupId);
        const updateDto = {
            name
        };
        await (0, _classvalidator.validate)(updateDto);
        await (0, _classvalidator.validate)(Object.assign(entity, updateDto));
        await this.groupRepository.update(entity.id, updateDto);
    }
    async deleteGroup(groupId) {
        const group = await this.getGroup(groupId);
        await this.groupRepository.delete({
            id: group.id
        });
    }
    async addPrinterToGroup(groupId, printerId) {
        const group = await this.getGroup(groupId);
        const alreadyExisting = await this.repository.findOneBy({
            groupId: group.id,
            printerId
        });
        if (alreadyExisting) return alreadyExisting;
        return await this.create({
            groupId: group.id,
            printerId
        });
    }
    async removePrinterFromGroup(groupId, printerId) {
        await this.getGroup(groupId);
        await this.repository.delete({
            groupId,
            printerId
        });
    }
    toDto(entity) {
        return {
            printerId: entity.printerId,
            groupId: entity.groupId
        };
    }
}

//# sourceMappingURL=printer-group.service.js.map