Skip to content
Snippets Groups Projects
  • Sean Hatfield's avatar
    d789920a
    [FEAT] Automated audit logging (#667) · d789920a
    Sean Hatfield authored
    
    * WIP event logging - new table for events and new settings view for viewing
    
    * WIP add logging
    
    * UI for log rows
    
    * rename files to Logging to prevent getting gitignore
    
    * add metadata for all logging events and colored badges in logs page
    
    * remove unneeded comment
    
    * cleanup namespace for logging
    
    * clean up backend calls
    
    * update logging to show to => from settings changes
    
    * add logging for invitations, created, deleted, and accepted
    
    * add logging for user created, updated, suspended, or removed
    
    * add logging for workspace deleted
    
    * add logging for chat logs exported
    
    * add logging for API keys, LLM, embedder, vector db, embed chat, and reset button
    
    * modify event logs
    
    * update to event log types
    
    * simplify rendering of event badges
    
    ---------
    
    Co-authored-by: default avatartimothycarambat <rambat1010@gmail.com>
    [FEAT] Automated audit logging (#667)
    Sean Hatfield authored
    
    * WIP event logging - new table for events and new settings view for viewing
    
    * WIP add logging
    
    * UI for log rows
    
    * rename files to Logging to prevent getting gitignore
    
    * add metadata for all logging events and colored badges in logs page
    
    * remove unneeded comment
    
    * cleanup namespace for logging
    
    * clean up backend calls
    
    * update logging to show to => from settings changes
    
    * add logging for invitations, created, deleted, and accepted
    
    * add logging for user created, updated, suspended, or removed
    
    * add logging for workspace deleted
    
    * add logging for chat logs exported
    
    * add logging for API keys, LLM, embedder, vector db, embed chat, and reset button
    
    * modify event logs
    
    * update to event log types
    
    * simplify rendering of event badges
    
    ---------
    
    Co-authored-by: default avatartimothycarambat <rambat1010@gmail.com>
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
apiKeys.js 2.29 KiB
const prisma = require("../utils/prisma");

const ApiKey = {
  tablename: "api_keys",
  writable: [],

  makeSecret: () => {
    const uuidAPIKey = require("uuid-apikey");
    return uuidAPIKey.create().apiKey;
  },

  create: async function (createdByUserId = null) {
    try {
      const apiKey = await prisma.api_keys.create({
        data: {
          secret: this.makeSecret(),
          createdBy: createdByUserId,
        },
      });

      return { apiKey, error: null };
    } catch (error) {
      console.error("FAILED TO CREATE API KEY.", error.message);
      return { apiKey: null, error: error.message };
    }
  },

  get: async function (clause = {}) {
    try {
      const apiKey = await prisma.api_keys.findFirst({ where: clause });
      return apiKey;
    } catch (error) {
      console.error("FAILED TO GET API KEY.", error.message);
      return null;
    }
  },

  count: async function (clause = {}) {
    try {
      const count = await prisma.api_keys.count({ where: clause });
      return count;
    } catch (error) {
      console.error("FAILED TO COUNT API KEYS.", error.message);
      return 0;
    }
  },

  delete: async function (clause = {}) {
    try {
      await prisma.api_keys.deleteMany({ where: clause });
      return true;
    } catch (error) {
      console.error("FAILED TO DELETE API KEY.", error.message);
      return false;
    }
  },

  where: async function (clause = {}, limit) {
    try {
      const apiKeys = await prisma.api_keys.findMany({
        where: clause,
        take: limit,
      });
      return apiKeys;
    } catch (error) {
      console.error("FAILED TO GET API KEYS.", error.message);
      return [];
    }
  },
  whereWithUser: async function (clause = {}, limit) {
    try {
      const { User } = require("./user");
      const apiKeys = await this.where(clause, limit);

      for (const apiKey of apiKeys) {
        if (!apiKey.createdBy) continue;
        const user = await User.get({ id: apiKey.createdBy });
        if (!user) continue;

        apiKey.createdBy = {
          id: user.id,
          username: user.username,
          role: user.role,
        };
      }

      return apiKeys;
    } catch (error) {
      console.error("FAILED TO GET API KEYS WITH USER.", error.message);
      return [];
    }
  },
};

module.exports = { ApiKey };