ddd refactor: api-keys repo

This commit is contained in:
Ramnique Singh 2025-08-15 12:57:19 +05:30
parent ab81fa3c46
commit 0d50777b93
4 changed files with 14 additions and 5 deletions

View file

@ -1,7 +1,7 @@
'use server'; 'use server';
import { redirect } from "next/navigation"; import { redirect } from "next/navigation";
import { ObjectId } from "mongodb"; import { ObjectId } from "mongodb";
import { db, dataSourcesCollection, projectsCollection, projectMembersCollection, apiKeysCollection, dataSourceDocsCollection } from "../lib/mongodb"; import { db, dataSourcesCollection, projectsCollection, projectMembersCollection, dataSourceDocsCollection } from "../lib/mongodb";
import { z } from 'zod'; import { z } from 'zod';
import crypto from 'crypto'; import crypto from 'crypto';
import { revalidatePath } from "next/cache"; import { revalidatePath } from "next/cache";
@ -18,12 +18,14 @@ import { IProjectActionAuthorizationPolicy } from "@/src/application/policies/pr
import { ICreateApiKeyController } from "@/src/interface-adapters/controllers/api-keys/create-api-key.controller"; import { ICreateApiKeyController } from "@/src/interface-adapters/controllers/api-keys/create-api-key.controller";
import { IListApiKeysController } from "@/src/interface-adapters/controllers/api-keys/list-api-keys.controller"; import { IListApiKeysController } from "@/src/interface-adapters/controllers/api-keys/list-api-keys.controller";
import { IDeleteApiKeyController } from "@/src/interface-adapters/controllers/api-keys/delete-api-key.controller"; import { IDeleteApiKeyController } from "@/src/interface-adapters/controllers/api-keys/delete-api-key.controller";
import { IApiKeysRepository } from "@/src/application/repositories/api-keys.repository.interface";
const KLAVIS_API_KEY = process.env.KLAVIS_API_KEY || ''; const KLAVIS_API_KEY = process.env.KLAVIS_API_KEY || '';
const projectActionAuthorizationPolicy = container.resolve<IProjectActionAuthorizationPolicy>('projectActionAuthorizationPolicy'); const projectActionAuthorizationPolicy = container.resolve<IProjectActionAuthorizationPolicy>('projectActionAuthorizationPolicy');
const createApiKeyController = container.resolve<ICreateApiKeyController>('createApiKeyController'); const createApiKeyController = container.resolve<ICreateApiKeyController>('createApiKeyController');
const listApiKeysController = container.resolve<IListApiKeysController>('listApiKeysController'); const listApiKeysController = container.resolve<IListApiKeysController>('listApiKeysController');
const deleteApiKeyController = container.resolve<IDeleteApiKeyController>('deleteApiKeyController'); const deleteApiKeyController = container.resolve<IDeleteApiKeyController>('deleteApiKeyController');
const apiKeysRepository = container.resolve<IApiKeysRepository>('apiKeysRepository');
export async function listTemplates() { export async function listTemplates() {
const templatesArray = Object.entries(templates) const templatesArray = Object.entries(templates)
@ -228,9 +230,7 @@ export async function deleteProject(projectId: string) {
await projectAuthCheck(projectId); await projectAuthCheck(projectId);
// delete api keys // delete api keys
await apiKeysCollection.deleteMany({ await apiKeysRepository.deleteAll(projectId);
projectId,
});
// delete embeddings // delete embeddings
const sources = await dataSourcesCollection.find({ const sources = await dataSourcesCollection.find({

View file

@ -19,7 +19,6 @@ export const dataSourceDocsCollection = db.collection<z.infer<typeof DataSourceD
export const projectsCollection = db.collection<z.infer<typeof Project>>("projects"); export const projectsCollection = db.collection<z.infer<typeof Project>>("projects");
export const projectMembersCollection = db.collection<z.infer<typeof ProjectMember>>("project_members"); export const projectMembersCollection = db.collection<z.infer<typeof ProjectMember>>("project_members");
export const agentWorkflowsCollection = db.collection<z.infer<typeof Workflow>>("agent_workflows"); export const agentWorkflowsCollection = db.collection<z.infer<typeof Workflow>>("agent_workflows");
export const apiKeysCollection = db.collection<z.infer<typeof ApiKey>>("api_keys");
export const chatsCollection = db.collection<z.infer<typeof apiV1.Chat>>("chats"); export const chatsCollection = db.collection<z.infer<typeof apiV1.Chat>>("chats");
export const chatMessagesCollection = db.collection<z.infer<typeof apiV1.ChatMessage>>("chat_messages"); export const chatMessagesCollection = db.collection<z.infer<typeof apiV1.ChatMessage>>("chat_messages");
export const twilioConfigsCollection = db.collection<z.infer<typeof TwilioConfig>>("twilio_configs"); export const twilioConfigsCollection = db.collection<z.infer<typeof TwilioConfig>>("twilio_configs");

View file

@ -31,6 +31,12 @@ export interface IApiKeysRepository {
*/ */
delete(projectId: string, id: string): Promise<boolean>; delete(projectId: string, id: string): Promise<boolean>;
/**
* Deletes all API keys for a given project.
* @param projectId - The ID of the project.
*/
deleteAll(projectId: string): Promise<void>;
/** /**
* Checks if an API key is valid for a project and consumes it (e.g., for rate limiting or one-time use). * Checks if an API key is valid for a project and consumes it (e.g., for rate limiting or one-time use).
* @param projectId - The ID of the project. * @param projectId - The ID of the project.

View file

@ -56,4 +56,8 @@ export class MongoDBApiKeysRepository implements IApiKeysRepository {
const result = await this.collection.deleteOne({ projectId, _id: new ObjectId(id) }); const result = await this.collection.deleteOne({ projectId, _id: new ObjectId(id) });
return result.deletedCount > 0; return result.deletedCount > 0;
} }
async deleteAll(projectId: string): Promise<void> {
await this.collection.deleteMany({ projectId });
}
} }