"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "UserRoleService", {
    enumerable: true,
    get: function() {
        return UserRoleService;
    }
});
const _baseservice = require("./base.service");
const _userroleentity = require("../../entities/user-role.entity");
const _userroledto = require("../interfaces/user-role.dto");
const _typeorm = require("typeorm");
class UserRoleService extends (0, _baseservice.BaseService)(_userroleentity.UserRole, _userroledto.UserRoleDto) {
    toDto(entity) {
        return {
            id: entity.id,
            roleId: entity.roleId,
            userId: entity.userId,
            createdAt: entity.createdAt
        };
    }
    async setUserRoles(userId, roleIds) {
        const previousRoles = await this.listUserRoles(userId);
        const previousIds = previousRoles.map((p)=>p.roleId);
        const deletedRoleIds = previousIds.filter((p)=>!roleIds.includes(p));
        await this.deleteManyUserRoles(userId, deletedRoleIds);
        const createdRoleIds = roleIds.filter((r)=>!previousIds.includes(r));
        for (let roleId of createdRoleIds){
            await this.create({
                roleId,
                userId
            });
        }
    }
    deleteManyUserRoles(userId, roleIds) {
        return this.repository.delete({
            userId,
            roleId: (0, _typeorm.In)(roleIds)
        });
    }
    listByRoleId(roleId) {
        return this.repository.findBy({
            roleId
        });
    }
    async listUserRoles(userId) {
        return this.repository.findBy({
            userId
        });
    }
}

//# sourceMappingURL=user-role.service.js.map