2021-04-22 10:07:10 +02:00
|
|
|
import { Request, Response } from 'express';
|
|
|
|
import Controller from '../controller';
|
2022-06-22 12:49:18 +02:00
|
|
|
import { IUnleashConfig, IUnleashServices } from '../../types';
|
2021-04-22 10:07:10 +02:00
|
|
|
import { Logger } from '../../logger';
|
|
|
|
import AddonService from '../../services/addon-service';
|
2021-01-19 10:42:45 +01:00
|
|
|
|
2021-09-14 19:58:48 +02:00
|
|
|
import { extractUsername } from '../../util/extract-user';
|
2021-08-12 15:04:37 +02:00
|
|
|
import {
|
|
|
|
CREATE_ADDON,
|
|
|
|
DELETE_ADDON,
|
2022-06-22 12:49:18 +02:00
|
|
|
NONE,
|
|
|
|
UPDATE_ADDON,
|
2021-08-12 15:04:37 +02:00
|
|
|
} from '../../types/permissions';
|
2021-09-14 19:58:48 +02:00
|
|
|
import { IAuthRequest } from '../unleash-types';
|
2022-07-01 08:06:33 +02:00
|
|
|
import { createRequestSchema } from '../../openapi/util/create-request-schema';
|
|
|
|
import { createResponseSchema } from '../../openapi/util/create-response-schema';
|
2022-06-22 12:49:18 +02:00
|
|
|
import { OpenApiService } from '../../services/openapi-service';
|
|
|
|
import { AddonSchema, addonSchema } from '../../openapi/spec/addon-schema';
|
|
|
|
import { serializeDates } from '../../types/serialize-dates';
|
|
|
|
import { AddonsSchema, addonsSchema } from '../../openapi/spec/addons-schema';
|
2022-06-30 14:48:39 +02:00
|
|
|
import { emptyResponse } from '../../openapi/util/standard-responses';
|
2022-06-22 12:49:18 +02:00
|
|
|
|
|
|
|
type AddonServices = Pick<IUnleashServices, 'addonService' | 'openApiService'>;
|
|
|
|
|
|
|
|
const PATH = '/';
|
2021-01-19 10:42:45 +01:00
|
|
|
|
|
|
|
class AddonController extends Controller {
|
2021-04-22 10:07:10 +02:00
|
|
|
private logger: Logger;
|
|
|
|
|
|
|
|
private addonService: AddonService;
|
|
|
|
|
2022-06-22 12:49:18 +02:00
|
|
|
private openApiService: OpenApiService;
|
|
|
|
|
2021-04-22 10:07:10 +02:00
|
|
|
constructor(
|
|
|
|
config: IUnleashConfig,
|
2022-06-22 12:49:18 +02:00
|
|
|
{ addonService, openApiService }: AddonServices,
|
2021-04-22 10:07:10 +02:00
|
|
|
) {
|
2021-01-19 10:42:45 +01:00
|
|
|
super(config);
|
2021-08-12 15:04:37 +02:00
|
|
|
this.logger = config.getLogger('/admin-api/addon.ts');
|
2021-01-19 10:42:45 +01:00
|
|
|
this.addonService = addonService;
|
2022-06-22 12:49:18 +02:00
|
|
|
this.openApiService = openApiService;
|
|
|
|
|
|
|
|
this.route({
|
|
|
|
method: 'get',
|
|
|
|
path: '',
|
|
|
|
permission: NONE,
|
|
|
|
handler: this.getAddons,
|
|
|
|
middleware: [
|
|
|
|
openApiService.validPath({
|
|
|
|
tags: ['admin'],
|
|
|
|
operationId: 'getAddons',
|
|
|
|
responses: {
|
|
|
|
200: createResponseSchema('addonsSchema'),
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
});
|
|
|
|
|
|
|
|
this.route({
|
|
|
|
method: 'post',
|
|
|
|
path: '',
|
|
|
|
handler: this.createAddon,
|
|
|
|
permission: CREATE_ADDON,
|
|
|
|
middleware: [
|
|
|
|
openApiService.validPath({
|
|
|
|
tags: ['admin'],
|
|
|
|
operationId: 'createAddon',
|
|
|
|
requestBody: createRequestSchema('addonSchema'),
|
|
|
|
responses: { 200: createResponseSchema('addonSchema') },
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
});
|
2021-01-19 10:42:45 +01:00
|
|
|
|
2022-06-22 12:49:18 +02:00
|
|
|
this.route({
|
|
|
|
method: 'get',
|
|
|
|
path: `${PATH}:id`,
|
|
|
|
handler: this.getAddon,
|
|
|
|
permission: NONE,
|
|
|
|
middleware: [
|
|
|
|
openApiService.validPath({
|
|
|
|
tags: ['admin'],
|
|
|
|
operationId: 'getAddon',
|
|
|
|
responses: { 200: createResponseSchema('addonSchema') },
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
});
|
|
|
|
|
|
|
|
this.route({
|
|
|
|
method: 'put',
|
|
|
|
path: `${PATH}:id`,
|
|
|
|
handler: this.updateAddon,
|
|
|
|
permission: UPDATE_ADDON,
|
|
|
|
middleware: [
|
|
|
|
openApiService.validPath({
|
|
|
|
tags: ['admin'],
|
|
|
|
operationId: 'updateAddon',
|
|
|
|
requestBody: createRequestSchema('addonSchema'),
|
|
|
|
responses: { 200: createResponseSchema('addonSchema') },
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
});
|
|
|
|
|
|
|
|
this.route({
|
|
|
|
method: 'delete',
|
|
|
|
path: `${PATH}:id`,
|
|
|
|
handler: this.deleteAddon,
|
|
|
|
acceptAnyContentType: true,
|
|
|
|
permission: DELETE_ADDON,
|
|
|
|
middleware: [
|
|
|
|
openApiService.validPath({
|
|
|
|
tags: ['admin'],
|
|
|
|
operationId: 'deleteAddon',
|
|
|
|
responses: { 200: emptyResponse },
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
});
|
2021-01-19 10:42:45 +01:00
|
|
|
}
|
|
|
|
|
2022-06-22 12:49:18 +02:00
|
|
|
async getAddons(req: Request, res: Response<AddonsSchema>): Promise<void> {
|
2021-08-13 10:36:19 +02:00
|
|
|
const addons = await this.addonService.getAddons();
|
|
|
|
const providers = this.addonService.getProviderDefinitions();
|
2022-06-22 12:49:18 +02:00
|
|
|
|
|
|
|
this.openApiService.respondWithValidation(200, res, addonsSchema.$id, {
|
|
|
|
addons: serializeDates(addons),
|
|
|
|
providers: serializeDates(providers),
|
|
|
|
});
|
2021-01-19 10:42:45 +01:00
|
|
|
}
|
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
async getAddon(
|
|
|
|
req: Request<{ id: number }, any, any, any>,
|
2022-06-22 12:49:18 +02:00
|
|
|
res: Response<AddonSchema>,
|
2021-08-12 15:04:37 +02:00
|
|
|
): Promise<void> {
|
2021-01-19 10:42:45 +01:00
|
|
|
const { id } = req.params;
|
2021-08-13 10:36:19 +02:00
|
|
|
const addon = await this.addonService.getAddon(id);
|
2022-06-22 12:49:18 +02:00
|
|
|
this.openApiService.respondWithValidation(
|
|
|
|
200,
|
|
|
|
res,
|
|
|
|
addonSchema.$id,
|
|
|
|
serializeDates(addon),
|
|
|
|
);
|
2021-01-19 10:42:45 +01:00
|
|
|
}
|
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
async updateAddon(
|
2021-09-14 19:58:48 +02:00
|
|
|
req: IAuthRequest<{ id: number }, any, any, any>,
|
2022-06-22 12:49:18 +02:00
|
|
|
res: Response<AddonSchema>,
|
2021-08-12 15:04:37 +02:00
|
|
|
): Promise<void> {
|
2021-01-19 10:42:45 +01:00
|
|
|
const { id } = req.params;
|
2021-09-14 19:58:48 +02:00
|
|
|
const createdBy = extractUsername(req);
|
2021-01-19 10:42:45 +01:00
|
|
|
const data = req.body;
|
|
|
|
|
2021-08-13 10:36:19 +02:00
|
|
|
const addon = await this.addonService.updateAddon(id, data, createdBy);
|
2022-06-22 12:49:18 +02:00
|
|
|
|
|
|
|
this.openApiService.respondWithValidation(
|
|
|
|
200,
|
|
|
|
res,
|
|
|
|
addonSchema.$id,
|
|
|
|
serializeDates(addon),
|
|
|
|
);
|
2021-01-19 10:42:45 +01:00
|
|
|
}
|
|
|
|
|
2022-06-22 12:49:18 +02:00
|
|
|
async createAddon(
|
|
|
|
req: IAuthRequest<AddonSchema, any, any, any>,
|
|
|
|
res: Response<AddonSchema>,
|
|
|
|
): Promise<void> {
|
2021-09-14 19:58:48 +02:00
|
|
|
const createdBy = extractUsername(req);
|
2021-01-19 10:42:45 +01:00
|
|
|
const data = req.body;
|
2021-08-13 10:36:19 +02:00
|
|
|
const addon = await this.addonService.createAddon(data, createdBy);
|
2022-06-22 12:49:18 +02:00
|
|
|
|
|
|
|
this.openApiService.respondWithValidation(
|
|
|
|
201,
|
|
|
|
res,
|
|
|
|
addonSchema.$id,
|
|
|
|
serializeDates(addon),
|
|
|
|
);
|
2021-01-19 10:42:45 +01:00
|
|
|
}
|
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
async deleteAddon(
|
2021-09-14 19:58:48 +02:00
|
|
|
req: IAuthRequest<{ id: number }, any, any, any>,
|
2022-06-22 12:49:18 +02:00
|
|
|
res: Response<void>,
|
2021-08-12 15:04:37 +02:00
|
|
|
): Promise<void> {
|
2021-01-19 10:42:45 +01:00
|
|
|
const { id } = req.params;
|
2021-09-14 19:58:48 +02:00
|
|
|
const username = extractUsername(req);
|
2021-08-13 10:36:19 +02:00
|
|
|
await this.addonService.removeAddon(id, username);
|
2022-06-22 12:49:18 +02:00
|
|
|
|
2021-08-13 10:36:19 +02:00
|
|
|
res.status(200).end();
|
2021-01-19 10:42:45 +01:00
|
|
|
}
|
|
|
|
}
|
2021-04-22 10:07:10 +02:00
|
|
|
export default AddonController;
|
2021-01-19 10:42:45 +01:00
|
|
|
module.exports = AddonController;
|