Skip to content

Commit 71f9601

Browse files
authored
Merge pull request #506 from The-Purple-Warehouse/main
[staging deploy] added ability to add items to shop through admin page
2 parents a934bc1 + e7b487d commit 71f9601

4 files changed

Lines changed: 322 additions & 92 deletions

File tree

helpers/shop.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,46 @@ export async function purchaseShopItem(
133133
}
134134
}
135135

136+
export async function addShopItem(
137+
name: string,
138+
description: string,
139+
image: string,
140+
priceBolts: number,
141+
priceNuts: number,
142+
type: string
143+
): Promise<{ success: boolean; error?: string; item?: ShopItem }> {
144+
try {
145+
const item = (await ShopItem.create({
146+
name,
147+
description,
148+
image,
149+
price: {
150+
bolts: priceBolts,
151+
nuts: priceNuts
152+
},
153+
type,
154+
enabled: true
155+
})) as any;
156+
return {
157+
success: true,
158+
item: {
159+
id: item._id.toString(),
160+
name: item.name,
161+
description: item.description,
162+
image: item.image,
163+
price: item.price,
164+
type: item.type,
165+
enabled: item.enabled
166+
}
167+
};
168+
} catch (error) {
169+
return {
170+
success: false,
171+
error: error.message
172+
};
173+
}
174+
}
175+
136176
export async function getUserInventory(
137177
teamNumber: string,
138178
username: string

package-lock.json

Lines changed: 106 additions & 91 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

routers/api/scouting.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ import Team from "../../models/team";
4242
import {
4343
getShopItems,
4444
purchaseShopItem,
45-
getUserInventory
45+
getUserInventory,
46+
addShopItem
4647
} from "../../helpers/shop";
4748
import { processAdmin } from "../../helpers/adminHelpers";
4849

@@ -733,4 +734,31 @@ router.get("/shop/inventory", requireScoutingAuth, async (ctx, next) => {
733734
}
734735
});
735736

737+
router.post("/shop/item/add", requireScoutingAuth, async (ctx, next) => {
738+
let body = ctx.request.body as any;
739+
if (config.auth.adminTokens[body.adminToken] != null) {
740+
ctx.body = {
741+
success: true,
742+
body: {
743+
item: (
744+
await addShopItem(
745+
body.name,
746+
body.description,
747+
body.image,
748+
Number(body.priceBolts),
749+
Number(body.priceNuts),
750+
body.type
751+
)
752+
).item
753+
}
754+
};
755+
} else {
756+
ctx.body = {
757+
success: false,
758+
error: "Invalid admin token!"
759+
};
760+
}
761+
addAPIHeaders(ctx);
762+
});
763+
736764
export default router;

0 commit comments

Comments
 (0)