001/*
002 *   Copyright 2024 Vonage
003 *
004 *   Licensed under the Apache License, Version 2.0 (the "License");
005 *   you may not use this file except in compliance with the License.
006 *   You may obtain a copy of the License at
007 *
008 *        http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *   Unless required by applicable law or agreed to in writing, software
011 *   distributed under the License is distributed on an "AS IS" BASIS,
012 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *   See the License for the specific language governing permissions and
014 *   limitations under the License.
015 */
016package com.vonage.client.subaccounts;
017
018import com.vonage.client.QueryParamsRequest;
019import java.time.Instant;
020import java.time.temporal.ChronoUnit;
021import java.util.LinkedHashMap;
022import java.util.Map;
023
024public class ListTransfersFilter implements QueryParamsRequest {
025        private final Instant startDate, endDate;
026        private final String subaccount;
027
028        ListTransfersFilter(Builder builder) {
029                startDate = builder.startDate != null ? builder.startDate : Instant.EPOCH;
030                endDate = builder.endDate;
031                if ((subaccount = builder.subaccount) != null) {
032                        AbstractTransfer.validateAccountKey(builder.subaccount, "Subaccount");
033                }
034        }
035
036        private static String formatTime(Instant timestamp) {
037                return timestamp.truncatedTo(ChronoUnit.SECONDS).toString();
038        }
039
040        @Override
041        public Map<String, String> makeParams() {
042                Map<String, String> params = new LinkedHashMap<>(4);
043                params.put("start_date", formatTime(startDate));
044                if (endDate != null) {
045                        params.put("end_date", formatTime(endDate));
046                }
047                if (subaccount != null) {
048                        params.put("subaccount", subaccount);
049                }
050                return params;
051        }
052
053        /**
054         * Start of the retrieval period.
055         *
056         * @return The start date of the range or {@linkplain Instant#EPOCH} if unspecified (the default).
057         */
058        public Instant getStartDate() {
059                return startDate;
060        }
061
062        /**
063         * End of the retrieval period. If absent then all transfers until now are returned.
064         *
065         * @return The end date of the range or {@code null} if unspecified (the default).
066         */
067        public Instant getEndDate() {
068                return endDate;
069        }
070
071        /**
072         * Subaccount API key to filter by.
073         *
074         * @return The subaccount ID to filter by, or null if unspecified (the default).
075         */
076        public String getSubaccount() {
077                return subaccount;
078        }
079
080        /**
081         * Entry point for constructing an instance of this class.
082         * 
083         * @return A new Builder.
084         */
085        public static Builder builder() {
086                return new Builder();
087        }
088
089        public static class Builder {
090                private Instant startDate, endDate;
091                private String subaccount;
092        
093                Builder() {}
094        
095                /**
096                 * (OPTIONAL) Start of the retrieval period.
097                 *
098                 * @param startDate The start timestamp.
099                 *
100                 * @return This builder.
101                 */
102                public Builder startDate(Instant startDate) {
103                        this.startDate = startDate;
104                        return this;
105                }
106
107                /**
108                 * (OPTIONAL) End of the retrieval period. If absent then all transfers until now is returned.
109                 *
110                 * @param endDate The end timestamp.
111                 *
112                 * @return This builder.
113                 */
114                public Builder endDate(Instant endDate) {
115                        this.endDate = endDate;
116                        return this;
117                }
118
119                /**
120                 * (OPTIONAL) Subaccount ID to include in the search. If you set this,
121                 * all other subaccounts will be excluded from the search results.
122                 *
123                 * @param subaccount The subaccount API key to filter by.
124                 *
125                 * @return This builder.
126                 */
127                public Builder subaccount(String subaccount) {
128                        this.subaccount = subaccount;
129                        return this;
130                }
131        
132                /**
133                 * Builds the {@linkplain ListTransfersFilter}.
134                 *
135                 * @return An instance of ListTransfersFilter, populated with all fields from this builder.
136                 */
137                public ListTransfersFilter build() {
138                        return new ListTransfersFilter(this);
139                }
140        }
141}