"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.NotificationRuleQuery = void 0;
const tslib_1 = require("tslib");
const type_graphql_1 = require("type-graphql");
const attachment_base_1 = require("@things-factory/attachment-base");
const shell_1 = require("@things-factory/shell");
const auth_base_1 = require("@things-factory/auth-base");
const notification_rule_1 = require("./notification-rule");
const notification_rule_type_1 = require("./notification-rule-type");
let NotificationRuleQuery = class NotificationRuleQuery {
    async notificationRule(id, context) {
        const { domain } = context.state;
        return await (0, shell_1.getRepository)(notification_rule_1.NotificationRule).findOne({
            where: { domain: { id: domain.id }, id }
        });
    }
    async notificationRules(params, context) {
        const { domain } = context.state;
        const queryBuilder = (0, shell_1.getQueryBuilderFromListParams)({
            domain,
            params,
            repository: await (0, shell_1.getRepository)(notification_rule_1.NotificationRule),
            searchables: ['name', 'description']
        });
        const [items, total] = await queryBuilder.getManyAndCount();
        return { items, total };
    }
    async thumbnail(notificationRule) {
        const attachment = await (0, shell_1.getRepository)(attachment_base_1.Attachment).findOne({
            where: {
                domain: { id: notificationRule.domainId },
                refType: notification_rule_1.NotificationRule.name,
                refBy: notificationRule.id
            }
        });
        return attachment === null || attachment === void 0 ? void 0 : attachment.fullpath;
    }
    async domain(notificationRule) {
        return notificationRule.domainId && (await (0, shell_1.getRepository)(shell_1.Domain).findOneBy({ id: notificationRule.domainId }));
    }
    async updater(notificationRule) {
        return notificationRule.updaterId && (await (0, shell_1.getRepository)(auth_base_1.User).findOneBy({ id: notificationRule.updaterId }));
    }
    async creator(notificationRule) {
        return notificationRule.creatorId && (await (0, shell_1.getRepository)(auth_base_1.User).findOneBy({ id: notificationRule.creatorId }));
    }
};
exports.NotificationRuleQuery = NotificationRuleQuery;
tslib_1.__decorate([
    (0, type_graphql_1.Query)(returns => notification_rule_1.NotificationRule, { nullable: true, description: 'To fetch a NotificationRule' }),
    tslib_1.__param(0, (0, type_graphql_1.Arg)('id')),
    tslib_1.__param(1, (0, type_graphql_1.Ctx)()),
    tslib_1.__metadata("design:type", Function),
    tslib_1.__metadata("design:paramtypes", [String, Object]),
    tslib_1.__metadata("design:returntype", Promise)
], NotificationRuleQuery.prototype, "notificationRule", null);
tslib_1.__decorate([
    (0, type_graphql_1.Query)(returns => notification_rule_type_1.NotificationRuleList, { description: 'To fetch multiple NotificationRules' }),
    tslib_1.__param(0, (0, type_graphql_1.Args)(type => shell_1.ListParam)),
    tslib_1.__param(1, (0, type_graphql_1.Ctx)()),
    tslib_1.__metadata("design:type", Function),
    tslib_1.__metadata("design:paramtypes", [shell_1.ListParam, Object]),
    tslib_1.__metadata("design:returntype", Promise)
], NotificationRuleQuery.prototype, "notificationRules", null);
tslib_1.__decorate([
    (0, type_graphql_1.FieldResolver)(type => String),
    tslib_1.__param(0, (0, type_graphql_1.Root)()),
    tslib_1.__metadata("design:type", Function),
    tslib_1.__metadata("design:paramtypes", [notification_rule_1.NotificationRule]),
    tslib_1.__metadata("design:returntype", Promise)
], NotificationRuleQuery.prototype, "thumbnail", null);
tslib_1.__decorate([
    (0, type_graphql_1.FieldResolver)(type => shell_1.Domain),
    tslib_1.__param(0, (0, type_graphql_1.Root)()),
    tslib_1.__metadata("design:type", Function),
    tslib_1.__metadata("design:paramtypes", [notification_rule_1.NotificationRule]),
    tslib_1.__metadata("design:returntype", Promise)
], NotificationRuleQuery.prototype, "domain", null);
tslib_1.__decorate([
    (0, type_graphql_1.FieldResolver)(type => auth_base_1.User),
    tslib_1.__param(0, (0, type_graphql_1.Root)()),
    tslib_1.__metadata("design:type", Function),
    tslib_1.__metadata("design:paramtypes", [notification_rule_1.NotificationRule]),
    tslib_1.__metadata("design:returntype", Promise)
], NotificationRuleQuery.prototype, "updater", null);
tslib_1.__decorate([
    (0, type_graphql_1.FieldResolver)(type => auth_base_1.User),
    tslib_1.__param(0, (0, type_graphql_1.Root)()),
    tslib_1.__metadata("design:type", Function),
    tslib_1.__metadata("design:paramtypes", [notification_rule_1.NotificationRule]),
    tslib_1.__metadata("design:returntype", Promise)
], NotificationRuleQuery.prototype, "creator", null);
exports.NotificationRuleQuery = NotificationRuleQuery = tslib_1.__decorate([
    (0, type_graphql_1.Resolver)(notification_rule_1.NotificationRule)
], NotificationRuleQuery);
//# sourceMappingURL=notification-rule-query.js.map