This repository has been archived on 2024-12-31. You can view files and clone it, but cannot push or open issues or pull requests.

135 lines
5.0 KiB
JavaScript

const app = require('express').Router();
const crypto = require('crypto');
const checkPermission = require("../middlewares/checkPermission");
const giftValidation = require("../validation/giftValidation");
const Article = require("../models/Article");
const Gift = require("../models/Gift");
const ActivatedArticle = require("../models/ActivatedArticle");
app.put("/", checkPermission("admin.gift.create"), async (req, res) => {
// Validate request
const {error} = giftValidation.create.validate(req.body);
if (error) return res.status(400).json({message: error.details[0].message});
// Set optional fields
const giftID = (req.body.giftID || crypto.randomBytes(15).toString("hex")).toUpperCase();
const expiry_date = new Date(req.body.expiry_date || new Date("4000-01-01"));
const item_expiry_date = new Date(req.body.item_expiry_date || new Date("4000-01-01"));
// Check if article exists
const {count: articleCount} = await Article.findAndCountAll({where: {id: req.body.articleID}});
if (articleCount === 0) return res.status(400).json({message: "Article does not exist"});
// Check if code already exists
const {count: codeCount} = await Gift.findAndCountAll({where: {giftID: giftID}});
if (codeCount === 1) return res.status(400).json({message: "Code already exists"});
// Create gift
await Gift.create({
giftID: giftID,
articleID: req.body.articleID,
expiry_date: expiry_date,
item_expiry_date: item_expiry_date
});
// Return gift
res.status(200).json({
giftID: giftID, articleID: req.body.articleID,
expiry_date: expiry_date.toISOString(), item_expiry_date: item_expiry_date.toISOString()
});
});
app.get("/:giftID", checkPermission("admin.gift.info"), async (req, res) => {
// Validate request
if (!req.params.giftID) return res.status(400).json({message: "Gift ID is required"});
// Check if code is in database
const giftInfo = await Gift.findOne({where: {giftID: req.params.giftID}});
// Return info
if (giftInfo)
res.status(200).json(giftInfo);
else res.status(404).json({message: "Gift not found"});
});
app.delete("/:giftID", checkPermission("admin.gift.delete"), async (req, res) => {
// Validate request
if (!req.params.giftID) return res.status(400).json({message: "Gift ID is required"});
// Check if code exists
const {count} = await Gift.findAndCountAll({where: {giftID: req.params.giftID}});
if (count === 0) return res.status(400).json({message: "Gift not found"});
// Delete code
await Gift.destroy({where: {giftID: req.params.giftID}});
// Send response
res.status(200).json({message: "Gift successfully deleted"});
});
app.patch("/", checkPermission("admin.gift.update"), async (req, res) => {
// Validate request
const {error} = giftValidation.update.validate(req.body);
if (error) return res.status(400).json({message: error.details[0].message});
// Check if code exists
const {count} = await Gift.findAndCountAll({where: {giftID: req.body.giftID}});
if (count === 0) return res.status(400).json({message: "Gift not found"});
// Add changes to object
const updatedGift = {};
if (req.body.articleID) {
// Check if article exists
const {count: articleCount} = await Article.findAndCountAll({where: {articleID: req.body.articleID}});
if (articleCount === 0) return res.status(400).json({message: "Article does not exist"});
updatedGift["articleID"] = req.body.articleID;
}
if (req.body.expiry_date) updatedGift["expiry_date"] = req.body.expiry_date;
if (req.body.item_expiry_date) updatedGift["item_expiry_date"] = req.body.item_expiry_date;
// Update
await Gift.update(updatedGift, {where: {giftID: req.body.giftID}});
// Send response
res.status(200).json({message: "Gift updated successfully"});
});
app.post("/redeem", async (req, res) => {
// Validate request
const {error} = giftValidation.redeem.validate(req.body);
if (error) return res.status(400).json({message: error.details[0].message});
// Check if code is in database
const giftInfo = await Gift.findOne({where: {giftID: req.body.giftID}});
// Check if gift exists
if (!giftInfo) return res.status(404).json({message: "Gift not found"});
// Get the article
const article = await Article.findOne({where: {id: giftInfo.articleID}});
// Check if article limit reached
const {count} = await ActivatedArticle.findAndCountAll({
where: {
articleID: giftInfo.articleID,
guildID: req.body.guildID
}
});
if (count >= article.maxOwnCount) return res.status(400).json({message: "Maximum limit of this item reached"});
// Redeem article
await ActivatedArticle.create({
articleID: giftInfo.articleID,
expiry_date: giftInfo.item_expiry_date,
guildID: req.body.guildID
});
await Gift.destroy({where: {giftID: req.body.giftID}});
// Send response
res.json({message: "Article successfully redeemed"});
});
module.exports = app;