diff --git a/frontend/src/infra/template/createTemplate.ts b/frontend/src/infra/template/createTemplate.ts new file mode 100644 index 000000000..9d48ec1d7 --- /dev/null +++ b/frontend/src/infra/template/createTemplate.ts @@ -0,0 +1,34 @@ +import { Template, TemplateIntent } from '@/domain/Template' +import { ApiClient } from '../lib/apiClient' +import { BaseError } from '@/_lib/errors/BaseError' + +const createTemplateError = (message = 'Failed to create template') => { + const name = 'CreateTemplateError' + + return new BaseError({ message, name, code: name }) +} + +export const makeCreateTemplate = (apiClient: ApiClient) => async (template: TemplateIntent) => { + const validation = TemplateIntent.safeParse(template) + + if (!validation.success) { + throw createTemplateError( + validation.error.issues.reduce((acc, nextValue) => `${acc} ${nextValue.message}`, '') + ) + } + + try { + const response = await apiClient('/v1/timelog/templates', { + method: 'POST', + body: JSON.stringify(template) + }) + + if (!response.ok) { + throw createTemplateError() + } + + return { data: (await response.json()) as Template } + } catch (e) { + throw createTemplateError() + } +}