feat: prisma and remove mongodb

This commit is contained in:
Davidson Gomes
2024-06-06 14:47:58 -03:00
parent 35f97e08dd
commit 36ec67cef9
76 changed files with 1663 additions and 3894 deletions

View File

@@ -4,7 +4,7 @@ import { Auth, configService, Database } from '../../config/env.config';
import { Logger } from '../../config/logger.config';
import { ForbiddenException, UnauthorizedException } from '../../exceptions';
import { InstanceDto } from '../dto/instance.dto';
import { mongodbRepository } from '../server.module';
import { prismaRepository } from '../server.module';
const logger = new Logger('GUARD');
@@ -28,13 +28,18 @@ async function apikey(req: Request, _: Response, next: NextFunction) {
try {
if (param?.instanceName) {
const instanceKey = await mongodbRepository.auth.find(param.instanceName);
if (instanceKey?.apikey === key) {
const instance = await prismaRepository.instance.findUnique({
where: { name: param.instanceName },
include: { Auth: true },
});
if (instance.Auth?.apikey === key) {
return next();
}
} else {
if (req.originalUrl.includes('/instance/fetchInstances') && db.ENABLED) {
const instanceByKey = await mongodbRepository.auth.findByKey(key);
const instanceByKey = await prismaRepository.auth.findFirst({
where: { apikey: key },
});
if (instanceByKey) {
return next();
}

View File

@@ -10,7 +10,7 @@ import {
InternalServerErrorException,
NotFoundException,
} from '../../exceptions';
import { mongodbServer } from '../../libs/mongodb.connect';
import { prismaServer } from '../../libs/prisma.connect';
import { InstanceDto } from '../dto/instance.dto';
import { cache, waMonitor } from '../server.module';
@@ -28,11 +28,9 @@ async function getInstance(instanceName: string) {
}
if (db.ENABLED) {
const collection = mongodbServer
.getClient()
.db(db.CONNECTION.DB_PREFIX_NAME + '-instances')
.collection(instanceName);
return exists || (await collection.find({}).toArray()).length > 0;
const prisma = prismaServer;
return exists || (await prisma.instance.findMany({ where: { name: instanceName } })).length > 0;
}
return exists || existsSync(join(INSTANCE_DIR, instanceName));