Merge pull request #2460 from automatisch/aut-1550

feat(api): add get template endpoint
This commit is contained in:
Ali BARIN
2025-04-25 12:04:01 +02:00
committed by GitHub
5 changed files with 66 additions and 2 deletions

View File

@@ -0,0 +1,10 @@
import { renderObject } from '../../../../helpers/renderer.js';
import Template from '../../../../models/template.ee.js';
export default async (request, response) => {
const template = await Template.query()
.findById(request.params.templateId)
.throwIfNotFound();
renderObject(response, template, { serializer: 'PublicTemplate' });
};

View File

@@ -0,0 +1,30 @@
import request from 'supertest';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { createApiToken } from '../../../../../test/factories/api-token.js';
import { createTemplate } from '../../../../../test/factories/template.js';
import getTemplateMock from '../../../../../test/mocks/rest/api/v1/templates/get-template.ee.js';
import app from '../../../../app.js';
import * as license from '../../../../helpers/license.ee.js';
describe('GET /api/v1/templates/:templateId', () => {
let token;
beforeEach(async () => {
vi.spyOn(license, 'hasValidLicense').mockResolvedValue(true);
token = (await createApiToken()).token;
});
it('should return templates', async () => {
const template = await createTemplate();
const response = await request(app)
.get(`/api/v1/templates/${template.id}`)
.set('x-api-token', token)
.expect(200);
const expectedPayload = await getTemplateMock(template);
expect(response.body).toStrictEqual(expectedPayload);
});
});

View File

@@ -1,8 +1,10 @@
import { Router } from 'express';
import getTemplateAction from '../../../controllers/api/v1/templates/get-template.ee.js';
import getTemplatesAction from '../../../controllers/api/v1/templates/get-templates.ee.js';
const router = Router();
router.get('/', getTemplatesAction);
router.get('/:templateId', getTemplateAction);
export default router;

View File

@@ -0,0 +1,22 @@
const getTemplateMock = async (template) => {
const data = {
id: template.id,
name: template.name,
createdAt: template.createdAt.getTime(),
updatedAt: template.updatedAt.getTime(),
flowData: template.getFlowDataWithIconUrls(),
};
return {
data: data,
meta: {
count: 1,
currentPage: null,
isArray: false,
totalPages: null,
type: 'Template',
},
};
};
export default getTemplateMock;

View File

@@ -29,8 +29,8 @@ export default defineConfig({
thresholds: {
autoUpdate: true,
statements: 99.41,
branches: 98.34,
functions: 99.06,
branches: 98.35,
functions: 99.07,
lines: 99.41,
},
},