Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

43.5 KB
Raw Blame
import { Knex } from "knex";

import { TDbClient } from "@app/db";
import {
  AccessScope,
  SecretApprovalRequestsSchema,
  TableName,
  TIdentities,
  TMemberships,
  TSecretApprovalRequests,
  TSecretApprovalRequestsSecrets,
  TUserGroupMembership,
  TUsers
} from "@app/db/schemas";
import { DatabaseError } from "@app/lib/errors";
import { sanitizeSqlLikeString } from "@app/lib/fn";
import {
  ormify,
  rowWithSameIdIn,
  selectAllTableCols,
  sqlNestRelationships,
  stripUndefinedInWhere,
  TFindFilter
} from "@app/lib/knex";
import { OrderByDirection } from "@app/lib/types";

import { RequestState, SecretApprovalRequestOrderBy } from "./secret-approval-request-types";

export type TSecretApprovalRequestDALFactory = ReturnType<typeof secretApprovalRequestDALFactory>;

export type TSecretApprovalRequestListFilter = {
  projectId: string;
  userId?: string;
  status?: RequestState;
  environment?: string;
  committer?: string;
  limit?: number;
  offset?: number;
  search?: string;
  orderBy?: SecretApprovalRequestOrderBy;
  orderDirection?: OrderByDirection;
};

type TFindQueryFilter = TSecretApprovalRequestListFilter;

// Type for the query result documents in findByProjectId and findByProjectIdBridgeSecretV2
type TSecretApprovalRequestDoc = TSecretApprovalRequests & {
  environment: string;
  environmentName: string;
  projectId: string;
  policyId: string;
  policyName: string;
  policyApprovals: number;
  policySecretPath: string | null;
  policyEnforcementLevel: string;
  policyAllowedSelfApprovals: boolean;
  committerUserId: string | null;
  committerUserEmail: string | null;
  committerUserFirstName: string | null;
  committerUserLastName: string | null;
  committerUserUsername: string | null;
  committerIdentityId: string | null;
  committerIdentityName: string | null;
  reviewerId: string | null;
  reviewerUserId: string | null;
  reviewerStatus: string | null;
  approverUserId: string | null;
  approverGroupUserId: string | null;
  bypasserUserId: string | null;
  bypasserGroupUserId: string | null;
  commitId: string | null;
  commitOp: string | null;
  commitSecretId: string | null;
};

// Helper to filter approval requests by user access (committer, approver, or group member)
// Only applies filtering when userId is provided (users without SecretApprovalRequest.Read permission)
const buildUserAccessFilter = (qb: Knex.QueryBuilder, userId?: string) => {
  if (userId) {
    void qb.andWhere(
      (bd) =>
        void bd
          .where(`${TableName.SecretApprovalPolicyApprover}.approverUserId`, userId)
          .orWhere(`${TableName.SecretApprovalRequest}.committerUserId`, userId)
          .orWhere(`${TableName.UserGroupMembership}.userId`, userId)
    );
  }
};

const getOrderExpression = (orderBy = SecretApprovalRequestOrderBy.CreatedAt) => {
  switch (orderBy) {
    case SecretApprovalRequestOrderBy.Environment:
      return 'LOWER(COALESCE("environmentName", "environment", \'\'))';
    case SecretApprovalRequestOrderBy.SecretPath:
      return "LOWER(COALESCE(\"policySecretPath\", ''))";
    case SecretApprovalRequestOrderBy.Author:
      return `LOWER(COALESCE(
        NULLIF(TRIM(CONCAT_WS(' ', "committerUserFirstName", "committerUserLastName")), ''),
        "committerUserEmail",
        "committerIdentityName",
        ''
      ))`;
    case SecretApprovalRequestOrderBy.CreatedAt:
    default:
      return '"createdAt"';
  }
};

export const applyRequestOrdering = (
  query: Knex.QueryBuilder,
  orderBy?: SecretApprovalRequestOrderBy,
  orderDirection = OrderByDirection.DESC
) => query.orderByRaw(`${getOrderExpression(orderBy)} ${orderDirection}`).orderBy("id", OrderByDirection.ASC);

