Skip to content

Commit

Permalink
[update] edit voucher
Browse files Browse the repository at this point in the history
  • Loading branch information
aqidd committed Jan 3, 2025
1 parent e83d6c1 commit d217923
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 12 deletions.
54 changes: 43 additions & 11 deletions src/components/voucher/voucher.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
});
}
};

Expand Down
67 changes: 66 additions & 1 deletion src/components/voucher/voucher.routes.js
Original file line number Diff line number Diff line change
@@ -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();
Expand Down Expand Up @@ -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'
*/

/**
Expand Down Expand Up @@ -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;

0 comments on commit d217923

Please sign in to comment.