feat: Clan creation & persistence
This commit is contained in:
		
							parent
							
								
									3acd8e9f74
								
							
						
					
					
						commit
						e2c378b6a9
					
				
							
								
								
									
										31
									
								
								src/controllers/api/createGuildController.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										31
									
								
								src/controllers/api/createGuildController.ts
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,31 @@
 | 
			
		||||
import { RequestHandler } from "express";
 | 
			
		||||
import { getJSONfromString } from "@/src/helpers/stringHelpers";
 | 
			
		||||
import { Inventory } from "@/src/models/inventoryModels/inventoryModel";
 | 
			
		||||
import { Guild } from "@/src/models/guildModel";
 | 
			
		||||
import { IGuild, guildDbToResponse } from "@/src/types/guildTypes";
 | 
			
		||||
 | 
			
		||||
const createGuildController: RequestHandler = async (req, res) => {
 | 
			
		||||
    let payload = getJSONfromString(req.body.toString());
 | 
			
		||||
 | 
			
		||||
    if (!payload.guildName) {
 | 
			
		||||
        res.status(400);
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // Create guild on database
 | 
			
		||||
    let guild = new Guild({
 | 
			
		||||
        Name: payload.guildName as string
 | 
			
		||||
    } satisfies IGuild);
 | 
			
		||||
    await guild.save();
 | 
			
		||||
 | 
			
		||||
    // Update account's guild
 | 
			
		||||
    let inventory = await Inventory.findOne({ accountOwnerId: req.query.accountId });
 | 
			
		||||
    if (inventory) {
 | 
			
		||||
        inventory.GuildId = guild._id;
 | 
			
		||||
        await inventory.save();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    res.json(guildDbToResponse(guild));
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
export { createGuildController };
 | 
			
		||||
@ -1,6 +1,25 @@
 | 
			
		||||
import { RequestHandler } from "express";
 | 
			
		||||
import { Inventory } from "@/src/models/inventoryModels/inventoryModel";
 | 
			
		||||
import { Guild } from "@/src/models/guildModel";
 | 
			
		||||
import { guildDbToResponse } from "@/src/types/guildTypes";
 | 
			
		||||
 | 
			
		||||
const getGuildController: RequestHandler = (_req, res) => {
 | 
			
		||||
const getGuildController: RequestHandler = async (req, res) => {
 | 
			
		||||
    if (!req.query.accountId) {
 | 
			
		||||
        res.status(400).json({ error: "accountId was not provided" });
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
    const inventory = await Inventory.findOne({ accountOwnerId: req.query.accountId });
 | 
			
		||||
    if (!inventory) {
 | 
			
		||||
        res.status(400).json({ error: "inventory was undefined" });
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
    if (inventory.GuildId) {
 | 
			
		||||
        const guild = await Guild.findOne({ _id: inventory.GuildId });
 | 
			
		||||
        if (guild) {
 | 
			
		||||
            res.json(guildDbToResponse(guild));
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    res.json({});
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										8
									
								
								src/models/guildModel.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								src/models/guildModel.ts
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,8 @@
 | 
			
		||||
import { IGuild, IDatabaseGuild } from "@/src/types/guildTypes";
 | 
			
		||||
import { model, Schema } from "mongoose";
 | 
			
		||||
 | 
			
		||||
const guildSchema = new Schema<IGuild>({
 | 
			
		||||
    Name: { type: String, required: true }
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
export const Guild = model<IGuild>("Guild", guildSchema);
 | 
			
		||||
@ -874,6 +874,9 @@ const inventorySchema = new Schema<IInventoryDatabase, InventoryDocumentProps>(
 | 
			
		||||
        //You first Dialog with NPC or use new Item
 | 
			
		||||
        NodeIntrosCompleted: [String],
 | 
			
		||||
 | 
			
		||||
        //Current guild id, if applicable.
 | 
			
		||||
        GuildId: { type: Schema.Types.ObjectId, ref: "Guild" },
 | 
			
		||||
 | 
			
		||||
        //https://warframe.fandom.com/wiki/Heist
 | 
			
		||||
        //ProfitTaker(1-4) Example:"LocationTag": "EudicoHeists", "Jobs":Mission name
 | 
			
		||||
        CompletedJobChains: [completedJobChainsSchema],
 | 
			
		||||
 | 
			
		||||
@ -43,6 +43,7 @@ import { setActiveShipController } from "@/src/controllers/api/setActiveShipCont
 | 
			
		||||
import { updateThemeController } from "../controllers/api/updateThemeController";
 | 
			
		||||
import { getGuildController } from "@/src/controllers/api/getGuildController";
 | 
			
		||||
import { addFriendImageController } from "@/src/controllers/api/addFriendImageController";
 | 
			
		||||
import { createGuildController } from "@/src/controllers/api/createGuildController";
 | 
			
		||||
 | 
			
		||||
const apiRouter = express.Router();
 | 
			
		||||
 | 
			
		||||
@ -96,5 +97,6 @@ apiRouter.post("/saveLoadout.php", saveLoadoutController);
 | 
			
		||||
apiRouter.post("/trainingResult.php", trainingResultController);
 | 
			
		||||
apiRouter.post("/updateTheme.php", updateThemeController);
 | 
			
		||||
apiRouter.post("/addFriendImage.php", addFriendImageController);
 | 
			
		||||
apiRouter.post("/createGuild.php", createGuildController);
 | 
			
		||||
 | 
			
		||||
export { apiRouter };
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										20
									
								
								src/types/guildTypes.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								src/types/guildTypes.ts
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,20 @@
 | 
			
		||||
import { Types } from "mongoose";
 | 
			
		||||
import { IOid } from "@/src/types/commonTypes";
 | 
			
		||||
import { toOid } from "@/src/helpers/inventoryHelpers";
 | 
			
		||||
 | 
			
		||||
export interface IGuild {
 | 
			
		||||
    Name: string;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export interface IDatabaseGuild extends IGuild {
 | 
			
		||||
    _id: Types.ObjectId;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export interface IGuildResponse extends IGuild {
 | 
			
		||||
    _id: IOid;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export function guildDbToResponse(guild: IDatabaseGuild): IGuildResponse {
 | 
			
		||||
    (guild as IGuild as IGuildResponse)._id = toOid(guild._id);
 | 
			
		||||
    return guild as IGuild as IGuildResponse;
 | 
			
		||||
}
 | 
			
		||||
@ -21,6 +21,7 @@ export interface IInventoryDatabase
 | 
			
		||||
        | "TrainingDate"
 | 
			
		||||
        | "LoadOutPresets"
 | 
			
		||||
        | "Mailbox"
 | 
			
		||||
        | "GuildId"
 | 
			
		||||
        | "PendingRecipes"
 | 
			
		||||
        | "Created"
 | 
			
		||||
        | "QuestKeys"
 | 
			
		||||
@ -32,6 +33,7 @@ export interface IInventoryDatabase
 | 
			
		||||
    TrainingDate: Date; // TrainingDate changed from IMongoDate to Date
 | 
			
		||||
    LoadOutPresets: Types.ObjectId; // LoadOutPresets changed from ILoadOutPresets to Types.ObjectId for population
 | 
			
		||||
    Mailbox: Types.ObjectId; // Mailbox changed from IMailbox to Types.ObjectId
 | 
			
		||||
    GuildId?: Types.ObjectId; // GuildId changed from ?IOid to ?Types.ObjectId
 | 
			
		||||
    PendingRecipes: IPendingRecipe[];
 | 
			
		||||
    QuestKeys: IQuestKeyDatabase[];
 | 
			
		||||
    BlessingCooldown: Date;
 | 
			
		||||
@ -232,6 +234,7 @@ export interface IInventoryResponse {
 | 
			
		||||
    RecentVendorPurchases: Array<number | string>;
 | 
			
		||||
    Hoverboards: IHoverboard[];
 | 
			
		||||
    NodeIntrosCompleted: string[];
 | 
			
		||||
    GuildId?: IOid;
 | 
			
		||||
    CompletedJobChains: ICompletedJobChain[];
 | 
			
		||||
    SeasonChallengeHistory: ISeasonChallengeHistory[];
 | 
			
		||||
    MoaPets: IMoaPet[];
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user