UNPKG

@nestjs-mod/webhook

Version:

Webhook module with an error filter, guard, controller, database migrations and rest-sdk for work with module from other nodejs appliaction

188 lines 9.59 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.WebhookLogsController = void 0; const tslib_1 = require("tslib"); const swagger_1 = require("@nestjs-mod/swagger"); const prisma_1 = require("@nestjs-mod/prisma"); const prisma_tools_1 = require("@nestjs-mod/prisma-tools"); const validation_1 = require("@nestjs-mod/validation"); const common_1 = require("@nestjs/common"); const swagger_2 = require("@nestjs/swagger"); const class_validator_1 = require("class-validator"); const nestjs_translates_1 = require("nestjs-translates"); const webhook_log_entity_1 = require("../generated/rest-dto/webhook-log.entity"); const webhook_user_entity_1 = require("../generated/rest-dto/webhook-user.entity"); const prisma_client_1 = require("../generated/prisma-client"); const webhook_tools_service_1 = require("../services/webhook-tools.service"); const find_many_webhook_log_args_1 = require("../types/find-many-webhook-log-args"); const find_many_webhook_log_response_1 = require("../types/find-many-webhook-log-response"); const webhook_constants_1 = require("../webhook.constants"); const webhook_decorators_1 = require("../webhook.decorators"); const webhook_errors_1 = require("../webhook.errors"); let WebhookLogsController = class WebhookLogsController { constructor(prismaClient, prismaToolsService, webhookToolsService, translatesService) { this.prismaClient = prismaClient; this.prismaToolsService = prismaToolsService; this.webhookToolsService = webhookToolsService; this.translatesService = translatesService; } async findManyLogs(externalTenantId, webhookUser, args) { const { take, skip, curPage, perPage } = this.prismaToolsService.getFirstSkipFromCurPerPage({ curPage: args.curPage, perPage: args.perPage, }); const searchText = args.searchText; const orderBy = (args.sort || 'createdAt:desc') .split(',') .map((s) => s.split(':')) .reduce((all, [key, value]) => ({ ...all, ...(key in prisma_client_1.Prisma.WebhookLogScalarFieldEnum ? { [key]: value === 'desc' ? 'desc' : 'asc', } : {}), }), {}); const result = await this.prismaClient.$transaction(async (prisma) => { return { webhookLogs: await prisma.webhookLog.findMany({ where: { ...(searchText ? { OR: [ ...((0, class_validator_1.isUUID)(searchText) ? [ { id: { equals: searchText } }, { externalTenantId: { equals: searchText } }, { webhookId: { equals: searchText } }, ] : []), { response: { string_contains: searchText } }, { request: { string_contains: searchText } }, { responseStatus: { contains: searchText, mode: 'insensitive', }, }, ], } : {}), ...this.webhookToolsService.externalTenantIdQuery(webhookUser, webhookUser.userRole === prisma_client_1.WebhookRole.Admin ? undefined : externalTenantId), webhookId: args.webhookId, }, take, skip, orderBy, }), totalResults: await prisma.webhookLog.count({ where: { ...(searchText ? { OR: [ ...((0, class_validator_1.isUUID)(searchText) ? [ { id: { equals: searchText } }, { externalTenantId: { equals: searchText } }, { webhookId: { equals: searchText } }, ] : []), { response: { string_contains: searchText } }, { request: { string_contains: searchText } }, { responseStatus: { contains: searchText, mode: 'insensitive', }, }, ], } : {}), ...this.webhookToolsService.externalTenantIdQuery(webhookUser, webhookUser.userRole === prisma_client_1.WebhookRole.Admin ? undefined : externalTenantId), webhookId: args.webhookId, }, }), }; }); return { webhookLogs: result.webhookLogs, meta: { totalResults: result.totalResults, curPage, perPage, }, }; } async deleteOne(externalTenantId, webhookUser, id, locale) { await this.prismaClient.webhookLog.delete({ where: { id, ...this.webhookToolsService.externalTenantIdQuery(webhookUser, webhookUser.userRole === prisma_client_1.WebhookRole.Admin ? undefined : externalTenantId), }, }); return { message: this.translatesService.translate('ok', locale) }; } async findOne(externalTenantId, webhookUser, id) { return await this.prismaClient.webhookLog.findFirstOrThrow({ where: { id, ...this.webhookToolsService.externalTenantIdQuery(webhookUser, webhookUser.userRole === prisma_client_1.WebhookRole.Admin ? undefined : externalTenantId), }, }); } }; exports.WebhookLogsController = WebhookLogsController; tslib_1.__decorate([ (0, common_1.Get)(), (0, swagger_2.ApiOkResponse)({ type: find_many_webhook_log_response_1.FindManyWebhookLogResponse }), tslib_1.__param(0, (0, webhook_decorators_1.CurrentWebhookExternalTenantId)()), tslib_1.__param(1, (0, webhook_decorators_1.CurrentWebhookUser)()), tslib_1.__param(2, (0, common_1.Query)()), tslib_1.__metadata("design:type", Function), tslib_1.__metadata("design:paramtypes", [String, webhook_user_entity_1.WebhookUser, find_many_webhook_log_args_1.FindManyWebhookLogArgs]), tslib_1.__metadata("design:returntype", Promise) ], WebhookLogsController.prototype, "findManyLogs", null); tslib_1.__decorate([ (0, common_1.Delete)(':id'), (0, swagger_2.ApiOkResponse)({ type: swagger_1.StatusResponse }), tslib_1.__param(0, (0, webhook_decorators_1.CurrentWebhookExternalTenantId)()), tslib_1.__param(1, (0, webhook_decorators_1.CurrentWebhookUser)()), tslib_1.__param(2, (0, common_1.Param)('id', new common_1.ParseUUIDPipe())), tslib_1.__param(3, (0, nestjs_translates_1.CurrentLocale)()), tslib_1.__metadata("design:type", Function), tslib_1.__metadata("design:paramtypes", [String, webhook_user_entity_1.WebhookUser, String, String]), tslib_1.__metadata("design:returntype", Promise) ], WebhookLogsController.prototype, "deleteOne", null); tslib_1.__decorate([ (0, common_1.Get)(':id'), (0, swagger_2.ApiOkResponse)({ type: webhook_log_entity_1.WebhookLog }), tslib_1.__param(0, (0, webhook_decorators_1.CurrentWebhookExternalTenantId)()), tslib_1.__param(1, (0, webhook_decorators_1.CurrentWebhookUser)()), tslib_1.__param(2, (0, common_1.Param)('id', new common_1.ParseUUIDPipe())), tslib_1.__metadata("design:type", Function), tslib_1.__metadata("design:paramtypes", [String, webhook_user_entity_1.WebhookUser, String]), tslib_1.__metadata("design:returntype", Promise) ], WebhookLogsController.prototype, "findOne", null); exports.WebhookLogsController = WebhookLogsController = tslib_1.__decorate([ (0, swagger_2.ApiBadRequestResponse)({ schema: { allOf: (0, swagger_2.refs)(webhook_errors_1.WebhookError, validation_1.ValidationError) }, }), (0, swagger_2.ApiTags)('Webhook'), (0, webhook_decorators_1.CheckWebhookRole)([prisma_client_1.WebhookRole.User, prisma_client_1.WebhookRole.Admin]), (0, common_1.Controller)('/webhook/logs'), tslib_1.__param(0, (0, prisma_1.InjectPrismaClient)(webhook_constants_1.WEBHOOK_FEATURE)), tslib_1.__metadata("design:paramtypes", [prisma_client_1.PrismaClient, prisma_tools_1.PrismaToolsService, webhook_tools_service_1.WebhookToolsService, nestjs_translates_1.TranslatesService]) ], WebhookLogsController); //# sourceMappingURL=webhook-logs.controller.js.map