feat: list of refunds (#284)

This commit is contained in:
Sangamesh Kulkarni
2023-01-06 14:33:30 +05:30
committed by GitHub
parent 38649130bb
commit e5330528fa
7 changed files with 219 additions and 3 deletions

View File

@ -455,6 +455,40 @@ pub async fn validate_and_create_refund(
Ok(refund.foreign_into())
}
// ********************************************** Refund list **********************************************
/// If payment-id is provided, lists all the refunds associated with that particular payment-id
/// If payment-id is not provided, lists the refunds associated with that particular merchant - to the limit specified,if no limits given, it is 10 by default
#[instrument(skip_all)]
pub async fn refund_list(
db: &dyn db::StorageInterface,
merchant_account: storage::merchant_account::MerchantAccount,
req: api_models::refunds::RefundListRequest,
) -> RouterResponse<api_models::refunds::RefundListResponse> {
let limit = validator::validate_refund_list(req.limit)?;
let refund_list = db
.filter_refund_by_constraints(
&merchant_account.merchant_id,
&req,
merchant_account.storage_scheme,
limit,
)
.await
.change_context(errors::ApiErrorResponse::RefundNotFound)?;
let data: Vec<refunds::RefundResponse> = refund_list
.into_iter()
.map(ForeignInto::foreign_into)
.collect();
utils::when(data.is_empty(), || {
Err(errors::ApiErrorResponse::RefundNotFound)
})?;
Ok(services::BachResponse::Json(
api_models::refunds::RefundListResponse { data },
))
}
// ********************************************** UTILS **********************************************
// FIXME: function should not have more than 3 arguments.

View File

@ -122,3 +122,19 @@ pub async fn validate_uniqueness_of_refund_id_against_merchant_id(
}
}
}
pub fn validate_refund_list(limit: Option<i64>) -> CustomResult<i64, errors::ApiErrorResponse> {
match limit {
Some(limit_val) => {
if !(1..=100).contains(&limit_val) {
Err(errors::ApiErrorResponse::InvalidRequestData {
message: "limit should be in between 1 and 100".to_string(),
}
.into())
} else {
Ok(limit_val)
}
}
None => Ok(10),
}
}