0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2024-12-16 20:26:19 -05:00

fix(core): get cf id before insert (#3934)

This commit is contained in:
wangsijie 2023-06-01 16:04:15 +08:00 committed by GitHub
parent a1ea4c388f
commit b53258ba56
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 26 additions and 26 deletions

View file

@ -30,8 +30,9 @@ const { MockQueries } = await import('#src/test-utils/tenant.js');
const { createDomainLibrary } = await import('./domain.js'); const { createDomainLibrary } = await import('./domain.js');
const updateDomainById = jest.fn(async (_, data) => data); const updateDomainById = jest.fn(async (_, data) => data);
const { syncDomainStatus, addDomainToCloudflare } = createDomainLibrary( const insertDomain = jest.fn(async (data) => data);
new MockQueries({ domains: { updateDomainById } }) const { syncDomainStatus, addDomain } = createDomainLibrary(
new MockQueries({ domains: { updateDomainById, insertDomain } })
); );
const fallbackOrigin = 'fake_origin'; const fallbackOrigin = 'fake_origin';
@ -51,9 +52,9 @@ afterAll(() => {
describe('addDomainToCloudflare()', () => { describe('addDomainToCloudflare()', () => {
it('should call createCustomHostname and return cloudflare data', async () => { 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(createCustomHostname).toBeCalledTimes(1);
expect(updateDomainById).toBeCalledTimes(1); expect(insertDomain).toBeCalledTimes(1);
expect(response.cloudflareData).toMatchObject(mockCloudflareData); expect(response.cloudflareData).toMatchObject(mockCloudflareData);
}); });
}); });

View file

@ -4,6 +4,7 @@ import {
type DomainDnsRecords, type DomainDnsRecords,
DomainStatus, DomainStatus,
} from '@logto/schemas'; } from '@logto/schemas';
import { generateStandardId } from '@logto/shared';
import { conditional } from '@silverhand/essentials'; import { conditional } from '@silverhand/essentials';
import type Queries from '#src/tenants/Queries.js'; import type Queries from '#src/tenants/Queries.js';
@ -26,7 +27,7 @@ const getDomainStatusFromCloudflareData = (data: CloudflareData): DomainStatus =
export const createDomainLibrary = (queries: Queries) => { export const createDomainLibrary = (queries: Queries) => {
const { const {
domains: { updateDomainById }, domains: { updateDomainById, insertDomain },
} = queries; } = queries;
const syncDomainStatusFromCloudflareData = async ( const syncDomainStatusFromCloudflareData = async (
@ -87,23 +88,22 @@ export const createDomainLibrary = (queries: Queries) => {
); );
}; };
const addDomainToCloudflare = async (domain: Domain): Promise<Domain> => { const addDomain = async (hostname: string): Promise<Domain> => {
const { hostnameProviderConfig } = SystemContext.shared; const { hostnameProviderConfig } = SystemContext.shared;
assertThat(hostnameProviderConfig, 'domain.not_configured'); assertThat(hostnameProviderConfig, 'domain.not_configured');
const cloudflareData = await createCustomHostname(hostnameProviderConfig, domain.domain); const cloudflareData = await createCustomHostname(hostnameProviderConfig, hostname);
return syncDomainStatusFromCloudflareData(
{ return insertDomain({
...domain, domain: hostname,
cloudflareData, id: generateStandardId(),
},
cloudflareData, cloudflareData,
hostnameProviderConfig.fallbackOrigin status: DomainStatus.PendingVerification,
); });
}; };
return { return {
syncDomainStatus, syncDomainStatus,
addDomainToCloudflare, addDomain,
}; };
}; };

View file

@ -24,12 +24,17 @@ const domains = {
}; };
const syncDomainStatus = jest.fn(async (domain: Domain): Promise<Domain> => domain); const syncDomainStatus = jest.fn(async (domain: Domain): Promise<Domain> => domain);
const addDomainToCloudflare = jest.fn(async (domain: Domain): Promise<Domain> => domain); const addDomain = jest.fn(
async (domain: string): Promise<Domain> => ({
...mockDomain,
domain,
})
);
const mockLibraries = { const mockLibraries = {
domains: { domains: {
syncDomainStatus, syncDomainStatus,
addDomainToCloudflare, addDomain,
}, },
}; };

View file

@ -1,5 +1,4 @@
import { Domains, domainResponseGuard, domainSelectFields } from '@logto/schemas'; import { Domains, domainResponseGuard, domainSelectFields } from '@logto/schemas';
import { generateStandardId } from '@logto/shared';
import { pick } from '@silverhand/essentials'; import { pick } from '@silverhand/essentials';
import { z } from 'zod'; import { z } from 'zod';
@ -13,10 +12,10 @@ export default function domainRoutes<T extends AuthedRouter>(
...[router, { queries, libraries }]: RouterInitArgs<T> ...[router, { queries, libraries }]: RouterInitArgs<T>
) { ) {
const { const {
domains: { findAllDomains, findDomainById, insertDomain, deleteDomainById }, domains: { findAllDomains, findDomainById, deleteDomainById },
} = queries; } = queries;
const { const {
domains: { syncDomainStatus, addDomainToCloudflare }, domains: { syncDomainStatus, addDomain },
} = libraries; } = libraries;
router.get( router.get(
@ -70,12 +69,7 @@ export default function domainRoutes<T extends AuthedRouter>(
}) })
); );
const syncedDomain = await addDomainToCloudflare( const syncedDomain = await addDomain(ctx.guard.body.domain);
await insertDomain({
...ctx.guard.body,
id: generateStandardId(),
})
);
ctx.status = 201; ctx.status = 201;
ctx.body = pick(syncedDomain, ...domainSelectFields); ctx.body = pick(syncedDomain, ...domainSelectFields);