| @@ -8,11 +8,10 @@ | ||
| 8 | 8 | * Represents the discounts table (wp_yatra_discounts) containing comprehensive |
| 9 | 9 | * discount management data including promo codes, group discounts, |
| 10 | 10 | * usage tracking, and advanced discount configurations. |
| 11 | 11 | * |
| 12 | - * This table follows the new simplified pattern with only two static methods: | |
| 13 | - * - getTableName(): Returns the prefixed table name | |
| 14 | - * - getSchema(): Returns the complete CREATE TABLE SQL statement | |
| 12 | + * Static API: {@see getTableName()}, {@see getWritableColumnNames()}, | |
| 13 | + * {@see getRestRequestBodyColumnNames()}, {@see getSchema()}. | |
| 15 | 14 | * |
| 16 | 15 | * Usage: |
| 17 | 16 | * DiscountsTable::getTableName() // Returns 'wp_yatra_discounts' |
| 18 | 17 | * DiscountsTable::getSchema() // Returns complete SQL schema |
| @@ -26,17 +25,77 @@ | ||
| 26 | 25 | * Table name without prefix |
| 27 | 26 | * |
| 28 | 27 | * @var string The base table name without WordPress prefix |
| 29 | 28 | */ |
| 30 | - protected static string $table = 'yatra_new_discounts'; | |
| 29 | + protected static string $table = 'yatra_discounts'; | |
| 31 | 30 | |
| 32 | 31 | /** |
| 33 | - * Get the complete table schema as raw SQL CREATE TABLE statement | |
| 34 | - * | |
| 32 | + * Columns that exist on {@see getSchema()} (excluding `id`) for repository mass-assignment. | |
| 33 | + * | |
| 34 | + * @return list<string> | |
| 35 | + */ | |
| 36 | + public static function getWritableColumnNames(): array | |
| 37 | + { | |
| 38 | + return [ | |
| 39 | + 'code', | |
| 40 | + 'description', | |
| 41 | + 'type', | |
| 42 | + 'amount', | |
| 43 | + 'max_discount_amount', | |
| 44 | + 'usage_limit', | |
| 45 | + 'usage_limit_per_customer', | |
| 46 | + 'usage_count', | |
| 47 | + 'valid_from', | |
| 48 | + 'expiry_date', | |
| 49 | + 'status', | |
| 50 | + 'applicable_to', | |
| 51 | + 'trip_ids', | |
| 52 | + 'min_amount', | |
| 53 | + 'first_time_customer_only', | |
| 54 | + 'is_group_discount', | |
| 55 | + 'discount_mode', | |
| 56 | + 'min_group_size', | |
| 57 | + 'max_group_size', | |
| 58 | + 'group_discount_type', | |
| 59 | + 'group_discount_amount', | |
| 60 | + 'group_discount_mode', | |
| 61 | + 'group_discount_ranges', | |
| 62 | + 'category_discounts', | |
| 63 | + 'created_at', | |
| 64 | + 'updated_at', | |
| 65 | + 'created_by', | |
| 66 | + 'updated_by', | |
| 67 | + ]; | |
| 68 | + } | |
| 69 | + | |
| 70 | + /** | |
| 71 | + * REST request body keys allowed through {@see \Yatra\Controllers\DiscountController}. | |
| 72 | + * Omits display-only keys (e.g. created_by_name) and server-controlled timestamps. | |
| 73 | + * Updates do not accept `created_by` from the client. | |
| 74 | + * | |
| 75 | + * @return list<string> | |
| 76 | + */ | |
| 77 | + public static function getRestRequestBodyColumnNames(bool $forCreate): array | |
| 78 | + { | |
| 79 | + $base = array_values(array_diff( | |
| 80 | + self::getWritableColumnNames(), | |
| 81 | + ['created_at', 'updated_at'] | |
| 82 | + )); | |
| 83 | + | |
| 84 | + if (!$forCreate) { | |
| 85 | + $base = array_values(array_diff($base, ['created_by'])); | |
| 86 | + } | |
| 87 | + | |
| 88 | + return $base; | |
| 89 | + } | |
| 90 | + | |
| 91 | + /** | |
| 92 | + * Get the complete table schema as raw SQL CREATE TABLE statement. | |
| 93 | + * | |
| 35 | 94 | * Returns the full SQL schema for the discounts table using heredoc syntax |
| 36 | 95 | * for proper IDE syntax highlighting. Includes all columns, indexes, |
| 37 | 96 | * and constraints from the original Database.php schema. |
| 38 | - * | |
| 97 | + * | |
| 39 | 98 | * @return string Complete CREATE TABLE SQL statement |
| 40 | 99 | */ |
| 41 | 100 | public static function getSchema(): string |
| 42 | 101 | { |