From b53258ba561d8cfac2e35c42808dfe4e2405f959 Mon Sep 17 00:00:00 2001 From: wangsijie Date: Thu, 1 Jun 2023 16:04:15 +0800 Subject: [PATCH] fix(core): get cf id before insert (#3934) --- packages/core/src/libraries/domain.test.ts | 9 +++++---- packages/core/src/libraries/domain.ts | 22 +++++++++++----------- packages/core/src/routes/domain.test.ts | 9 +++++++-- packages/core/src/routes/domain.ts | 12 +++--------- 4 files changed, 26 insertions(+), 26 deletions(-) diff --git a/packages/core/src/libraries/domain.test.ts b/packages/core/src/libraries/domain.test.ts index 54f390264..8b6ff0ea6 100644 --- a/packages/core/src/libraries/domain.test.ts +++ b/packages/core/src/libraries/domain.test.ts @@ -30,8 +30,9 @@ const { MockQueries } = await import('#src/test-utils/tenant.js'); const { createDomainLibrary } = await import('./domain.js'); const updateDomainById = jest.fn(async (_, data) => data); -const { syncDomainStatus, addDomainToCloudflare } = createDomainLibrary( - new MockQueries({ domains: { updateDomainById } }) +const insertDomain = jest.fn(async (data) => data); +const { syncDomainStatus, addDomain } = createDomainLibrary( + new MockQueries({ domains: { updateDomainById, insertDomain } }) ); const fallbackOrigin = 'fake_origin'; @@ -51,9 +52,9 @@ afterAll(() => { describe('addDomainToCloudflare()', () => { it('should call createCustomHostname and return cloudflare data', async () => { - const response = await addDomainToCloudflare(mockDomain); + const response = await addDomain(mockDomain.domain); expect(createCustomHostname).toBeCalledTimes(1); - expect(updateDomainById).toBeCalledTimes(1); + expect(insertDomain).toBeCalledTimes(1); expect(response.cloudflareData).toMatchObject(mockCloudflareData); }); }); diff --git a/packages/core/src/libraries/domain.ts b/packages/core/src/libraries/domain.ts index 33210487e..8afddb487 100644 --- a/packages/core/src/libraries/domain.ts +++ b/packages/core/src/libraries/domain.ts @@ -4,6 +4,7 @@ import { type DomainDnsRecords, DomainStatus, } from '@logto/schemas'; +import { generateStandardId } from '@logto/shared'; import { conditional } from '@silverhand/essentials'; import type Queries from '#src/tenants/Queries.js'; @@ -26,7 +27,7 @@ const getDomainStatusFromCloudflareData = (data: CloudflareData): DomainStatus = export const createDomainLibrary = (queries: Queries) => { const { - domains: { updateDomainById }, + domains: { updateDomainById, insertDomain }, } = queries; const syncDomainStatusFromCloudflareData = async ( @@ -87,23 +88,22 @@ export const createDomainLibrary = (queries: Queries) => { ); }; - const addDomainToCloudflare = async (domain: Domain): Promise => { + const addDomain = async (hostname: string): Promise => { const { hostnameProviderConfig } = SystemContext.shared; assertThat(hostnameProviderConfig, 'domain.not_configured'); - const cloudflareData = await createCustomHostname(hostnameProviderConfig, domain.domain); - return syncDomainStatusFromCloudflareData( - { - ...domain, - cloudflareData, - }, + const cloudflareData = await createCustomHostname(hostnameProviderConfig, hostname); + + return insertDomain({ + domain: hostname, + id: generateStandardId(), cloudflareData, - hostnameProviderConfig.fallbackOrigin - ); + status: DomainStatus.PendingVerification, + }); }; return { syncDomainStatus, - addDomainToCloudflare, + addDomain, }; }; diff --git a/packages/core/src/routes/domain.test.ts b/packages/core/src/routes/domain.test.ts index 1d14f83aa..43a92c659 100644 --- a/packages/core/src/routes/domain.test.ts +++ b/packages/core/src/routes/domain.test.ts @@ -24,12 +24,17 @@ const domains = { }; const syncDomainStatus = jest.fn(async (domain: Domain): Promise => domain); -const addDomainToCloudflare = jest.fn(async (domain: Domain): Promise => domain); +const addDomain = jest.fn( + async (domain: string): Promise => ({ + ...mockDomain, + domain, + }) +); const mockLibraries = { domains: { syncDomainStatus, - addDomainToCloudflare, + addDomain, }, }; diff --git a/packages/core/src/routes/domain.ts b/packages/core/src/routes/domain.ts index 297a7944d..8d8c122c7 100644 --- a/packages/core/src/routes/domain.ts +++ b/packages/core/src/routes/domain.ts @@ -1,5 +1,4 @@ import { Domains, domainResponseGuard, domainSelectFields } from '@logto/schemas'; -import { generateStandardId } from '@logto/shared'; import { pick } from '@silverhand/essentials'; import { z } from 'zod'; @@ -13,10 +12,10 @@ export default function domainRoutes( ...[router, { queries, libraries }]: RouterInitArgs ) { const { - domains: { findAllDomains, findDomainById, insertDomain, deleteDomainById }, + domains: { findAllDomains, findDomainById, deleteDomainById }, } = queries; const { - domains: { syncDomainStatus, addDomainToCloudflare }, + domains: { syncDomainStatus, addDomain }, } = libraries; router.get( @@ -70,12 +69,7 @@ export default function domainRoutes( }) ); - const syncedDomain = await addDomainToCloudflare( - await insertDomain({ - ...ctx.guard.body, - id: generateStandardId(), - }) - ); + const syncedDomain = await addDomain(ctx.guard.body.domain); ctx.status = 201; ctx.body = pick(syncedDomain, ...domainSelectFields);