use reqwest::Method;
use serde::{Deserialize, Serialize};
use crate::{
core::{
api_req::ApiRequest,
api_resp::{ApiResponseTrait, BaseResponse, ResponseFormat},
config::Config,
constants::AccessTokenType,
endpoints::{EndpointBuilder, Endpoints},
http::Transport,
req_option::RequestOption,
SDKResult,
},
service::payroll::models::{
PageResponse, PaymentDetail, PaymentDetailListRequest, PaymentDetailQueryRequest,
},
};
pub struct PaymentDetailService {
pub config: Config,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct PaymentDetailListResponse {
#[serde(flatten)]
pub details: PageResponse<PaymentDetail>,
}
impl ApiResponseTrait for PaymentDetailListResponse {
fn data_format() -> ResponseFormat {
ResponseFormat::Data
}
}
#[derive(Debug, Serialize, Deserialize)]
pub struct PaymentDetailQueryResponse {
pub payment_details: Vec<PaymentDetail>,
}
impl ApiResponseTrait for PaymentDetailQueryResponse {
fn data_format() -> ResponseFormat {
ResponseFormat::Data
}
}
impl PaymentDetailService {
pub fn new(config: Config) -> Self {
Self { config }
}
pub async fn list_details(
&self,
request: PaymentDetailListRequest,
option: Option<RequestOption>,
) -> SDKResult<BaseResponse<PaymentDetailListResponse>> {
let mut api_req = ApiRequest {
http_method: Method::GET,
api_path: EndpointBuilder::replace_param(
Endpoints::PAYROLL_V1_PAYMENT_DETAILS,
"payment_activity_id",
&request.payment_activity_id,
),
supported_access_token_types: vec![AccessTokenType::Tenant],
body: vec![],
..Default::default()
};
if let Some(page_size) = request.page_size {
api_req
.query_params
.insert("page_size", page_size.to_string());
}
if let Some(page_token) = request.page_token {
api_req.query_params.insert("page_token", page_token);
}
if let Some(employee_id) = request.employee_id {
api_req.query_params.insert("employee_id", employee_id);
}
if let Some(user_id_type) = request.user_id_type {
api_req.query_params.insert("user_id_type", user_id_type);
}
if let Some(department_id_type) = request.department_id_type {
api_req
.query_params
.insert("department_id_type", department_id_type);
}
Transport::request(api_req, &self.config, option).await
}
pub async fn query_details(
&self,
request: PaymentDetailQueryRequest,
option: Option<RequestOption>,
) -> SDKResult<BaseResponse<PaymentDetailQueryResponse>> {
let mut api_req = ApiRequest {
http_method: Method::POST,
api_path: EndpointBuilder::replace_param(
Endpoints::PAYROLL_V1_PAYMENT_DETAILS_QUERY,
"payment_activity_id",
&request.payment_activity_id,
),
supported_access_token_types: vec![AccessTokenType::Tenant],
body: serde_json::to_vec(&request).unwrap_or_default(),
..Default::default()
};
if let Some(user_id_type) = request.user_id_type {
api_req.query_params.insert("user_id_type", user_id_type);
}
Transport::request(api_req, &self.config, option).await
}
}