"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.BankService = 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 BankService = class BankService {
    constructor(prisma) {
        this.prisma = prisma;
    }
    async findAll(query) {
        const allowedFilters = {
            code: 'string',
            name: 'string',
            status: 'enum',
        };
        const where = (0, prisma_query_builder_1.buildWhereClause)(query, allowedFilters, ['code', 'name']);
        const allowedSortColumns = ['code', 'name', 'status', 'createdAt'];
        const orderBy = {};
        if (query.sort && allowedSortColumns.includes(query.sort)) {
            orderBy[query.sort] = query.sortOrder;
        }
        else {
            orderBy.name = 'asc';
        }
        const [total, rows] = await this.prisma.$transaction([
            this.prisma.bank.count({ where }),
            this.prisma.bank.findMany({
                where,
                skip: query.skip,
                take: query.take,
                orderBy,
            }),
        ]);
        return new bootstrap_table_response_dto_1.BootstrapTableResponseDto(total, rows);
    }
    async findOne(id) {
        const bank = await this.prisma.bank.findUnique({ where: { id } });
        if (!bank)
            throw new common_1.NotFoundException(`Bank with id ${id} not found`);
        return bank;
    }
    async create(dto) {
        const existing = await this.prisma.bank.findUnique({
            where: { code: dto.code },
        });
        if (existing)
            throw new common_1.ConflictException(`Bank code "${dto.code}" already exists`);
        return this.prisma.bank.create({ data: dto });
    }
    async update(id, dto) {
        await this.findOne(id);
        if (dto.code) {
            const conflict = await this.prisma.bank.findFirst({
                where: { code: dto.code, NOT: { id } },
            });
            if (conflict)
                throw new common_1.ConflictException(`Bank code "${dto.code}" already exists`);
        }
        return this.prisma.bank.update({ where: { id }, data: dto });
    }
    async remove(id) {
        await this.findOne(id);
        return this.prisma.bank.delete({ where: { id } });
    }
};
exports.BankService = BankService;
exports.BankService = BankService = __decorate([
    (0, common_1.Injectable)(),
    __metadata("design:paramtypes", [prisma_service_1.PrismaService])
], BankService);
//# sourceMappingURL=bank.service.js.map