0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2025-03-10 22:22:45 -05:00

feat(core): add GET /custom-phrases route (#1935)

This commit is contained in:
IceHe 2022-09-15 20:15:33 +08:00 committed by GitHub
parent 577ca48c07
commit 5fe0cf4257
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 46 additions and 1 deletions

View file

@ -2,12 +2,21 @@ import { CreateCustomPhrase, CustomPhrase, CustomPhrases } from '@logto/schemas'
import { sql } from 'slonik';
import { buildInsertInto } from '@/database/insert-into';
import { convertToIdentifiers } from '@/database/utils';
import { convertToIdentifiers, manyRows } from '@/database/utils';
import envSet from '@/env-set';
import { DeletionError } from '@/errors/SlonikError';
const { table, fields } = convertToIdentifiers(CustomPhrases);
export const findAllCustomPhrases = async () =>
manyRows(
envSet.pool.query<CustomPhrase>(sql`
select ${sql.join(Object.values(fields), sql`,`)}
from ${table}
order by ${fields.languageKey}
`)
);
export const findCustomPhraseByLanguageKey = async (languageKey: string): Promise<CustomPhrase> =>
envSet.pool.one<CustomPhrase>(sql`
select ${sql.join(Object.values(fields), sql`, `)}

View file

@ -37,10 +37,13 @@ const findCustomPhraseByLanguageKey = jest.fn(async (languageKey: string) => {
return mockCustomPhrase;
});
const findAllCustomPhrases = jest.fn(async (): Promise<CustomPhrase[]> => []);
const upsertCustomPhrase = jest.fn(async (customPhrase: CustomPhrase) => customPhrase);
jest.mock('@/queries/custom-phrase', () => ({
deleteCustomPhraseByLanguageKey: async (key: string) => deleteCustomPhraseByLanguageKey(key),
findAllCustomPhrases: async () => findAllCustomPhrases(),
findCustomPhraseByLanguageKey: async (key: string) => findCustomPhraseByLanguageKey(key),
upsertCustomPhrase: async (customPhrase: CustomPhrase) => upsertCustomPhrase(customPhrase),
}));
@ -52,6 +55,26 @@ describe('customPhraseRoutes', () => {
jest.clearAllMocks();
});
describe('GET /custom-phrases', () => {
it('should call findAllCustomPhrases', async () => {
await customPhraseRequest.get('/custom-phrases');
expect(findAllCustomPhrases).toBeCalledTimes(1);
});
it('should return all custom phrases', async () => {
const mockCustomPhrase = {
languageKey: 'zh-HK',
translation: {
input: { username: '用戶名', password: '密碼' },
},
};
findAllCustomPhrases.mockImplementationOnce(async () => [mockCustomPhrase]);
const response = await customPhraseRequest.get('/custom-phrases');
expect(response.status).toEqual(200);
expect(response.body).toEqual([mockCustomPhrase]);
});
});
describe('GET /custom-phrases/:languageKey', () => {
it('should call findCustomPhraseByLanguageKey once', async () => {
await customPhraseRequest.get(`/custom-phrases/${mockLanguageKey}`);

View file

@ -3,6 +3,7 @@ import { CustomPhrases, translationGuard } from '@logto/schemas';
import koaGuard from '@/middleware/koa-guard';
import {
deleteCustomPhraseByLanguageKey,
findAllCustomPhrases,
findCustomPhraseByLanguageKey,
upsertCustomPhrase,
} from '@/queries/custom-phrase';
@ -10,6 +11,18 @@ import {
import { AuthedRouter } from './types';
export default function customPhraseRoutes<T extends AuthedRouter>(router: T) {
router.get(
'/custom-phrases',
koaGuard({
response: CustomPhrases.guard.array(),
}),
async (ctx, next) => {
ctx.body = await findAllCustomPhrases();
return next();
}
);
router.get(
'/custom-phrases/:languageKey',
koaGuard({