import { __decorate, __metadata } from "tslib";
import { GraphQLError } from 'graphql';
import { Injectable, Scope } from 'graphql-modules';
import { TenantAwareDBClient } from '../../app-providers/tenant-db-client.js';
import { AuthContextProvider } from '../../auth/providers/auth-context.provider.js';
import { AuthorizationProvider } from '../../auth/providers/authorization.provider.js';
let ChargesAuthorizationProvider = class ChargesAuthorizationProvider extends AuthorizationProvider {
    db;
    constructor(authProvider, db) {
        super(authProvider);
        this.db = db;
    }
    async canReadCharges() {
        await this.requireAuth();
    }
    async canWriteCharge() {
        await this.requireRole(['business_owner', 'accountant', 'gmail_listener', 'scraper']);
    }
    async canDeleteChargesByIds(chargeIds) {
        await this.requireRole(['business_owner', 'accountant']);
        if (!chargeIds.length) {
            return;
        }
        const uniqueChargeIds = [...new Set(chargeIds)];
        const { rows } = await this.db.query('SELECT id FROM accounter_schema.charges WHERE id = ANY($1::uuid[])', [uniqueChargeIds]);
        if (rows.length !== uniqueChargeIds.length) {
            throw new GraphQLError('Charge not found or access denied', {
                extensions: { code: 'NOT_FOUND' },
            });
        }
    }
    async canDeleteCharge(chargeId) {
        await this.canDeleteChargesByIds([chargeId]);
    }
};
ChargesAuthorizationProvider = __decorate([
    Injectable({ scope: Scope.Operation }),
    __metadata("design:paramtypes", [AuthContextProvider,
        TenantAwareDBClient])
], ChargesAuthorizationProvider);
export { ChargesAuthorizationProvider };
//# sourceMappingURL=charges-authorization.provider.js.map