[BUG]: Unique violation when adding reverse lookup in refunds
- Dominant language
- Rust
- Stars
- 43.7k
- Forks
- 5.1k
- Avg merge
- 2d 20h
- Merged PRs (30d)
- 205
Description
### Bug Description
```rust
// storage_types::ReverseLookupNew {
// sk_id: field.clone(),
// lookup_id: format!(
// "{}_{}",
// created_refund.merchant_id,
// created_refund.connector_transaction_id
// ),
// pk_id: key.clone(),
// source: "refund".to_string(),
// },
```
This query causes unique violation when being added during create_refund.
### Expected Behavior
Unique Violation should not occur.
### Actual Behavior
Reference Logs:
```log
2023-02-02T11:14:37.088535Z ERROR router::services::api: error: {"error":{"type":"server_not_available","code":"HE_00","message":"Something went wrong"}}
├╴ crates/router/src/core/errors/utils.rs:38:18
│
├─▶ KV error
│ ╰╴ crates/router/src/db/refund.rs:349:34
│
├─▶ A unique constraint violation occurred
│ ├╴ /Users/nishantjoshi/Work/orca/crates/storage_models/src/query/generics.rs:48:28
│ ╰╴ Error while inserting [ReverseLookupNew { lookup_id: "merchant_1675241610_ref_2toMVDhozJ9dV0udlwR2", pk_id: "merchant_1675241610_pay_Lzdk9197D9YAiDZGDfpL", sk_id: "pa_b240f7b0-fe08-4db6-a7fc-205ae77280b9_ref_ref_2toMVDhozJ9dV0udlwR2", source: "refund" }, ReverseLookupNew { lookup_id: "merchant_1675241610_pi_3MX0kJD5R7gDAGff1S3XSAeh", pk_id: "merchant_1675241610_pay_Lzdk9197D9YAiDZGDfpL", sk_id: "pa_b240f7b0-fe08-4db6-a7fc-205ae77280b9_ref_ref_2toMVDhozJ9dV0udlwR2", source: "refund" }, ReverseLookupNew { lookup_id: "merchant_1675241610_refid_h9pZoJEArU4nhtSq7gyR", pk_id: "merchant_1675241610_pay_Lzdk9197D9YAiDZGDfpL", sk_id: "pa_b240f7b0-fe08-4db6-a7fc-205ae77280b9_ref_ref_2toMVDhozJ9dV0udlwR2", source: "refund" }]
│
╰─▶ Failed to issue a query: duplicate key value violates unique constraint "reverse_lookup_pkey"
╰╴ /Users/nishantjoshi/Work/orca/crates/storage_models/src/query/generics.rs:42:46
at crates/router/src/services/api.rs:479
in router::services::api::server_wrap with request_method: "POST", request_url_path: "/refunds"
```
### Steps To Reproduce
1. Have a merchant with storage schema `redis_kv`
2. Create a payment
3. Create a refund
### Context For The Bug
As discussed with @jarnura this field was intended, but after checking the database, I was able to deduce that a similar lookup_key exists and is being created during payment. It is created to find payment based on connector_transaction_id. While performing the query
### Environment
Local environment, testing for KV
### Have you spent some time to check if this bug has been raised before?
- [X] I checked and didn't find similar issue
### Have you read the Contributing Guidelines?
- [X] I have read the [Contributing Guidelines](https://github.com/juspay/hyperswitch/blob/main/docs/CONTRIBUTING.md)
### Are you willing to submit a PR?
Yes I am willing to submit a PR!
Contributor guide
Assessment
This issue has not been assessed yet.