const express = require('express')
const User = require('../schema/User')
const Certificate = require('../schema/Certificate')
const ECard = require('../schema/E-Card')
const router = express.Router()
router.post('/update-customer', async (req, res) => {
try {
const { customerid, credits, role, status } = req.body
console.log("req.body", req.body)
console.log("customerid", customerid)
const customer = await User.findById(customerid)
console.log("customer", customer)
customer.credits = credits
customer.role = role
customer.status = status
console.log(customer)
await customer.save()
// return console.log(req.body)
return res.status(200).json({ message: `Customer Modified Successfully...!!!` })
} catch (error) {
console.log(error)
return res.status(503).json({ message: error.message })
}
})
router.post('/delete-user', async (req, res) => {
try {
const { id } = req.body
await User.findByIdAndDelete(id)
return res.status(200).json({
message: `User Deleted Successfully!`
})
} catch (error) {
console.log(error)
return res.status(200).json({
error: error.message
})
}
})
router.post('/fetch-items', async (req, res) => {
try {
const { id } = req.body
console.log(id)
const certificates = await Certificate.find({
$or: [
{ sender: id },
{ receiver: id },
]
}).populate('sender').populate('receiver').sort({ createdAt: -1 })
const cards = await ECard.find({
$or: [
{ sender: id },
{
receiver: id,
sent: true
},
],
}).populate('sender').populate('receiver').sort({ createdAt: -1 })
return res.status(200).json({
cards,
certificates
})
} catch (error) {
console.log(error)
return res.status(200).json({
error: error.message
})
}
})
module.exports = router;