Voucher given to user

I am going to fix all design now
This commit is contained in:
ZacTohZY
2024-08-12 00:57:20 +08:00
parent 672eca530b
commit fbedd28d3b
13 changed files with 433 additions and 37 deletions

View File

@@ -46,6 +46,9 @@ app.use("/vouchers", vouchers);
const feedback = require("./routes/feedback.js");
app.use("/feedback", feedback)
const uservoucher = require("./routes/uservoucher.js");
app.use("/user-vouchers", uservoucher);
db.sequelize
.sync({ alter: true })
.then(() => {

View File

@@ -45,4 +45,4 @@ module.exports = (sequelize, DataTypes) => {
});
return HBCform;
}
};

View File

@@ -18,7 +18,7 @@ module.exports = (sequelize, DataTypes) => {
},
},
{
tableName: 'schedule'
tableName: "schedule"
});
return Schedule;
}
};

View File

@@ -0,0 +1,28 @@
const { DataTypes } = require("sequelize");
module.exports = (sequelize, DataTypes) => {
const UserVoucher = sequelize.define(
"UserVoucher",
{
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
allowNull: false,
primaryKey: true,
},
userId: {
type: DataTypes.UUID,
allowNull: false,
},
voucherId: {
type: DataTypes.UUID,
allowNull: false,
},
},
{
tableName: "user_vouchers",
timestamps: true,
});
return UserVoucher;
};

View File

@@ -41,4 +41,4 @@ module.exports = (sequelize, DataTypes) => {
});
return Voucher;
}
};

View File

@@ -0,0 +1,54 @@
const express = require('express');
const router = express.Router();
const { UserVoucher } = require("../models");
const { Op } = require('sequelize');
const yup = require('yup');
router.get("/:id", async (req, res) => {
let id = req.params.id;
let vouchers = await UserVoucher.findByPk(id);
if (!vouchers) {
res.sendStatus(404);
return;
}
res.json(vouchers);
});
router.post('/', async (req, res) => {
const { userId, voucherId } = req.body;
if (!userId || !voucherId) {
return res.status(400).json({ error: 'userId and voucherId are required' });
}
try {
const userVoucher = await UserVoucher.create({ userId, voucherId });
res.status(201).json(userVoucher);
} catch (error) {
res.status(500).json({ error: "Failed to create UserVoucher entry" });
}
});
router.get('/user/:userId', async (req, res) => {
try {
const { userId } = req.params;
// Fetch user vouchers by userId
const userVouchers = await UserVoucher.findAll({ where: { userId } });
if (!userVouchers) {
return res.status(404).json({ error: 'No vouchers found for this user' });
}
// Extract voucherIds from the userVouchers
const voucherIds = userVouchers.map(userVoucher => userVoucher.voucherId);
// Send the voucherIds along with userVoucher information
res.json({ userVouchers, voucherIds });
} catch (error) {
res.status(500).json({ error: 'Failed to fetch user vouchers' });
}
});
module.exports = router;