import {
getDojoClient,
getGuildForRequestEx,
hasAccessToDojo,
hasGuildPermission,
hasPermissionToDecorateComponent,
removeDojoDeco,
removeVaultPendingRecipe,
removeDojoRoom
} from "../../services/guildService.ts";
import { getInventory } from "../../services/inventoryService.ts";
import { getAccountForRequest, getBuildLabel } from "../../services/loginService.ts";
import { eGuildPermission, type IDojoClient } from "../../types/guildTypes.ts";
import type { Request, RequestHandler } from "express";
export const abortDojoComponentGetController: RequestHandler = async (req, res) => {
res.json(await abortDojoComponent(req, req.query.componentId as string, req.query.decoId as string | undefined));
};
export const abortDojoComponentPostController: RequestHandler = async (req, res) => {
const request = JSON.parse(String(req.body)) as IAbortDojoComponentRequest;
res.json(await abortDojoComponent(req, request.ComponentId, request.DecoId, request.ResultType));
};
const abortDojoComponent = async (
req: Request,
componentId: string,
decoId?: string,
resultType?: string
): Promise<{ DojoRequestStatus: number } | IDojoClient> => {
const account = await getAccountForRequest(req);
const buildLabel = getBuildLabel(req, account);
const inventory = await getInventory(account._id, "GuildId LevelKeys");
const guild = await getGuildForRequestEx(req, inventory);
if (resultType) {
if (
!hasAccessToDojo(inventory) ||
!(await hasGuildPermission(guild, account._id, eGuildPermission.Tactician))
) {
return { DojoRequestStatus: -1 };
}
removeVaultPendingRecipe(guild, componentId, resultType);
await guild.save();
return await getDojoClient(guild, 0, undefined, buildLabel);
} else if (decoId) {
if (
!hasAccessToDojo(inventory) ||
!(await hasPermissionToDecorateComponent(guild, account._id.toString(), componentId))
) {
return { DojoRequestStatus: -1 };
}
removeDojoDeco(guild, componentId, decoId);
await guild.save();
return await getDojoClient(guild, 0, componentId, buildLabel);
} else {
if (
!hasAccessToDojo(inventory) ||
!(await hasGuildPermission(guild, account._id, eGuildPermission.Architect))
) {
return { DojoRequestStatus: -1 };
}
removeDojoRoom(guild, componentId);
await guild.save();
return await getDojoClient(guild, 0, undefined, buildLabel);
}
};
interface IAbortDojoComponentRequest {
DecoType?: string;
ComponentId: string;
DecoId?: string;
ResultType?: string;
}
import {
getDojoClient,
getGuildForRequestEx,
hasAccessToDojo,
hasGuildPermission,
hasPermissionToDecorateComponent,
removeDojoDeco,
removeVaultPendingRecipe,
removeDojoRoom
} from "../../services/guildService.ts";
import { getInventory } from "../../services/inventoryService.ts";
import { getAccountForRequest, getBuildLabel } from "../../services/loginService.ts";
import { eGuildPermission, type IDojoClient } from "../../types/guildTypes.ts";
import type { Request, RequestHandler } from "express";
export const abortDojoComponentGetController: RequestHandler = async (req, res) => {
res.json(await abortDojoComponent(req, req.query.componentId as string, req.query.decoId as string | undefined));
};
export const abortDojoComponentPostController: RequestHandler = async (req, res) => {
const request = JSON.parse(String(req.body)) as IAbortDojoComponentRequest;
res.json(await abortDojoComponent(req, request.ComponentId, request.DecoId, request.ResultType));
};
const abortDojoComponent = async (
req: Request,
componentId: string,
decoId?: string,
resultType?: string
): Promise<{ DojoRequestStatus: number } | IDojoClient> => {
const account = await getAccountForRequest(req);
const buildLabel = getBuildLabel(req, account);
const inventory = await getInventory(account._id, "GuildId LevelKeys");
const guild = await getGuildForRequestEx(req, inventory);
if (resultType) {
if (
!hasAccessToDojo(inventory) ||
!(await hasGuildPermission(guild, account._id, eGuildPermission.Tactician))
) {
return { DojoRequestStatus: -1 };
}
removeVaultPendingRecipe(guild, componentId, resultType);
await guild.save();
return await getDojoClient(guild, 0, undefined, buildLabel);
} else if (decoId) {
if (
!hasAccessToDojo(inventory) ||
!(await hasPermissionToDecorateComponent(guild, account._id.toString(), componentId))
) {
return { DojoRequestStatus: -1 };
}
removeDojoDeco(guild, componentId, decoId);
await guild.save();
return await getDojoClient(guild, 0, componentId, buildLabel);
} else {
if (
!hasAccessToDojo(inventory) ||
!(await hasGuildPermission(guild, account._id, eGuildPermission.Architect))
) {
return { DojoRequestStatus: -1 };
}
removeDojoRoom(guild, componentId);
await guild.save();
return await getDojoClient(guild, 0, undefined, buildLabel);
}
};
interface IAbortDojoComponentRequest {
DecoType?: string;
ComponentId: string;
DecoId?: string;
ResultType?: string;
}