"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;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.RoleService = void 0;
const common_1 = require("@nestjs/common");
const prisma_service_1 = require("../prisma/prisma.service");
const bootstrap_table_response_dto_1 = require("../commons/dto/bootstrap-table-response.dto");
const prisma_query_builder_1 = require("../commons/utils/prisma-query-builder");
let RoleService = class RoleService {
    constructor(prisma) {
        this.prisma = prisma;
    }
    async findAll(query) {
        const allowedFilters = {
            code: 'string',
            name: 'string',
            accessAllBranch: 'enum',
        };
        const where = (0, prisma_query_builder_1.buildWhereClause)(query, allowedFilters, ['code', 'name']);
        const allowedSortColumns = [
            'code',
            'name',
            'accessAllBranch',
            'createdAt',
        ];
        const orderBy = {};
        if (query.sort && allowedSortColumns.includes(query.sort)) {
            orderBy[query.sort] = query.sortOrder;
        }
        else {
            orderBy.createdAt = 'desc';
        }
        const [total, rows] = await this.prisma.$transaction([
            this.prisma.role.count({ where }),
            this.prisma.role.findMany({
                where,
                skip: query.skip,
                take: query.take,
                orderBy,
            }),
        ]);
        return new bootstrap_table_response_dto_1.BootstrapTableResponseDto(total, rows);
    }
    async findOne(id) {
        const role = await this.prisma.role.findUnique({ where: { id } });
        if (!role)
            throw new common_1.NotFoundException(`Role with id ${id} not found`);
        return role;
    }
    async create(dto) {
        const existing = await this.prisma.role.findUnique({ where: { code: dto.code } });
        if (existing) {
            throw new common_1.ConflictException(`Role code "${dto.code}" already exists`);
        }
        return this.prisma.role.create({ data: dto });
    }
    async update(id, dto) {
        await this.findOne(id);
        if (dto.code) {
            const conflict = await this.prisma.role.findFirst({
                where: { code: dto.code, NOT: { id } },
            });
            if (conflict) {
                throw new common_1.ConflictException(`Role code "${dto.code}" already exists`);
            }
        }
        return this.prisma.role.update({ where: { id }, data: dto });
    }
    async remove(id) {
        await this.findOne(id);
        return this.prisma.role.delete({ where: { id } });
    }
};
exports.RoleService = RoleService;
exports.RoleService = RoleService = __decorate([
    (0, common_1.Injectable)(),
    __metadata("design:paramtypes", [prisma_service_1.PrismaService])
], RoleService);
//# sourceMappingURL=role.service.js.map