2023-03-03 16:23:46 +01:00
|
|
|
import { inject, injectable } from "tsyringe";
|
|
|
|
|
2023-10-19 19:21:17 +02:00
|
|
|
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 16:23:46 +01:00
|
|
|
|
|
|
|
@injectable()
|
|
|
|
export class ItemEventCallbacks
|
|
|
|
{
|
|
|
|
constructor(
|
|
|
|
@inject("HttpResponseUtil") protected httpResponse: HttpResponseUtil,
|
2023-11-10 21:19:56 +01:00
|
|
|
@inject("ItemEventRouter") protected itemEventRouter: ItemEventRouter,
|
2023-03-03 16:23:46 +01:00
|
|
|
)
|
2023-11-10 21:19:56 +01:00
|
|
|
{}
|
2023-03-03 16:23:46 +01:00
|
|
|
|
2023-11-10 21:19:56 +01:00
|
|
|
public handleEvents(
|
|
|
|
url: string,
|
|
|
|
info: IItemEventRouterRequest,
|
|
|
|
sessionID: string,
|
|
|
|
): IGetBodyResponseData<IItemEventRouterResponse>
|
2023-03-03 16:23:46 +01:00
|
|
|
{
|
|
|
|
const eventResponse = this.itemEventRouter.handleEvents(info, sessionID);
|
2023-11-13 18:29:16 +01:00
|
|
|
const result = (eventResponse.warnings.length > 0)
|
|
|
|
? this.httpResponse.getBody(
|
2023-11-10 21:19:56 +01:00
|
|
|
eventResponse,
|
|
|
|
this.getErrorCode(eventResponse.warnings),
|
|
|
|
eventResponse.warnings[0].errmsg,
|
|
|
|
) // TODO: map 228 to its enum value
|
2023-11-13 18:29:16 +01:00
|
|
|
: this.httpResponse.getBody(eventResponse);
|
2023-03-03 16:23:46 +01:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected getErrorCode(warnings: Warning[]): number
|
|
|
|
{
|
|
|
|
if (warnings[0]?.code)
|
|
|
|
{
|
|
|
|
return Number(warnings[0].code);
|
|
|
|
}
|
|
|
|
return BackendErrorCodes.UNKNOWN_ERROR;
|
|
|
|
}
|
2023-11-10 21:19:56 +01:00
|
|
|
}
|