storage.ts•3.59 kB
import { type User, type InsertUser, type GithubToken, type InsertGithubToken, type ToolExecution, type InsertToolExecution } from "@shared/schema";
import { randomUUID } from "crypto";
export interface IStorage {
getUser(id: string): Promise<User | undefined>;
getUserByUsername(username: string): Promise<User | undefined>;
createUser(user: InsertUser): Promise<User>;
getActiveGithubToken(): Promise<GithubToken | undefined>;
createGithubToken(token: InsertGithubToken): Promise<GithubToken>;
updateGithubToken(id: string, token: string): Promise<GithubToken>;
deactivateAllTokens(): Promise<void>;
createToolExecution(execution: InsertToolExecution): Promise<ToolExecution>;
getToolExecutions(): Promise<ToolExecution[]>;
updateToolExecutionResult(id: string, result: any, status: string): Promise<void>;
}
export class MemStorage implements IStorage {
private users: Map<string, User>;
private githubTokens: Map<string, GithubToken>;
private toolExecutions: Map<string, ToolExecution>;
constructor() {
this.users = new Map();
this.githubTokens = new Map();
this.toolExecutions = new Map();
}
async getUser(id: string): Promise<User | undefined> {
return this.users.get(id);
}
async getUserByUsername(username: string): Promise<User | undefined> {
return Array.from(this.users.values()).find(
(user) => user.username === username,
);
}
async createUser(insertUser: InsertUser): Promise<User> {
const id = randomUUID();
const user: User = { ...insertUser, id };
this.users.set(id, user);
return user;
}
async getActiveGithubToken(): Promise<GithubToken | undefined> {
return Array.from(this.githubTokens.values()).find(
(token) => token.isActive === true,
);
}
async createGithubToken(insertToken: InsertGithubToken): Promise<GithubToken> {
// Deactivate all existing tokens first
await this.deactivateAllTokens();
const id = randomUUID();
const now = new Date();
const token: GithubToken = {
...insertToken,
id,
isActive: true,
createdAt: now,
updatedAt: now
};
this.githubTokens.set(id, token);
return token;
}
async updateGithubToken(id: string, tokenValue: string): Promise<GithubToken> {
const existing = this.githubTokens.get(id);
if (!existing) {
throw new Error("Token not found");
}
const updated: GithubToken = {
...existing,
token: tokenValue,
updatedAt: new Date(),
};
this.githubTokens.set(id, updated);
return updated;
}
async deactivateAllTokens(): Promise<void> {
for (const [id, token] of Array.from(this.githubTokens.entries())) {
this.githubTokens.set(id, { ...token, isActive: false });
}
}
async createToolExecution(insertExecution: InsertToolExecution): Promise<ToolExecution> {
const id = randomUUID();
const execution: ToolExecution = {
...insertExecution,
id,
parameters: insertExecution.parameters || null,
result: null,
status: "pending",
executedAt: new Date(),
};
this.toolExecutions.set(id, execution);
return execution;
}
async getToolExecutions(): Promise<ToolExecution[]> {
return Array.from(this.toolExecutions.values());
}
async updateToolExecutionResult(id: string, result: any, status: string): Promise<void> {
const existing = this.toolExecutions.get(id);
if (existing) {
this.toolExecutions.set(id, { ...existing, result, status });
}
}
}
export const storage = new MemStorage();