"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.NotificationMutation = void 0;
const tslib_1 = require("tslib");
const type_graphql_1 = require("type-graphql");
const typeorm_1 = require("typeorm");
const notification_1 = require("./notification");
const notification_type_1 = require("./notification-type");
let NotificationMutation = class NotificationMutation {
    async createNotification(notification, context) {
        const { domain, user, tx } = context.state;
        return await tx.getRepository(notification_1.Notification).save(Object.assign(Object.assign({}, notification), { domain, creator: user, updater: user }));
    }
    async updateNotification(id, patch, context) {
        const { domain, user, tx } = context.state;
        const repository = tx.getRepository(notification_1.Notification);
        const notification = await repository.findOne({
            where: { domain: { id: domain.id }, id }
        });
        const result = await repository.save(Object.assign(Object.assign(Object.assign({}, notification), patch), { updater: user }));
        return result;
    }
    async updateMultipleNotification(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 notificationRepo = tx.getRepository(notification_1.Notification);
        if (_createRecords.length > 0) {
            for (let i = 0; i < _createRecords.length; i++) {
                const newRecord = _createRecords[i];
                const result = await notificationRepo.save(Object.assign(Object.assign({}, newRecord), { domain, creator: user, updater: user }));
                results.push(Object.assign(Object.assign({}, result), { cuFlag: '+' }));
            }
        }
        if (_updateRecords.length > 0) {
            for (let i = 0; i < _updateRecords.length; i++) {
                const updateRecord = _updateRecords[i];
                const notification = await notificationRepo.findOneBy({ id: updateRecord.id });
                const result = await notificationRepo.save(Object.assign(Object.assign(Object.assign({}, notification), updateRecord), { updater: user }));
                results.push(Object.assign(Object.assign({}, result), { cuFlag: 'M' }));
            }
        }
        return results;
    }
    async deleteNotification(id, context) {
        const { domain, tx } = context.state;
        await tx.getRepository(notification_1.Notification).delete({ domain: { id: domain.id }, id });
        return true;
    }
    async deleteNotificationes(ids, context) {
        const { domain, tx } = context.state;
        await tx.getRepository(notification_1.Notification).delete({
            domain: { id: domain.id },
            id: (0, typeorm_1.In)(ids)
        });
        return true;
    }
};
exports.NotificationMutation = NotificationMutation;
tslib_1.__decorate([
    (0, type_graphql_1.Directive)('@transaction'),
    (0, type_graphql_1.Mutation)(returns => notification_1.Notification, { description: 'To create new Notification' }),
    tslib_1.__param(0, (0, type_graphql_1.Arg)('notification')),
    tslib_1.__param(1, (0, type_graphql_1.Ctx)()),
    tslib_1.__metadata("design:type", Function),
    tslib_1.__metadata("design:paramtypes", [notification_type_1.NewNotification, Object]),
    tslib_1.__metadata("design:returntype", Promise)
], NotificationMutation.prototype, "createNotification", null);
tslib_1.__decorate([
    (0, type_graphql_1.Directive)('@transaction'),
    (0, type_graphql_1.Mutation)(returns => notification_1.Notification, { description: 'To modify Notification 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_type_1.NotificationPatch, Object]),
    tslib_1.__metadata("design:returntype", Promise)
], NotificationMutation.prototype, "updateNotification", null);
tslib_1.__decorate([
    (0, type_graphql_1.Directive)('@transaction'),
    (0, type_graphql_1.Mutation)(returns => [notification_1.Notification], { description: "To modify multiple Notificationes' information" }),
    tslib_1.__param(0, (0, type_graphql_1.Arg)('patches', type => [notification_type_1.NotificationPatch])),
    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)
], NotificationMutation.prototype, "updateMultipleNotification", null);
tslib_1.__decorate([
    (0, type_graphql_1.Directive)('@transaction'),
    (0, type_graphql_1.Mutation)(returns => Boolean, { description: 'To delete Notification' }),
    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)
], NotificationMutation.prototype, "deleteNotification", null);
tslib_1.__decorate([
    (0, type_graphql_1.Directive)('@transaction'),
    (0, type_graphql_1.Mutation)(returns => Boolean, { description: 'To delete multiple Notificationes' }),
    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)
], NotificationMutation.prototype, "deleteNotificationes", null);
exports.NotificationMutation = NotificationMutation = tslib_1.__decorate([
    (0, type_graphql_1.Resolver)(notification_1.Notification)
], NotificationMutation);
//# sourceMappingURL=notification-mutation.js.map