| @@ -3,8 +3,9 @@ | ||
| 3 | 3 | namespace FluentCart\Api\Resource; |
| 4 | 4 | |
| 5 | 5 | use FluentCart\App\App; |
| 6 | 6 | use FluentCart\App\Models\CustomerAddresses; |
| 7 | +use FluentCart\App\Services\Renderer\CheckoutFieldsSchema; | |
| 7 | 8 | use FluentCart\Framework\Database\Orm\Builder; |
| 8 | 9 | use FluentCart\Framework\Support\Arr; |
| 9 | 10 | |
| 10 | 11 | class CustomerAddressResource extends BaseResourceApi |
| @@ -122,8 +123,17 @@ | ||
| 122 | 123 | |
| 123 | 124 | // Assign the customer ID to the data array |
| 124 | 125 | $data['customer_id'] = $id; |
| 125 | 126 | |
| 127 | + // Request::getSafe() null-fills sanitize-map keys the client omitted; | |
| 128 | + // label is nullable at validation but NOT NULL DEFAULT '' at the column. | |
| 129 | + if (!isset($data['label'])) { | |
| 130 | + $data['label'] = ''; | |
| 131 | + } | |
| 132 | + | |
| 133 | + $data = static::normalizeBusinessFields($data); | |
| 134 | + $data = static::mergeAddressMetaFields($data); | |
| 135 | + | |
| 126 | 136 | // Create a new address record with the given data |
| 127 | 137 | $isCreated = static::getQuery()->create($data); |
| 128 | 138 | |
| 129 | 139 | // If the address was successfully created |
| @@ -205,8 +215,11 @@ | ||
| 205 | 215 | ['code' => 404, 'message' => __('Address not found, please reload the page and try again!', 'fluent-cart')] |
| 206 | 216 | ]); |
| 207 | 217 | } |
| 208 | 218 | |
| 219 | + $data = static::normalizeBusinessFields($data); | |
| 220 | + $data = static::mergeAddressMetaFields($data, $address); | |
| 221 | + | |
| 209 | 222 | $address->update($data); |
| 210 | 223 | $address->refresh(); |
| 211 | 224 | |
| 212 | 225 | if ($address) { |
| @@ -237,8 +250,58 @@ | ||
| 237 | 250 | ['code' => 400, 'message' => __('Failed updating shipping address', 'fluent-cart')] |
| 238 | 251 | ]); |
| 239 | 252 | } |
| 240 | 253 | |
| 254 | + public static function normalizeBusinessFields(array $data): array | |
| 255 | + { | |
| 256 | + $type = Arr::get($data, 'type', 'billing'); | |
| 257 | + | |
| 258 | + if ($type !== 'billing') { | |
| 259 | + unset($data['company_name'], $data['vat_number'], $data['legal_registration_id']); | |
| 260 | + return $data; | |
| 261 | + } | |
| 262 | + | |
| 263 | + if (!CheckoutFieldsSchema::isCompanyNameEnabled()) { | |
| 264 | + unset($data['company_name']); | |
| 265 | + } | |
| 266 | + | |
| 267 | + if (!CheckoutFieldsSchema::isVatNumberEnabled()) { | |
| 268 | + unset($data['vat_number']); | |
| 269 | + } | |
| 270 | + | |
| 271 | + if (!CheckoutFieldsSchema::isLegalRegistrationIdEnabled()) { | |
| 272 | + unset($data['legal_registration_id']); | |
| 273 | + } | |
| 274 | + | |
| 275 | + return $data; | |
| 276 | + } | |
| 277 | + | |
| 278 | + private static function mergeAddressMetaFields(array $data, ?CustomerAddresses $address = null): array | |
| 279 | + { | |
| 280 | + $meta = $address ? $address->meta : Arr::get($data, 'meta', []); | |
| 281 | + $meta = is_array($meta) ? $meta : []; | |
| 282 | + | |
| 283 | + $metaKeys = ['company_name', 'vat_number', 'legal_registration_id']; | |
| 284 | + foreach ($metaKeys as $metaKey) { | |
| 285 | + if (!array_key_exists($metaKey, $data)) { | |
| 286 | + continue; | |
| 287 | + } | |
| 288 | + | |
| 289 | + $value = Arr::get($data, $metaKey); | |
| 290 | + | |
| 291 | + if ($value === '' || $value === null) { | |
| 292 | + unset($meta['other_data'][$metaKey]); | |
| 293 | + } else { | |
| 294 | + Arr::set($meta, 'other_data.' . $metaKey, $value); | |
| 295 | + } | |
| 296 | + } | |
| 297 | + | |
| 298 | + $data['meta'] = $meta; | |
| 299 | + unset($data['company_name'], $data['vat_number'], $data['legal_registration_id']); | |
| 300 | + | |
| 301 | + return $data; | |
| 302 | + } | |
| 303 | + | |
| 241 | 304 | /** |
| 242 | 305 | * Delete an address based on the given ID and parameters. |
| 243 | 306 | * |
| 244 | 307 | * @param int $id Required. The ID of the customer address. |
| @@ -250,9 +313,9 @@ | ||
| 250 | 313 | // Check if a valid ID is provided |
| 251 | 314 | if (!$id) { |
| 252 | 315 | return static::makeErrorResponse([ |
| 253 | 316 | ['code' => 403, 'message' => __('Please use a valid address ID!', 'fluent-cart')] |
| 254 | - ]); | |
| 317 | + ], 403); | |
| 255 | 318 | } |
| 256 | 319 | |
| 257 | 320 | // Retrieve the customer address using the ID |
| 258 | 321 | $customerAddress = static::getQuery()->find($id); |
| @@ -261,9 +324,9 @@ | ||
| 261 | 324 | // Check if the address is marked as primary |
| 262 | 325 | if ($customerAddress->is_primary) { |
| 263 | 326 | return static::makeErrorResponse([ |
| 264 | 327 | ['code' => 403, 'message' => __('Primary address cannot be deleted!', 'fluent-cart')] |
| 265 | - ]); | |
| 328 | + ], 403); | |
| 266 | 329 | } |
| 267 | 330 | // Get the count of addresses for this customer |
| 268 | 331 | $addressCount = static::getQuery()->where('customer_id', $customerAddress->customer_id)->count(); |
| 269 | 332 | |
| @@ -270,9 +333,9 @@ | ||
| 270 | 333 | // Check if there's only one address, do not allow deletion in that case |
| 271 | 334 | if ($addressCount <= 1) { |
| 272 | 335 | return static::makeErrorResponse([ |
| 273 | 336 | ['code' => 403, 'message' => __('At least one address must remain. Address deletion failed!', 'fluent-cart')] |
| 274 | - ]); | |
| 337 | + ], 403); | |
| 275 | 338 | } |
| 276 | 339 | |
| 277 | 340 | // If the address is not primary and there are multiple addresses, proceed with deletion |
| 278 | 341 | if ($customerAddress->delete()) { |
| @@ -281,14 +344,14 @@ | ||
| 281 | 344 | |
| 282 | 345 | // Return an error if the address is not found in the database |
| 283 | 346 | return static::makeErrorResponse([ |
| 284 | 347 | ['code' => 400, 'message' => __('Address deletion failed!', 'fluent-cart')] |
| 285 | - ]); | |
| 348 | + ], 400); | |
| 286 | 349 | } |
| 287 | 350 | |
| 288 | 351 | return static::makeErrorResponse([ |
| 289 | 352 | ['code' => 404, 'message' => __('Address not found in database, failed to remove.', 'fluent-cart')] |
| 290 | - ]); | |
| 353 | + ], 404); | |
| 291 | 354 | } |
| 292 | 355 | |
| 293 | 356 | /** |
| 294 | 357 | * Make primary address for the specified customer. |
| @@ -317,5 +380,5 @@ | ||
| 317 | 380 | return static::makeErrorResponse([ |
| 318 | 381 | ['code' => 400, 'message' => __('Address set as primary failed.', 'fluent-cart')] |
| 319 | 382 | ]); |
| 320 | 383 | } |
| 321 | -} | |
| 384 | +} | |