diff --git a/src/components/voucher/voucher.controller.js b/src/components/voucher/voucher.controller.js index b71755d..7c54182 100644 --- a/src/components/voucher/voucher.controller.js +++ b/src/components/voucher/voucher.controller.js @@ -232,20 +232,52 @@ export const redeemVoucher = async (req, res) => { export const updateVoucher = async (req, res) => { try { const { code } = req.params; - const [updated] = await Voucher.update(req.body, { - where: { code } - }); + const { + name, + discount, + redemption, + start_date, + expiration_date, + is_active, + customer_id + } = req.body; - if (updated) { - const updatedVoucher = await Voucher.findOne({ where: { code } }); - const response = formatVoucherResponse(updatedVoucher); - res.json(response); - } else { - res.status(404).json({ error: 'Voucher not found' }); + const voucher = await Voucher.findOne({ where: { code } }); + + if (!voucher) { + return res.status(404).json({ + error: 'Voucher not found' + }); } + + const updateData = { + name, + discountType: discount?.type, + discountAmount: discount?.type === 'AMOUNT' ? discount.amount_off : discount.percent_off, + maxDiscountAmount: discount?.amount_limit || 0, + maxRedemptions: redemption?.quantity, + dailyQuota: redemption?.daily_quota, + startDate: start_date, + expirationDate: expiration_date, + isActive: is_active, + customerId: customer_id + }; + + // Remove undefined values + Object.keys(updateData).forEach(key => + updateData[key] === undefined && delete updateData[key] + ); + + await voucher.update(updateData); + + // Fetch updated voucher to return + const updatedVoucher = await Voucher.findOne({ where: { code } }); + return res.json(formatVoucherResponse(updatedVoucher)); } catch (error) { - console.error('Update voucher error:', error); - res.status(400).json({ error: 'Error updating voucher' }); + console.error('Error updating voucher:', error); + return res.status(500).json({ + error: 'Failed to update voucher' + }); } }; diff --git a/src/components/voucher/voucher.routes.js b/src/components/voucher/voucher.routes.js index e74fc69..fdae236 100644 --- a/src/components/voucher/voucher.routes.js +++ b/src/components/voucher/voucher.routes.js @@ -1,5 +1,5 @@ import express from 'express'; -import { createVoucher, getVouchers, getVoucherByCode, redeemVoucher, getRedemptionHistory } from './voucher.controller.js'; +import { createVoucher, getVouchers, getVoucherByCode, redeemVoucher, getRedemptionHistory, updateVoucher } from './voucher.controller.js'; import { authenticateToken } from '../auth/auth.middleware.js'; const router = express.Router(); @@ -264,6 +264,70 @@ const router = express.Router(); * application/json: * schema: * $ref: '#/components/schemas/Error' + * put: + * summary: Update a voucher + * tags: [Vouchers] + * security: + * - bearerAuth: [] + * parameters: + * - in: path + * name: code + * required: true + * schema: + * type: string + * requestBody: + * required: true + * content: + * application/json: + * schema: + * type: object + * properties: + * name: + * type: string + * discount: + * $ref: '#/components/schemas/Discount' + * redemption: + * type: object + * properties: + * quantity: + * type: integer + * daily_quota: + * type: integer + * start_date: + * type: string + * format: date-time + * expiration_date: + * type: string + * format: date-time + * is_active: + * type: boolean + * customer_id: + * type: string + * responses: + * 200: + * description: Voucher updated successfully + * content: + * application/json: + * schema: + * $ref: '#/components/schemas/VoucherResponse' + * 400: + * description: Invalid request + * content: + * application/json: + * schema: + * $ref: '#/components/schemas/Error' + * 401: + * description: Unauthorized + * content: + * application/json: + * schema: + * $ref: '#/components/schemas/Error' + * 404: + * description: Voucher not found + * content: + * application/json: + * schema: + * $ref: '#/components/schemas/Error' */ /** @@ -384,5 +448,6 @@ router.get('/', authenticateToken, getVouchers); router.get('/redemptions', authenticateToken, getRedemptionHistory); router.get('/:code', authenticateToken, getVoucherByCode); router.post('/:code/redeem', authenticateToken, redeemVoucher); +router.put('/:code', authenticateToken, updateVoucher); export default router;