Merge pull request #2461 from automatisch/aut-1551

feat(api): add delete template endpoint
This commit is contained in:
Ali BARIN
2025-04-25 12:54:03 +02:00
committed by GitHub
4 changed files with 61 additions and 1 deletions

View File

@@ -0,0 +1,11 @@
import Template from '../../../../models/template.ee.js';
export default async (request, response) => {
const template = await Template.query()
.findById(request.params.templateId)
.throwIfNotFound();
await template.$query().delete();
response.status(204).end();
};

View File

@@ -0,0 +1,47 @@
import Crypto from 'node:crypto';
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 app from '../../../../app.js';
import * as license from '../../../../helpers/license.ee.js';
import Template from '../../../../models/template.ee.js';
describe('DELETE /api/v1/templates/:templateId', () => {
let token;
beforeEach(async () => {
vi.spyOn(license, 'hasValidLicense').mockResolvedValue(true);
token = (await createApiToken()).token;
});
it('should delete the template', async () => {
const template = await createTemplate();
await request(app)
.delete(`/api/v1/templates/${template.id}`)
.set('x-api-token', token)
.expect(204);
const deletedTemplate = await Template.query().findById(template.id);
expect(deletedTemplate).toBeUndefined();
});
it('should return not found response for not existing template UUID', async () => {
const notExistingTemplateUUID = Crypto.randomUUID();
await request(app)
.delete(`/api/v1/templates/${notExistingTemplateUUID}`)
.set('x-api-token', token)
.expect(404);
});
it('should return bad request response for invalid UUID', async () => {
await request(app)
.delete('/api/v1/templates/invalidTemplateUUID')
.set('x-api-token', token)
.expect(400);
});
});

View File

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

View File

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