2025-08-24 21:41:20 -07:00
|
|
|
import type { RequestHandler } from "express";
|
2025-08-25 13:37:14 -07:00
|
|
|
import { getAccountIdForRequest } from "../../services/loginService.ts";
|
|
|
|
|
import { addFusionPoints, getInventory } from "../../services/inventoryService.ts";
|
2025-09-09 23:55:10 -07:00
|
|
|
import { getGuildForRequestEx, hasGuildPermission } from "../../services/guildService.ts";
|
|
|
|
|
import { GuildPermission } from "../../types/guildTypes.ts";
|
2025-02-09 07:17:42 -08:00
|
|
|
|
|
|
|
|
export const addCurrencyController: RequestHandler = async (req, res) => {
|
|
|
|
|
const accountId = await getAccountIdForRequest(req);
|
|
|
|
|
const request = req.body as IAddCurrencyRequest;
|
2025-09-09 23:55:10 -07:00
|
|
|
let projection = request.currency as string;
|
|
|
|
|
if (request.currency.startsWith("Vault")) projection = "GuildId";
|
|
|
|
|
const inventory = await getInventory(accountId, projection);
|
2025-04-23 11:37:10 -07:00
|
|
|
if (request.currency == "FusionPoints") {
|
|
|
|
|
addFusionPoints(inventory, request.delta);
|
2025-09-09 23:55:10 -07:00
|
|
|
} else if (request.currency == "VaultRegularCredits" || request.currency == "VaultPremiumCredits") {
|
|
|
|
|
const guild = await getGuildForRequestEx(req, inventory);
|
|
|
|
|
if (await hasGuildPermission(guild, accountId, GuildPermission.Treasurer)) {
|
|
|
|
|
guild[request.currency] ??= 0;
|
|
|
|
|
guild[request.currency]! += request.delta;
|
|
|
|
|
await guild.save();
|
|
|
|
|
}
|
2025-04-23 11:37:10 -07:00
|
|
|
} else {
|
|
|
|
|
inventory[request.currency] += request.delta;
|
|
|
|
|
}
|
2025-09-09 23:55:10 -07:00
|
|
|
if (!request.currency.startsWith("Vault")) {
|
|
|
|
|
await inventory.save();
|
|
|
|
|
}
|
2025-02-09 07:17:42 -08:00
|
|
|
res.end();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
interface IAddCurrencyRequest {
|
2025-09-09 23:55:10 -07:00
|
|
|
currency:
|
|
|
|
|
| "RegularCredits"
|
|
|
|
|
| "PremiumCredits"
|
|
|
|
|
| "FusionPoints"
|
|
|
|
|
| "PrimeTokens"
|
|
|
|
|
| "VaultRegularCredits"
|
|
|
|
|
| "VaultPremiumCredits";
|
2025-02-09 07:17:42 -08:00
|
|
|
delta: number;
|
|
|
|
|
}
|