|
| 1 | +import type { CouponRequestType } from '@stacksjs/orm' |
| 2 | +import type { CouponJsonResponse } from '../../../../orm/src/models/Coupon' |
| 3 | +import { db } from '@stacksjs/database' |
| 4 | +import { fetchById } from './fetch' |
| 5 | + |
| 6 | +/** |
| 7 | + * Update a coupon by ID |
| 8 | + * |
| 9 | + * @param id The ID of the coupon to update |
| 10 | + * @param request The updated coupon data |
| 11 | + * @returns The updated coupon record |
| 12 | + */ |
| 13 | +export async function update(id: number, request: CouponRequestType): Promise<CouponJsonResponse | undefined> { |
| 14 | + // Validate the request data |
| 15 | + await request.validate() |
| 16 | + |
| 17 | + // Check if coupon exists |
| 18 | + const existingCoupon = await fetchById(id) |
| 19 | + if (!existingCoupon) { |
| 20 | + throw new Error(`Coupon with ID ${id} not found`) |
| 21 | + } |
| 22 | + |
| 23 | + // Prepare update data object with proper type |
| 24 | + const updateData: Record<string, any> = { |
| 25 | + code: request.get<string>('code'), |
| 26 | + description: request.get<string>('description'), |
| 27 | + discount_type: request.get<string>('discount_type'), |
| 28 | + discount_value: request.get<number>('discount_value'), |
| 29 | + product_id: request.get<number>('product_id'), |
| 30 | + min_order_amount: request.get<number>('min_order_amount'), |
| 31 | + max_discount_amount: request.get<number>('max_discount_amount'), |
| 32 | + free_product_id: request.get<string>('free_product_id'), |
| 33 | + is_active: request.get<boolean>('is_active'), |
| 34 | + usage_limit: request.get<number>('usage_limit'), |
| 35 | + start_date: request.get<string>('start_date'), |
| 36 | + end_date: request.get<string>('end_date'), |
| 37 | + updated_at: new Date(), |
| 38 | + } |
| 39 | + |
| 40 | + // Process arrays to JSON strings if provided |
| 41 | + if (request.has('applicable_products')) { |
| 42 | + updateData.applicable_products = JSON.stringify(request.get<string[]>('applicable_products')) |
| 43 | + } |
| 44 | + |
| 45 | + if (request.has('applicable_categories')) { |
| 46 | + updateData.applicable_categories = JSON.stringify(request.get<string[]>('applicable_categories')) |
| 47 | + } |
| 48 | + |
| 49 | + // Remove undefined fields to avoid overwriting with null values |
| 50 | + Object.keys(updateData).forEach((key) => { |
| 51 | + if (updateData[key] === undefined) { |
| 52 | + delete updateData[key] |
| 53 | + } |
| 54 | + }) |
| 55 | + |
| 56 | + // If no fields to update, just return the existing coupon |
| 57 | + if (Object.keys(updateData).length === 0) { |
| 58 | + return existingCoupon |
| 59 | + } |
| 60 | + |
| 61 | + try { |
| 62 | + // Update the coupon |
| 63 | + await db |
| 64 | + .updateTable('coupons') |
| 65 | + .set(updateData) |
| 66 | + .where('id', '=', id) |
| 67 | + .execute() |
| 68 | + |
| 69 | + // Fetch and return the updated coupon |
| 70 | + return await fetchById(id) |
| 71 | + } |
| 72 | + catch (error) { |
| 73 | + if (error instanceof Error) { |
| 74 | + // Handle duplicate code error |
| 75 | + if (error.message.includes('Duplicate entry') && error.message.includes('code')) { |
| 76 | + throw new Error('A coupon with this code already exists') |
| 77 | + } |
| 78 | + |
| 79 | + throw new Error(`Failed to update coupon: ${error.message}`) |
| 80 | + } |
| 81 | + |
| 82 | + throw error |
| 83 | + } |
| 84 | +} |
0 commit comments