export const secretApprovalRequestDALFactory = (db: TDbClient) => {
  const secretApprovalRequestOrm = ormify(db, TableName.SecretApprovalRequest);

  const findQuery = (filter: TFindFilter<TSecretApprovalRequests>, tx: Knex) =>
    tx(TableName.SecretApprovalRequest)
      .where(filter)
      .whereNotExists(rowWithSameIdIn(TableName.ApprovalRequests, TableName.SecretApprovalRequest))
      .join(TableName.SecretFolder, `${TableName.SecretApprovalRequest}.folderId`, `${TableName.SecretFolder}.id`)
      .join(TableName.Environment, function joinActiveEnvForSecretFolder() {
        this.on(`${TableName.SecretFolder}.envId`, `${TableName.Environment}.id`).andOnNull(
          `${TableName.Environment}.deleteAfter`
        );
      })
      .join(TableName.Project, `${TableName.Environment}.projectId`, `${TableName.Project}.id`)
      .whereNull(`${TableName.Project}.deleteAfter`)
      .join(
        TableName.SecretApprovalPolicy,
        `${TableName.SecretApprovalRequest}.policyId`,
        `${TableName.SecretApprovalPolicy}.id`
      )
      .leftJoin(TableName.SecretApprovalPolicyEnvironment, (bd) => {
        bd.on(
          `${TableName.SecretApprovalPolicy}.id`,
          "=",
          `${TableName.SecretApprovalPolicyEnvironment}.policyId`
        ).andOn(`${TableName.SecretApprovalPolicyEnvironment}.envId`, "=", `${TableName.SecretFolder}.envId`);
      })
      .leftJoin<TUsers>(
        db(TableName.Users).as("statusChangedByUser"),
        `${TableName.SecretApprovalRequest}.statusChangedByUserId`,
        `statusChangedByUser.id`
      )
      .leftJoin<TUsers>(
        db(TableName.Users).as("committerUser"),
        `${TableName.SecretApprovalRequest}.committerUserId`,
        `committerUser.id`
      )
      .leftJoin<TIdentities>(
        db(TableName.Identity).as("committerIdentity"),
        `${TableName.SecretApprovalRequest}.committerIdentityId`,
        `committerIdentity.id`
      )
      .leftJoin(
        TableName.SecretApprovalPolicyApprover,
        `${TableName.SecretApprovalPolicy}.id`,
        `${TableName.SecretApprovalPolicyApprover}.policyId`
      )
      .leftJoin<TUsers>(
        db(TableName.Users).as("secretApprovalPolicyApproverUser"),
        `${TableName.SecretApprovalPolicyApprover}.approverUserId`,
        "secretApprovalPolicyApproverUser.id"
      )
      .leftJoin<TUserGroupMembership>(
        db(TableName.UserGroupMembership).as("approverUserGroupMembership"),
        `${TableName.SecretApprovalPolicyApprover}.approverGroupId`,
        `approverUserGroupMembership.groupId`
      )
      .leftJoin<TUsers>(
        db(TableName.Users).as("secretApprovalPolicyGroupApproverUser"),
        `approverUserGroupMembership.userId`,
        `secretApprovalPolicyGroupApproverUser.id`
      )
      .leftJoin(
        TableName.SecretApprovalPolicyBypasser,
        `${TableName.SecretApprovalPolicy}.id`,
        `${TableName.SecretApprovalPolicyBypasser}.policyId`
      )
      .leftJoin<TUsers>(
        db(TableName.Users).as("secretApprovalPolicyBypasserUser"),
        `${TableName.SecretApprovalPolicyBypasser}.bypasserUserId`,
        "secretApprovalPolicyBypasserUser.id"
      )
      .leftJoin<TUserGroupMembership>(
        db(TableName.UserGroupMembership).as("bypasserUserGroupMembership"),
        `${TableName.SecretApprovalPolicyBypasser}.bypasserGroupId`,
        `bypasserUserGroupMembership.groupId`
      )
      .leftJoin<TUsers>(
        db(TableName.Users).as("secretApprovalPolicyGroupBypasserUser"),
        `bypasserUserGroupMembership.userId`,
        `secretApprovalPolicyGroupBypasserUser.id`
      )
      .leftJoin(
        TableName.SecretApprovalRequestReviewer,
        `${TableName.SecretApprovalRequest}.id`,
        `${TableName.SecretApprovalRequestReviewer}.requestId`
      )
      .leftJoin<TUsers>(
        db(TableName.Users).as("secretApprovalReviewerUser"),
        `${TableName.SecretApprovalRequestReviewer}.reviewerUserId`,
        `secretApprovalReviewerUser.id`
      )

      .leftJoin<TMemberships>(db(TableName.Membership).as("approverOrgMembership"), (qb) => {
        qb.on(`${TableName.SecretApprovalPolicyApprover}.approverUserId`, `approverOrgMembership.actorUserId`)
          .andOn(`approverOrgMembership.scopeOrgId`, `${TableName.Project}.orgId`)
          .andOn(`approverOrgMembership.scope`, db.raw("?", [AccessScope.Organization]));
      })

      .leftJoin<TMemberships>(db(TableName.Membership).as("approverGroupOrgMembership"), (qb) => {
        qb.on(`secretApprovalPolicyGroupApproverUser.id`, `approverGroupOrgMembership.actorUserId`)
          .andOn(`approverGroupOrgMembership.scopeOrgId`, `${TableName.Project}.orgId`)
          .andOn(`approverGroupOrgMembership.scope`, db.raw("?", [AccessScope.Organization]));
      })
      .leftJoin<TMemberships>(db(TableName.Membership).as("reviewerOrgMembership"), (qb) => {
        qb.on(`${TableName.SecretApprovalRequestReviewer}.reviewerUserId`, `reviewerOrgMembership.actorUserId`)
          .andOn(`reviewerOrgMembership.scopeOrgId`, `${TableName.Project}.orgId`)
          .andOn(`reviewerOrgMembership.scope`, db.raw("?", [AccessScope.Organization]));
      })
      .select(selectAllTableCols(TableName.SecretApprovalRequest))
      .select(
        tx.ref("approverUserId").withSchema(TableName.SecretApprovalPolicyApprover),
        tx.ref("userId").withSchema("approverUserGroupMembership").as("approverGroupUserId"),
        tx.ref("email").withSchema("secretApprovalPolicyApproverUser").as("approverEmail"),
        tx.ref("isActive").withSchema("approverOrgMembership").as("approverIsOrgMembershipActive"),
        tx.ref("isActive").withSchema("approverGroupOrgMembership").as("approverGroupIsOrgMembershipActive"),
        tx.ref("email").withSchema("secretApprovalPolicyGroupApproverUser").as("approverGroupEmail"),
        tx.ref("username").withSchema("secretApprovalPolicyApproverUser").as("approverUsername"),
        tx.ref("username").withSchema("secretApprovalPolicyGroupApproverUser").as("approverGroupUsername"),
        tx.ref("firstName").withSchema("secretApprovalPolicyApproverUser").as("approverFirstName"),
        tx.ref("firstName").withSchema("secretApprovalPolicyGroupApproverUser").as("approverGroupFirstName"),
        tx.ref("lastName").withSchema("secretApprovalPolicyApproverUser").as("approverLastName"),
        tx.ref("lastName").withSchema("secretApprovalPolicyGroupApproverUser").as("approverGroupLastName"),

        // Bypasser fields
        tx.ref("bypasserUserId").withSchema(TableName.SecretApprovalPolicyBypasser),
        tx.ref("bypasserGroupId").withSchema(TableName.SecretApprovalPolicyBypasser),
        tx.ref("userId").withSchema("bypasserUserGroupMembership").as("bypasserGroupUserId"),
        tx.ref("email").withSchema("secretApprovalPolicyBypasserUser").as("bypasserEmail"),
        tx.ref("email").withSchema("secretApprovalPolicyGroupBypasserUser").as("bypasserGroupEmail"),
        tx.ref("username").withSchema("secretApprovalPolicyBypasserUser").as("bypasserUsername"),
        tx.ref("username").withSchema("secretApprovalPolicyGroupBypasserUser").as("bypasserGroupUsername"),
        tx.ref("firstName").withSchema("secretApprovalPolicyBypasserUser").as("bypasserFirstName"),
        tx.ref("firstName").withSchema("secretApprovalPolicyGroupBypasserUser").as("bypasserGroupFirstName"),
        tx.ref("lastName").withSchema("secretApprovalPolicyBypasserUser").as("bypasserLastName"),
        tx.ref("lastName").withSchema("secretApprovalPolicyGroupBypasserUser").as("bypasserGroupLastName"),

        tx.ref("email").withSchema("statusChangedByUser").as("statusChangedByUserEmail"),
        tx.ref("username").withSchema("statusChangedByUser").as("statusChangedByUserUsername"),
        tx.ref("firstName").withSchema("statusChangedByUser").as("statusChangedByUserFirstName"),
        tx.ref("lastName").withSchema("statusChangedByUser").as("statusChangedByUserLastName"),
        tx.ref("email").withSchema("committerUser").as("committerUserEmail"),
        tx.ref("username").withSchema("committerUser").as("committerUserUsername"),
        tx.ref("firstName").withSchema("committerUser").as("committerUserFirstName"),
        tx.ref("lastName").withSchema("committerUser").as("committerUserLastName"),
        tx.ref("id").withSchema("committerIdentity").as("committerIdentityId"),
        tx.ref("name").withSchema("committerIdentity").as("committerIdentityName"),
        tx.ref("reviewerUserId").withSchema(TableName.SecretApprovalRequestReviewer),
        tx.ref("status").withSchema(TableName.SecretApprovalRequestReviewer).as("reviewerStatus"),
        tx.ref("comment").withSchema(TableName.SecretApprovalRequestReviewer).as("reviewerComment"),
        tx.ref("createdAt").withSchema(TableName.SecretApprovalRequestReviewer).as("reviewerCreatedAt"),
        tx.ref("email").withSchema("secretApprovalReviewerUser").as("reviewerEmail"),
        tx.ref("username").withSchema("secretApprovalReviewerUser").as("reviewerUsername"),
        tx.ref("firstName").withSchema("secretApprovalReviewerUser").as("reviewerFirstName"),
        tx.ref("lastName").withSchema("secretApprovalReviewerUser").as("reviewerLastName"),
        tx.ref("isActive").withSchema("reviewerOrgMembership").as("reviewerIsOrgMembershipActive"),
        tx.ref("id").withSchema(TableName.SecretApprovalPolicy).as("policyId"),
        tx.ref("name").withSchema(TableName.SecretApprovalPolicy).as("policyName"),
        tx.ref("projectId").withSchema(TableName.Environment),
        tx.ref("slug").withSchema(TableName.Environment).as("environment"),
        tx.ref("secretPath").withSchema(TableName.SecretApprovalPolicy).as("policySecretPath"),
        tx.ref("envId").withSchema(TableName.SecretApprovalPolicyEnvironment).as("policyEnvId"),
        tx.ref("enforcementLevel").withSchema(TableName.SecretApprovalPolicy).as("policyEnforcementLevel"),
        tx.ref("allowedSelfApprovals").withSchema(TableName.SecretApprovalPolicy).as("policyAllowedSelfApprovals"),
        tx.ref("approvals").withSchema(TableName.SecretApprovalPolicy).as("policyApprovals"),
        tx.ref("deletedAt").withSchema(TableName.SecretApprovalPolicy).as("policyDeletedAt")
      );

  const findById = async (id: string, tx?: Knex) => {
    try {
      const sql = findQuery({ [`${TableName.SecretApprovalRequest}.id` as "id"]: id }, tx || db.replicaNode());
      const docs = await sql;
      const formattedDoc = sqlNestRelationships({
        data: docs,
        key: "id",
        parentMapper: (el) => ({
          ...SecretApprovalRequestsSchema.parse(el),
          projectId: el.projectId,
          environment: el.environment,
          statusChangedByUser: el.statusChangedByUserId
            ? {
                userId: el.statusChangedByUserId,
                email: el.statusChangedByUserEmail,
                firstName: el.statusChangedByUserFirstName,
                lastName: el.statusChangedByUserLastName,
                username: el.statusChangedByUserUsername
              }
            : undefined,
          committerUser: el.committerUserId
            ? {
                userId: el.committerUserId,
                email: el.committerUserEmail,
                firstName: el.committerUserFirstName,
                lastName: el.committerUserLastName,
                username: el.committerUserUsername
              }
            : null,
          committerIdentity: el.committerIdentityId
            ? {
                identityId: el.committerIdentityId,
                name: el.committerIdentityName ?? ""
              }
            : null,
          policy: {
            id: el.policyId,
            name: el.policyName,
            approvals: el.policyApprovals,
            secretPath: el.policySecretPath,
            enforcementLevel: el.policyEnforcementLevel,
            envId: el.policyEnvId,
            deletedAt: el.policyDeletedAt,
            allowedSelfApprovals: el.policyAllowedSelfApprovals
          }
        }),
        childrenMapper: [
          {
            key: "reviewerUserId",
            label: "reviewers" as const,
            mapper: ({
              reviewerUserId: userId,
              reviewerStatus: status,
              reviewerEmail: email,
              reviewerLastName: lastName,
              reviewerUsername: username,
              reviewerFirstName: firstName,
              reviewerComment: comment,
              reviewerCreatedAt: createdAt,
              reviewerIsOrgMembershipActive: isOrgMembershipActive
            }) =>
              userId
                ? {
                    userId,
                    status,
                    email,
                    firstName,
                    lastName,
                    username,
                    comment: comment ?? "",
                    createdAt,
                    isOrgMembershipActive
                  }
                : undefined
          },
          {
            key: "approverUserId",
            label: "approvers" as const,
            mapper: ({
              approverUserId: userId,
              approverEmail: email,
              approverUsername: username,
              approverLastName: lastName,
              approverFirstName: firstName,
              approverIsOrgMembershipActive: isOrgMembershipActive
            }) => ({
              userId,
              email,
              firstName,
              lastName,
              username,
              isOrgMembershipActive
            })
          },
          {
            key: "approverGroupUserId",
            label: "approvers" as const,
            mapper: ({
              approverGroupUserId: userId,
              approverGroupEmail: email,
              approverGroupUsername: username,
              approverGroupLastName: lastName,
              approverGroupFirstName: firstName,
              approverGroupIsOrgMembershipActive: isOrgMembershipActive
            }) => ({
              userId,
              email,
              firstName,
              lastName,
              username,
              isOrgMembershipActive
            })
          },
          {
            key: "bypasserUserId",
            label: "bypassers" as const,
            mapper: ({
              bypasserUserId: userId,
              bypasserEmail: email,
              bypasserUsername: username,
              bypasserLastName: lastName,
              bypasserFirstName: firstName
            }) => ({
              userId,
              email,
              firstName,
              lastName,
              username
            })
          },
          {
            key: "bypasserGroupUserId",
            label: "bypassers" as const,
            mapper: ({
              bypasserGroupUserId: userId,
              bypasserGroupEmail: email,
              bypasserGroupUsername: username,
              bypasserGroupLastName: lastName,
              bypasserGroupFirstName: firstName
            }) => ({
              userId,
              email,
              firstName,
              lastName,
              username
            })
          }
        ]
      });
      if (!formattedDoc?.[0]) return;
      return {
        ...formattedDoc[0],
        policy: {
          ...formattedDoc[0].policy,
          approvers: formattedDoc[0].approvers,
          bypassers: formattedDoc[0].bypassers
        }
      };
    } catch (error) {
      throw new DatabaseError({ error, name: "FindByIdSAR" });
    }
  };

  const findProjectRequestCount = async (projectId: string, userId?: string, policyId?: string, tx?: Knex) => {
    try {
      const docs = await (tx || db.replicaNode())
        .with(
          "temp",
          (tx || db.replicaNode())(TableName.SecretApprovalRequest)
            .join(TableName.SecretFolder, `${TableName.SecretApprovalRequest}.folderId`, `${TableName.SecretFolder}.id`)
            .join(TableName.Environment, function joinActiveEnvForSecretFolder() {
              this.on(`${TableName.SecretFolder}.envId`, `${TableName.Environment}.id`).andOnNull(
                `${TableName.Environment}.deleteAfter`
              );
            })
            .join(TableName.Project, `${TableName.Environment}.projectId`, `${TableName.Project}.id`)
            .whereNull(`${TableName.Project}.deleteAfter`)
            .join(
              TableName.SecretApprovalPolicy,
              `${TableName.SecretApprovalRequest}.policyId`,
              `${TableName.SecretApprovalPolicy}.id`
            )
            .leftJoin(
              TableName.SecretApprovalPolicyApprover,
              `${TableName.SecretApprovalPolicy}.id`,
              `${TableName.SecretApprovalPolicyApprover}.policyId`
            )
            .leftJoin(
              TableName.UserGroupMembership,
              `${TableName.SecretApprovalPolicyApprover}.approverGroupId`,
              `${TableName.UserGroupMembership}.groupId`
            )
            .where({ projectId })
            .whereNotExists(rowWithSameIdIn(TableName.ApprovalRequests, TableName.SecretApprovalRequest))
            .where((qb) => {
              if (policyId) void qb.where(`${TableName.SecretApprovalPolicy}.id`, policyId);
            })
            .modify((qb) => buildUserAccessFilter(qb, userId))
            .select("status", `${TableName.SecretApprovalRequest}.id`)
            .groupBy(`${TableName.SecretApprovalRequest}.id`, "status")
        )
        .select("status")
        .from("temp")
        .groupBy("status")
        .count("status");

      return {
        open: parseInt(
          (docs.find(({ status }) => status === RequestState.Open) as { count: string })?.count || "0",
          10
        ),
        closed: parseInt(
          (docs.find(({ status }) => status === RequestState.Closed) as { count: string })?.count || "0",
          10
        )
      };
    } catch (error) {
      throw new DatabaseError({ error, name: "FindRequestCount" });
    }
  };

  const findByProjectId = async (
    {
      status,
      limit = 20,
      offset = 0,
      projectId,
      committer,
      environment,
      userId,
      search,
      orderBy,
      orderDirection
    }: TFindQueryFilter,
    tx?: Knex
  ) => {
    try {
      // akhilmhdh: If ever u wanted a 1 to so many relationship connected with pagination
      // this is the place u wanna look at.
      const innerQuery = (tx || db.replicaNode())(TableName.SecretApprovalRequest)
        .join(TableName.SecretFolder, `${TableName.SecretApprovalRequest}.folderId`, `${TableName.SecretFolder}.id`)
        .join(TableName.Environment, function joinActiveEnvForSecretFolder() {
          this.on(`${TableName.SecretFolder}.envId`, `${TableName.Environment}.id`).andOnNull(
            `${TableName.Environment}.deleteAfter`
          );
        })
        .join(TableName.Project, `${TableName.Environment}.projectId`, `${TableName.Project}.id`)
        .whereNull(`${TableName.Project}.deleteAfter`)
        .join(
          TableName.SecretApprovalPolicy,
          `${TableName.SecretApprovalRequest}.policyId`,
          `${TableName.SecretApprovalPolicy}.id`
        )
        .leftJoin(
          TableName.SecretApprovalPolicyApprover,
          `${TableName.SecretApprovalPolicy}.id`,
          `${TableName.SecretApprovalPolicyApprover}.policyId`
        )
        .leftJoin(
          TableName.UserGroupMembership,
          `${TableName.SecretApprovalPolicyApprover}.approverGroupId`,
          `${TableName.UserGroupMembership}.groupId`
        )
        .leftJoin(
          TableName.SecretApprovalPolicyBypasser,
          `${TableName.SecretApprovalPolicy}.id`,
          `${TableName.SecretApprovalPolicyBypasser}.policyId`
        )
        .leftJoin<TUserGroupMembership>(
          db(TableName.UserGroupMembership).as("bypasserUserGroupMembership"),
          `${TableName.SecretApprovalPolicyBypasser}.bypasserGroupId`,
          `bypasserUserGroupMembership.groupId`
        )
        .leftJoin<TUsers>(
          db(TableName.Users).as("committerUser"),
          `${TableName.SecretApprovalRequest}.committerUserId`,
          `committerUser.id`
        )
        .leftJoin(
          db(TableName.Identity).as("committerIdentity"),
          `${TableName.SecretApprovalRequest}.committerIdentityId`,
          `committerIdentity.id`
        )
        .leftJoin(
          TableName.SecretApprovalRequestReviewer,
          `${TableName.SecretApprovalRequest}.id`,
          `${TableName.SecretApprovalRequestReviewer}.requestId`
        )
        .leftJoin<TSecretApprovalRequestsSecrets>(
          TableName.SecretApprovalRequestSecret,
          `${TableName.SecretApprovalRequestSecret}.requestId`,
          `${TableName.SecretApprovalRequest}.id`
        )
        .where(
          stripUndefinedInWhere({
            [`${TableName.Environment}.projectId` as "projectId"]: projectId,
            [`${TableName.Environment}.slug` as "slug"]: environment,
            [`${TableName.SecretApprovalRequest}.status`]: status,
            committerUserId: committer
          })
        )
        .whereNotExists(rowWithSameIdIn(TableName.ApprovalRequests, TableName.SecretApprovalRequest))
        .modify((qb) => buildUserAccessFilter(qb, userId))
        .select(selectAllTableCols(TableName.SecretApprovalRequest))
        .select(
          db.ref("projectId").withSchema(TableName.Environment),
          db.ref("slug").withSchema(TableName.Environment).as("environment"),
          db.ref("name").withSchema(TableName.Environment).as("environmentName"),
          db.ref("id").withSchema(TableName.SecretApprovalRequestReviewer).as("reviewerId"),
          db.ref("reviewerUserId").withSchema(TableName.SecretApprovalRequestReviewer),
          db.ref("status").withSchema(TableName.SecretApprovalRequestReviewer).as("reviewerStatus"),
          db.ref("id").withSchema(TableName.SecretApprovalPolicy).as("policyId"),
          db.ref("name").withSchema(TableName.SecretApprovalPolicy).as("policyName"),
          db.ref("op").withSchema(TableName.SecretApprovalRequestSecret).as("commitOp"),
          db.ref("secretId").withSchema(TableName.SecretApprovalRequestSecret).as("commitSecretId"),
          db.ref("id").withSchema(TableName.SecretApprovalRequestSecret).as("commitId"),
          db.ref("secretPath").withSchema(TableName.SecretApprovalPolicy).as("policySecretPath"),
          db.ref("enforcementLevel").withSchema(TableName.SecretApprovalPolicy).as("policyEnforcementLevel"),
          db.ref("allowedSelfApprovals").withSchema(TableName.SecretApprovalPolicy).as("policyAllowedSelfApprovals"),
          db.ref("approvals").withSchema(TableName.SecretApprovalPolicy).as("policyApprovals"),
          db.ref("approverUserId").withSchema(TableName.SecretApprovalPolicyApprover),
          db.ref("userId").withSchema(TableName.UserGroupMembership).as("approverGroupUserId"),

          // Bypasser fields
          db.ref("bypasserUserId").withSchema(TableName.SecretApprovalPolicyBypasser),
          db.ref("userId").withSchema("bypasserUserGroupMembership").as("bypasserGroupUserId"),

          db.ref("email").withSchema("committerUser").as("committerUserEmail"),
          db.ref("username").withSchema("committerUser").as("committerUserUsername"),
          db.ref("firstName").withSchema("committerUser").as("committerUserFirstName"),
          db.ref("lastName").withSchema("committerUser").as("committerUserLastName"),
          db.ref("id").withSchema("committerIdentity").as("committerIdentityId"),
          db.ref("name").withSchema("committerIdentity").as("committerIdentityName")
        )
        .distinctOn(`${TableName.SecretApprovalRequest}.id`)
        .as("inner");

      const query = (tx || db.replicaNode()).select("*").from(innerQuery);

      if (search) {
        void query.where((qb) => {
          void qb
            .whereRaw(`CONCAT_WS(' ', ??, ??) ilike ?`, [
              db.ref("committerUserFirstName"),
              db.ref("committerUserLastName"),
              `%${sanitizeSqlLikeString(search)}%`
            ])
            .orWhereRaw(`?? ilike ?`, [db.ref("committerUserUsername"), `%${sanitizeSqlLikeString(search)}%`])
            .orWhereRaw(`?? ilike ?`, [db.ref("committerUserEmail"), `%${sanitizeSqlLikeString(search)}%`])
            .orWhereRaw(`?? ilike ?`, [db.ref("committerIdentityName"), `%${sanitizeSqlLikeString(search)}%`])
            .orWhereILike("environmentName", `%${sanitizeSqlLikeString(search)}%`)
            .orWhereILike("environment", `%${sanitizeSqlLikeString(search)}%`)
            .orWhereILike("policySecretPath", `%${sanitizeSqlLikeString(search)}%`);
        });
      }

      void applyRequestOrdering(query, orderBy, orderDirection);

      // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
      const countResult = await (tx || db.replicaNode())
        .count({ count: "*" })
        .from(query.clone().clearOrder().as("count_query"))
        .first();
      // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
      const totalCount = Number(countResult?.count || 0);

      const docs = await query.clone().offset(offset).limit(limit);

      const formattedDoc = sqlNestRelationships({
        data: docs as TSecretApprovalRequestDoc[],
        key: "id",
        parentMapper: (el) => ({
          ...SecretApprovalRequestsSchema.parse(el),
          environment: el.environment,
          environmentName: el.environmentName,
          projectId: el.projectId,
          policy: {
            id: el.policyId,
            name: el.policyName,
            approvals: el.policyApprovals,
            secretPath: el.policySecretPath,
            enforcementLevel: el.policyEnforcementLevel,
            allowedSelfApprovals: el.policyAllowedSelfApprovals
          },
          committerUser: el.committerUserId
            ? {
                userId: el.committerUserId,
                email: el.committerUserEmail,
                firstName: el.committerUserFirstName,
                lastName: el.committerUserLastName,
                username: el.committerUserUsername!
              }
            : null,
          committerIdentity: el.committerIdentityId
            ? {
                identityId: el.committerIdentityId,
                name: el.committerIdentityName ?? ""
              }
            : null
        }),
        childrenMapper: [
          {
            key: "reviewerId",
            label: "reviewers" as const,
            mapper: ({ reviewerUserId, reviewerStatus: s }) =>
              reviewerUserId ? { userId: reviewerUserId, status: s! } : undefined
          },
          {
            key: "approverUserId",
            label: "approvers" as const,
            mapper: ({ approverUserId }) => ({ userId: approverUserId })
          },
          {
            key: "commitId",
            label: "commits" as const,
            mapper: ({ commitSecretId: secretId, commitOp: op }) => ({
              op: op!,
              secretId
            })
          },
          {
            key: "approverGroupUserId",
            label: "approvers" as const,
            mapper: ({ approverGroupUserId }) => ({ userId: approverGroupUserId })
          },
          {
            key: "bypasserUserId",
            label: "bypassers" as const,
            mapper: ({ bypasserUserId }) => ({ userId: bypasserUserId })
          },
          {
            key: "bypasserGroupUserId",
            label: "bypassers" as const,
            mapper: ({ bypasserGroupUserId }) => ({ userId: bypasserGroupUserId })
          }
        ]
      });
      return {
        approvals: formattedDoc.map((el) => ({
          ...el,
          policy: { ...el.policy, approvers: el.approvers, bypassers: el.bypassers }
        })),
        totalCount
      };
    } catch (error) {
      throw new DatabaseError({ error, name: "FindSAR" });
    }
  };

  const findByProjectIdBridgeSecretV2 = async (
    {
      status,
      limit = 20,
      offset = 0,
      projectId,
      committer,
      environment,
      userId,
      search,
      orderBy,
      orderDirection
    }: TFindQueryFilter,
    tx?: Knex
  ) => {
    try {
      // akhilmhdh: If ever u wanted a 1 to so many relationship connected with pagination
      // this is the place u wanna look at.
      const innerQuery = (tx || db.replicaNode())(TableName.SecretApprovalRequest)
        .join(TableName.SecretFolder, `${TableName.SecretApprovalRequest}.folderId`, `${TableName.SecretFolder}.id`)
        .join(TableName.Environment, function joinActiveEnvForSecretFolder() {
          this.on(`${TableName.SecretFolder}.envId`, `${TableName.Environment}.id`).andOnNull(
            `${TableName.Environment}.deleteAfter`
          );
        })
        .join(TableName.Project, `${TableName.Environment}.projectId`, `${TableName.Project}.id`)
        .whereNull(`${TableName.Project}.deleteAfter`)
        .join(
          TableName.SecretApprovalPolicy,
          `${TableName.SecretApprovalRequest}.policyId`,
          `${TableName.SecretApprovalPolicy}.id`
        )
        .leftJoin(
          TableName.SecretApprovalPolicyApprover,
          `${TableName.SecretApprovalPolicy}.id`,
          `${TableName.SecretApprovalPolicyApprover}.policyId`
        )
        .leftJoin(
          TableName.UserGroupMembership,
          `${TableName.SecretApprovalPolicyApprover}.approverGroupId`,
          `${TableName.UserGroupMembership}.groupId`
        )
        .leftJoin(
          TableName.SecretApprovalPolicyBypasser,
          `${TableName.SecretApprovalPolicy}.id`,
          `${TableName.SecretApprovalPolicyBypasser}.policyId`
        )
        .leftJoin<TUserGroupMembership>(
          db(TableName.UserGroupMembership).as("bypasserUserGroupMembership"),
          `${TableName.SecretApprovalPolicyBypasser}.bypasserGroupId`,
          `bypasserUserGroupMembership.groupId`
        )
        .leftJoin<TUsers>(
          db(TableName.Users).as("committerUser"),
          `${TableName.SecretApprovalRequest}.committerUserId`,
          `committerUser.id`
        )
        .leftJoin(
          db(TableName.Identity).as("committerIdentity"),
          `${TableName.SecretApprovalRequest}.committerIdentityId`,
          `committerIdentity.id`
        )
        .leftJoin(
          TableName.SecretApprovalRequestReviewer,
          `${TableName.SecretApprovalRequest}.id`,
          `${TableName.SecretApprovalRequestReviewer}.requestId`
        )
        .leftJoin<TSecretApprovalRequestsSecrets>(
          TableName.SecretApprovalRequestSecretV2,
          `${TableName.SecretApprovalRequestSecretV2}.requestId`,
          `${TableName.SecretApprovalRequest}.id`
        )
        .where(
          stripUndefinedInWhere({
            [`${TableName.Environment}.projectId` as "projectId"]: projectId,
            [`${TableName.Environment}.slug` as "slug"]: environment,
            [`${TableName.SecretApprovalRequest}.status`]: status,
            committerUserId: committer
          })
        )
        .whereNotExists(rowWithSameIdIn(TableName.ApprovalRequests, TableName.SecretApprovalRequest))
        .modify((qb) => buildUserAccessFilter(qb, userId))
        .select(selectAllTableCols(TableName.SecretApprovalRequest))
        .select(
          db.ref("projectId").withSchema(TableName.Environment),
          db.ref("slug").withSchema(TableName.Environment).as("environment"),
          db.ref("name").withSchema(TableName.Environment).as("environmentName"),
          db.ref("name").withSchema(TableName.SecretFolder).as("requestFolderPath"),
          db.ref("id").withSchema(TableName.SecretApprovalRequestReviewer).as("reviewerId"),
          db.ref("reviewerUserId").withSchema(TableName.SecretApprovalRequestReviewer),
          db.ref("status").withSchema(TableName.SecretApprovalRequestReviewer).as("reviewerStatus"),
          db.ref("id").withSchema(TableName.SecretApprovalPolicy).as("policyId"),
          db.ref("name").withSchema(TableName.SecretApprovalPolicy).as("policyName"),
          db.ref("op").withSchema(TableName.SecretApprovalRequestSecretV2).as("commitOp"),
          db.ref("secretId").withSchema(TableName.SecretApprovalRequestSecretV2).as("commitSecretId"),
          db.ref("id").withSchema(TableName.SecretApprovalRequestSecretV2).as("commitId"),
          db.ref("key").withSchema(TableName.SecretApprovalRequestSecretV2).as("secretKey"),
          db.ref("secretPath").withSchema(TableName.SecretApprovalPolicy).as("policySecretPath"),
          db.ref("allowedSelfApprovals").withSchema(TableName.SecretApprovalPolicy).as("policyAllowedSelfApprovals"),
          db.ref("approvals").withSchema(TableName.SecretApprovalPolicy).as("policyApprovals"),
          db.ref("enforcementLevel").withSchema(TableName.SecretApprovalPolicy).as("policyEnforcementLevel"),
          db.ref("approverUserId").withSchema(TableName.SecretApprovalPolicyApprover),
          db.ref("userId").withSchema(TableName.UserGroupMembership).as("approverGroupUserId"),

          // Bypasser
          db.ref("bypasserUserId").withSchema(TableName.SecretApprovalPolicyBypasser),
          db.ref("userId").withSchema("bypasserUserGroupMembership").as("bypasserGroupUserId"),

          db.ref("email").withSchema("committerUser").as("committerUserEmail"),
          db.ref("username").withSchema("committerUser").as("committerUserUsername"),
          db.ref("firstName").withSchema("committerUser").as("committerUserFirstName"),
          db.ref("lastName").withSchema("committerUser").as("committerUserLastName"),
          db.ref("id").withSchema("committerIdentity").as("committerIdentityId"),
          db.ref("name").withSchema("committerIdentity").as("committerIdentityName")
        )
        .as("inner");

      const query = (tx || db.replicaNode()).select("*").from(innerQuery);

      if (search) {
        void query.where((qb) => {
          void qb
            .whereRaw(`CONCAT_WS(' ', ??, ??) ilike ?`, [
              db.ref("committerUserFirstName"),
              db.ref("committerUserLastName"),
              `%${sanitizeSqlLikeString(search)}%`
            ])
            .orWhereRaw(`?? ilike ?`, [db.ref("committerUserUsername"), `%${sanitizeSqlLikeString(search)}%`])
            .orWhereRaw(`?? ilike ?`, [db.ref("committerUserEmail"), `%${sanitizeSqlLikeString(search)}%`])
            .orWhereRaw(`?? ilike ?`, [db.ref("committerIdentityName"), `%${sanitizeSqlLikeString(search)}%`])
            .orWhereILike(`environmentName`, `%${sanitizeSqlLikeString(search)}%`)
            .orWhereILike(`environment`, `%${sanitizeSqlLikeString(search)}%`)
            .orWhereILike(`policySecretPath`, `%${sanitizeSqlLikeString(search)}%`)
            .orWhereILike(`requestFolderPath`, `%${sanitizeSqlLikeString(search)}%`)
            .orWhereILike(`secretKey`, `%${sanitizeSqlLikeString(search)}%`);
        });
      }

      const distinctRequestsSub = query
        .clone()
        .clearSelect()
        .clearOrder()
        .distinctOn("id")
        .select(
          "id",
          "createdAt",
          "environmentName",
          "environment",
          "policySecretPath",
          "committerUserFirstName",
          "committerUserLastName",
          "committerUserEmail",
          "committerIdentityName"
        )
        .orderBy("id")
        .orderBy("createdAt", "desc");

      const distinctRequestsQuery = (tx || db.replicaNode()).select("*").from(distinctRequestsSub.as("requests"));
      void applyRequestOrdering(distinctRequestsQuery, orderBy, orderDirection);

      const pageIdsResult = (await distinctRequestsQuery
        .clone()
        .clearSelect()
        .select("id", db.raw("COUNT(*) OVER () as total"))
        .offset(offset)
        .limit(limit)) as Array<{ id: string; total: string }>;
      const pageIds = pageIdsResult.map(({ id }) => id);
      let totalCount = Number(pageIdsResult[0]?.total ?? 0);

      if (!pageIdsResult.length) {
        const countResult = (await (tx || db.replicaNode())
          .count({ count: "*" })
          .from(distinctRequestsQuery.clone().clearOrder().as("count_query"))
          .first()) as { count: string } | undefined;
        totalCount = Number(countResult?.count ?? 0);
      }

      // Phase 2: Full data for this page's request ids (all commit rows for display)
      const docs =
        pageIds.length === 0
          ? []
          : ((await (tx || db.replicaNode()).select("*").from(innerQuery).whereIn(`id`, pageIds)) as Awaited<
              typeof query
            >[number][]);

      const formattedDoc = sqlNestRelationships({
        data: docs as TSecretApprovalRequestDoc[],
        key: "id",
        parentMapper: (el) => ({
          ...SecretApprovalRequestsSchema.parse(el),
          environment: el.environment,
          environmentName: el.environmentName,
          projectId: el.projectId,
          policy: {
            id: el.policyId,
            name: el.policyName,
            approvals: el.policyApprovals,
            secretPath: el.policySecretPath,
            enforcementLevel: el.policyEnforcementLevel,
            allowedSelfApprovals: el.policyAllowedSelfApprovals
          },
          committerUser: el.committerUserId
            ? {
                userId: el.committerUserId,
                email: el.committerUserEmail,
                firstName: el.committerUserFirstName,
                lastName: el.committerUserLastName,
                username: el.committerUserUsername!
              }
            : null,
          committerIdentity: el.committerIdentityId
            ? {
                identityId: el.committerIdentityId,
                name: el.committerIdentityName ?? ""
              }
            : null
        }),
        childrenMapper: [
          {
            key: "reviewerId",
            label: "reviewers" as const,
            mapper: ({ reviewerUserId, reviewerStatus: s }) =>
              reviewerUserId ? { userId: reviewerUserId, status: s! } : undefined
          },
          {
            key: "approverUserId",
            label: "approvers" as const,
            mapper: ({ approverUserId }) => ({ userId: approverUserId })
          },
          {
            key: "commitId",
            label: "commits" as const,
            mapper: ({ commitSecretId: secretId, commitOp: op }) => ({
              op: op!,
              secretId
            })
          },
          {
            key: "approverGroupUserId",
            label: "approvers" as const,
            mapper: ({ approverGroupUserId }) => ({
              userId: approverGroupUserId
            })
          },
          {
            key: "bypasserUserId",
            label: "bypassers" as const,
            mapper: ({ bypasserUserId }) => ({ userId: bypasserUserId })
          },
          {
            key: "bypasserGroupUserId",
            label: "bypassers" as const,
            mapper: ({ bypasserGroupUserId }) => ({
              userId: bypasserGroupUserId
            })
          }
        ]
      });
      const pageOrder = new Map(pageIds.map((id, index) => [id, index]));
      const capped = formattedDoc
        .sort((requestOne, requestTwo) => pageOrder.get(requestOne.id)! - pageOrder.get(requestTwo.id)!)
        .slice(0, limit);
      const approvals = capped.map((el) => ({
        ...el,
        policy: { ...el.policy, approvers: el.approvers, bypassers: el.bypassers }
      }));
      return { approvals, totalCount };
    } catch (error) {
      throw new DatabaseError({ error, name: "FindSAR" });
    }
  };

  const findOpenByFolderIds = async (folderIds: string[], tx?: Knex) => {
    try {
      const docs = await (tx || db.replicaNode())(TableName.SecretApprovalRequest)
        .whereIn("folderId", folderIds)
        .where({ status: RequestState.Open })
        .whereNotExists(rowWithSameIdIn(TableName.ApprovalRequests, TableName.SecretApprovalRequest))
        .select("id", "folderId");
      return docs;
    } catch (error) {
      throw new DatabaseError({ error, name: "FindOpenByFolderIds" });
    }
  };

  const deleteByProjectId = async (projectId: string, tx?: Knex) => {
    try {
      const query = await (tx || db)(TableName.SecretApprovalRequest)
        .join(TableName.SecretFolder, `${TableName.SecretApprovalRequest}.folderId`, `${TableName.SecretFolder}.id`)
        .join(TableName.Environment, `${TableName.SecretFolder}.envId`, `${TableName.Environment}.id`)
        .where({ projectId })
        .whereNull(`${TableName.Environment}.deleteAfter`)
        .delete();

      return query;
    } catch (error) {
      throw new DatabaseError({ error, name: "DeleteByProjectId" });
    }
  };

  return {
    ...secretApprovalRequestOrm,
    findById,
    findProjectRequestCount,
    findByProjectId,
    findByProjectIdBridgeSecretV2,
    findOpenByFolderIds,
    deleteByProjectId
  };
};