Refine code

This commit is contained in:
Fang-Pen Lin
2025-10-28 17:10:30 -07:00
parent f0d2c30384
commit ebd2c8ae45

View File

@@ -31,7 +31,7 @@ import {
TRawJwsPayload, TRawJwsPayload,
TRespondToAcmeChallengeResponse TRespondToAcmeChallengeResponse
} from "./pki-acme-types"; } from "./pki-acme-types";
import { TPkiAcmeAccount } from "@app/db/schemas/pki-acme-accounts"; import { TPkiAcmeAccounts } from "@app/db/schemas/pki-acme-accounts";
import { TPkiAcmeAccountDALFactory } from "./pki-acme-account-dal"; import { TPkiAcmeAccountDALFactory } from "./pki-acme-account-dal";
type TPkiAcmeServiceFactoryDep = { type TPkiAcmeServiceFactoryDep = {
@@ -109,26 +109,26 @@ export const pkiAcmeServiceFactory = ({
{ onlyReturnExisting, contact }: TCreateAcmeAccountPayload { onlyReturnExisting, contact }: TCreateAcmeAccountPayload
): Promise<TAcmeResponse<TCreateAcmeAccountResponse>> => { ): Promise<TAcmeResponse<TCreateAcmeAccountResponse>> => {
const profile = await validateAcmeProfile(profileId); const profile = await validateAcmeProfile(profileId);
let account: TPkiAcmeAccount | null = await pkiAcmeAccountDAL.findByPublicKey(jwk); const existingAccount: TPkiAcmeAccounts | null = await pkiAcmeAccountDAL.findByPublicKey(jwk);
if (onlyReturnExisting && !account) { if (onlyReturnExisting && !existingAccount) {
throw new AcmeAccountDoesNotExistError({ message: "ACME account not found" }); throw new AcmeAccountDoesNotExistError({ message: "ACME account not found" });
} }
if (account) { if (existingAccount) {
// With the same public key, we found an existing account, just return it // With the same public key, we found an existing account, just return it
return { return {
status: 200, status: 200,
body: { body: {
status: "valid", status: "valid",
contact: account.emails, contact: existingAccount.emails,
orders: buildUrl(`/api/v1/pki/acme/profiles/${profile.id}/accounts/${account.id}/orders`) orders: buildUrl(`/api/v1/pki/acme/profiles/${profile.id}/accounts/${existingAccount.id}/orders`)
}, },
headers: { headers: {
Location: buildUrl(`/api/v1/pki/acme/profiles/${profile.id}/accounts/${account.id}`) Location: buildUrl(`/api/v1/pki/acme/profiles/${profile.id}/accounts/${existingAccount.id}`)
} }
}; };
} }
account = await pkiAcmeAccountDAL.create({ const newAccount = await pkiAcmeAccountDAL.create({
profileId: profile.id, profileId: profile.id,
publicKey: jwk, publicKey: jwk,
emails: contact ?? [] emails: contact ?? []
@@ -138,11 +138,11 @@ export const pkiAcmeServiceFactory = ({
status: 201, status: 201,
body: { body: {
status: "valid", status: "valid",
contact: account.emails, contact: newAccount.emails,
orders: buildUrl(`/api/v1/pki/acme/profiles/${profile.id}/accounts/${account.id}/orders`) orders: buildUrl(`/api/v1/pki/acme/profiles/${profile.id}/accounts/${newAccount.id}/orders`)
}, },
headers: { headers: {
Location: buildUrl(`/api/v1/pki/acme/profiles/${profile.id}/accounts/${account.id}`) Location: buildUrl(`/api/v1/pki/acme/profiles/${profile.id}/accounts/${newAccount.id}`)
} }
}; };
}; };