297 lines
11 KiB
TypeScript
297 lines
11 KiB
TypeScript
import {
|
|
PaginateWapServerResults,
|
|
SysAdminCreateWapServerParams,
|
|
SysAdminDeleteWapServerRequest,
|
|
SysAdminDeleteWapServerResult,
|
|
SysAdminAuditWapServerParams,
|
|
SysAdminGetWapServerParams,
|
|
SysAdminRecalculateWapServerCountParams,
|
|
SysAdminWapServerDeleteBotResult,
|
|
SysAdminWapServerDetachResult,
|
|
SysAdminWapServerAuditResult,
|
|
SysAdminWapServerOrganizationActionRequest,
|
|
SysAdminWapServerQrResult,
|
|
SysAdminWapServerRecalculateCountResult,
|
|
SysAdminUpdateWapServerParams,
|
|
IWapServer,
|
|
} from "../../../Models/WapServer/WapServer.Interface";
|
|
import { ApiValidationError } from "../../../Models/Server.Error.model";
|
|
import { ApiVoidResult } from "../../../Models/Api.VoidResult.type";
|
|
import axios from "axios";
|
|
import { Body, Controller, Middlewares, Post, Response, Route, SuccessResponse } from "tsoa";
|
|
import { SA_WapServerService } from "./SysAdminWapServer.Service";
|
|
import { sysAdminAuthMiddleware } from "../../../middleware/sysadminAuth";
|
|
|
|
const stringifyErrorBody = (data: unknown): string => {
|
|
if (data === undefined || data === null) {
|
|
return "";
|
|
}
|
|
|
|
if (typeof data === "string") {
|
|
return data;
|
|
}
|
|
|
|
const body = JSON.stringify(data);
|
|
|
|
return body.length > 1000 ? `${body.slice(0, 1000)}...` : body;
|
|
};
|
|
|
|
const getErrorDesc = (error: unknown): string => {
|
|
if (axios.isAxiosError(error)) {
|
|
const responseData = error.response?.data as any;
|
|
const responseMessage =
|
|
responseData?.desc || responseData?.message || responseData?.error || stringifyErrorBody(responseData);
|
|
const requestUrl = error.config?.url ? ` url ${error.config.url}` : "";
|
|
const status = error.response?.status ? `status ${error.response.status}` : error.code || "no response";
|
|
|
|
return responseMessage || `Upstream request failed:${requestUrl} ${status}`.trim();
|
|
}
|
|
|
|
if (error instanceof Error && error.message) {
|
|
return error.message;
|
|
}
|
|
|
|
const fallback = stringifyErrorBody(error);
|
|
|
|
return fallback || "Unknown error while auditing WAP server";
|
|
};
|
|
|
|
@Route("sysadmin/wapserver/create")
|
|
export class SysAdminWapServerCreateController extends Controller {
|
|
@Response<ApiValidationError>(500, "Ha ocurrido un error")
|
|
@SuccessResponse(200, "Done")
|
|
@Post()
|
|
@Middlewares(sysAdminAuthMiddleware)
|
|
public async sysAdminCreateWapServer(
|
|
@Body() requestBody: SysAdminCreateWapServerParams,
|
|
): Promise<IWapServer | ApiValidationError> {
|
|
try {
|
|
const { payload, ...cleanRequestBody } = requestBody as any;
|
|
const result = await new SA_WapServerService().sysAdminCreateWapServer(cleanRequestBody);
|
|
this.setStatus(200);
|
|
return result;
|
|
} catch (e) {
|
|
this.setStatus(500);
|
|
return new ApiValidationError(500, getErrorDesc(e));
|
|
}
|
|
}
|
|
}
|
|
|
|
@Route("sysadmin/wapserver/get")
|
|
export class SysAdminWapServerGetController extends Controller {
|
|
@Response<ApiValidationError>(500, "Ha ocurrido un error")
|
|
@SuccessResponse(200, "Done")
|
|
@Post()
|
|
@Middlewares(sysAdminAuthMiddleware)
|
|
public async sysAdminGetWapServers(
|
|
@Body() requestBody: SysAdminGetWapServerParams,
|
|
): Promise<PaginateWapServerResults | ApiValidationError> {
|
|
try {
|
|
const result = await new SA_WapServerService().sysAdminGetWapServers(requestBody);
|
|
this.setStatus(200);
|
|
return result;
|
|
} catch (e) {
|
|
this.setStatus(500);
|
|
return new ApiValidationError(500, getErrorDesc(e));
|
|
}
|
|
}
|
|
}
|
|
|
|
@Route("sysadmin/wapserver/update")
|
|
export class SysAdminWapServerUpdateController extends Controller {
|
|
@Response<ApiValidationError>(500, "Ha ocurrido un error")
|
|
@SuccessResponse(200, "Done")
|
|
@Post()
|
|
@Middlewares(sysAdminAuthMiddleware)
|
|
public async sysAdminUpdateWapServer(
|
|
@Body() requestBody: SysAdminUpdateWapServerParams,
|
|
): Promise<IWapServer | ApiValidationError> {
|
|
try {
|
|
const { payload, ...cleanRequestBody } = requestBody as any;
|
|
const result = await new SA_WapServerService().sysAdminUpdateWapServer(cleanRequestBody);
|
|
this.setStatus(200);
|
|
return result;
|
|
} catch (e) {
|
|
this.setStatus(500);
|
|
return new ApiValidationError(500, getErrorDesc(e));
|
|
}
|
|
}
|
|
}
|
|
|
|
@Route("sysadmin/wapserver/audit")
|
|
export class SysAdminWapServerAuditController extends Controller {
|
|
@Response<ApiValidationError>(500, "Ha ocurrido un error")
|
|
@SuccessResponse(200, "Done")
|
|
@Post()
|
|
@Middlewares(sysAdminAuthMiddleware)
|
|
public async sysAdminAuditWapServer(
|
|
@Body() requestBody: SysAdminAuditWapServerParams,
|
|
): Promise<SysAdminWapServerAuditResult | ApiValidationError> {
|
|
try {
|
|
const { payload, ...cleanRequestBody } = requestBody as any;
|
|
const result = await new SA_WapServerService().sysAdminAuditWapServer(cleanRequestBody);
|
|
this.setStatus(200);
|
|
return result;
|
|
} catch (e) {
|
|
this.setStatus(500);
|
|
return new ApiValidationError(500, getErrorDesc(e));
|
|
}
|
|
}
|
|
}
|
|
|
|
@Route("sysadmin/wapserver/recalculate-count")
|
|
export class SysAdminWapServerRecalculateCountController extends Controller {
|
|
@Response<ApiValidationError>(500, "Ha ocurrido un error")
|
|
@SuccessResponse(200, "Done")
|
|
@Post()
|
|
@Middlewares(sysAdminAuthMiddleware)
|
|
public async sysAdminRecalculateWapServerCount(
|
|
@Body() requestBody: SysAdminRecalculateWapServerCountParams,
|
|
): Promise<SysAdminWapServerRecalculateCountResult | ApiValidationError> {
|
|
try {
|
|
const { payload, ...cleanRequestBody } = requestBody as any;
|
|
const result = await new SA_WapServerService().sysAdminRecalculateWapServerCount(cleanRequestBody);
|
|
this.setStatus(200);
|
|
return result;
|
|
} catch (e) {
|
|
const errorOccurred: Error = e as Error;
|
|
this.setStatus(500);
|
|
return new ApiValidationError(500, errorOccurred.message);
|
|
}
|
|
}
|
|
}
|
|
|
|
@Route("sysadmin/wapserver/delete")
|
|
export class SysAdminWapServerDeleteController extends Controller {
|
|
@Response<ApiValidationError>(500, "Ha ocurrido un error")
|
|
@SuccessResponse(200, "Done")
|
|
@Post()
|
|
@Middlewares(sysAdminAuthMiddleware)
|
|
public async sysAdminDeleteWapServer(
|
|
@Body() requestBody: SysAdminDeleteWapServerRequest,
|
|
): Promise<SysAdminDeleteWapServerResult | ApiValidationError> {
|
|
try {
|
|
const { payload, ...cleanRequestBody } = requestBody as any;
|
|
const result = await new SA_WapServerService().sysAdminDeleteWapServer(cleanRequestBody);
|
|
this.setStatus(200);
|
|
return result;
|
|
} catch (e) {
|
|
this.setStatus(500);
|
|
return new ApiValidationError(500, getErrorDesc(e));
|
|
}
|
|
}
|
|
}
|
|
|
|
@Route("sysadmin/wapserver/bot")
|
|
export class SysAdminWapServerBotController extends Controller {
|
|
private cleanRequestBody(requestBody: SysAdminWapServerOrganizationActionRequest) {
|
|
const { payload, ...cleanRequestBody } = requestBody as any;
|
|
|
|
return cleanRequestBody;
|
|
}
|
|
|
|
@Response<ApiValidationError>(500, "Ha ocurrido un error")
|
|
@SuccessResponse(200, "Done")
|
|
@Post("start")
|
|
@Middlewares(sysAdminAuthMiddleware)
|
|
public async sysAdminStartBot(
|
|
@Body() requestBody: SysAdminWapServerOrganizationActionRequest,
|
|
): Promise<ApiVoidResult | ApiValidationError> {
|
|
try {
|
|
await new SA_WapServerService().sysAdminStartBot(this.cleanRequestBody(requestBody));
|
|
this.setStatus(200);
|
|
return { success: true, message: "ok" };
|
|
} catch (e) {
|
|
this.setStatus(500);
|
|
return new ApiValidationError(500, getErrorDesc(e));
|
|
}
|
|
}
|
|
|
|
@Response<ApiValidationError>(500, "Ha ocurrido un error")
|
|
@SuccessResponse(200, "Done")
|
|
@Post("stop")
|
|
@Middlewares(sysAdminAuthMiddleware)
|
|
public async sysAdminStopBot(
|
|
@Body() requestBody: SysAdminWapServerOrganizationActionRequest,
|
|
): Promise<ApiVoidResult | ApiValidationError> {
|
|
try {
|
|
await new SA_WapServerService().sysAdminStopBot(this.cleanRequestBody(requestBody));
|
|
this.setStatus(200);
|
|
return { success: true, message: "ok" };
|
|
} catch (e) {
|
|
this.setStatus(500);
|
|
return new ApiValidationError(500, getErrorDesc(e));
|
|
}
|
|
}
|
|
|
|
@Response<ApiValidationError>(500, "Ha ocurrido un error")
|
|
@SuccessResponse(200, "Done")
|
|
@Post("restart")
|
|
@Middlewares(sysAdminAuthMiddleware)
|
|
public async sysAdminRestartBot(
|
|
@Body() requestBody: SysAdminWapServerOrganizationActionRequest,
|
|
): Promise<ApiVoidResult | ApiValidationError> {
|
|
try {
|
|
await new SA_WapServerService().sysAdminRestartBot(this.cleanRequestBody(requestBody));
|
|
this.setStatus(200);
|
|
return { success: true, message: "ok" };
|
|
} catch (e) {
|
|
this.setStatus(500);
|
|
return new ApiValidationError(500, getErrorDesc(e));
|
|
}
|
|
}
|
|
|
|
@Response<ApiValidationError>(500, "Ha ocurrido un error")
|
|
@SuccessResponse(200, "Done")
|
|
@Post("qr")
|
|
@Middlewares(sysAdminAuthMiddleware)
|
|
public async sysAdminGetQr(
|
|
@Body() requestBody: SysAdminWapServerOrganizationActionRequest,
|
|
): Promise<SysAdminWapServerQrResult | ApiValidationError> {
|
|
try {
|
|
const result = await new SA_WapServerService().sysAdminGetQr(this.cleanRequestBody(requestBody));
|
|
this.setStatus(200);
|
|
return result;
|
|
} catch (e) {
|
|
this.setStatus(500);
|
|
return new ApiValidationError(500, getErrorDesc(e));
|
|
}
|
|
}
|
|
|
|
@Response<ApiValidationError>(500, "Ha ocurrido un error")
|
|
@SuccessResponse(200, "Done")
|
|
@Post("delete")
|
|
@Middlewares(sysAdminAuthMiddleware)
|
|
public async sysAdminDeleteBot(
|
|
@Body() requestBody: SysAdminWapServerOrganizationActionRequest,
|
|
): Promise<SysAdminWapServerDeleteBotResult | ApiValidationError> {
|
|
try {
|
|
const result = await new SA_WapServerService().sysAdminDeleteBot(this.cleanRequestBody(requestBody));
|
|
this.setStatus(200);
|
|
return result;
|
|
} catch (e) {
|
|
this.setStatus(500);
|
|
return new ApiValidationError(500, getErrorDesc(e));
|
|
}
|
|
}
|
|
|
|
@Response<ApiValidationError>(500, "Ha ocurrido un error")
|
|
@SuccessResponse(200, "Done")
|
|
@Post("detach")
|
|
@Middlewares(sysAdminAuthMiddleware)
|
|
public async sysAdminDetachOrganizationServer(
|
|
@Body() requestBody: SysAdminWapServerOrganizationActionRequest,
|
|
): Promise<SysAdminWapServerDetachResult | ApiValidationError> {
|
|
try {
|
|
const result = await new SA_WapServerService().sysAdminDetachOrganizationServer(
|
|
this.cleanRequestBody(requestBody),
|
|
);
|
|
this.setStatus(200);
|
|
return result;
|
|
} catch (e) {
|
|
this.setStatus(500);
|
|
return new ApiValidationError(500, getErrorDesc(e));
|
|
}
|
|
}
|
|
}
|