"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.NotificationQuery = void 0;
const tslib_1 = require("tslib");
const type_graphql_1 = require("type-graphql");
const shell_1 = require("@things-factory/shell");
const auth_base_1 = require("@things-factory/auth-base");
const notification_1 = require("./notification");
const notification_type_1 = require("./notification-type");
let NotificationQuery = class NotificationQuery {
    async notification(id, context) {
        const { domain } = context.state;
        return await (0, shell_1.getRepository)(notification_1.Notification).findOne({
            where: { domain: { id: domain.id }, id }
        });
    }
    async notificationes(params, context) {
        const { domain } = context.state;
        const queryBuilder = (0, shell_1.getQueryBuilderFromListParams)({
            domain,
            params,
            repository: await (0, shell_1.getRepository)(notification_1.Notification),
            searchables: ['title', 'body']
        });
        const [items, total] = await queryBuilder.getManyAndCount();
        return { items, total };
    }
    async myNotifications(params, context) {
        const { domain, user } = context.state;
        const queryBuilder = (0, shell_1.getQueryBuilderFromListParams)({
            domain,
            params,
            alias: 'nb',
            repository: await (0, shell_1.getRepository)(notification_1.Notification),
            searchables: ['title', 'body']
        }).andWhere('nb.owner = :user', { user: user.id });
        const [items, total] = await queryBuilder.getManyAndCount();
        return { items, total };
    }
    async owner(notification) {
        return notification.ownerId && (await (0, shell_1.getRepository)(auth_base_1.User).findOneBy({ id: notification.ownerId }));
    }
    async domain(notification) {
        return notification.domainId && (await (0, shell_1.getRepository)(shell_1.Domain).findOneBy({ id: notification.domainId }));
    }
    async updater(notification) {
        return notification.updaterId && (await (0, shell_1.getRepository)(auth_base_1.User).findOneBy({ id: notification.updaterId }));
    }
    async creator(notification) {
        return notification.creatorId && (await (0, shell_1.getRepository)(auth_base_1.User).findOneBy({ id: notification.creatorId }));
    }
};
exports.NotificationQuery = NotificationQuery;
tslib_1.__decorate([
    (0, type_graphql_1.Query)(returns => notification_1.Notification, { nullable: true, description: 'To fetch a 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)
], NotificationQuery.prototype, "notification", null);
tslib_1.__decorate([
    (0, type_graphql_1.Query)(returns => notification_type_1.NotificationList, { description: 'To fetch multiple Notificationes' }),
    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)
], NotificationQuery.prototype, "notificationes", null);
tslib_1.__decorate([
    (0, type_graphql_1.Query)(returns => notification_type_1.NotificationList, { description: 'To fetch my notifications' }),
    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)
], NotificationQuery.prototype, "myNotifications", 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_1.Notification]),
    tslib_1.__metadata("design:returntype", Promise)
], NotificationQuery.prototype, "owner", 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_1.Notification]),
    tslib_1.__metadata("design:returntype", Promise)
], NotificationQuery.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_1.Notification]),
    tslib_1.__metadata("design:returntype", Promise)
], NotificationQuery.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_1.Notification]),
    tslib_1.__metadata("design:returntype", Promise)
], NotificationQuery.prototype, "creator", null);
exports.NotificationQuery = NotificationQuery = tslib_1.__decorate([
    (0, type_graphql_1.Resolver)(notification_1.Notification)
], NotificationQuery);
//# sourceMappingURL=notification-query.js.map