Voucher given to user
I am going to fix all design now
This commit is contained in:
@@ -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(() => {
|
||||
|
||||
@@ -45,4 +45,4 @@ module.exports = (sequelize, DataTypes) => {
|
||||
});
|
||||
|
||||
return HBCform;
|
||||
}
|
||||
};
|
||||
@@ -18,7 +18,7 @@ module.exports = (sequelize, DataTypes) => {
|
||||
},
|
||||
},
|
||||
{
|
||||
tableName: 'schedule'
|
||||
tableName: "schedule"
|
||||
});
|
||||
return Schedule;
|
||||
}
|
||||
};
|
||||
28
server/models/UserVoucher.js
Normal file
28
server/models/UserVoucher.js
Normal 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;
|
||||
};
|
||||
@@ -41,4 +41,4 @@ module.exports = (sequelize, DataTypes) => {
|
||||
});
|
||||
|
||||
return Voucher;
|
||||
}
|
||||
};
|
||||
54
server/routes/uservoucher.js
Normal file
54
server/routes/uservoucher.js
Normal 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;
|
||||
Reference in New Issue
Block a user