Set up vouchers base
This commit is contained in:
@@ -40,6 +40,9 @@ app.use("/hbcform", HBCformRoute);
|
||||
const connections = require("./routes/connections");
|
||||
app.use("/connections", connections);
|
||||
|
||||
const vouchers = require("./routes/vouchers.js");
|
||||
app.use("/vouchers", vouchers);
|
||||
|
||||
db.sequelize
|
||||
.sync({ alter: true })
|
||||
.then(() => {
|
||||
|
||||
44
server/models/Voucher.js
Normal file
44
server/models/Voucher.js
Normal file
@@ -0,0 +1,44 @@
|
||||
const { DataTypes } = require("sequelize");
|
||||
|
||||
module.exports = (sequelize, DataTypes) => {
|
||||
const Voucher = sequelize.define(
|
||||
"Voucher",
|
||||
{
|
||||
id: {
|
||||
type: DataTypes.UUID,
|
||||
defaultValue: DataTypes.UUIDV4,
|
||||
allowNull: false,
|
||||
primaryKey: true,
|
||||
},
|
||||
brandLogo: {
|
||||
type: DataTypes.BLOB("long"),
|
||||
allowNull: true,
|
||||
},
|
||||
brand: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false,
|
||||
},
|
||||
description: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false,
|
||||
},
|
||||
expirationDate: {
|
||||
type: DataTypes.DATE,
|
||||
allowNull: false,
|
||||
},
|
||||
quantityAvailable: {
|
||||
type: DataTypes.INTEGER,
|
||||
allowNull: false
|
||||
},
|
||||
code: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false,
|
||||
},
|
||||
},
|
||||
{
|
||||
tableName: "vouchers",
|
||||
timestamps: true,
|
||||
});
|
||||
|
||||
return Voucher;
|
||||
}
|
||||
154
server/routes/vouchers.js
Normal file
154
server/routes/vouchers.js
Normal file
@@ -0,0 +1,154 @@
|
||||
const express = require('express');
|
||||
const router = express.Router();
|
||||
const { Voucher } = require('../models');
|
||||
const { Op } = require("sequelize");
|
||||
const yup = require("yup");
|
||||
const multer = require("multer");
|
||||
const sharp = require("sharp");
|
||||
|
||||
const upload = multer({ storage: multer.memoryStorage() });
|
||||
|
||||
router.post(
|
||||
"/",
|
||||
upload.fields([{ name: "brandLogo", maxCount: 1 }]),
|
||||
async (req, res) => {
|
||||
let data = req.body;
|
||||
let files = req.files;
|
||||
|
||||
// Validate request body
|
||||
let validationSchema = yup.object().shape({
|
||||
brand: yup.string().trim().max(100).required(),
|
||||
description: yup.string().trim().required(),
|
||||
expirationDate: yup.date().required(),
|
||||
quantityAvailable: yup.number().min(0).positive().required(),
|
||||
code: yup.string().trim().required(),
|
||||
brandLogo: yup.string().trim().max(255), // Optional field
|
||||
});
|
||||
|
||||
try {
|
||||
data = await validationSchema.validate(data, { abortEarly: false });
|
||||
|
||||
// Process brandLogo if it exists
|
||||
let brandLogo = files.brandLogo ? files.brandLogo[0].buffer : null;
|
||||
|
||||
if (brandLogo) {
|
||||
// Resize and compress image
|
||||
brandLogo = await sharp(brandLogo)
|
||||
.resize(512, 512, {
|
||||
fit: sharp.fit.inside,
|
||||
withoutEnlargement: true,
|
||||
})
|
||||
.jpeg({ quality: 80 })
|
||||
.toBuffer();
|
||||
}
|
||||
|
||||
// Include brandLogo in data if processed
|
||||
let result = await Voucher.create({ ...data, brandLogo });
|
||||
res.json(result);
|
||||
} catch (err) {
|
||||
res.status(400).json({ errors: err.errors });
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
|
||||
router.get("/", async (req, res) => {
|
||||
let condition = {};
|
||||
let search = req.query.search;
|
||||
if (search) {
|
||||
condition[Op.or] = [
|
||||
{ brand: { [Op.like]: `%${search}%` } },
|
||||
];
|
||||
}
|
||||
let list = await Voucher.findAll({
|
||||
where: condition,
|
||||
order: [["createdAt", "ASC"]]
|
||||
});
|
||||
res.json(list);
|
||||
});
|
||||
|
||||
router.get("/:id", async (req, res) => {
|
||||
let id = req.params.id;
|
||||
let vouchers = await Voucher.findByPk(id);
|
||||
if (!vouchers) {
|
||||
res.sendStatus(404);
|
||||
return;
|
||||
}
|
||||
res.json(vouchers);
|
||||
});
|
||||
|
||||
router.get("/brandLogo/:id", async (req, res) => {
|
||||
let id = req.params.id;
|
||||
let vouchers = await Voucher.findByPk(id);
|
||||
|
||||
if (!vouchers || !vouchers.brandLogo) {
|
||||
res.sendStatus(404);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
res.set("Content-Type", "image/jpeg"); // Adjust the content type as necessary
|
||||
res.send(vouchers.brandLogo);
|
||||
} catch (err) {
|
||||
res
|
||||
.status(500)
|
||||
.json({ message: "Error retrieving brand", error: err });
|
||||
}
|
||||
});
|
||||
|
||||
router.put("/:id", async (req, res) => { //update
|
||||
let id = req.params.id;
|
||||
let vouchers = await Voucher.findByPk(id);
|
||||
if (!vouchers) {
|
||||
res.sendStatus(404);
|
||||
return;
|
||||
}
|
||||
let data = req.body;
|
||||
let validationSchema = yup.object().shape({
|
||||
brand: yup.string().trim().max(100).required(),
|
||||
description: yup.string().trim().required(),
|
||||
expirationDate: yup.date().required(),
|
||||
quantityAvailable: yup.number().min(0).positive().required(),
|
||||
code: yup.string().trim().required(),
|
||||
});
|
||||
try {
|
||||
data = await validationSchema.validate(data,
|
||||
{ abortEarly: false });
|
||||
let num = await Voucher.update(data, {
|
||||
where: { id: id }
|
||||
});
|
||||
if (num == 1) {
|
||||
res.json({
|
||||
message: "Voucher was updated successfully."
|
||||
});
|
||||
}
|
||||
else {
|
||||
res.status(400).json({
|
||||
message: `Cannot update voucher with id ${id}.`
|
||||
});
|
||||
}
|
||||
}
|
||||
catch (err) {
|
||||
res.status(400).json({ errors: err.errors });
|
||||
}
|
||||
});
|
||||
|
||||
router.delete("/:id", async (req, res) => {
|
||||
let id = req.params.id;
|
||||
let num = await Voucher.destroy({
|
||||
where: { id: id }
|
||||
})
|
||||
if (num == 1) {
|
||||
res.json({
|
||||
message: "Voucher was deleted successfully."
|
||||
});
|
||||
}
|
||||
else {
|
||||
res.status(400).json({
|
||||
message: `Cannot delete voucher with id ${id}.`
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
module.exports = router;
|
||||
Reference in New Issue
Block a user