"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.NotificationRuleMutation = void 0;
const tslib_1 = require("tslib");
const type_graphql_1 = require("type-graphql");
const typeorm_1 = require("typeorm");
const attachment_base_1 = require("@things-factory/attachment-base");
const notification_rule_1 = require("./notification-rule");
const notification_rule_type_1 = require("./notification-rule-type");
let NotificationRuleMutation = class NotificationRuleMutation {
    async createNotificationRule(notificationRule, context) {
        const { domain, user, tx } = context.state;
        const result = await tx.getRepository(notification_rule_1.NotificationRule).save({
            ...notificationRule,
            domain,
            creator: user,
            updater: user
        });
        if (notificationRule.thumbnail) {
            await (0, attachment_base_1.createAttachment)(null, {
                attachment: {
                    file: notificationRule.thumbnail,
                    refType: notification_rule_1.NotificationRule.name,
                    refBy: result.id
                }
            }, context);
        }
        return result;
    }
    async updateNotificationRule(id, patch, context) {
        const { domain, user, tx } = context.state;
        const repository = tx.getRepository(notification_rule_1.NotificationRule);
        const notificationRule = await repository.findOne({
            where: { domain: { id: domain.id }, id }
        });
        const result = await repository.save({
            ...notificationRule,
            ...patch,
            updater: user
        });
        if (patch.thumbnail) {
            await (0, attachment_base_1.deleteAttachmentsByRef)(null, { refBys: [result.id] }, context);
            await (0, attachment_base_1.createAttachment)(null, {
                attachment: {
                    file: patch.thumbnail,
                    refType: notification_rule_1.NotificationRule.name,
                    refBy: result.id
                }
            }, context);
        }
        return result;
    }
    async updateMultipleNotificationRule(patches, context) {
        const { domain, user, tx } = context.state;
        let results = [];
        const _createRecords = patches.filter((patch) => patch.cuFlag.toUpperCase() === '+');
        const _updateRecords = patches.filter((patch) => patch.cuFlag.toUpperCase() === 'M');
        const notificationRuleRepo = tx.getRepository(notification_rule_1.NotificationRule);
        if (_createRecords.length > 0) {
            for (let i = 0; i < _createRecords.length; i++) {
                const newRecord = _createRecords[i];
                const result = await notificationRuleRepo.save({
                    ...newRecord,
                    domain,
                    creator: user,
                    updater: user
                });
                if (newRecord.thumbnail) {
                    await (0, attachment_base_1.createAttachment)(null, {
                        attachment: {
                            file: newRecord.thumbnail,
                            refType: notification_rule_1.NotificationRule.name,
                            refBy: result.id
                        }
                    }, context);
                }
                results.push({ ...result, cuFlag: '+' });
            }
        }
        if (_updateRecords.length > 0) {
            for (let i = 0; i < _updateRecords.length; i++) {
                const updateRecord = _updateRecords[i];
                const notificationRule = await notificationRuleRepo.findOneBy({ id: updateRecord.id });
                const result = await notificationRuleRepo.save({
                    ...notificationRule,
                    ...updateRecord,
                    updater: user
                });
                if (updateRecord.thumbnail) {
                    await (0, attachment_base_1.deleteAttachmentsByRef)(null, { refBys: [result.id] }, context);
                    await (0, attachment_base_1.createAttachment)(null, {
                        attachment: {
                            file: updateRecord.thumbnail,
                            refType: notification_rule_1.NotificationRule.name,
                            refBy: result.id
                        }
                    }, context);
                }
                results.push({ ...result, cuFlag: 'M' });
            }
        }
        return results;
    }
    async deleteNotificationRule(id, context) {
        const { domain, tx } = context.state;
        await tx.getRepository(notification_rule_1.NotificationRule).delete({ domain: { id: domain.id }, id });
        await (0, attachment_base_1.deleteAttachmentsByRef)(null, { refBys: [id] }, context);
        return true;
    }
    async deleteNotificationRules(ids, context) {
        const { domain, tx } = context.state;
        await tx.getRepository(notification_rule_1.NotificationRule).delete({
            domain: { id: domain.id },
            id: (0, typeorm_1.In)(ids)
        });
        await (0, attachment_base_1.deleteAttachmentsByRef)(null, { refBys: ids }, context);
        return true;
    }
    async importNotificationRules(notificationRules, context) {
        const { domain, tx } = context.state;
        await Promise.all(notificationRules.map(async (notificationRule) => {
            const createdNotificationRule = await tx
                .getRepository(notification_rule_1.NotificationRule)
                .save({ domain, ...notificationRule });
        }));
        return true;
    }
};
exports.NotificationRuleMutation = NotificationRuleMutation;
tslib_1.__decorate([
    (0, type_graphql_1.Directive)('@transaction'),
    (0, type_graphql_1.Mutation)(returns => notification_rule_1.NotificationRule, { description: 'To create new NotificationRule' }),
    tslib_1.__param(0, (0, type_graphql_1.Arg)('notificationRule')),
    tslib_1.__param(1, (0, type_graphql_1.Ctx)()),
    tslib_1.__metadata("design:type", Function),
    tslib_1.__metadata("design:paramtypes", [notification_rule_type_1.NewNotificationRule, Object]),
    tslib_1.__metadata("design:returntype", Promise)
], NotificationRuleMutation.prototype, "createNotificationRule", null);
tslib_1.__decorate([
    (0, type_graphql_1.Directive)('@transaction'),
    (0, type_graphql_1.Mutation)(returns => notification_rule_1.NotificationRule, { description: 'To modify NotificationRule information' }),
    tslib_1.__param(0, (0, type_graphql_1.Arg)('id')),
    tslib_1.__param(1, (0, type_graphql_1.Arg)('patch')),
    tslib_1.__param(2, (0, type_graphql_1.Ctx)()),
    tslib_1.__metadata("design:type", Function),
    tslib_1.__metadata("design:paramtypes", [String, notification_rule_type_1.NotificationRulePatch, Object]),
    tslib_1.__metadata("design:returntype", Promise)
], NotificationRuleMutation.prototype, "updateNotificationRule", null);
tslib_1.__decorate([
    (0, type_graphql_1.Directive)('@transaction'),
    (0, type_graphql_1.Mutation)(returns => [notification_rule_1.NotificationRule], { description: "To modify multiple NotificationRules' information" }),
    tslib_1.__param(0, (0, type_graphql_1.Arg)('patches', type => [notification_rule_type_1.NotificationRulePatch])),
    tslib_1.__param(1, (0, type_graphql_1.Ctx)()),
    tslib_1.__metadata("design:type", Function),
    tslib_1.__metadata("design:paramtypes", [Array, Object]),
    tslib_1.__metadata("design:returntype", Promise)
], NotificationRuleMutation.prototype, "updateMultipleNotificationRule", null);
tslib_1.__decorate([
    (0, type_graphql_1.Directive)('@transaction'),
    (0, type_graphql_1.Mutation)(returns => Boolean, { description: 'To delete 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)
], NotificationRuleMutation.prototype, "deleteNotificationRule", null);
tslib_1.__decorate([
    (0, type_graphql_1.Directive)('@transaction'),
    (0, type_graphql_1.Mutation)(returns => Boolean, { description: 'To delete multiple NotificationRules' }),
    tslib_1.__param(0, (0, type_graphql_1.Arg)('ids', type => [String])),
    tslib_1.__param(1, (0, type_graphql_1.Ctx)()),
    tslib_1.__metadata("design:type", Function),
    tslib_1.__metadata("design:paramtypes", [Array, Object]),
    tslib_1.__metadata("design:returntype", Promise)
], NotificationRuleMutation.prototype, "deleteNotificationRules", null);
tslib_1.__decorate([
    (0, type_graphql_1.Directive)('@transaction'),
    (0, type_graphql_1.Mutation)(returns => Boolean, { description: 'To import multiple NotificationRules' }),
    tslib_1.__param(0, (0, type_graphql_1.Arg)('notificationRules', type => [notification_rule_type_1.NotificationRulePatch])),
    tslib_1.__param(1, (0, type_graphql_1.Ctx)()),
    tslib_1.__metadata("design:type", Function),
    tslib_1.__metadata("design:paramtypes", [Array, Object]),
    tslib_1.__metadata("design:returntype", Promise)
], NotificationRuleMutation.prototype, "importNotificationRules", null);
exports.NotificationRuleMutation = NotificationRuleMutation = tslib_1.__decorate([
    (0, type_graphql_1.Resolver)(notification_rule_1.NotificationRule)
], NotificationRuleMutation);
//# sourceMappingURL=notification-rule-mutation.js.map