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 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);
});
});

View file

@ -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<Domain> => {
const addDomain = async (hostname: string): Promise<Domain> => {
const { hostnameProviderConfig } = SystemContext.shared;
assertThat(hostnameProviderConfig, 'domain.not_configured');
const cloudflareData = await createCustomHostname(hostnameProviderConfig, domain.domain);
return syncDomainStatusFromCloudflareData(
{
...domain,
const cloudflareData = await createCustomHostname(hostnameProviderConfig, hostname);
return insertDomain({
domain: hostname,
id: generateStandardId(),
cloudflareData,
},
cloudflareData,
hostnameProviderConfig.fallbackOrigin
);
status: DomainStatus.PendingVerification,
});
};
return {
syncDomainStatus,
addDomainToCloudflare,
addDomain,
};
};

View file

@ -24,12 +24,17 @@ const domains = {
};
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 = {
domains: {
syncDomainStatus,
addDomainToCloudflare,
addDomain,
},
};

View file

@ -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<T extends AuthedRouter>(
...[router, { queries, libraries }]: RouterInitArgs<T>
) {
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<T extends AuthedRouter>(
})
);
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);