feat: implement full CRUD and administrative management endpoints for WAP servers in the sysadmin API
This commit is contained in:
@@ -1,12 +1,60 @@
|
||||
import {
|
||||
PaginateWapServerResults,
|
||||
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/get")
|
||||
export class SysAdminWapServerGetController extends Controller {
|
||||
@Response<ApiValidationError>(500, "Ha ocurrido un error")
|
||||
@@ -20,6 +68,69 @@ export class SysAdminWapServerGetController extends Controller {
|
||||
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);
|
||||
@@ -27,3 +138,137 @@ export class SysAdminWapServerGetController extends Controller {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,17 @@ import { connect } from "mongoose";
|
||||
import {
|
||||
PaginateWapServerParams,
|
||||
PaginateWapServerResults,
|
||||
SysAdminDeleteWapServerParams,
|
||||
SysAdminDeleteWapServerResult,
|
||||
SysAdminWapServerAuditParams,
|
||||
SysAdminWapServerAuditResult,
|
||||
SysAdminWapServerDeleteBotResult,
|
||||
SysAdminWapServerDetachResult,
|
||||
SysAdminWapServerOrganizationActionParams,
|
||||
SysAdminWapServerQrResult,
|
||||
SysAdminWapServerRecalculateCountParams,
|
||||
SysAdminWapServerRecalculateCountResult,
|
||||
UpdateWapServerParams,
|
||||
} from "../../../Models/WapServer/WapServer.Interface";
|
||||
import WapServerList from "../../../Models/WapServer/WapServer";
|
||||
|
||||
@@ -11,4 +22,60 @@ export class SA_WapServerService {
|
||||
await connect(`${process.env.DATABASE_CONNECTION}`);
|
||||
return await WapServerList.sysAdminGetWapServers(data);
|
||||
}
|
||||
|
||||
public async sysAdminUpdateWapServer(data: UpdateWapServerParams) {
|
||||
await connect(`${process.env.DATABASE_CONNECTION}`);
|
||||
return await WapServerList.sysAdminUpdateWapServer(data);
|
||||
}
|
||||
|
||||
public async sysAdminAuditWapServer(data: SysAdminWapServerAuditParams): Promise<SysAdminWapServerAuditResult> {
|
||||
await connect(`${process.env.DATABASE_CONNECTION}`);
|
||||
return await WapServerList.sysAdminAuditWapServer(data);
|
||||
}
|
||||
|
||||
public async sysAdminRecalculateWapServerCount(
|
||||
data: SysAdminWapServerRecalculateCountParams,
|
||||
): Promise<SysAdminWapServerRecalculateCountResult> {
|
||||
await connect(`${process.env.DATABASE_CONNECTION}`);
|
||||
return await WapServerList.sysAdminRecalculateWapServerCount(data);
|
||||
}
|
||||
|
||||
public async sysAdminDeleteWapServer(data: SysAdminDeleteWapServerParams): Promise<SysAdminDeleteWapServerResult> {
|
||||
await connect(`${process.env.DATABASE_CONNECTION}`);
|
||||
return await WapServerList.sysAdminDeleteWapServer(data);
|
||||
}
|
||||
|
||||
public async sysAdminStartBot(data: SysAdminWapServerOrganizationActionParams): Promise<void> {
|
||||
await connect(`${process.env.DATABASE_CONNECTION}`);
|
||||
await WapServerList.sysAdminStartBot(data);
|
||||
}
|
||||
|
||||
public async sysAdminStopBot(data: SysAdminWapServerOrganizationActionParams): Promise<void> {
|
||||
await connect(`${process.env.DATABASE_CONNECTION}`);
|
||||
await WapServerList.sysAdminStopBot(data);
|
||||
}
|
||||
|
||||
public async sysAdminRestartBot(data: SysAdminWapServerOrganizationActionParams): Promise<void> {
|
||||
await connect(`${process.env.DATABASE_CONNECTION}`);
|
||||
await WapServerList.sysAdminRestartBot(data);
|
||||
}
|
||||
|
||||
public async sysAdminGetQr(data: SysAdminWapServerOrganizationActionParams): Promise<SysAdminWapServerQrResult> {
|
||||
await connect(`${process.env.DATABASE_CONNECTION}`);
|
||||
return await WapServerList.sysAdminGetQr(data);
|
||||
}
|
||||
|
||||
public async sysAdminDeleteBot(
|
||||
data: SysAdminWapServerOrganizationActionParams,
|
||||
): Promise<SysAdminWapServerDeleteBotResult> {
|
||||
await connect(`${process.env.DATABASE_CONNECTION}`);
|
||||
return await WapServerList.sysAdminDeleteBot(data);
|
||||
}
|
||||
|
||||
public async sysAdminDetachOrganizationServer(
|
||||
data: SysAdminWapServerOrganizationActionParams,
|
||||
): Promise<SysAdminWapServerDetachResult> {
|
||||
await connect(`${process.env.DATABASE_CONNECTION}`);
|
||||
return await WapServerList.sysAdminDetachOrganizationServer(data);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user