add getGuildClient
This commit is contained in:
parent
4ba1adb2e3
commit
f8ee99f2db
@ -1,20 +1,13 @@
|
|||||||
import { RequestHandler } from "express";
|
import { RequestHandler } from "express";
|
||||||
import { Inventory } from "@/src/models/inventoryModels/inventoryModel";
|
import { Guild } from "@/src/models/guildModel";
|
||||||
import { Guild, GuildMember } from "@/src/models/guildModel";
|
|
||||||
import { getAccountIdForRequest } from "@/src/services/loginService";
|
import { getAccountIdForRequest } from "@/src/services/loginService";
|
||||||
import { toMongoDate, toOid } from "@/src/helpers/inventoryHelpers";
|
|
||||||
import { fillInInventoryDataForGuildMember, getGuildVault } from "@/src/services/guildService";
|
|
||||||
import { logger } from "@/src/utils/logger";
|
import { logger } from "@/src/utils/logger";
|
||||||
import { IGuildMemberClient } from "@/src/types/guildTypes";
|
import { getInventory } from "@/src/services/inventoryService";
|
||||||
import { Account } from "@/src/models/loginModel";
|
import { getGuildClient } from "@/src/services/guildService";
|
||||||
|
|
||||||
const getGuildController: RequestHandler = async (req, res) => {
|
const getGuildController: RequestHandler = async (req, res) => {
|
||||||
const accountId = await getAccountIdForRequest(req);
|
const accountId = await getAccountIdForRequest(req);
|
||||||
const inventory = await Inventory.findOne({ accountOwnerId: accountId });
|
const inventory = await getInventory(accountId);
|
||||||
if (!inventory) {
|
|
||||||
res.status(400).json({ error: "inventory was undefined" });
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (inventory.GuildId) {
|
if (inventory.GuildId) {
|
||||||
const guild = await Guild.findOne({ _id: inventory.GuildId });
|
const guild = await Guild.findOne({ _id: inventory.GuildId });
|
||||||
if (guild) {
|
if (guild) {
|
||||||
@ -25,96 +18,7 @@ const getGuildController: RequestHandler = async (req, res) => {
|
|||||||
guild.CeremonyResetDate = undefined;
|
guild.CeremonyResetDate = undefined;
|
||||||
await guild.save();
|
await guild.save();
|
||||||
}
|
}
|
||||||
const guildMembers = await GuildMember.find({ guildId: inventory.GuildId });
|
res.json(await getGuildClient(guild, accountId));
|
||||||
|
|
||||||
const members: IGuildMemberClient[] = [];
|
|
||||||
let missingEntry = true;
|
|
||||||
for (const guildMember of guildMembers) {
|
|
||||||
const member: IGuildMemberClient = {
|
|
||||||
_id: toOid(guildMember.accountId),
|
|
||||||
Rank: guildMember.rank,
|
|
||||||
Status: guildMember.status
|
|
||||||
};
|
|
||||||
if (guildMember.accountId.equals(accountId)) {
|
|
||||||
missingEntry = false;
|
|
||||||
} else {
|
|
||||||
member.DisplayName = (await Account.findOne(
|
|
||||||
{
|
|
||||||
_id: guildMember.accountId
|
|
||||||
},
|
|
||||||
"DisplayName"
|
|
||||||
))!.DisplayName;
|
|
||||||
await fillInInventoryDataForGuildMember(member);
|
|
||||||
}
|
|
||||||
members.push(member);
|
|
||||||
}
|
|
||||||
if (missingEntry) {
|
|
||||||
// Handle clans created prior to creation of the GuildMember model.
|
|
||||||
await GuildMember.insertOne({
|
|
||||||
accountId: accountId,
|
|
||||||
guildId: inventory.GuildId,
|
|
||||||
status: 0,
|
|
||||||
rank: 0
|
|
||||||
});
|
|
||||||
members.push({
|
|
||||||
_id: { $oid: accountId },
|
|
||||||
Status: 0,
|
|
||||||
Rank: 0
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
res.json({
|
|
||||||
_id: toOid(guild._id),
|
|
||||||
Name: guild.Name,
|
|
||||||
MOTD: guild.MOTD,
|
|
||||||
LongMOTD: guild.LongMOTD,
|
|
||||||
Members: members,
|
|
||||||
Ranks: [
|
|
||||||
{
|
|
||||||
Name: "/Lotus/Language/Game/Rank_Creator",
|
|
||||||
Permissions: 16351
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Name: "/Lotus/Language/Game/Rank_Warlord",
|
|
||||||
Permissions: 14303
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Name: "/Lotus/Language/Game/Rank_General",
|
|
||||||
Permissions: 4318
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Name: "/Lotus/Language/Game/Rank_Officer",
|
|
||||||
Permissions: 4314
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Name: "/Lotus/Language/Game/Rank_Leader",
|
|
||||||
Permissions: 4106
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Name: "/Lotus/Language/Game/Rank_Sage",
|
|
||||||
Permissions: 4304
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Name: "/Lotus/Language/Game/Rank_Soldier",
|
|
||||||
Permissions: 4098
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Name: "/Lotus/Language/Game/Rank_Initiate",
|
|
||||||
Permissions: 4096
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Name: "/Lotus/Language/Game/Rank_Utility",
|
|
||||||
Permissions: 4096
|
|
||||||
}
|
|
||||||
],
|
|
||||||
Tier: 1,
|
|
||||||
Vault: getGuildVault(guild),
|
|
||||||
Class: guild.Class,
|
|
||||||
XP: guild.XP,
|
|
||||||
IsContributor: !!guild.CeremonyContributors?.find(x => x.equals(accountId)),
|
|
||||||
NumContributors: guild.CeremonyContributors?.length ?? 0,
|
|
||||||
CeremonyResetDate: guild.CeremonyResetDate ? toMongoDate(guild.CeremonyResetDate) : undefined
|
|
||||||
});
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,13 +1,14 @@
|
|||||||
import { Request } from "express";
|
import { Request } from "express";
|
||||||
import { getAccountIdForRequest } from "@/src/services/loginService";
|
import { getAccountIdForRequest } from "@/src/services/loginService";
|
||||||
import { addRecipes, getInventory } from "@/src/services/inventoryService";
|
import { addRecipes, getInventory } from "@/src/services/inventoryService";
|
||||||
import { Guild, TGuildDatabaseDocument } from "@/src/models/guildModel";
|
import { Guild, GuildMember, TGuildDatabaseDocument } from "@/src/models/guildModel";
|
||||||
import { Inventory, TInventoryDatabaseDocument } from "@/src/models/inventoryModels/inventoryModel";
|
import { TInventoryDatabaseDocument } from "@/src/models/inventoryModels/inventoryModel";
|
||||||
import {
|
import {
|
||||||
IDojoClient,
|
IDojoClient,
|
||||||
IDojoComponentClient,
|
IDojoComponentClient,
|
||||||
IDojoContributable,
|
IDojoContributable,
|
||||||
IDojoDecoClient,
|
IDojoDecoClient,
|
||||||
|
IGuildClient,
|
||||||
IGuildMemberClient,
|
IGuildMemberClient,
|
||||||
IGuildVault
|
IGuildVault
|
||||||
} from "@/src/types/guildTypes";
|
} from "@/src/types/guildTypes";
|
||||||
@ -16,6 +17,7 @@ import { Types } from "mongoose";
|
|||||||
import { ExportDojoRecipes, IDojoBuild } from "warframe-public-export-plus";
|
import { ExportDojoRecipes, IDojoBuild } from "warframe-public-export-plus";
|
||||||
import { logger } from "../utils/logger";
|
import { logger } from "../utils/logger";
|
||||||
import { config } from "./configService";
|
import { config } from "./configService";
|
||||||
|
import { Account } from "../models/loginModel";
|
||||||
|
|
||||||
export const getGuildForRequest = async (req: Request): Promise<TGuildDatabaseDocument> => {
|
export const getGuildForRequest = async (req: Request): Promise<TGuildDatabaseDocument> => {
|
||||||
const accountId = await getAccountIdForRequest(req);
|
const accountId = await getAccountIdForRequest(req);
|
||||||
@ -38,6 +40,99 @@ export const getGuildForRequestEx = async (
|
|||||||
return guild;
|
return guild;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const getGuildClient = async (guild: TGuildDatabaseDocument, accountId: string): Promise<IGuildClient> => {
|
||||||
|
const guildMembers = await GuildMember.find({ guildId: guild._id });
|
||||||
|
|
||||||
|
const members: IGuildMemberClient[] = [];
|
||||||
|
let missingEntry = true;
|
||||||
|
for (const guildMember of guildMembers) {
|
||||||
|
const member: IGuildMemberClient = {
|
||||||
|
_id: toOid(guildMember.accountId),
|
||||||
|
Rank: guildMember.rank,
|
||||||
|
Status: guildMember.status
|
||||||
|
};
|
||||||
|
if (guildMember.accountId.equals(accountId)) {
|
||||||
|
missingEntry = false;
|
||||||
|
} else {
|
||||||
|
member.DisplayName = (await Account.findOne(
|
||||||
|
{
|
||||||
|
_id: guildMember.accountId
|
||||||
|
},
|
||||||
|
"DisplayName"
|
||||||
|
))!.DisplayName;
|
||||||
|
await fillInInventoryDataForGuildMember(member);
|
||||||
|
}
|
||||||
|
members.push(member);
|
||||||
|
}
|
||||||
|
if (missingEntry) {
|
||||||
|
// Handle clans created prior to creation of the GuildMember model.
|
||||||
|
await GuildMember.insertOne({
|
||||||
|
accountId: accountId,
|
||||||
|
guildId: guild._id,
|
||||||
|
status: 0,
|
||||||
|
rank: 0
|
||||||
|
});
|
||||||
|
members.push({
|
||||||
|
_id: { $oid: accountId },
|
||||||
|
Status: 0,
|
||||||
|
Rank: 0
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
_id: toOid(guild._id),
|
||||||
|
Name: guild.Name,
|
||||||
|
MOTD: guild.MOTD,
|
||||||
|
LongMOTD: guild.LongMOTD,
|
||||||
|
Members: members,
|
||||||
|
Ranks: [
|
||||||
|
{
|
||||||
|
Name: "/Lotus/Language/Game/Rank_Creator",
|
||||||
|
Permissions: 16351
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "/Lotus/Language/Game/Rank_Warlord",
|
||||||
|
Permissions: 14303
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "/Lotus/Language/Game/Rank_General",
|
||||||
|
Permissions: 4318
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "/Lotus/Language/Game/Rank_Officer",
|
||||||
|
Permissions: 4314
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "/Lotus/Language/Game/Rank_Leader",
|
||||||
|
Permissions: 4106
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "/Lotus/Language/Game/Rank_Sage",
|
||||||
|
Permissions: 4304
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "/Lotus/Language/Game/Rank_Soldier",
|
||||||
|
Permissions: 4098
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "/Lotus/Language/Game/Rank_Initiate",
|
||||||
|
Permissions: 4096
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "/Lotus/Language/Game/Rank_Utility",
|
||||||
|
Permissions: 4096
|
||||||
|
}
|
||||||
|
],
|
||||||
|
Tier: 1,
|
||||||
|
Vault: getGuildVault(guild),
|
||||||
|
Class: guild.Class,
|
||||||
|
XP: guild.XP,
|
||||||
|
IsContributor: !!guild.CeremonyContributors?.find(x => x.equals(accountId)),
|
||||||
|
NumContributors: guild.CeremonyContributors?.length ?? 0,
|
||||||
|
CeremonyResetDate: guild.CeremonyResetDate ? toMongoDate(guild.CeremonyResetDate) : undefined
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
export const getGuildVault = (guild: TGuildDatabaseDocument): IGuildVault => {
|
export const getGuildVault = (guild: TGuildDatabaseDocument): IGuildVault => {
|
||||||
return {
|
return {
|
||||||
DojoRefundRegularCredits: guild.VaultRegularCredits,
|
DojoRefundRegularCredits: guild.VaultRegularCredits,
|
||||||
|
@ -2,6 +2,25 @@ import { Types } from "mongoose";
|
|||||||
import { IOid, IMongoDate } from "@/src/types/commonTypes";
|
import { IOid, IMongoDate } from "@/src/types/commonTypes";
|
||||||
import { IFusionTreasure, IMiscItem, ITypeCount } from "@/src/types/inventoryTypes/inventoryTypes";
|
import { IFusionTreasure, IMiscItem, ITypeCount } from "@/src/types/inventoryTypes/inventoryTypes";
|
||||||
|
|
||||||
|
export interface IGuildClient {
|
||||||
|
_id: IOid;
|
||||||
|
Name: string;
|
||||||
|
MOTD: string;
|
||||||
|
LongMOTD?: ILongMOTD;
|
||||||
|
Members: IGuildMemberClient[];
|
||||||
|
Ranks: {
|
||||||
|
Name: string;
|
||||||
|
Permissions: number;
|
||||||
|
}[];
|
||||||
|
Tier: number;
|
||||||
|
Vault: IGuildVault;
|
||||||
|
Class: number;
|
||||||
|
XP: number;
|
||||||
|
IsContributor: boolean;
|
||||||
|
NumContributors: number;
|
||||||
|
CeremonyResetDate?: IMongoDate;
|
||||||
|
}
|
||||||
|
|
||||||
export interface IGuildDatabase {
|
export interface IGuildDatabase {
|
||||||
_id: Types.ObjectId;
|
_id: Types.ObjectId;
|
||||||
Name: string;
|
Name: string;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user