Server/project/src/callbacks/ItemEventCallbacks.ts

67 lines
2.3 KiB
TypeScript
Raw Normal View History

2023-03-03 15:23:46 +00:00
import { inject, injectable } from "tsyringe";
import { IGetBodyResponseData } from "@spt-aki/models/eft/httpResponse/IGetBodyResponseData";
import { Warning } from "@spt-aki/models/eft/itemEvent/IItemEventRouterBase";
import { IItemEventRouterRequest } from "@spt-aki/models/eft/itemEvent/IItemEventRouterRequest";
import { IItemEventRouterResponse } from "@spt-aki/models/eft/itemEvent/IItemEventRouterResponse";
import { BackendErrorCodes } from "@spt-aki/models/enums/BackendErrorCodes";
import { ItemEventRouter } from "@spt-aki/routers/ItemEventRouter";
import { HttpResponseUtil } from "@spt-aki/utils/HttpResponseUtil";
2023-03-03 15:23:46 +00:00
@injectable()
export class ItemEventCallbacks
{
constructor(
@inject("HttpResponseUtil") protected httpResponse: HttpResponseUtil,
2023-11-10 15:19:56 -05:00
@inject("ItemEventRouter") protected itemEventRouter: ItemEventRouter,
2023-03-03 15:23:46 +00:00
)
2023-11-10 15:19:56 -05:00
{}
2023-03-03 15:23:46 +00:00
public async handleEvents(
2023-11-10 15:19:56 -05:00
url: string,
info: IItemEventRouterRequest,
sessionID: string,
): Promise<IGetBodyResponseData<IItemEventRouterResponse>>
2023-03-03 15:23:46 +00:00
{
const eventResponse = await this.itemEventRouter.handleEvents(info, sessionID);
const result = this.isCriticalError(eventResponse.warnings)
? this.httpResponse.getBody(
2023-11-10 15:19:56 -05:00
eventResponse,
this.getErrorCode(eventResponse.warnings),
eventResponse.warnings[0].errmsg,
)
: this.httpResponse.getBody(eventResponse);
2023-03-03 15:23:46 +00:00
return result;
}
/**
* Return true if the passed in list of warnings contains critical issues
* @param warnings The list of warnings to check for critical errors
* @returns
*/
private isCriticalError(warnings: Warning[]): boolean
{
// List of non-critical error codes, we return true if any error NOT included is passed in
const nonCriticalErrorCodes: BackendErrorCodes[] = [BackendErrorCodes.NOTENOUGHSPACE];
for (const warning of warnings)
{
if (!nonCriticalErrorCodes.includes(+warning.code))
{
return true;
}
}
return false;
}
2023-03-03 15:23:46 +00:00
protected getErrorCode(warnings: Warning[]): number
{
if (warnings[0]?.code)
{
return Number(warnings[0].code);
}
return BackendErrorCodes.UNKNOWN_ERROR;
}
2023-11-10 15:19:56 -05:00
}