Skip to content

Commit

Permalink
⚙️ general configuration for postgres
Browse files Browse the repository at this point in the history
  • Loading branch information
jrCleber committed Dec 9, 2023
1 parent 9b574ec commit 7b2389f
Showing 1 changed file with 137 additions and 0 deletions.
137 changes: 137 additions & 0 deletions 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<T> {
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<Webhook> & { 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,
},
},
},
});
}
}

0 comments on commit 7b2389f

Please sign in to comment.