chore: use model.findById where possible

This commit is contained in:
Sainan 2025-03-24 14:30:08 +01:00
parent 0085c20e11
commit 621067c50a
14 changed files with 16 additions and 16 deletions

View File

@ -18,7 +18,7 @@ export const addToGuildController: RequestHandler = async (req, res) => {
return;
}
const guild = (await Guild.findOne({ _id: payload.GuildId.$oid }, "Name"))!;
const guild = (await Guild.findById(payload.GuildId.$oid, "Name"))!;
const senderAccount = await getAccountForRequest(req);
if (!(await hasGuildPermission(guild, senderAccount._id.toString(), GuildPermission.Recruiter))) {
res.status(400).json("Invalid permission");

View File

@ -19,7 +19,7 @@ export const confirmGuildInvitationController: RequestHandler = async (req, res)
new Types.ObjectId(req.query.clanId as string)
);
const guild = (await Guild.findOne({ _id: req.query.clanId as string }))!;
const guild = (await Guild.findById(req.query.clanId as string))!;
guild.RosterActivity ??= [];
guild.RosterActivity.push({

View File

@ -11,7 +11,7 @@ import { Types } from "mongoose";
export const contributeGuildClassController: RequestHandler = async (req, res) => {
const accountId = await getAccountIdForRequest(req);
const payload = getJSONfromString<IContributeGuildClassRequest>(String(req.body));
const guild = (await Guild.findOne({ _id: payload.GuildId }))!;
const guild = (await Guild.findById(payload.GuildId))!;
// First contributor initiates ceremony and locks the pending class.
if (!guild.CeremonyContributors) {

View File

@ -9,7 +9,7 @@ const getGuildController: RequestHandler = async (req, res) => {
const accountId = await getAccountIdForRequest(req);
const inventory = await getInventory(accountId, "GuildId");
if (inventory.GuildId) {
const guild = await Guild.findOne({ _id: inventory.GuildId });
const guild = await Guild.findById(inventory.GuildId);
if (guild) {
// Handle guilds created before we added discriminators
if (guild.Name.indexOf("#") == -1) {

View File

@ -6,7 +6,7 @@ import { getDojoClient } from "@/src/services/guildService";
export const getGuildDojoController: RequestHandler = async (req, res) => {
const guildId = req.query.guildId as string;
const guild = await Guild.findOne({ _id: guildId });
const guild = await Guild.findById(guildId);
if (!guild) {
res.status(404).end();
return;

View File

@ -9,7 +9,7 @@ export const getGuildLogController: RequestHandler = async (req, res) => {
const accountId = await getAccountIdForRequest(req);
const inventory = await getInventory(accountId, "GuildId");
if (inventory.GuildId) {
const guild = await Guild.findOne({ _id: inventory.GuildId });
const guild = await Guild.findById(inventory.GuildId);
if (guild) {
const log: Record<string, IGuildLogEntryClient[]> = {
RoomChanges: [],

View File

@ -4,7 +4,7 @@ import { Account } from "@/src/models/loginModel";
const logoutController: RequestHandler = async (req, res) => {
const accountId = await getAccountIdForRequest(req);
const account = await Account.findOne({ _id: accountId });
const account = await Account.findById(accountId);
if (account) {
account.Nonce = 0;
await account.save();

View File

@ -42,7 +42,7 @@ export const removeFromGuildController: RequestHandler = async (req, res) => {
guild.RosterActivity ??= [];
if (isKick) {
const kickee = (await Account.findOne({ _id: payload.userId }))!;
const kickee = (await Account.findById(payload.userId))!;
guild.RosterActivity.push({
dateTime: new Date(),
entryType: 12,

View File

@ -8,7 +8,7 @@ import { RequestHandler } from "express";
export const setGuildMotdController: RequestHandler = async (req, res) => {
const account = await getAccountForRequest(req);
const inventory = await getInventory(account._id.toString(), "GuildId");
const guild = (await Guild.findOne({ _id: inventory.GuildId! }))!;
const guild = (await Guild.findById(inventory.GuildId!))!;
if (!(await hasGuildPermission(guild, account._id, GuildPermission.Herald))) {
res.status(400).json("Invalid permission");
return;

View File

@ -12,7 +12,7 @@ export const getAccountInfoController: RequestHandler = async (req, res) => {
}
const guildMember = await GuildMember.findOne({ accountId: account._id, status: 0 }, "guildId rank");
if (guildMember) {
const guild = (await Guild.findOne({ _id: guildMember.guildId }, "Ranks"))!;
const guild = (await Guild.findById(guildMember.guildId, "Ranks"))!;
info.GuildId = guildMember.guildId.toString();
info.GuildPermissions = guild.Ranks[guildMember.rank].Permissions;
}

View File

@ -26,13 +26,13 @@ export const getProfileViewingDataController: RequestHandler = async (req, res)
res.status(400).end();
return;
}
const account = await Account.findOne({ _id: req.query.playerId as string }, "DisplayName");
const account = await Account.findById(req.query.playerId as string, "DisplayName");
if (!account) {
res.status(400).send("No account or guild ID specified");
return;
}
const inventory = (await Inventory.findOne({ accountOwnerId: account._id }))!;
const loadout = (await Loadout.findOne({ _id: inventory.LoadOutPresets }, "NORMAL"))!;
const loadout = (await Loadout.findById(inventory.LoadOutPresets, "NORMAL"))!;
const result: IPlayerProfileViewingDataResult = {
AccountId: toOid(account._id),
@ -88,7 +88,7 @@ export const getProfileViewingDataController: RequestHandler = async (req, res)
}
}
if (inventory.GuildId) {
const guild = (await Guild.findOne({ _id: inventory.GuildId }, "Name Tier XP Class"))!;
const guild = (await Guild.findById(inventory.GuildId, "Name Tier XP Class"))!;
result.GuildId = toOid(inventory.GuildId);
result.GuildName = guild.Name;
result.GuildTier = guild.Tier;

View File

@ -37,7 +37,7 @@ export const getGuildForRequestEx = async (
if (!inventory.GuildId || inventory.GuildId.toString() != guildId) {
throw new Error("Account is not in the guild that it has sent a request for");
}
const guild = await Guild.findOne({ _id: guildId });
const guild = await Guild.findById(guildId);
if (!guild) {
throw new Error("Account thinks it is in a guild that doesn't exist");
}

View File

@ -11,7 +11,7 @@ export const getAllMessagesSorted = async (accountId: string): Promise<HydratedD
};
export const getMessage = async (messageId: string): Promise<HydratedDocument<IMessageDatabase>> => {
const message = await Inbox.findOne({ _id: messageId });
const message = await Inbox.findById(messageId);
if (!message) {
throw new Error(`Message not found ${messageId}`);

View File

@ -21,7 +21,7 @@ export const createShip = async (
};
export const getShip = async (shipId: Types.ObjectId, fieldSelection: string = ""): Promise<TShipDatabaseDocument> => {
const ship = await Ship.findOne({ _id: shipId }, fieldSelection);
const ship = await Ship.findById(shipId, fieldSelection);
if (!ship) {
throw new Error(`error finding a ship with id ${shipId.toString()}`);