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

292 lines 14.4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.WebhookController = 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 prisma_client_1 = require("../generated/prisma-client"); const create_webhook_dto_1 = require("../generated/rest-dto/create-webhook.dto"); const update_webhook_dto_1 = require("../generated/rest-dto/update-webhook.dto"); const webhook_user_entity_1 = require("../generated/rest-dto/webhook-user.entity"); const webhook_entity_1 = require("../generated/rest-dto/webhook.entity"); const webhook_service_1 = require("../services/webhook.service"); const find_many_webhook_args_1 = require("../types/find-many-webhook-args"); const find_many_webhook_response_1 = require("../types/find-many-webhook-response"); const webhook_event_1 = require("../types/webhook-event"); const webhook_test_request_response_1 = require("../types/webhook-test-request-response"); const webhook_constants_1 = require("../webhook.constants"); const webhook_decorators_1 = require("../webhook.decorators"); const webhook_errors_1 = require("../webhook.errors"); let WebhookController = class WebhookController { constructor(prismaClient, prismaToolsService, webhookService, translatesService, translatesStorage) { this.prismaClient = prismaClient; this.prismaToolsService = prismaToolsService; this.webhookService = webhookService; this.translatesService = translatesService; this.translatesStorage = translatesStorage; } async profile(webhookUser) { return webhookUser; } async events() { return this.webhookService.getAllEvents().map((e) => ({ ...e, descriptionLocale: { ...Object.fromEntries(this.translatesStorage.locales.map((locale) => [ locale, e.descriptionLocale?.[locale] || this.translatesService.translate(e.description, locale), ])), en: e.description, }, })); } async findMany(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.WebhookScalarFieldEnum ? { [key]: value === 'desc' ? 'desc' : 'asc', } : {}), }), {}); const result = await this.prismaClient.$transaction(async (prisma) => { return { webhooks: await prisma.webhook.findMany({ where: { ...(searchText ? { OR: [ ...((0, class_validator_1.isUUID)(searchText) ? [{ id: { equals: searchText } }, { externalTenantId: { equals: searchText } }] : []), { endpoint: { contains: searchText, mode: 'insensitive' } }, { eventName: { contains: searchText, mode: 'insensitive' }, }, ], } : {}), ...(webhookUser.userRole === prisma_client_1.WebhookRole.Admin ? { externalTenantId: args.tenantId } : { externalTenantId: webhookUser?.userRole === prisma_client_1.WebhookRole.User ? webhookUser.externalTenantId : args.tenantId, }), }, take, skip, orderBy, }), totalResults: await prisma.webhook.count({ where: { ...(searchText ? { OR: [ ...((0, class_validator_1.isUUID)(searchText) ? [{ id: { equals: searchText } }, { externalTenantId: { equals: searchText } }] : []), { endpoint: { contains: searchText, mode: 'insensitive' } }, { eventName: { contains: searchText, mode: 'insensitive' }, }, ], } : {}), ...(webhookUser.userRole === prisma_client_1.WebhookRole.Admin ? { externalTenantId: args.tenantId } : { externalTenantId: webhookUser?.userRole === prisma_client_1.WebhookRole.User ? webhookUser.externalTenantId : args.tenantId, }), }, }), }; }); return { webhooks: result.webhooks, meta: { totalResults: result.totalResults, curPage, perPage, }, }; } async testRequest(externalTenantId, args) { const event = this.webhookService.getAllEvents().find((e) => e.eventName === args.eventName); const { response, responseStatus, webhookStatus, request } = await this.webhookService.httpRequest({ endpoint: args.endpoint, eventBody: event?.example || {}, headers: args.headers, requestTimeout: args.requestTimeout || 0, }); return { externalTenantId, request, response, responseStatus, webhookStatus, }; } async createOne(externalTenantId, webhookUser, args) { return await this.prismaClient.webhook.create({ data: { ...args, WebhookUser_Webhook_createdByToWebhookUser: { connect: { id: webhookUser.id }, }, WebhookUser_Webhook_updatedByToWebhookUser: { connect: { id: webhookUser.id }, }, ...(webhookUser.userRole === prisma_client_1.WebhookRole.Admin ? { externalTenantId } : { externalTenantId: webhookUser.externalTenantId, }), }, }); } async updateOne(externalTenantId, webhookUser, id, args) { return await this.prismaClient.webhook.update({ data: { ...args, updatedAt: new Date() }, where: { id, ...(webhookUser.userRole === prisma_client_1.WebhookRole.Admin ? {} : { externalTenantId: webhookUser?.userRole === prisma_client_1.WebhookRole.User ? webhookUser.externalTenantId : externalTenantId, }), }, }); } async deleteOne(externalTenantId, webhookUser, id, locale) { await this.prismaClient.webhook.delete({ where: { id, ...(webhookUser.userRole === prisma_client_1.WebhookRole.Admin ? {} : { externalTenantId: webhookUser?.userRole === prisma_client_1.WebhookRole.User ? webhookUser.externalTenantId : externalTenantId, }), }, }); return { message: this.translatesService.translate('ok', locale) }; } async findOne(externalTenantId, webhookUser, id) { return await this.prismaClient.webhook.findFirstOrThrow({ where: { id, ...(webhookUser.userRole === prisma_client_1.WebhookRole.Admin ? {} : { externalTenantId: webhookUser?.userRole === prisma_client_1.WebhookRole.User ? webhookUser.externalTenantId : externalTenantId, }), }, }); } }; exports.WebhookController = WebhookController; tslib_1.__decorate([ (0, common_1.Get)('profile'), (0, swagger_2.ApiOkResponse)({ type: webhook_user_entity_1.WebhookUser }), tslib_1.__param(0, (0, webhook_decorators_1.CurrentWebhookUser)()), tslib_1.__metadata("design:type", Function), tslib_1.__metadata("design:paramtypes", [webhook_user_entity_1.WebhookUser]), tslib_1.__metadata("design:returntype", Promise) ], WebhookController.prototype, "profile", null); tslib_1.__decorate([ (0, common_1.Get)('events'), (0, swagger_2.ApiOkResponse)({ type: webhook_event_1.WebhookEvent, isArray: true }), tslib_1.__metadata("design:type", Function), tslib_1.__metadata("design:paramtypes", []), tslib_1.__metadata("design:returntype", Promise) ], WebhookController.prototype, "events", null); tslib_1.__decorate([ (0, common_1.Get)(), (0, swagger_2.ApiOkResponse)({ type: find_many_webhook_response_1.FindManyWebhookResponse }), 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_args_1.FindManyWebhookArgs]), tslib_1.__metadata("design:returntype", Promise) ], WebhookController.prototype, "findMany", null); tslib_1.__decorate([ (0, common_1.Post)('test-request'), (0, swagger_2.ApiCreatedResponse)({ type: webhook_test_request_response_1.WebhookTestRequestResponse }), tslib_1.__param(0, (0, webhook_decorators_1.CurrentWebhookExternalTenantId)()), tslib_1.__param(1, (0, common_1.Body)()), tslib_1.__metadata("design:type", Function), tslib_1.__metadata("design:paramtypes", [String, create_webhook_dto_1.CreateWebhookDto]), tslib_1.__metadata("design:returntype", Promise) ], WebhookController.prototype, "testRequest", null); tslib_1.__decorate([ (0, common_1.Post)(), (0, swagger_2.ApiCreatedResponse)({ type: webhook_entity_1.Webhook }), 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.Body)()), tslib_1.__metadata("design:type", Function), tslib_1.__metadata("design:paramtypes", [String, webhook_user_entity_1.WebhookUser, create_webhook_dto_1.CreateWebhookDto]), tslib_1.__metadata("design:returntype", Promise) ], WebhookController.prototype, "createOne", null); tslib_1.__decorate([ (0, common_1.Put)(':id'), (0, swagger_2.ApiOkResponse)({ type: webhook_entity_1.Webhook }), 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, common_1.Body)()), tslib_1.__metadata("design:type", Function), tslib_1.__metadata("design:paramtypes", [String, webhook_user_entity_1.WebhookUser, String, update_webhook_dto_1.UpdateWebhookDto]), tslib_1.__metadata("design:returntype", Promise) ], WebhookController.prototype, "updateOne", 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) ], WebhookController.prototype, "deleteOne", null); tslib_1.__decorate([ (0, common_1.Get)(':id'), (0, swagger_2.ApiOkResponse)({ type: webhook_entity_1.Webhook }), 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) ], WebhookController.prototype, "findOne", null); exports.WebhookController = WebhookController = 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'), 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_service_1.WebhookService, nestjs_translates_1.TranslatesService, nestjs_translates_1.TranslatesStorage]) ], WebhookController); //# sourceMappingURL=webhook.controller.js.map