Repository navigation
fix: [TESIS-153] validate the warehouse of each product stock row - #106
Merged
Merged
Conversation
The products POROs compared the warehouse ids of the stock rows raw against
the ids in the database. A row without warehouse_id made the sort raise
ArgumentError (nil against Integer), so POST and PUT /products answered 500;
ids that came as text ("5") never matched and gave a false 422 saying the
warehouse did not belong to the company. The order edition had the same
false 422 with the same warehouse sent as "1" and 1.
The rows now need a positive integer warehouse_id, as a number or as digits,
and are compared as unique integers by count. A missing one answers 422
naming the row. Warehouses of another company are still refused with the
same generic message.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Contributor
|
Revisado. Lo veo bien para implementar. Las dos fallas son reales y las dos se alcanzan desde la UI:
Lo que verifiqué
Antes de mergear
|
LauAubert
marked this pull request as ready for review
October 3, 2026 23:25
TomasMartin2004
approved these changes
Oct 4, 2026
TomasMartin2004
left a comment
Contributor
There was a problem hiding this comment.
Revisión del diff completo. El código es idéntico al que leí cuando los PRs estaban en draft —ningún commit nuevo—, así que lo que sigue es el veredicto formal.
✅ Aprobado
Las dos fallas son reales y las dos se alcanzan desde la UI:
- una fila de
stockssinwarehouse_iddejaba[id, nil]y elsortlevantabaArgumentError: 500 enPOSTy enPUT /api/v1/products; - un id que llega como texto (
"5") no coincidía con el entero de la base y la API respondía un 422 de «One or more warehouses do not belong to this company» que era mentira. Ese es el peor de los dos: acusa a un depósito propio de ser de otra empresa.
Lo que verifiqué
owned.sort == warehouse_ids.sort→Warehouse.where(id: ids).count == ids.sizees equivalente y además no trae ids a memoria para ordenarlos.warehouse_id_of!aceptaIntegeryStringde sólo dígitos y rechaza el resto nombrando la fila. Nombrarla está bien: es un dato del request del propio usuario, no filtra nada de otro tenant.- En
ReplaceOrderLines,item[:warehouse_id].to_s.to_iconviertenilen0, que no existe y cae igual en el 422 — el mismo resultado que antes. Yvalidate_lines_without_warehouse!corre antes para las líneas que mueven stock. No cambia comportamiento. - El spec cubre que un depósito de otra empresa sigue dando el 422 genérico. Era lo importante: al normalizar los ids no había que aflojar la validación de tenant, y no se aflojó.
LauAubert
added a commit
that referenced
this pull request
Oct 5, 2026
The products POROs compared the warehouse ids of the stock rows raw against
the ids in the database. A row without warehouse_id made the sort raise
ArgumentError (nil against Integer), so POST and PUT /products answered 500;
ids that came as text ("5") never matched and gave a false 422 saying the
warehouse did not belong to the company. The order edition had the same
false 422 with the same warehouse sent as "1" and 1.
The rows now need a positive integer warehouse_id, as a number or as digits,
and are compared as unique integers by count. A missing one answers 422
naming the row. Warehouses of another company are still refused with the
same generic message.
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Ticket de Jira
https://proyectofinalfrlp.atlassian.net/browse/TESIS-153
Descripción
Products::Concerns::WarehouseValidationcomparaba loswarehouse_iddestocks[]crudos contra los de la base (owned.sort == warehouse_ids.sort). Dos fallas salían de ahí:warehouse_iddejaba[id, nil], y elsortlevantabaArgumentError (comparison of Integer with nil failed), que nadie rescata. Le pasaba aPOSTy aPUT /api/v1/products."5"),[5] == ["5"]daba falso y la API respondía «One or more warehouses do not belong to this company» por un depósito que sí era de la empresa.Orders::ReplaceOrderLines#validate_new_warehouses!tenía el mismo 422 falso cuando dos líneas nuevas nombraban el mismo depósito como"1"y como1(contaban como dos).WarehouseValidationnormaliza cada id a entero (acepta número o dígitos) y responde 422 nombrando la fila cuando falta o no es válido:stocks[1]: warehouse_id must be a positive integer.Warehouse.where(id: ids).count == ids.size); el scope deCompanyScopedsigue acotando al tenant y el mensaje para un depósito ajeno sigue siendo el genérico.ReplaceOrderLines#validate_new_warehouses!compara enteros.Evidencia visual
N/A
Cómo probar
PUT /api/v1/products/:idcon{ product: { name: 'X', stocks: [{ warehouse_id: W, quantity: 1 }, { quantity: 3 }] } }→ 422stocks[1]: warehouse_id must be a positive integer. Enmaster, 500.stocks: [{ warehouse_id: "W", quantity: 4 }](texto) → 200 y el stock queda en 4. Enmaster, 422.Verificación:
bundle exec rspec(1572 ejemplos, 0 fallas, cobertura 99.89% de línea),rubocopybrakemanlimpios.Dientes: con la versión de
masterdel concern, fallan 2 de los 4 specs nuevos de productos.Impacto y consideraciones
¿Introduce breaking changes?
No. Un request que antes fallaba con 500 o con un 422 equivocado ahora responde lo correcto.
¿Requiere nuevas variables de entorno?
No
¿Afecta la arquitectura o genera un nuevo patrón?
No. Mismo criterio de
positive_integerque ya usabaReplaceOrderLines.🤖 Generated with Claude Code