feat: added subscriber endpoint for fetching active cert

This commit is contained in:
Sheen Capadngan
2025-05-18 23:37:23 +08:00
parent 10ed6f6b52
commit b358f2dbb7
8 changed files with 227 additions and 6 deletions
@@ -3,11 +3,24 @@ import { TableName } from "@app/db/schemas";
import { DatabaseError } from "@app/lib/errors";
import { ormify } from "@app/lib/knex";
import { CertStatus } from "./certificate-types";
export type TCertificateDALFactory = ReturnType<typeof certificateDALFactory>;
export const certificateDALFactory = (db: TDbClient) => {
const certificateOrm = ormify(db, TableName.Certificate);
const findLatestActiveCertForSubscriber = async ({ subscriberId }: { subscriberId: string }) => {
const cert = await db
.replicaNode()(TableName.Certificate)
.where({ pkiSubscriberId: subscriberId, status: CertStatus.ACTIVE })
.where("notAfter", ">", new Date())
.orderBy("notBefore", "desc")
.first();
return cert;
};
const countCertificatesInProject = async ({
projectId,
friendlyName,
@@ -65,6 +78,7 @@ export const certificateDALFactory = (db: TDbClient) => {
return {
...certificateOrm,
countCertificatesInProject,
countCertificatesForPkiSubscriber
countCertificatesForPkiSubscriber,
findLatestActiveCertForSubscriber
};
};