-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtrading.ts
More file actions
64 lines (59 loc) · 1.68 KB
/
trading.ts
File metadata and controls
64 lines (59 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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;
}