Bug: [Analytics] - Disputes fix psql




diff --git a/crates/diesel_models/src/dispute.rs b/crates/diesel_models/src/dispute.rs
index 9bdac332295..eae8281a99d 100644
--- a/crates/diesel_models/src/dispute.rs
+++ b/crates/diesel_models/src/dispute.rs
@@ -29,6 +29,7 @@ pub struct DisputeNew {
     pub evidence: Option<Secret<serde_json::Value>>,
     pub profile_id: Option<String>,
     pub merchant_connector_id: Option<String>,
+    pub dispute_amount: i64,
 }
 
 #[derive(Clone, Debug, PartialEq, Serialize, Identifiable, Queryable)]
@@ -59,6 +60,7 @@ pub struct Dispute {
     pub evidence: Secret<serde_json::Value>,
     pub profile_id: Option<String>,
     pub merchant_connector_id: Option<String>,
+    pub dispute_amount: i64,
 }
 
 #[derive(Debug)]
diff --git a/crates/diesel_models/src/schema.rs b/crates/diesel_models/src/schema.rs
index 9e72d8dc21d..9db9f8c2d43 100644
--- a/crates/diesel_models/src/schema.rs
+++ b/crates/diesel_models/src/schema.rs
@@ -295,6 +295,7 @@ diesel::table! {
         profile_id -> Nullable<Varchar>,
         #[max_length = 32]
         merchant_connector_id -> Nullable<Varchar>,
+        dispute_amount -> Int8,
     }
 }
 
diff --git a/crates/router/src/core/webhooks.rs b/crates/router/src/core/webhooks.rs
index ef313b62228..9ec8de21f94 100644
--- a/crates/router/src/core/webhooks.rs
+++ b/crates/router/src/core/webhooks.rs
@@ -352,7 +352,7 @@ pub async fn get_or_update_dispute_object(
             let dispute_id = generate_id(consts::ID_LENGTH, "dp");
             let new_dispute = diesel_models::dispute::DisputeNew {
                 dispute_id,
-                amount: dispute_details.amount,
+                amount: dispute_details.amount.clone(),
                 currency: dispute_details.currency,
                 dispute_stage: dispute_details.dispute_stage,
                 dispute_status: event_type
@@ -374,6 +374,7 @@ pub async fn get_or_update_dispute_object(
                 profile_id: Some(business_profile.profile_id.clone()),
                 evidence: None,
                 merchant_connector_id: payment_attempt.merchant_connector_id.clone(),
+                dispute_amount: dispute_details.amount.parse::<i64>().unwrap_or(0),
             };
             state
                 .store
diff --git a/crates/router/src/db/dispute.rs b/crates/router/src/db/dispute.rs
index 0c34123b6f3..b0d0594c013 100644
--- a/crates/router/src/db/dispute.rs
+++ b/crates/router/src/db/dispute.rs
@@ -180,6 +180,7 @@ impl DisputeInterface for MockDb {
             profile_id: dispute.profile_id,
             evidence,
             merchant_connector_id: dispute.merchant_connector_id,
+            dispute_amount: dispute.dispute_amount,
         };
 
         locked_disputes.push(new_dispute.clone());
@@ -414,6 +415,7 @@ mod tests {
                 evidence: Some(Secret::from(Value::String("evidence".into()))),
                 profile_id: None,
                 merchant_connector_id: None,
+                dispute_amount: 1040,
             }
         }
 
diff --git a/migrations/2024-02-28-103308_add_dispute_amount_to_dispute/down.sql b/migrations/2024-02-28-103308_add_dispute_amount_to_dispute/down.sql
new file mode 100644
index 00000000000..e89bb9a356b
--- /dev/null
+++ b/migrations/2024-02-28-103308_add_dispute_amount_to_dispute/down.sql
@@ -0,0 +1,17 @@
+-- This file should undo anything in `up.sql`
+-- Drop the new column
+ALTER TABLE dispute
+DROP COLUMN IF EXISTS dispute_amount;
+
+-- Optionally, if you want to revert the UPDATE statement as well (assuming you have a backup)
+-- You can restore the original data from the backup or use the old values to update the table
+
+-- For example, if you have a backup and want to revert the changes made by the UPDATE statement
+-- You can replace the data with the backup data or the original values
+-- For demonstration purposes, we're assuming you have a backup table named dispute_backup
+
+-- Restore the original values from the backup or any other source
+-- UPDATE dispute
+-- SET dispute_amount = backup.dispute_amount
+-- FROM dispute_backup AS backup
+-- WHERE dispute.id = backup.id;
diff --git a/migrations/2024-02-28-103308_add_dispute_amount_to_dispute/up.sql b/migrations/2024-02-28-103308_add_dispute_amount_to_dispute/up.sql
new file mode 100644
index 00000000000..b56ffc4fe9c
--- /dev/null
+++ b/migrations/2024-02-28-103308_add_dispute_amount_to_dispute/up.sql
@@ -0,0 +1,8 @@
+-- Your SQL goes here
+-- Add the new column with a default value
+ALTER TABLE dispute
+ADD COLUMN dispute_amount BIGINT NOT NULL DEFAULT 0;
+
+-- Update existing rows to set the default value based on the integer equivalent of the amount column
+UPDATE dispute
+SET dispute_amount = CAST(amount AS BIGINT);

## Type of Change
<!-- Put an `x` in the boxes that apply -->

- [ ] Bugfix
- [ ] New feature
- [x] Enhancement
- [ ] Refactoring
- [ ] Dependency updates
- [ ] Documentation
- [ ] CI/CD

## Description
adding dispute_amount column as int type because amount in dispute table is of string type on which we cannot have aggregation for analytics 


### Additional Changes

- [ ] This PR modifies the API contract
- [x] This PR modifies the database schema
- [ ] This PR modifies application configuration/environment variables

<!--
Provide links to the files with corresponding changes.

Following are the paths where you can find config files:
1. `config`
2. `crates/router/src/configs`
3. `loadtest/config`
-->


## Motivation and Context
<!--
Why is this change required? What problem does it solve?
If it fixes an open issue, please link to the issue here.

If you don't have an issue, we'd recommend starting with one first so the PR
can focus on the implementation (unless it is an obvious bug or documentation fix
that will have little conversation).
-->


## How did you test it?
<!--
Did you write an integration/unit/API test to verify the code changes?
Or did you test this change manually (provide relevant screenshots)?
-->run the migration, one column will be added to dispute table called dispute_amount as BIGINT type and will backfill from amount column which is string type


## Checklist
<!-- Put an `x` in the boxes that apply -->

- [x] I formatted the code `cargo +nightly fmt --all`
- [x] I addressed lints thrown by `cargo clippy`
- [x] I reviewed the submitted code
- [ ] I added unit tests for my changes where possible
- [ ] I added a [CHANGELOG](/CHANGELOG.md) entry if applicable
