From 7b2389f32aba1feb887df2b937b98962b44cdc8b Mon Sep 17 00:00:00 2001 From: jrcleber Date: Sat, 9 Dec 2023 20:07:02 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=99=EF=B8=8F=20general=20configuration=20?= =?UTF-8?q?for=20postgres?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/repository/repository.service.ts | 137 +++++++++++++++++++++++++++ 1 file changed, 137 insertions(+) create mode 100644 src/repository/repository.service.ts diff --git a/src/repository/repository.service.ts b/src/repository/repository.service.ts new file mode 100644 index 00000000..b137162d --- /dev/null +++ b/src/repository/repository.service.ts @@ -0,0 +1,137 @@ +/** + * ┌──────────────────────────────────────────────────────────────────────────────┐ + * │ @author jrCleber │ + * │ @filename message.model.ts │ + * │ Developed by: Cleber Wilson │ + * │ Creation date: Dez 02, 2023 │ + * │ Contact: contato@codechat.dev │ + * ├──────────────────────────────────────────────────────────────────────────────┤ + * │ @copyright © Cleber Wilson 2022. All rights reserved. │ + * │ Licensed under the Apache License, Version 2.0 │ + * │ │ + * │ @license "https://github.com/code-chat-br/whatsapp-api/blob/main/LICENSE" │ + * │ │ + * │ You may not use this file except in compliance with the License. │ + * │ You may obtain a copy of the License at │ + * │ │ + * │ http://www.apache.org/licenses/LICENSE-2.0 │ + * │ │ + * │ Unless required by applicable law or agreed to in writing, software │ + * │ distributed under the License is distributed on an "AS IS" BASIS, │ + * │ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. │ + * │ │ + * │ See the License for the specific language governing permissions and │ + * │ limitations under the License. │ + * │ │ + * │ @class Repository │ + * │ @type {ITypebotModel} │ + * │ @type {CreateLogs} │ + * ├──────────────────────────────────────────────────────────────────────────────┤ + * │ @important │ + * │ For any future changes to the code in this file, it is recommended to │ + * │ contain, together with the modification, the information of the developer │ + * │ who changed it and the date of modification. │ + * └──────────────────────────────────────────────────────────────────────────────┘ + */ + +export class Query { + where?: T; + sort?: 'asc' | 'desc'; + page?: number; + offset?: number; +} + +import { Prisma, PrismaClient, Webhook } from '@prisma/client'; +import { WebhookEvents } from '../whatsapp/dto/webhook.dto'; +import { BadRequestException, NotFoundException } from '../exceptions'; +import { Logger } from '../config/logger.config'; +import { ConfigService } from '../config/env.config'; + +type CreateLogs = { + context: string; + description?: string; + type: 'error' | 'info' | 'warning' | 'log'; + content: any; +}; + +export class Repository extends PrismaClient { + constructor(private readonly configService: ConfigService) { + super(); + } + + private readonly logger = new Logger(this.configService, Repository.name); + + public async onModuleInit() { + await this.$connect(); + this.logger.info('Repository:Connected - ON'); + } + + public async onModuleDestroy() { + await this.$disconnect(); + this.logger.warn('Repository:Prisma - OFF'); + } + + public async updateWebhook( + webhookId: number, + data: Partial & { events?: WebhookEvents }, + ) { + const find = await this.webhook.findUnique({ + where: { + id: webhookId, + }, + }); + if (!find) { + throw new NotFoundException(['Webhook not found', `Webhook id: ${webhookId}`]); + } + try { + for await (const [key, value] of Object.entries(data?.events)) { + if (value === undefined) { + continue; + } + + const k = `ARRAY['${key}']`; + const v = `to_jsonb(${value}::boolean)`; + + await this.$queryRaw( + Prisma.sql`UPDATE "Webhook" SET events = jsonb_set(events, ${Prisma.raw( + k, + )}, ${Prisma.raw(v)}) WHERE id = ${webhookId}`, + ); + } + + const updated = await this.webhook.update({ + where: { + id: webhookId, + }, + data: { + url: data?.url, + enabled: data?.enabled, + }, + select: { + id: true, + url: true, + enabled: true, + events: true, + instanceId: true, + }, + }); + + return updated; + } catch (error) { + throw new BadRequestException([error?.message, error?.stack]); + } + } + + public async createLogs(instance: string, logs: CreateLogs) { + return await this.activityLogs.create({ + data: { + ...logs, + Instance: { + connect: { + name: instance, + }, + }, + }, + }); + } +}