Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions helpers/trading.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import TradingListing from "../models/tradingListing";

interface ListingData {
type: string;
team: number;
teamName?: string;
item: string;
category: string;
quantity?: number;
description?: string;
contact: string;
event?: string;
}

interface ListingFilters {
type?: string;
category?: string;
event?: string;
}

export async function addListing(data: ListingData) {
const listing = new TradingListing({
type: data.type,
team: data.team,
teamName: data.teamName || "",
item: data.item,
category: data.category,
quantity: data.quantity || 1,
description: data.description || "",
contact: data.contact,
event: data.event || "general",
timestamp: new Date()
});
await listing.save();
return listing.toObject();
}

export async function getListings(filters?: ListingFilters) {
const query: any = {};
if (filters) {
if (filters.type && filters.type !== "all") {
query.type = filters.type;
}
if (filters.category && filters.category !== "all") {
query.category = filters.category;
}
if (filters.event && filters.event !== "all") {
query.event = filters.event;
}
}
return TradingListing.find(query).sort({ timestamp: -1 }).lean();
}

export async function deleteListing(id: string, team: number) {
const listing: any = await TradingListing.findById(id);
if (!listing) {
throw new Error("Listing not found");
}
if (listing.team !== team) {
throw new Error("You can only delete your own listings");
}
await listing.deleteOne();
return true;
}
2 changes: 2 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import resourcesAPIRouter from "./routers/api/resources";
import scoutingRouter from "./routers/scouting";
import scoutingAPIRouter from "./routers/api/scouting";
import tpsAPIRouter from "./routers/api/tps";
import tradingAPIRouter from "./routers/trading";

const app = new Koa();

Expand Down Expand Up @@ -136,6 +137,7 @@ if (config.features.includes("scouting")) {
if (config.features.includes("tps")) {
router.use("/api/v1/tps", tpsAPIRouter.routes());
}
router.use("/api/v1/trading", tradingAPIRouter.routes());

function formatNumber(num) {
if (num < 10) {
Expand Down
39 changes: 39 additions & 0 deletions models/tradingListing.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import mongoose from "../db";

export default mongoose.model(
"TradingListing",
new mongoose.Schema({
type: {
type: String,
enum: ["offer", "request"],
required: true
},
team: {
type: Number,
required: true
},
teamName: String,
item: {
type: String,
required: true
},
category: {
type: String,
required: true
},
quantity: {
type: Number,
default: 1
},
description: String,
contact: {
type: String,
required: true
},
event: String,
timestamp: {
type: Date,
default: Date.now
}
})
);
130 changes: 130 additions & 0 deletions routers/trading.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
import Koa from "koa";
import Router from "koa-router";
import bodyParser from "koa-bodyparser";
import { getTeam } from "../helpers/tba";
import { addListing, getListings, deleteListing } from "../helpers/trading";
import { addAPIHeaders } from "../helpers/utils";

const router = new Router<Koa.DefaultState, Koa.Context>();

router.get("/team/:number", async (ctx) => {
addAPIHeaders(ctx);
try {
const teamNumber = parseInt(ctx.params.number);
if (!teamNumber || teamNumber < 1) {
ctx.body = { success: false, error: "Invalid team number" };
return;
}
const team = await getTeam(teamNumber);
if (team && (team as any).nickname) {
ctx.body = {
success: true,
body: {
team: teamNumber,
teamName: (team as any).nickname
}
};
} else {
ctx.body = { success: false, error: "Team not found" };
}
} catch (err) {
ctx.body = { success: false, error: "Could not look up team" };
}
});

router.get("/listings", async (ctx) => {
addAPIHeaders(ctx);
try {
const query = ctx.query as any;
const listings = await getListings({
type: query.type,
category: query.category,
event: query.event
});
ctx.body = {
success: true,
body: { listings }
};
} catch (err) {
ctx.body = {
success: false,
error: "Could not fetch listings"
};
}
});

router.post("/listings", bodyParser(), async (ctx) => {
addAPIHeaders(ctx);
try {
const body = ctx.request.body as any;

if (
!body.type ||
!body.team ||
!body.item ||
!body.category ||
!body.contact
) {
ctx.body = {
success: false,
error: "Missing required fields: type, team, item, category, contact"
};
return;
}

if (!["offer", "request"].includes(body.type)) {
ctx.body = {
success: false,
error: "Type must be 'offer' or 'request'"
};
return;
}

const listing = await addListing({
type: body.type,
team: parseInt(body.team),
teamName: body.teamName || "",
item: body.item,
category: body.category,
quantity: parseInt(body.quantity) || 1,
description: body.description || "",
contact: body.contact,
event: body.event || "general"
});

ctx.body = {
success: true,
body: {
listing
}
};
} catch (err) {
ctx.body = {
success: false,
error: "Could not create listing"
};
}
});

router.delete("/listings/:id", bodyParser(), async (ctx) => {
addAPIHeaders(ctx);
try {
const body = ctx.request.body as any;
if (!body.team) {
ctx.body = {
success: false,
error: "Team number required"
};
return;
}
await deleteListing(ctx.params.id, parseInt(body.team));
ctx.body = { success: true, body: { message: "Listing deleted" } };
} catch (err) {
ctx.body = {
success: false,
error: err.message || "Could not delete listing"
};
}
});

export default router;
Loading
Loading